diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 695f59ddb..f41a7cafd 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -19,9 +19,10 @@ jobs: context: containers/php file: containers/php/Dockerfile-PHP-5.6 push: false - tags: lucatume/wp-browser-v3-build:php5.6-apache + tags: lucatume/wp-browser-v3-wordpress:php5.6-apache cache-from: type=gha,scope=5.6 cache-to: type=gha,mode=max,scope=5.6 + load: true build-args: | PHP_VERSION=5.6 WP_IMAGE_VERSION=5 @@ -35,9 +36,10 @@ jobs: context: containers/php file: containers/php/Dockerfile-PHP-5.6 push: false - tags: lucatume/wp-browser-v3-build:php5.6-codeception + tags: lucatume/wp-browser-v3-codecept:php5.6 cache-from: type=gha,scope=5.6 cache-to: type=gha,mode=max,scope=5.6 + load: true build-args: | PHP_VERSION=5.6 WP_IMAGE_VERSION=5 @@ -70,14 +72,15 @@ jobs: uses: docker/build-push-action@v4 with: context: containers/php - file: containers/php/Dockerfile + file: containers/php/Dockerfile-PHP-5.6 push: false - tags: lucatume/wp-browser-v3-build:php${{ matrix.php_version }}-apache - cache-from: type=gha,scope=${{ matrix.php_version }} - cache-to: type=gha,mode=max,scope=${{ matrix.php_version }} + tags: lucatume/wp-browser-v3-wordpress:php5.6-apache + cache-from: type=gha,scope=5.6 + cache-to: type=gha,mode=max,scope=5.6 + load: true build-args: | - PHP_VERSION=${{ matrix.php_version }} - WP_IMAGE_VERSION=6 + PHP_VERSION=5.6 + WP_IMAGE_VERSION=5 TARGET=wordpress USER_UID=33 USER_GID=33 @@ -88,9 +91,10 @@ jobs: context: containers/php file: containers/php/Dockerfile push: false - tags: lucatume/wp-browser-v3-build:php${{ matrix.php_version }}-codeception + tags: lucatume/wp-browser-v3-codecept:php${{ matrix.php_version }} cache-from: type=gha,scope=${{ matrix.php_version }} cache-to: type=gha,mode=max,scope=${{ matrix.php_version }} + load: true build-args: | PHP_VERSION=${{ matrix.php_version }} WP_IMAGE_VERSION=6 @@ -99,7 +103,7 @@ jobs: USER_GID=33 USER_NAME=www-data - name: Build - run: bin/stack -p${{ matrix.php_version }} build + run: bin/stack -p5.6 build - name: Cache Composer dependencies uses: actions/cache@v3 with: diff --git a/CHANGELOG.md b/CHANGELOG.md index 74a5f52c7..8fd527e22 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ This project adheres to [Semantic Versioning](http://semver.org/). ## [unreleased] Unreleased +### Fixed + +- Ensure `_before` and `_after` methods are called correctly by `WPTestCase`. + ## [3.2.0] 2023-09-15; ### Breaking change diff --git a/Makefile b/Makefile index 4b8994106..d6191f9bd 100644 --- a/Makefile +++ b/Makefile @@ -1,24 +1,8 @@ build: + bin/stack build_images bin/stack -p5.6 build - bin/stack -p7.0 build - bin/stack -p7.1 build - bin/stack -p7.2 build - bin/stack -p7.3 build - bin/stack -p7.4 build - bin/stack -p8.0 build - bin/stack -p8.1 build .PHONY: build -build_images: - bin/stack -p5.6 build_images - bin/stack -p7.0 build_images - bin/stack -p7.1 build_images - bin/stack -p7.2 build_images - bin/stack -p7.3 build_images - bin/stack -p7.4 build_images - bin/stack -p8.0 build_images - bin/stack -p8.1 build_images - build_56_lock_files: [ -d config/composer ] || mkdir config/composer rm -f config/composer/*5.6* @@ -40,46 +24,6 @@ build_56_lock_files: mv config/composer/composer.json.bak composer.json -build_74_lock_files: - [ -d config/composer ] || mkdir config/composer - cp composer.json config/composer/composer.json.bak - - bin/stack -p7.4 -d -c2 composer_update - mv composer.json config/composer/composer-7.4-codeception-2.json - mv composer.lock config/composer/composer-7.4-codeception-2.lock - cp config/composer/composer.json.bak composer.json - - bin/stack -p7.4 -d -c3 composer_update - mv composer.json config/composer/composer-7.4-codeception-3.json - mv composer.lock config/composer/composer-7.4-codeception-3.lock - cp config/composer/composer.json.bak composer.json - - bin/stack -p7.4 -d -c4 composer_update - mv composer.json config/composer/composer-7.4-codeception-4.json - mv composer.lock config/composer/composer-7.4-codeception-4.lock - - mv config/composer/composer.json.bak composer.json - -build_80_lock_files: - [ -d config/composer ] || mkdir config/composer - cp composer.json config/composer/composer.json.bak - - bin/stack -p8.0 -d -c4 composer_update - mv composer.json config/composer/composer-8.0-codeception-4.json - mv composer.lock config/composer/composer-8.0-codeception-4.lock - - mv config/composer/composer.json.bak composer.json - -build_81_lock_files: - [ -d config/composer ] || mkdir config/composer - cp composer.json config/composer/composer.json.bak - - bin/stack -p8.1 -d -c4 composer_update - mv composer.json config/composer/composer-8.1-codeception-4.json - mv composer.lock config/composer/composer-8.1-codeception-4.lock - - mv config/composer/composer.json.bak composer.json - lint: docker run --rm \ --volume "$(PWD):$(PWD):ro" \ @@ -140,14 +84,11 @@ static_analysis: lint phpcs phpstan .PHONE: static_analysis test: static_analysis - bin/stack -p5.6 xdebug-off && bin/stack -c2 composer_update && bin/stack -p5.6 test - bin/stack -p5.6 xdebug-off && bin/stack -c3 composer_update && bin/stack -p5.6 test - bin/stack -p5.6 xdebug-off && bin/stack -c4 composer_update && bin/stack -p5.6 test - bin/stack -p7.4 xdebug-off && bin/stack -c2 composer_update && bin/stack -p7.4 test - bin/stack -p7.4 xdebug-off && bin/stack -c3 composer_update && bin/stack -p7.4 test - bin/stack -p7.4 xdebug-off && bin/stack -c4 composer_update && bin/stack -p7.4 test - bin/stack -p8.0 xdebug-off && bin/stack -c4 composer_update && bin/stack -p8.0 test - bin/stack -p8.1 xdebug-off && bin/stack -c4 composer_update && bin/stack -p8.1 test + bin/stack -p5.6 xdebug-off && bin/stack composer_update && bin/stack -p5.6 test + bin/stack -p7.4 xdebug-off && bin/stack composer_update && bin/stack -p7.4 test + bin/stack -p8.0 xdebug-off && bin/stack composer_update && bin/stack -p8.0 test + bin/stack -p8.1 xdebug-off && bin/stack composer_update && bin/stack -p8.1 test + bin/stack -p8.2 xdebug-off && bin/stack composer_update && bin/stack -p8.2 test .PHONY: test clean: diff --git a/bin/stack b/bin/stack index a26f03bb3..a8d4d4f76 100755 --- a/bin/stack +++ b/bin/stack @@ -87,6 +87,12 @@ export USER_GID=$(id -g) export USER_NAME=$(id -un) export PWD="$(pwd)" +if [[ "${PHP_VERSION}" =~ ^(5.6|7.0|7.1|7.2|7.3)$ ]]; then + export WORDPRESS_VERSION=5 +else + export WORDPRESS_VERSION=6 +fi + # If the current architecture is arm64, use the arm64v8 images. if [ "$(arch)" = "arm64" ]; then export DOCKER_CHROME_IMAGE="seleniarm/standalone-chromium:116.0-20230828" @@ -154,10 +160,10 @@ function ensure_wordpress_scaffolded() { function run_wp_cli_command() { docker run --rm -v "${PWD}/var/wordpress:/var/www/html" \ - --network "wpbrowser_php_${PHP_VERSION}" \ + --network "wpbrowser_v3" \ -w /var/www/html \ -u "${USER_UID}:${USER_GID}" \ - "lucatume/wp-browser-v3-build:php${PHP_VERSION}-apache" \ + "lucatume/wp-browser-v3-wordpress:php5.6-apache" \ wp --allow-root --url=http://wordpress.test --path=/var/www/html "$@" } @@ -175,10 +181,10 @@ function ensure_wordpress_configured() { # If the var/wordpress/wp-config.php file is not found, configure WordPress using wp-cli. # Configure WordPress using wp-cli. if ! docker run --rm -v "${PWD}/var/wordpress:/var/www/html" \ - --network "wpbrowser_php_${PHP_VERSION}" \ + --network "wpbrowser_v3" \ -w /var/www/html \ -u "${USER_UID}:${USER_GID}" \ - "lucatume/wp-browser-v3-build:php${PHP_VERSION}-apache" \ + "lucatume/wp-browser-v3-wordpress:php5.6-apache" \ bash -c "wp --allow-root core config --dbname=wordpress --dbuser=root --dbpass=password --dbhost=database --dbprefix=wp_ --extra-php <settings = $settings; $this->name = $this->removeSuffix($name, 'Test'); $this->baseClass = $baseClass; - $this->compatibilityLayer = new Compatibility(); } /** @@ -106,7 +99,7 @@ public function produce() { $ns = $this->getNamespaceHeader($this->settings['namespace'] . '\\' . $this->name); - $phpunitSeries = $this->compatibilityLayer->phpunitVersion(); + $phpunitSeries = Version::series(); /** @var string $phpunitSeries */ $voidReturnType = is_string($phpunitSeries) && version_compare($phpunitSeries, '8.0', '<') ? @@ -155,16 +148,4 @@ protected function getTester() return ltrim($testerFrag); } - - /** - * Injects the compatibility layer object. - * - * @param Compatibility $compatibility An instance of the compatibility layer. - * - * @return void - */ - public function setCompatibilityLayer(Compatibility $compatibility) - { - $this->compatibilityLayer = $compatibility; - } } diff --git a/src/Codeception/TestCase/WPTestCase.php b/src/Codeception/TestCase/WPTestCase.php index c64cba188..0aa05dd01 100644 --- a/src/Codeception/TestCase/WPTestCase.php +++ b/src/Codeception/TestCase/WPTestCase.php @@ -28,10 +28,10 @@ use function version_compare; if (!class_exists('WP_UnitTest_Factory')) { - require_once dirname(dirname(dirname(__FILE__))) . '/includes/factory.php'; + require_once dirname(dirname( __DIR__ )) . '/includes/factory.php'; } if (!class_exists('TracTickets')) { - require_once dirname(dirname(dirname(__FILE__))) . '/includes/trac.php'; + require_once dirname(dirname( __DIR__ )) . '/includes/trac.php'; } /* @@ -49,22 +49,6 @@ setupPhpunitBootstrapGlobal(); } -// Load the PHPUnit compatibility layer. -// @phpstan-ignore-next-line -if (version_compare(Codecept::VERSION, '3.0.0', '<')) { - class_alias( - '\\tad\WPBrowser\\Compat\\Codeception\\Version2\\Unit', - '\\tad\\WPBrowser\\Compat\\Codeception\\Unit' - ); -} else { - class_alias( - '\\Codeception\\Test\\Unit', - '\\tad\\WPBrowser\\Compat\\Codeception\\Unit' - ); -} -// phpcs:enable - - /** * Defines a basic fixture to run multiple tests. * @@ -74,7 +58,7 @@ class_alias( * * All WordPress unit/integrations tests should inherit from this class. */ -class WPTestCase extends \tad\WPBrowser\Compat\Codeception\Unit +class WPTestCase extends Unit { use WithCodeceptionTestCaseEnhancements; @@ -277,15 +261,7 @@ public function _setUp() $this->maybeEnhanceTestCaseIfWoDiService(); - /** - * After WordPress has been initialized in the test context initialize the Codeception Unit test case. - * Check on what methods `\Codeception\Test\Unit` provides to call the correct one depending on the PHPUnit and - * Codeception versions. - */ - $unitSetupMethod = Compatibility::setupMethodFor(Unit::class); - if (method_exists(Unit::class, $unitSetupMethod)) { - Unit::{$unitSetupMethod}(); - } + Unit::_setUp(); } public function scan_user_uploads() @@ -512,6 +488,8 @@ public function _tearDown() $this->_restore_hooks(); wp_set_current_user(0); $this->requestTimeTearDown(); + + Unit::_tearDown(); } /** diff --git a/src/tad/WPBrowser/Compat/Codeception/Version2/Unit.php b/src/tad/WPBrowser/Compat/Codeception/Version2/Unit.php deleted file mode 100644 index f2cc48170..000000000 --- a/src/tad/WPBrowser/Compat/Codeception/Version2/Unit.php +++ /dev/null @@ -1,57 +0,0 @@ -_setUp(); - } - } - - protected function tearDown() - { - if (method_exists($this, '_tearDown')) { - $this->_tearDown(); - } - } -} diff --git a/src/tad/WPBrowser/Compat/Compatibility.php b/src/tad/WPBrowser/Compat/Compatibility.php deleted file mode 100644 index e00456529..000000000 --- a/src/tad/WPBrowser/Compat/Compatibility.php +++ /dev/null @@ -1,46 +0,0 @@ -setPhpUnitSeriesTo('8.0'); - $settings = ['namespace' => 'Acme']; - $name = 'SomeClass'; - $generator = new WPUnit($settings, $name, WPTestCase::class); - $generator->setCompatibilityLayer($this->compatibility); - - $code = $generator->produce(); - - $this->assertMatchesCodeSnapshot($code, 'php'); - } - - protected function setPhpUnitSeriesTo($series) - { - $this->compatibility = $this->make(Compatibility::class, [ - 'phpunitVersion' => $series - ]); - } - - public function phpUnitLt8Series() - { - return [ - '5.5' => ['5.5'], - '5.5.5' => ['5.5'], - '6.2' => ['6.2'], - '6.2.3' => ['6.2'], - '7.5' => ['7.5'], - '7.5.6' => ['7.5'], - ]; - } - - /** - * It should scaffold PHPUnit lt 8.0 compatible code on series lt 8 - * - * @test - * @dataProvider phpUnitLt8Series - */ - public function should_scaffold_php_unit_lt_8_0_compatible_code_on_series_lt_8($series) - { - $this->setPhpUnitSeriesTo($series); - $settings = ['namespace' => 'Acme']; - $name = 'SomeClass'; - $generator = new WPUnit($settings, $name, WPTestCase::class); - $generator->setCompatibilityLayer($this->compatibility); - - $code = $generator->produce(); - - $this->assertMatchesCodeSnapshot($code, 'php'); - } - - /** - * It should correctly add the tester property if actor is set in the settings - * - * @test - */ - public function should_correctly_add_the_tester_property_if_actor_is_set_in_the_settings() - { - $this->setPhpUnitSeriesTo('6.0'); - $settings = ['namespace' => 'Acme', 'actor' => 'Fixer']; - $name = 'SomeClass'; - $generator = new WPUnit($settings, $name, WPTestCase::class); - $generator->setCompatibilityLayer($this->compatibility); - - $code = $generator->produce(); - - $this->assertMatchesCodeSnapshot($code, 'php'); - } - - public function phpUnitEq8Series() - { - return [ - '8.0' => ['8.0'], - '8.0.4' => ['8.0.4'], - '8.1' => ['8.1'], - '8.1.6' => ['8.1.6'], - ]; - } - - protected function _before() - { - $this->phpunitSeriesEnv = getenv('WPBROWSER_PHPUNIT_SERIES'); - } - - protected function _after() - { - putenv('WPBROWSER_PHPUNIT_SERIES='.$this->phpunitSeriesEnv); - } +class WPUnitTest extends \Codeception\Test\Unit { + use WithUopz; + use SnapshotAssertions; + + /** + * A backup of the current PHPUnit Series env var. + * @var array|false|string + */ + protected $phpunitSeriesEnv; + /** + * @var \UnitTester + */ + protected $tester; + + /** + * @var string + */ + private $phpunitVersionPropertyBackup = null; + + private function setPhpUnitSeriesTo( $series ) { + $reflectionClass = new ReflectionClass( Version::class ); + $versionProp = $reflectionClass->getProperty( 'version' ); + $versionProp->setAccessible( true ); + $this->phpunitVersionPropertyBackup = $versionProp->getValue(); + $versionProp->setValue( $series ); + } + + /** + * @after + */ + public function restorePhpunitVersionProperty() { + if ( null === $this->phpunitVersionPropertyBackup ) { + return; + } + $reflectionClass = new ReflectionClass( Version::class ); + $versionProp = $reflectionClass->getProperty( 'version' ); + $versionProp->setAccessible( true ); + $versionProp->setValue( $this->phpunitVersionPropertyBackup ); + $this->phpunitVersionPropertyBackup = null; + } + + /** + * It should scaffold PHPUnit v8 compatible code on series 8 + * + * @test + * @dataProvider phpUnitEq8Series + */ + public function should_scaffold_php_unit_v_8_code_on_series_8( $series ) { + $this->setPhpUnitSeriesTo( '8.0' ); + $settings = [ 'namespace' => 'Acme' ]; + $name = 'SomeClass'; + + $generator = new WPUnit( $settings, $name, WPTestCase::class ); + $code = $generator->produce(); + + $this->assertMatchesCodeSnapshot( $code, 'php' ); + } + + public function phpUnitLt8Series() { + return [ + '5.5' => [ '5.5' ], + '5.5.5' => [ '5.5' ], + '6.2' => [ '6.2' ], + '6.2.3' => [ '6.2' ], + '7.5' => [ '7.5' ], + '7.5.6' => [ '7.5' ], + ]; + } + + /** + * It should scaffold PHPUnit lt 8.0 compatible code on series lt 8 + * + * @test + * @dataProvider phpUnitLt8Series + */ + public function should_scaffold_php_unit_lt_8_0_compatible_code_on_series_lt_8( $series ) { + $this->setPhpUnitSeriesTo( $series ); + $settings = [ 'namespace' => 'Acme' ]; + $name = 'SomeClass'; + + $generator = new WPUnit( $settings, $name, WPTestCase::class ); + $code = $generator->produce(); + + $this->assertMatchesCodeSnapshot( $code, 'php' ); + } + + /** + * It should correctly add the tester property if actor is set in the settings + * + * @test + */ + public function should_correctly_add_the_tester_property_if_actor_is_set_in_the_settings() { + $this->setPhpUnitSeriesTo( '6.0' ); + $settings = [ 'namespace' => 'Acme', 'actor' => 'Fixer' ]; + $name = 'SomeClass'; + + $generator = new WPUnit( $settings, $name, WPTestCase::class ); + $code = $generator->produce(); + + $this->assertMatchesCodeSnapshot( $code, 'php' ); + } + + public function phpUnitEq8Series() { + return [ + '8.0' => [ '8.0' ], + '8.0.4' => [ '8.0.4' ], + '8.1' => [ '8.1' ], + '8.1.6' => [ '8.1.6' ], + ]; + } } diff --git a/tests/wploadersuite/BeforeAfterMethodsTest.php b/tests/wploadersuite/BeforeAfterMethodsTest.php new file mode 100644 index 000000000..d83b24d73 --- /dev/null +++ b/tests/wploadersuite/BeforeAfterMethodsTest.php @@ -0,0 +1,24 @@ +canary = true; + } + + public function _after() { + self::$staticCanary = false; + } + + public static function wpTearDownAfterClass() { + Assert::assertFalse( self::$staticCanary ); + } + + public function test_before_method_is_loaded() { + $this->assertTrue( $this->canary ); + } +}