Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/updated autoloading #236

Open
wants to merge 29 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
ae39a1a
Install the spatie package and remove the haydenpierce class finder
darylldoyle Aug 13, 2024
3c40936
Update the logic for class loading
darylldoyle Aug 13, 2024
5621fa1
Update the docs
darylldoyle Aug 13, 2024
91e499f
Add unit tests
darylldoyle Aug 13, 2024
227041b
PHPCS
darylldoyle Aug 13, 2024
4d361e9
DB name changes
darylldoyle Aug 13, 2024
f934517
Do we need to load the plugin?
darylldoyle Aug 13, 2024
3cefa47
Typ updated config
darylldoyle Aug 14, 2024
794f9e5
yml fix
darylldoyle Aug 14, 2024
19a4e66
Use v2 of the action
darylldoyle Aug 14, 2024
b728f13
Split the unit tests into separate suites
darylldoyle Aug 14, 2024
4b88720
separate tests
darylldoyle Aug 14, 2024
63a59ec
Install all the things
darylldoyle Aug 14, 2024
faed559
install deps
darylldoyle Aug 14, 2024
a5e888d
Remove caches and ignore. Fix bootstrapping during tests to remove ca…
darylldoyle Aug 14, 2024
a656c65
PHP matrix
darylldoyle Aug 14, 2024
137834f
Don’t fast fail
darylldoyle Aug 14, 2024
7cdc487
Only support 8.1+
darylldoyle Aug 14, 2024
5d18d81
Update tests to make less flakey
darylldoyle Nov 11, 2024
5a014c7
Merge branch 'trunk' into feature/updated-autoloading
darylldoyle Dec 5, 2024
1db75f4
Fix test namespaces
darylldoyle Dec 5, 2024
829cd1c
Linting fixes
darylldoyle Dec 5, 2024
a2cd6be
Don’t lint anything from plugins by default
darylldoyle Dec 5, 2024
9934b71
Only supporting 8.3 and 8.4 moving forward
darylldoyle Dec 5, 2024
d13d67e
Merge branch 'trunk' into feature/updated-autoloading
darylldoyle Dec 5, 2024
58ed619
Merge branch 'trunk' into feature/updated-autoloading
darylldoyle Dec 5, 2024
d127b5a
Merge branch 'trunk' into feature/updated-autoloading
darylldoyle Dec 5, 2024
40f16a5
Readme fix
darylldoyle Dec 9, 2024
ab307d3
Remove warning check
darylldoyle Dec 11, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 33 additions & 8 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,36 @@ permissions:
contents: read

jobs:
build:
phpcs:
runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
php: [ '8.3', '8.4' ]

services:
mysql:
image: mysql:5.7
env:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: wordpress_test
ports:
- 3306
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3

steps:
- uses: actions/checkout@v3

- name: Setup PHP with composer v2
- name: Validate composer.json and composer.lock
run: composer run check-deps

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: "8.3"
php-version: ${{ matrix.php }}
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, mysql, mysqli, pdo_mysql, bcmath, soap, intl, gd, exif, iconv, imagick
coverage: none
tools: composer:v2

- name: Validate Root composer.json and composer.lock
Expand All @@ -34,11 +54,6 @@ jobs:
- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v3
with:
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-

- name: Install Root dependencies
run: composer install --prefer-dist --no-progress
Expand All @@ -54,3 +69,13 @@ jobs:

- name: Run PHPStan
run: composer static

- name: Setup WP Tests
run: bin/install-wp-tests.sh wordpress_test root root 127.0.0.1:${{ job.services.mysql.ports['3306'] }} latest true

- name: Run PHPUnit on Plugin
run: composer run test:plugin

- name: Run PHPUnit on Theme
run: composer run test:theme

