Skip to content

Commit

Permalink
Merge pull request #737 from lucatume/v4-phpunit-gte-10-support
Browse files Browse the repository at this point in the history
Explore PHPUnit gte 10 support
  • Loading branch information
lucatume authored Aug 21, 2024
2 parents 3e61ad8 + bd0268d commit a17c705
Show file tree
Hide file tree
Showing 57 changed files with 4,646 additions and 557 deletions.
24 changes: 22 additions & 2 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ jobs:
- climodule
- functional
- muloader
- unit
- unit --skip-group=slow
- unit --group=isolated-1
- unit --group=isolated-2
- webdriver
- wpcli_module
- wploader_multisite
Expand Down Expand Up @@ -70,10 +72,28 @@ jobs:
restore-keys: |
${{ runner.os }}-composer-
- name: Get the vendor/bin directory path
id: vendor-bin-dir
run: |
echo "dir=${{ github.workspace }}/vendor/bin" >> $GITHUB_OUTPUT
- name: Ensure the vendor/bin directory exists
run: mkdir -p ${{ steps.vendor-bin-dir.outputs.dir }}

- name: Restore vendor/bin from cache
uses: actions/cache@v3
id: vendor-bin-dir-cache
with:
path: ${{ steps.vendor-bin-dir.outputs.dir }}
key: ${{ runner.os }}-chromedriver-${{ steps.chrome-version.outputs.version }}
restore-keys: |
${{ runner.os }}-chromedriver-
- name: Install dependencies
run: composer update

- name: Update ChromeDriver
- name: Update ChromeDriver if required
if: steps.vendor-bin-dir-cache.outputs.cache-hit != 'true'
run: vendor/bin/codecept chromedriver:update --binary /usr/bin/google-chrome

- name: Create var/wordpress directory
Expand Down
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ clean:

clean_tmp:
rm -rf var/_output var/_tmp
rm -f var/_output/tmp/_monkeypatch var/_output/tmp/*.sqlite var/_output/tmp/*.sqlite_snapshot
.PHONY: clean_tmp

update_core_phpunit_includes:
Expand Down Expand Up @@ -45,7 +46,8 @@ wordpress_install:

clean_procs:
pgrep -f 'php -S' | xargs kill
pgrep chromedriver | xargs kill
-pkill -9 -f chromedriver
-pkill -9 -f mysqld
rm -f var/_output/*.pid var/_output/*.running
set -o allexport && source tests/.env && set +o allexport && docker compose down
.PHONY: clean_procs
Expand Down
11 changes: 9 additions & 2 deletions bin/setup-wp.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Codeception\Configuration;
use lucatume\WPBrowser\ManagedProcess\MysqlServer;
use lucatume\WPBrowser\Utils\Filesystem;
use lucatume\WPBrowser\WordPress\ConfigurationData;
use lucatume\WPBrowser\WordPress\Database\MysqlDatabase;
Expand All @@ -9,8 +10,6 @@
use lucatume\WPBrowser\WordPress\InstallationState\EmptyDir;
use lucatume\WPBrowser\WordPress\InstallationState\InstallationStateInterface;
use lucatume\WPBrowser\WordPress\InstallationState\Scaffolded;
$dockerComposeEnvFile = escapeshellarg(dirname(__DIR__) . '/tests/.env');
`docker compose --env-file $dockerComposeEnvFile up --wait`;

require_once dirname(__DIR__) . '/vendor/autoload.php';

Expand All @@ -23,6 +22,14 @@
$dotenv = Dotenv\Dotenv::createImmutable(dirname(__DIR__) . '/tests');
$env = $dotenv->load();

$mysqlServer = new MysqlServer(
codecept_output_dir('_mysql_server'),
$_ENV['WORDPRESS_DB_LOCALHOST_PORT'],
$_ENV['WORDPRESS_DB_NAME'],
$_ENV['WORDPRESS_DB_USER'],
$_ENV['WORDPRESS_DB_PASSWORD'],);
$mysqlServer->start();

$wpRootDir = $env['WORDPRESS_ROOT_DIR'];

echo "Checking WordPress directory $wpRootDir ...\n";
Expand Down
17 changes: 11 additions & 6 deletions codeception.dist.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ coverage:
- src/*
wpFolder: '%WORDPRESS_ROOT_DIR%'
extensions:
enabled:
- "lucatume\\WPBrowser\\Extension\\EventDispatcherBridge"
- "lucatume\\WPBrowser\\Extension\\BuiltInServerController"
- "lucatume\\WPBrowser\\Extension\\ChromeDriverController"
- "lucatume\\WPBrowser\\Extension\\DockerComposeController"
- "lucatume\\WPBrowser\\Extension\\IsolationSupport"
config:
"lucatume\\WPBrowser\\Extension\\BuiltInServerController":
docroot: '%WORDPRESS_ROOT_DIR%'
Expand All @@ -35,6 +29,17 @@ extensions:
"lucatume\\WPBrowser\\Extension\\DockerComposeController":
compose-file: docker-compose.yml
env-file: tests/.env
"lucatume\\WPBrowser\\Extension\\MysqlServerController":
port: '%WORDPRESS_DB_LOCALHOST_PORT%'
database: '%WORDPRESS_DB_NAME%'
user: '%WORDPRESS_DB_USER%'
password: '%WORDPRESS_DB_PASSWORD%'
enabled:
- "lucatume\\WPBrowser\\Extension\\EventDispatcherBridge"
- "lucatume\\WPBrowser\\Extension\\BuiltInServerController"
- "lucatume\\WPBrowser\\Extension\\ChromeDriverController"
- "lucatume\\WPBrowser\\Extension\\MysqlServerController"
- "lucatume\\WPBrowser\\Extension\\IsolationSupport"
commands:
- "lucatume\\WPBrowser\\Command\\RunOriginal"
- "lucatume\\WPBrowser\\Command\\RunAll"
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@
"ext-curl": "*",
"ext-zip": "*",
"composer-runtime-api": "^2.2",
"phpunit/phpunit": "<=12.0.0",
"codeception/codeception": "^5.0",
"codeception/module-asserts": "^2.0 || ^3.0",
"codeception/module-phpbrowser": "^2.0 || ^3.0",
"codeception/module-webdriver": "^2.0 || ^3.0",
"codeception/module-webdriver": "^2.0 || ^3.0 || ^4.0",
"codeception/module-db": "^2.0 || ^3.0",
"codeception/module-filesystem": "^2.0 || ^3.0",
"codeception/module-cli": "^2.0 || ^3.0",
Expand Down
48 changes: 45 additions & 3 deletions config/patches/core-phpunit/includes/abstract-testcase.php.patch
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
diff --git a/includes/core-phpunit/includes/abstract-testcase.php b/includes/core-phpunit/includes/abstract-testcase.php
index f2978644..22427643 100644
index f2978644..5c823a53 100644
--- a/includes/core-phpunit/includes/abstract-testcase.php
+++ b/includes/core-phpunit/includes/abstract-testcase.php
@@ -1,5 +1,7 @@
Expand Down Expand Up @@ -64,7 +64,49 @@ index f2978644..22427643 100644

if ( method_exists( $class, 'wpTearDownAfterClass' ) ) {
call_user_func( array( $class, 'wpTearDownAfterClass' ) );
@@ -651,7 +659,7 @@ public function expectedDeprecated() {
@@ -543,16 +551,31 @@ public function wp_die_handler( $message, $title, $args ) {
* @since 3.7.0
*/
public function expectDeprecated() {
- if ( method_exists( $this, 'getAnnotations' ) ) {
- // PHPUnit < 9.5.0.
- $annotations = $this->getAnnotations();
- } else {
- // PHPUnit >= 9.5.0.
- $annotations = \PHPUnit\Util\Test::parseTestMethodAnnotations(
- static::class,
- $this->getName( false )
- );
- }
+ if ( method_exists( $this, 'getAnnotations' ) ) {
+ // PHPUnit < 9.5.0.
+ $annotations = $this->getAnnotations();
+ } else if( version_compare(tests_get_phpunit_version(),'10.0.0','<')) {
+ // PHPUnit >= 9.5.0 < 10.0.0.
+ $annotations = \PHPUnit\Util\Test::parseTestMethodAnnotations(
+ static::class,
+ $this->getName( false )
+ );
+ } else {
+ // PHPUnit >= 10.0.0.
+ if (method_exists(static::class, $this->name())) {
+ $reflectionMethod = new \ReflectionMethod(static::class, $this->name());
+ $docBlock = \PHPUnit\Metadata\Annotation\Parser\DocBlock::ofMethod($reflectionMethod);
+ $annotations = [
+ 'method' => $docBlock->symbolAnnotations(),
+ 'class' => [],
+ ];
+ } else {
+ $annotations = [
+ 'method' => null,
+ 'class' => [],
+ ];
+ }
+ }

foreach ( array( 'class', 'method' ) as $depth ) {
if ( ! empty( $annotations[ $depth ]['expectedDeprecated'] ) ) {
@@ -651,7 +674,7 @@ public function expectedDeprecated() {
*
* @since 4.2.0
*/
Expand All @@ -73,7 +115,7 @@ index f2978644..22427643 100644
$this->expectedDeprecated();
}

@@ -1660,4 +1668,9 @@ public static function touch( $file ) {
@@ -1660,4 +1683,9 @@ public static function touch( $file ) {

touch( $file );
}
Expand Down
1 change: 1 addition & 0 deletions docs/extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ file, `codeception.yml` or `codeception.dist.yml` by default, in the `extensions
* [IsolationSupport](extensions/IsolationSupport.md)
* [Symlinker](extensions/Symlinker.md)
* [EventDispatcherBridge](extensions/EventDispatcherBridge.md)
* [MySqlServerController](extensions/MySqlServerController.md)
94 changes: 94 additions & 0 deletions docs/extensions/MySqlServerController.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
This extension will start and stop the MySQL server before and after the tests run.
The extension will take care of downloading the MySQL Community Server archive from
the [MySQL Community Server](https://dev.mysql.com/downloads/mysql/) site, place it in the `_mysql-server`
directory under the codeception output directory and initialize the server in the same directory.

The aim of this extension is to allow running integration tests against a real MySQL server, without having to
install and configure a MySQL server on the machine.

!!! warning

Currently the MySQL Community Server version installed by this extension (8.4.2 LTS) **is not available for Windows on ARM.**

If you are running Windows on ARM, you can either:
- Use a custom binary, see the [configuration examples](#configuration-examples) below
- Use the [Docker controller extension](DockerComposeController.md) to run the database from a Docker container.

### Configuration

The extension can be configured with the following parameters:

* required
* `port` - the localhost port to use for the MySQL server, defaults to `8906`.
* `database` - the database that will be created when starting the server, defaults to `wordpress`.
* `user` - the user that will be created when starting the server, defaults to `wordpress`. The user will be granted
all privileges on the database specified by the `database` parameter. If the user is `root`, no further user will
be created.
* `password` - the password to use for the user specified by the `user` parameter, defaults to `wordpress`. If the
user is `root`, the root user will be set to the password specified by this parameter.
* optional
* `suites` - an array of Codeception suites to run the server for; if not set the server will be started for all the
suites.
* `binary` - the path to the MySQL server binary to use, defaults to `mysqld`, defaults to `null` to download and
initialize the correct version of MySQL server for the current platform and architecture.
* `shareDir` - the path to the directory to use for the MySQL server share, defaults to `null`. **This is required
when providing a custom binary**.

### Configuration Examples

Example configuration starting the server for all suites:

```yaml
extensions:
enabled:
- "lucatume\WPBrowser\Extension\MySqlServerController":
port: 8906
database: wordpress
user: wordpress
password: wordpress
```
The extension can access environment variables defined in the tests configuration file:
```yaml
extensions:
enabled:
- "lucatume\WPBrowser\Extension\MySqlServerController":
port: '%MYSQL_SERVER_PORT%'
database: '%MYSQL_SERVER_DATABASE%'
user: '%MYSQL_SERVER_USER%'
password: '%MYSQL_SERVER_PASSWORD%'
```
Example configuration using the `root` user:

```yaml
extensions:
enabled:
- "lucatume\WPBrowser\Extension\MySqlServerController":
port: 33446
database: wordpress
user: root
password: secret
```

### Using a custom MySQL server binary

The extension can be configured to use a custom MySQL server binary by setting the `binary` configuration parameter to
the absolute path to the binary:

```yaml
extensions:
enabled:
- "lucatume\WPBrowser\Extension\MySqlServerController":
port: 33446
database: wordpress
user: root
password: secret
binary: /usr/local/mysql/bin/mysqld
```

### This is a service extension

This is a service extension that will be started and stopped by [the `dev:start`](../commands.md#devstart)
and [`dev:stop`](../commands.md#devstop) commands.
Loading

0 comments on commit a17c705

Please sign in to comment.