2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ uploads
upgrade
cache
plugins
.phpunit.result.cache
class-loader-cache/*

# Project Files
dist
Expand Down
196 changes: 196 additions & 0 deletions bin/install-wp-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
#!/usr/bin/env bash

function display_help() {
cat << EOF
$(tput setaf 3)Usage:$(tput sgr0)
$(tput setaf 2)./install-wp-tests.sh <db-name> <db-user> <db-pass> [db-host] [wp-version] [skip-database-creation]$(tput sgr0)

$(tput setaf 3)Parameters:$(tput sgr0)
$(tput setaf 4)<db-name>$(tput sgr0) The name of the database to use or create for WordPress tests.
$(tput setaf 4)<db-user>$(tput sgr0) The MySQL user that has access to your test database.
$(tput setaf 4)<db-pass>$(tput sgr0) The password for the MySQL user.

$(tput setaf 3)Optional Parameters:$(tput sgr0)
$(tput setaf 4)[db-host]$(tput sgr0) The host of your database server along with the port or socket if necessary. Default is 'localhost' with the MySQL socket automatically detected.
$(tput setaf 4)[wp-version]$(tput sgr0) The specific version of WordPress you want to test against. Can be a version number like '5.8', '5.8.1', or special strings 'nightly' or 'trunk' for the latest development version. Default is the latest stable release.
$(tput setaf 4)[skip-database-creation]$(tput sgr0) A flag to indicate whether to skip the database creation step ('true' or 'false'). Defaults to 'true', meaning it will not attempt to create the database if it already exists.

$(tput setaf 3)Default Behavior:$(tput sgr0)
Without any parameters, the script will use the following defaults:
- Database name: 'wordpress-unit-tests'
- Database user: 'root'
- Database password: 'root'
- Database host: 'localhost' with the auto-detected MySQL socket
- WordPress version: latest stable release
- Skip database creation: true (it will not create the database if it exists)

$(tput setaf 1)This script requires that MySQL is accessible with the provided user credentials and that the appropriate permissions are set to create or access databases.$(tput sgr0)
EOF
exit 1
}

while [[ "$#" -gt 0 ]]; do
case $1 in
-h|--help) display_help;;
*) break;;
esac
done

SOCKET=$(mysqld --verbose --help | grep ^socket | awk '{print $2, $3, $4}' | xargs)

DB_NAME=${1-wordpress-unit-tests}
DB_USER=${2-root}
DB_PASS=${3-root}
DB_HOST=${4-"localhost:$SOCKET"}
WP_VERSION=${5-latest}
SKIP_DB_CREATE=${6-true}

mysql -e "CREATE DATABASE IF NOT EXISTS \`$DB_NAME\` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci"

TMPDIR=${TMPDIR-/tmp}
TMPDIR=$(echo $TMPDIR | sed -e "s/\/$//")
WP_TESTS_DIR=${WP_TESTS_DIR-$TMPDIR/wordpress-tests-lib}
WP_CORE_DIR=${WP_CORE_DIR-$TMPDIR/wordpress/}

download() {
if [ `which wget` ]; then
wget -nv -O "$2" "$1"
elif [ `which curl` ]; then
curl -s "$1" > "$2";
fi
}

if [[ $WP_VERSION =~ ^[0-9]+\.[0-9]+$ ]]; then
WP_TESTS_TAG="branches/$WP_VERSION"
elif [[ $WP_VERSION =~ [0-9]+\.[0-9]+\.[0-9]+ ]]; then
if [[ $WP_VERSION =~ [0-9]+\.[0-9]+\.[0] ]]; then
# version x.x.0 means the first release of the major version, so strip off the .0 and download version x.x
WP_TESTS_TAG="tags/${WP_VERSION%??}"
else
WP_TESTS_TAG="tags/$WP_VERSION"
fi
elif [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then
WP_TESTS_TAG="trunk"
else
# http serves a single offer, whereas https serves multiple. we only want one
download http://api.wordpress.org/core/version-check/1.7/ /tmp/wp-latest.json
grep '[0-9]+\.[0-9]+(\.[0-9]+)?' /tmp/wp-latest.json
LATEST_VERSION=$(grep -o '"version":"[^"]*' /tmp/wp-latest.json | sed 's/"version":"//')
if [[ -z "$LATEST_VERSION" ]]; then
echo "Latest WordPress version could not be found"
exit 1
fi
WP_TESTS_TAG="tags/$LATEST_VERSION"
fi

set -ex

install_wp() {

rm -rf $WP_CORE_DIR

if [ -d $WP_CORE_DIR ]; then
return;
fi

mkdir -p $WP_CORE_DIR

if [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then
mkdir -p $TMPDIR/wordpress-nightly
download https://wordpress.org/nightly-builds/wordpress-latest.zip $TMPDIR/wordpress-nightly/wordpress-nightly.zip
unzip -q $TMPDIR/wordpress-nightly/wordpress-nightly.zip -d $TMPDIR/wordpress-nightly/
mv $TMPDIR/wordpress-nightly/wordpress/* $WP_CORE_DIR
else
if [ $WP_VERSION == 'latest' ]; then
local ARCHIVE_NAME='latest'
elif [[ $WP_VERSION =~ [0-9]+\.[0-9]+ ]]; then
# https serves multiple offers, whereas http serves single.
download https://api.wordpress.org/core/version-check/1.7/ $TMPDIR/wp-latest.json
if [[ $WP_VERSION =~ [0-9]+\.[0-9]+\.[0] ]]; then
# version x.x.0 means the first release of the major version, so strip off the .0 and download version x.x
LATEST_VERSION=${WP_VERSION%??}
else
# otherwise, scan the releases and get the most up to date minor version of the major release
local VERSION_ESCAPED=`echo $WP_VERSION | sed 's/\./\\\\./g'`
LATEST_VERSION=$(grep -o '"version":"'$VERSION_ESCAPED'[^"]*' $TMPDIR/wp-latest.json | sed 's/"version":"//' | head -1)
fi
if [[ -z "$LATEST_VERSION" ]]; then
local ARCHIVE_NAME="wordpress-$WP_VERSION"
else
local ARCHIVE_NAME="wordpress-$LATEST_VERSION"
fi
else
local ARCHIVE_NAME="wordpress-$WP_VERSION"
fi
download https://wordpress.org/${ARCHIVE_NAME}.tar.gz $TMPDIR/wordpress.tar.gz
tar --strip-components=1 -zxmf $TMPDIR/wordpress.tar.gz -C $WP_CORE_DIR
fi

download https://raw.github.com/markoheijnen/wp-mysqli/master/db.php $WP_CORE_DIR/wp-content/db.php
}

install_test_suite() {
# portable in-place argument for both GNU sed and Mac OSX sed
if [[ $(uname -s) == 'Darwin' ]]; then
local ioption='-i .bak'
else
local ioption='-i'
fi

rm -rf $WP_TESTS_DIR

# set up testing suite if it doesn't yet exist
if [ ! -d $WP_TESTS_DIR ]; then
# set up testing suite
mkdir -p $WP_TESTS_DIR
svn co --quiet --ignore-externals https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/includes/ $WP_TESTS_DIR/includes
svn co --quiet --ignore-externals https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/data/ $WP_TESTS_DIR/data
fi

if [ ! -f wp-tests-config.php ]; then
download https://develop.svn.wordpress.org/${WP_TESTS_TAG}/wp-tests-config-sample.php "$WP_TESTS_DIR"/wp-tests-config.php
# remove all forward slashes in the end
WP_CORE_DIR=$(echo $WP_CORE_DIR | sed "s:/\+$::")
sed $ioption "s:dirname( __FILE__ ) . '/src/':'$WP_CORE_DIR/':" "$WP_TESTS_DIR"/wp-tests-config.php
sed $ioption "s/youremptytestdbnamehere/$DB_NAME/" "$WP_TESTS_DIR"/wp-tests-config.php
sed $ioption "s/yourusernamehere/$DB_USER/" "$WP_TESTS_DIR"/wp-tests-config.php
sed $ioption "s/yourpasswordhere/$DB_PASS/" "$WP_TESTS_DIR"/wp-tests-config.php
sed $ioption "s|localhost|${DB_HOST}|" "$WP_TESTS_DIR"/wp-tests-config.php
fi

}

install_db() {

if [ ${SKIP_DB_CREATE} = "true" ]; then
return 0
fi

# parse DB_HOST for port or socket references
local PARTS=(${DB_HOST//\:/ })
local DB_HOSTNAME=${PARTS[0]};
local DB_SOCK_OR_PORT=${PARTS[1]};
local EXTRA=""

if ! [ -z $DB_HOSTNAME ] ; then
if [ $(echo $DB_SOCK_OR_PORT | grep -e '^[0-9]\{1,\}$') ]; then
EXTRA=" --host=$DB_HOSTNAME --port=$DB_SOCK_OR_PORT --protocol=tcp"
elif ! [ -z $DB_SOCK_OR_PORT ] ; then
EXTRA=" --socket=$DB_SOCK_OR_PORT"
elif ! [ -z $DB_HOSTNAME ] ; then
EXTRA=" --host=$DB_HOSTNAME --protocol=tcp"
fi
fi

# create database
if [ -z $DB_PASS ] ; then
echo 'No DB password'
mysqladmin create $DB_NAME --user="$DB_USER" $EXTRA
else
mysqladmin create $DB_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA
fi
}

install_wp
install_test_suite
install_db
16 changes: 15 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,26 @@
"phpstan/phpstan-deprecation-rules": "^1.2"
},
"scripts": {
"check-deps": [
"composer validate --strict",
"composer validate --strict --working-dir=mu-plugins/10up-plugin",
"composer validate --strict --working-dir=themes/10up-theme"
],
"setup": [
"composer install",
"composer install --working-dir=mu-plugins/10up-plugin",
"composer install --working-dir=themes/10up-theme"
],
"lint": "phpcs .",
"lint-fix": "phpcbf .",
"static": [
"Composer\\Config::disableProcessTimeout",
"phpstan --memory-limit=1G"
]
],
"install-tests": "./bin/install-wp-tests.sh",
"test": "composer run test:plugin && composer run test:theme",
"test:plugin": "cd ./mu-plugins/10up-plugin/ && composer run test",
"test:theme": "cd ./themes/10up-theme/ && composer run test"
},
"extra": {
"installer-paths": {
Expand Down
14 changes: 1 addition & 13 deletions docs/registering-classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

The MU-Plugin and the theme utilize a system to uniformly, auto-register classes that lie within their namespaces. Whilst there are a few constraints, it eases the requirements for engineers to add their classes to multiple locations each time they add one to the system.

To do this, it uses the [haydenpierce/class-finder](https://packagist.org/packages/haydenpierce/class-finder) package, which reads the `composer.json` file to help locate files that belong in certain namespaces.
To do this, it uses the [spatie/php-structure-discoverer](https://github.com/spatie/php-structure-discoverer) package, which recursively locates files that belong in certain namespaces.

## How do I define a class to be auto-registered?

Expand Down Expand Up @@ -189,15 +189,3 @@ class PostTypeFactory extends \TenUpPlugin\Module {
We've defined two classes, one using the default load order (`10`) and another with a custom load order (`9`).

Because of this, the `TaxonomyFactory` class will always be loaded before the `PostTypeFactory` class.

## Known Issues

### Could not locate `composer.json`

During deployment, we must deploy the `composer.json` file. This is how the class finder works, so if it doesn't exist you'll get an exception that states:

```
Could not locate composer.json. You can get around this by setting ClassFinder::$appRoot manually.
```

More information on this issue is available [here](https://gitlab.com/hpierce1102/ClassFinder/-/blob/master/docs/exceptions/missingComposerConfig.md).
14 changes: 13 additions & 1 deletion mu-plugins/10up-plugin/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@
],
"require": {
"php": ">=8.3",
"haydenpierce/class-finder": "^0.5.3"
"spatie/php-structure-discoverer": "^2.1"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"yoast/phpunit-polyfills": "^2.0"
},
"scripts": {
"test": "./vendor/bin/phpunit"
},
"autoload": {
"psr-4": {
Expand All @@ -20,5 +27,10 @@
"files": [
"includes/helpers/helpers.php"
]
},
"autoload-dev": {
"psr-4": {
"TenUpPluginTests\\": "tests/include/classes/"
}
}
}
Loading
Loading