From daa5a080447540a4dc954c7d190cd515dec90839 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Tue, 19 Nov 2024 10:24:19 -0500 Subject: [PATCH 01/18] update to phpunit 3 --- composer.json | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 14b09ff0..6b71011a 100644 --- a/composer.json +++ b/composer.json @@ -39,19 +39,17 @@ }, "require-dev": { "php": "^7 || ^8", - "brain/monkey": "^2.0", "coenjacobs/mozart": "^0.7", "dealerdirect/phpcodesniffer-composer-installer": "^0.7.0", "deliciousbrains/wp-background-processing": "~1.0", "league/container": "^3.0", "mnsami/composer-custom-directory-installer": "^2.0", "phpcompatibility/phpcompatibility-wp": "^2.0", - "phpunit/phpunit": "^7.5", "roave/security-advisories": "dev-master", "woocommerce/action-scheduler": "^3.4", "wp-coding-standards/wpcs": "^2", "wp-media/event-manager": "^3.1", - "wp-media/phpunit": "2.0" + "wp-media/phpunit": "3.0" }, "autoload": { "psr-4": { From 1d4a5446286a2c12ae164ee9b9875036d779ef04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Tue, 19 Nov 2024 10:24:31 -0500 Subject: [PATCH 02/18] add void typehint --- Tests/Unit/TestCase.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Tests/Unit/TestCase.php b/Tests/Unit/TestCase.php index e0246a05..79c9f7ff 100644 --- a/Tests/Unit/TestCase.php +++ b/Tests/Unit/TestCase.php @@ -21,7 +21,7 @@ abstract class TestCase extends PHPUnitTestCase { /** * Prepares the test environment before each test. */ - protected function setUp() { + protected function setUp(): void { parent::setUp(); Monkey\setUp(); @@ -35,7 +35,7 @@ protected function setUp() { /** * Cleans up the test environment after each test. */ - protected function tearDown() { + protected function tearDown(): void { Monkey\tearDown(); parent::tearDown(); } From 05215d1bd09398c46e145b0f020735e166cde260 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Tue, 19 Nov 2024 10:24:40 -0500 Subject: [PATCH 03/18] update declaration --- .../classes/ImagifySettings/updateSiteOptionOnNetwork.php | 6 +++--- Tests/Unit/inc/functions/imagifySanitizeContext.php | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Tests/Unit/inc/classes/ImagifySettings/updateSiteOptionOnNetwork.php b/Tests/Unit/inc/classes/ImagifySettings/updateSiteOptionOnNetwork.php index e3482348..c8a9c207 100644 --- a/Tests/Unit/inc/classes/ImagifySettings/updateSiteOptionOnNetwork.php +++ b/Tests/Unit/inc/classes/ImagifySettings/updateSiteOptionOnNetwork.php @@ -13,7 +13,7 @@ */ class Test_UpdateSiteOptionOnNetwork extends TestCase { - public function setUp() { + public function setUp(): void { parent::setUp(); if ( ! defined( 'IMAGIFY_SLUG' ) ) { @@ -23,10 +23,10 @@ public function setUp() { Functions\when( 'imagify_is_active_for_network' )->justReturn( true ); } - public function tearDown() { + public function tearDown(): void { unset( $_POST['option_page'] ); - return parent::tearDown(); + parent::tearDown(); } /** diff --git a/Tests/Unit/inc/functions/imagifySanitizeContext.php b/Tests/Unit/inc/functions/imagifySanitizeContext.php index 4de4b681..7c7f250d 100644 --- a/Tests/Unit/inc/functions/imagifySanitizeContext.php +++ b/Tests/Unit/inc/functions/imagifySanitizeContext.php @@ -7,7 +7,7 @@ class Test_ImagifySanitizeContext extends TestCase { - protected function setUp() { + protected function setUp(): void { parent::setUp(); require_once IMAGIFY_PLUGIN_ROOT . 'inc/functions/common.php'; From 9418af23a8050f43a731128e318e41252a682839 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Tue, 19 Nov 2024 10:25:03 -0500 Subject: [PATCH 04/18] ignore phpunit cache file --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 66154b4b..14c61583 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ composer.lock package-lock.json /_dev/node_modules +.phpunit.result.cache From 6e3033a6226e383741b95b2df0b1a3e960648198 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Wed, 20 Nov 2024 08:48:06 -0500 Subject: [PATCH 05/18] remove files --- Tests/TestCaseTrait.php | 151 ---------------------------------- Tests/bootstrap-functions.php | 50 ----------- 2 files changed, 201 deletions(-) delete mode 100644 Tests/TestCaseTrait.php delete mode 100644 Tests/bootstrap-functions.php diff --git a/Tests/TestCaseTrait.php b/Tests/TestCaseTrait.php deleted file mode 100644 index 3debd4c6..00000000 --- a/Tests/TestCaseTrait.php +++ /dev/null @@ -1,151 +0,0 @@ -setPropertyValue( 'instance', $class, $instance ); - } - - /** - * Reset the value of a private/protected property. - * - * @param string $property Property name for which to gain access. - * @param string|object $class Class name for a static property, or instance for an instance property. - * - * @return mixed The previous value of the property. - * @throws ReflectionException Throws an exception if property does not exist. - * - */ - protected function resetPropertyValue( $property, $class ) { - return $this->setPropertyValue( $property, $class, null ); - } - - /** - * Set the value of a private/protected property. - * - * @param string $property Property name for which to gain access. - * @param string|object $class Class name for a static property, or instance for an instance property. - * @param mixed $value The value to set to the property. - * - * @return mixed The previous value of the property. - * @throws ReflectionException Throws an exception if property does not exist. - * - */ - protected function setPropertyValue( $property, $class, $value ) { - $ref = $this->get_reflective_property( $property, $class ); - - if ( is_object( $class ) ) { - $previous = $ref->getValue( $class ); - // Instance property. - $ref->setValue( $class, $value ); - } else { - $previous = $ref->getValue(); - // Static property. - $ref->setValue( $value ); - } - - return $previous; - } - - /** ----------------------------------------------------------------------------------------- */ - /** REFLECTIONS ============================================================================= */ - /** ----------------------------------------------------------------------------------------- */ - - /** - * Get reflective access to a private/protected method. - * - * @param string $method_name Method name for which to gain access. - * @param string $class_name Name of the target class. - * - * @return ReflectionMethod - * @throws ReflectionException Throws an exception if method does not exist. - * - */ - protected function get_reflective_method( $method_name, $class_name ) { - $class = new ReflectionClass( $class_name ); - $method = $class->getMethod( $method_name ); - $method->setAccessible( true ); - - return $method; - } - - /** - * Get reflective access to a private/protected property. - * - * @param string $property Property name for which to gain access. - * @param string|mixed $class Class name or instance. - * - * @return ReflectionProperty - * @throws ReflectionException Throws an exception if property does not exist. - * - */ - protected function get_reflective_property( $property, $class ) { - $class = new ReflectionClass( $class ); - $property = $class->getProperty( $property ); - $property->setAccessible( true ); - - return $property; - } - - /** - * Set the value of a private/protected property. - * - * @param mixed $value The value to set for the property. - * @param string $property Property name for which to gain access. - * @param mixed $instance Instance of the target object. - * - * @return ReflectionProperty - * @throws ReflectionException Throws an exception if property does not exist. - * - */ - protected function set_reflective_property( $value, $property, $instance ) { - $property = $this->get_reflective_property( $property, $instance ); - $property->setValue( $instance, $value ); - $property->setAccessible( false ); - - return $property; - } -} diff --git a/Tests/bootstrap-functions.php b/Tests/bootstrap-functions.php deleted file mode 100644 index 7e8f12f3..00000000 --- a/Tests/bootstrap-functions.php +++ /dev/null @@ -1,50 +0,0 @@ - Date: Wed, 20 Nov 2024 08:48:38 -0500 Subject: [PATCH 06/18] update unit tests setup --- Tests/Unit/bootstrap.php | 7 ++++--- Tests/Unit/init-tests.php | 11 +++++++++++ Tests/Unit/phpunit.xml.dist | 2 +- 3 files changed, 16 insertions(+), 4 deletions(-) create mode 100644 Tests/Unit/init-tests.php diff --git a/Tests/Unit/bootstrap.php b/Tests/Unit/bootstrap.php index e0861164..1ef50450 100644 --- a/Tests/Unit/bootstrap.php +++ b/Tests/Unit/bootstrap.php @@ -7,7 +7,8 @@ namespace Imagify\Tests\Unit; -use function Imagify\Tests\init_test_suite; +define( 'IMAGIFY_PLUGIN_ROOT', dirname( dirname( __DIR__ ) ) . DIRECTORY_SEPARATOR ); +define( 'IMAGIFY_PLUGIN_TESTS_FIXTURES_DIR', dirname( __DIR__ ) . '/Fixtures' ); -require_once dirname( dirname( __FILE__ ) ) . '/bootstrap-functions.php'; -init_test_suite( 'Unit' ); +require_once IMAGIFY_PLUGIN_ROOT . 'inc/functions/api.php'; +include_once IMAGIFY_PLUGIN_TESTS_FIXTURES_DIR . '/WP/class-wp-error.php'; diff --git a/Tests/Unit/init-tests.php b/Tests/Unit/init-tests.php new file mode 100644 index 00000000..6ca7d05e --- /dev/null +++ b/Tests/Unit/init-tests.php @@ -0,0 +1,11 @@ + Date: Wed, 20 Nov 2024 08:48:49 -0500 Subject: [PATCH 07/18] Update test case --- Tests/Unit/TestCase.php | 92 +++++++++++++++++------------------------ 1 file changed, 37 insertions(+), 55 deletions(-) diff --git a/Tests/Unit/TestCase.php b/Tests/Unit/TestCase.php index 79c9f7ff..e224d22e 100644 --- a/Tests/Unit/TestCase.php +++ b/Tests/Unit/TestCase.php @@ -7,37 +7,18 @@ namespace Imagify\Tests\Unit; -use WPMedia\PHPUnit\Unit\TestCase as PHPUnitTestCase; -use Brain\Monkey; -use Imagify\Tests\TestCaseTrait; use ReflectionObject; -use WP_Error; +use WPMedia\PHPUnit\Unit\TestCase as PHPUnitTestCase; abstract class TestCase extends PHPUnitTestCase { - use TestCaseTrait; - protected $config; - /** - * Prepares the test environment before each test. - */ - protected function setUp(): void { - parent::setUp(); - Monkey\setUp(); - + protected function setUp() : void { if ( empty( $this->config ) ) { $this->loadTestDataConfig(); } - $this->mockCommonWpFunctions(); - } - - /** - * Cleans up the test environment after each test. - */ - protected function tearDown(): void { - Monkey\tearDown(); - parent::tearDown(); + parent::setUp(); } public function configTestData() { @@ -58,42 +39,43 @@ protected function loadTestDataConfig() { } /** - * Mock common WP functions. + * Reset the value of a private/protected property. + * + * @param string $property Property name for which to gain access. + * @param string|object $class Class name for a static property, or instance for an instance property. + * + * @return mixed The previous value of the property. + * @throws ReflectionException Throws an exception if property does not exist. + * */ - protected function mockCommonWpFunctions() { - Monkey\Functions\stubs( - [ - '__', - 'esc_attr__', - 'esc_html__', - '_x', - 'esc_attr_x', - 'esc_html_x', - '_n', - '_nx', - 'esc_attr', - 'esc_html', - 'esc_textarea', - 'esc_url', - ] - ); - - $functions = [ - '_e', - 'esc_attr_e', - 'esc_html_e', - '_ex', - ]; + protected function resetPropertyValue( $property, $class ) { + return $this->setPropertyValue( $property, $class, null ); + } - foreach ( $functions as $function ) { - Monkey\Functions\when( $function )->echoArg(); + /** + * Set the value of a private/protected property. + * + * @param string $property Property name for which to gain access. + * @param string|object $class Class name for a static property, or instance for an instance property. + * @param mixed $value The value to set to the property. + * + * @return mixed The previous value of the property. + * @throws ReflectionException Throws an exception if property does not exist. + * + */ + protected function setPropertyValue( $property, $class, $value ) { + $ref = $this->get_reflective_property( $property, $class ); + + if ( is_object( $class ) ) { + $previous = $ref->getValue( $class ); + // Instance property. + $ref->setValue( $class, $value ); + } else { + $previous = $ref->getValue(); + // Static property. + $ref->setValue( $value ); } - include_once IMAGIFY_PLUGIN_TESTS_ROOT . '../Fixtures/WP/class-wp-error.php'; - - Monkey\Functions\when( 'is_wp_error' ) - ->alias( function( $thing ) { - return $thing instanceof WP_Error; - } ); + return $previous; } } From 6bba38257131e3c3b4a6008df2ccae7cf0b03cda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Wed, 20 Nov 2024 08:49:13 -0500 Subject: [PATCH 08/18] update test --- .../Unit/classes/Admin/AdminSubscriber/pluginActionLinks.php | 5 +++++ .../classes/ImagifySettings/updateSiteOptionOnNetwork.php | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/Tests/Unit/classes/Admin/AdminSubscriber/pluginActionLinks.php b/Tests/Unit/classes/Admin/AdminSubscriber/pluginActionLinks.php index 8adb3e75..97418380 100644 --- a/Tests/Unit/classes/Admin/AdminSubscriber/pluginActionLinks.php +++ b/Tests/Unit/classes/Admin/AdminSubscriber/pluginActionLinks.php @@ -1,4 +1,6 @@ user = Mockery::mock( User::class ); $this->admin_subscriber = new AdminSubscriber( $this->user ); + + $this->stubTranslationFunctions(); + $this->stubEscapeFunctions(); } /** diff --git a/Tests/Unit/inc/classes/ImagifySettings/updateSiteOptionOnNetwork.php b/Tests/Unit/inc/classes/ImagifySettings/updateSiteOptionOnNetwork.php index c8a9c207..809d735b 100644 --- a/Tests/Unit/inc/classes/ImagifySettings/updateSiteOptionOnNetwork.php +++ b/Tests/Unit/inc/classes/ImagifySettings/updateSiteOptionOnNetwork.php @@ -1,4 +1,6 @@ justReturn( true ); + + $this->stubTranslationFunctions(); } public function tearDown(): void { From 2da0d6496f9efb9a0bbee56be3d216f6250672e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Wed, 20 Nov 2024 08:49:24 -0500 Subject: [PATCH 09/18] update integration tests setup --- Tests/Integration/bootstrap.php | 60 ++++-------------------------- Tests/Integration/init-tests.php | 11 ++++++ Tests/Integration/phpunit.xml.dist | 2 +- 3 files changed, 20 insertions(+), 53 deletions(-) create mode 100644 Tests/Integration/init-tests.php diff --git a/Tests/Integration/bootstrap.php b/Tests/Integration/bootstrap.php index 9c6d7749..1a464f09 100644 --- a/Tests/Integration/bootstrap.php +++ b/Tests/Integration/bootstrap.php @@ -7,58 +7,14 @@ namespace Imagify\Tests\Integration; -use function Imagify\Tests\init_test_suite; -require_once dirname( dirname( __FILE__ ) ) . '/bootstrap-functions.php'; -init_test_suite( 'Integration' ); +define( 'IMAGIFY_PLUGIN_ROOT', dirname( dirname( __DIR__ ) ) . DIRECTORY_SEPARATOR ); -/** - * Get the WordPress' tests suite directory. - * - * @return string Returns The directory path to the WordPress testing environment. - */ -function get_wp_tests_dir() { - $tests_dir = getenv( 'WP_TESTS_DIR' ); - - // Travis CI & Vagrant SSH tests directory. - if ( empty( $tests_dir ) ) { - $tests_dir = '/tmp/wordpress-tests-lib'; - } - - // If the tests' includes directory does not exist, try a relative path to Core tests directory. - if ( ! file_exists( $tests_dir . '/includes/' ) ) { - $tests_dir = '../../../../tests/phpunit'; - } - - // Check it again. If it doesn't exist, stop here and post a message as to why we stopped. - if ( ! file_exists( $tests_dir . '/includes/' ) ) { - trigger_error( 'Unable to run the integration tests, because the WordPress test suite could not be located.', E_USER_ERROR ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error -- Valid use case for our testing suite. +// Manually load the plugin being tested. +tests_add_filter( + 'muplugins_loaded', + function() { + // Load the plugin. + require IMAGIFY_PLUGIN_ROOT . '/imagify.php'; } - - // Strip off the trailing directory separator, if it exists. - return rtrim( $tests_dir, DIRECTORY_SEPARATOR ); -} - -/** - * Bootstraps the integration testing environment with WordPress and Imagify. - * - * @param string $wp_tests_dir The directory path to the WordPress testing environment. - */ -function bootstrap_integration_suite( $wp_tests_dir ) { - // Give access to tests_add_filter() function. - require_once $wp_tests_dir . '/includes/functions.php'; - - // Manually load the plugin being tested. - tests_add_filter( - 'muplugins_loaded', - function() { - // Load the plugin. - require IMAGIFY_PLUGIN_ROOT . '/imagify.php'; - } - ); - - // Start up the WP testing environment. - require_once $wp_tests_dir . '/includes/bootstrap.php'; -} - -bootstrap_integration_suite( get_wp_tests_dir() ); +); diff --git a/Tests/Integration/init-tests.php b/Tests/Integration/init-tests.php new file mode 100644 index 00000000..4323edbd --- /dev/null +++ b/Tests/Integration/init-tests.php @@ -0,0 +1,11 @@ + Date: Wed, 20 Nov 2024 08:49:31 -0500 Subject: [PATCH 10/18] update base test --- Tests/Integration/TestCase.php | 92 ++++++++++++++++++++++++++++++---- 1 file changed, 82 insertions(+), 10 deletions(-) diff --git a/Tests/Integration/TestCase.php b/Tests/Integration/TestCase.php index 20203e7c..be55aed7 100644 --- a/Tests/Integration/TestCase.php +++ b/Tests/Integration/TestCase.php @@ -2,26 +2,27 @@ namespace Imagify\Tests\Integration; -use Brain\Monkey; use Imagify; -use Imagify\Tests\TestCaseTrait; +use ReflectionObject; use WPMedia\PHPUnit\Integration\TestCase as BaseTestCase; abstract class TestCase extends BaseTestCase { - use TestCaseTrait; - protected $useApi = true; protected $api_credentials_config_file = 'imagify-api.php'; protected $invalidApiKey = '1234567890abcdefghijklmnopqrstuvwxyz'; protected $originalImagifyInstance; protected $originalApiKeyOption; + protected $config; /** * Prepares the test environment before each test. */ - public function setUp() { - parent::setUp(); - Monkey\setUp(); + public function set_up() { + parent::set_up(); + + if ( empty( $this->config ) ) { + $this->loadTestDataConfig(); + } if ( ! $this->useApi ) { return; @@ -35,9 +36,8 @@ public function setUp() { /** * Cleans up the test environment after each test. */ - public function tearDown() { - Monkey\tearDown(); - parent::tearDown(); + public function tear_down() { + parent::tear_down(); if ( ! $this->useApi ) { return; @@ -48,6 +48,23 @@ public function tearDown() { update_imagify_option( 'api_key', $this->originalApiKeyOption ); } + public function configTestData() { + if ( empty( $this->config ) ) { + $this->loadTestDataConfig(); + } + + return isset( $this->config['test_data'] ) + ? $this->config['test_data'] + : $this->config; + } + + protected function loadTestDataConfig() { + $obj = new ReflectionObject( $this ); + $filename = $obj->getFileName(); + + $this->config = $this->getTestData( dirname( $filename ), basename( $filename, '.php' ) ); + } + /** * Gets the credential's value from either an environment variable (stored locally on the machine or CI) or from a * local constant defined in `tests/env/local/imagify-api.php`. @@ -86,4 +103,59 @@ protected function getApiCredential( $name ) { return constant( $name ); } + + /** + * Set the singleton's `$instance` property to the given instance. + * + * @param string $class Name of the target class. + * @param mixed $instance Instance of the target object. + * + * @return mixed Previous value. + * @throws ReflectionException Throws an exception if property does not exist. + * + */ + protected function setSingletonInstance( $class, $instance ) { + return $this->setPropertyValue( 'instance', $class, $instance ); + } + + /** + * Set the value of a private/protected property. + * + * @param string $property Property name for which to gain access. + * @param string|object $class Class name for a static property, or instance for an instance property. + * @param mixed $value The value to set to the property. + * + * @return mixed The previous value of the property. + * @throws ReflectionException Throws an exception if property does not exist. + * + */ + protected function setPropertyValue( $property, $class, $value ) { + $ref = $this->get_reflective_property( $property, $class ); + + if ( is_object( $class ) ) { + $previous = $ref->getValue( $class ); + // Instance property. + $ref->setValue( $class, $value ); + } else { + $previous = $ref->getValue(); + // Static property. + $ref->setValue( $value ); + } + + return $previous; + } + + /** + * Reset the value of a private/protected property. + * + * @param string $property Property name for which to gain access. + * @param string|object $class Class name for a static property, or instance for an instance property. + * + * @return mixed The previous value of the property. + * @throws ReflectionException Throws an exception if property does not exist. + * + */ + protected function resetPropertyValue( $property, $class ) { + return $this->setPropertyValue( $property, $class, null ); + } } From 810651aa10b62a23ce4f9206d134eac18c504951 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Wed, 20 Nov 2024 08:49:36 -0500 Subject: [PATCH 11/18] update tests --- .../ImagifySettings/updateSiteOptionOnNetwork.php | 12 ++++-------- .../Integration/inc/classes/ImagifyUser/TestCase.php | 8 ++++---- .../Integration/inc/classes/ImagifyUser/getError.php | 2 +- .../classes/ImagifyUser/getPercentConsumedQuota.php | 8 ++++---- .../inc/classes/ImagifyUser/isOverQuota.php | 8 ++++---- 5 files changed, 17 insertions(+), 21 deletions(-) diff --git a/Tests/Integration/inc/classes/ImagifySettings/updateSiteOptionOnNetwork.php b/Tests/Integration/inc/classes/ImagifySettings/updateSiteOptionOnNetwork.php index 645d92f8..cd825d1e 100644 --- a/Tests/Integration/inc/classes/ImagifySettings/updateSiteOptionOnNetwork.php +++ b/Tests/Integration/inc/classes/ImagifySettings/updateSiteOptionOnNetwork.php @@ -1,4 +1,5 @@ getTestData( __DIR__, 'updateSiteOptionOnNetwork' ); + return parent::tear_down(); } /** - * @dataProvider provideData + * @dataProvider configTestData */ public function testShouldUpdateNetworkSettings( $config, $expected ) { $_POST['option_page'] = $config['option_page']; diff --git a/Tests/Integration/inc/classes/ImagifyUser/TestCase.php b/Tests/Integration/inc/classes/ImagifyUser/TestCase.php index 79c1a9a5..0e268515 100644 --- a/Tests/Integration/inc/classes/ImagifyUser/TestCase.php +++ b/Tests/Integration/inc/classes/ImagifyUser/TestCase.php @@ -8,8 +8,8 @@ abstract class TestCase extends BaseTestCase { protected $originalUserInstance; - public function setUp() { - parent::setUp(); + public function set_up() { + parent::set_up(); $this->originalUserInstance = $this->resetPropertyValue( 'user', Imagify::class ); @@ -17,8 +17,8 @@ public function setUp() { delete_transient('imagify_user_cache'); } - public function tearDown() { - parent::tearDown(); + public function tear_down() { + parent::tear_down(); // Restore the user on the static property. $this->setPropertyValue( 'user', Imagify::class, $this->originalUserInstance ); diff --git a/Tests/Integration/inc/classes/ImagifyUser/getError.php b/Tests/Integration/inc/classes/ImagifyUser/getError.php index aade75ab..9c0078f2 100644 --- a/Tests/Integration/inc/classes/ImagifyUser/getError.php +++ b/Tests/Integration/inc/classes/ImagifyUser/getError.php @@ -46,6 +46,6 @@ public function testShouldReturnErrorWhenCouldNotFetchUserData() { $user_data = $this->getNonPublicPropertyValue( 'user', Imagify::class ); $this->assertInstanceOf( WP_Error::class, $user_data ); - $this->assertContains( 'Invalid token', $user_data->get_error_message() ); + $this->assertStringContainsString( 'Invalid token', $user_data->get_error_message() ); } } diff --git a/Tests/Integration/inc/classes/ImagifyUser/getPercentConsumedQuota.php b/Tests/Integration/inc/classes/ImagifyUser/getPercentConsumedQuota.php index c17400be..76607c4d 100644 --- a/Tests/Integration/inc/classes/ImagifyUser/getPercentConsumedQuota.php +++ b/Tests/Integration/inc/classes/ImagifyUser/getPercentConsumedQuota.php @@ -14,14 +14,14 @@ class Test_GetPercentConsumedQuota extends TestCase { private $originalPreviousQuotaOption; - public function setUp() { - parent::setUp(); + public function set_up() { + parent::set_up(); $this->originalPreviousQuotaOption = get_imagify_option( 'previous_quota_percent' ); } - public function tearDown() { - parent::tearDown(); + public function tear_down() { + parent::tear_down(); // Restore the original option. update_imagify_option( 'previous_quota_percent', $this->originalPreviousQuotaOption ); diff --git a/Tests/Integration/inc/classes/ImagifyUser/isOverQuota.php b/Tests/Integration/inc/classes/ImagifyUser/isOverQuota.php index d8f03084..f772aecc 100644 --- a/Tests/Integration/inc/classes/ImagifyUser/isOverQuota.php +++ b/Tests/Integration/inc/classes/ImagifyUser/isOverQuota.php @@ -13,14 +13,14 @@ class Test_IsOverQuota extends TestCase { private $originalPreviousQuotaOption; - public function setUp() { - parent::setUp(); + public function set_up() { + parent::set_up(); $this->originalPreviousQuotaOption = get_imagify_option( 'previous_quota_percent' ); } - public function tearDown() { - parent::tearDown(); + public function tear_down() { + parent::tear_down(); // Restore the original option. update_imagify_option( 'previous_quota_percent', $this->originalPreviousQuotaOption ); From d0758e56a8b3cd5bb92a86be917473b6f36639e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Wed, 20 Nov 2024 08:54:45 -0500 Subject: [PATCH 12/18] update phpunit constrains --- composer.json | 1 + 1 file changed, 1 insertion(+) diff --git a/composer.json b/composer.json index 6b71011a..f2266031 100644 --- a/composer.json +++ b/composer.json @@ -45,6 +45,7 @@ "league/container": "^3.0", "mnsami/composer-custom-directory-installer": "^2.0", "phpcompatibility/phpcompatibility-wp": "^2.0", + "phpunit/phpunit": "^7.5 || ^8 || ^9", "roave/security-advisories": "dev-master", "woocommerce/action-scheduler": "^3.4", "wp-coding-standards/wpcs": "^2", From 1d1c62ca8b57d61c1955adf7878d53af5b4fc3f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Wed, 20 Nov 2024 08:58:37 -0500 Subject: [PATCH 13/18] force PHPUnit version --- .github/workflows/test_plugin_legacy.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/test_plugin_legacy.yml b/.github/workflows/test_plugin_legacy.yml index a7ac2101..380996cf 100644 --- a/.github/workflows/test_plugin_legacy.yml +++ b/.github/workflows/test_plugin_legacy.yml @@ -52,6 +52,9 @@ jobs: - name: Remove unmet dependencies by legacy versions run: composer remove --dev --no-scripts coenjacobs/mozart + - name: Require PHPUnit 7.5 for WP compatibility + run: composer require --dev --no-scripts phpunit/phpunit "^7.5" -W + - name: Install dependencies run: composer install --prefer-dist --no-interaction --no-scripts From 360bb0078d9037b372ae492543939a9cbf0c366b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Wed, 20 Nov 2024 09:03:09 -0500 Subject: [PATCH 14/18] update brain monkey constrains --- composer.json | 1 + 1 file changed, 1 insertion(+) diff --git a/composer.json b/composer.json index f2266031..c3df36cd 100644 --- a/composer.json +++ b/composer.json @@ -39,6 +39,7 @@ }, "require-dev": { "php": "^7 || ^8", + "brain/monkey": "^2.0", "coenjacobs/mozart": "^0.7", "dealerdirect/phpcodesniffer-composer-installer": "^0.7.0", "deliciousbrains/wp-background-processing": "~1.0", From d4cb83eb8e4565fdce935bd11692c54d1306a6a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Wed, 20 Nov 2024 09:06:40 -0500 Subject: [PATCH 15/18] update config --- .github/workflows/test_plugin_legacy.yml | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test_plugin_legacy.yml b/.github/workflows/test_plugin_legacy.yml index 380996cf..f6938f68 100644 --- a/.github/workflows/test_plugin_legacy.yml +++ b/.github/workflows/test_plugin_legacy.yml @@ -27,7 +27,7 @@ jobs: steps: - name: Checkout - uses: actions/checkout@v2 + uses: actions/checkout@v4 - name: Setup PHP uses: shivammathur/setup-php@v2 @@ -35,11 +35,28 @@ jobs: php-version: ${{ matrix.php-versions }} extensions: mysqli coverage: none # XDebug can be enabled here 'coverage: xdebug' - tools: composer:v2 + tools: composer:v2, phpunit - name: Start mysql service run: sudo /etc/init.d/mysql start + - name: Setup problem matchers for PHP + run: echo "::add-matcher::${{ runner.tool_cache }}/php.json" + + - name: Setup problem matchers for PHPUnit + run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json" + + - name: Get composer cache directory + id: composercache + run: echo "::set-output name=dir::$(composer config cache-files-dir)" + + - name: Cache dependencies + uses: actions/cache@v4 + with: + path: ${{ steps.composercache.outputs.dir }} + key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }} + restore-keys: ${{ runner.os }}-composer- + - name: Mysql8 auth plugin workaround run: sudo mysql -u root -proot -e "ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root';" From 59a21ec38b05ba7fcfabcafe3eca9aadead15076 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Wed, 20 Nov 2024 09:09:51 -0500 Subject: [PATCH 16/18] update config --- .github/workflows/test_plugin_legacy.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.github/workflows/test_plugin_legacy.yml b/.github/workflows/test_plugin_legacy.yml index f6938f68..4e7b6189 100644 --- a/.github/workflows/test_plugin_legacy.yml +++ b/.github/workflows/test_plugin_legacy.yml @@ -66,9 +66,6 @@ jobs: - name: Setup problem matchers for PHPUnit run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json" - - name: Remove unmet dependencies by legacy versions - run: composer remove --dev --no-scripts coenjacobs/mozart - - name: Require PHPUnit 7.5 for WP compatibility run: composer require --dev --no-scripts phpunit/phpunit "^7.5" -W From 2382ca752db77abbce6817a19e8f007a7e731a2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Wed, 20 Nov 2024 09:19:34 -0500 Subject: [PATCH 17/18] add more PHP versions to the matrix --- .github/workflows/test_php8.yml | 2 +- .github/workflows/test_plugin_legacy.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test_php8.yml b/.github/workflows/test_php8.yml index cf2e5c4d..53ebdefb 100644 --- a/.github/workflows/test_php8.yml +++ b/.github/workflows/test_php8.yml @@ -15,7 +15,7 @@ jobs: fail-fast: false matrix: operating-system: [ubuntu-latest] - php-versions: ['8.0'] + php-versions: ['8.0', '8.1', '8.2'] wp-versions: ['latest'] name: WP ${{ matrix.wp-versions }} with PHP ${{ matrix.php-versions }} on ${{ matrix.operating-system }}. diff --git a/.github/workflows/test_plugin_legacy.yml b/.github/workflows/test_plugin_legacy.yml index 4e7b6189..f654fa4a 100644 --- a/.github/workflows/test_plugin_legacy.yml +++ b/.github/workflows/test_plugin_legacy.yml @@ -15,7 +15,7 @@ jobs: fail-fast: false matrix: operating-system: [ubuntu-latest] - php-versions: ['7.3'] + php-versions: ['7.3', '7.4'] wp-versions: ['5.3'] name: WP ${{ matrix.wp-versions }} with PHP ${{ matrix.php-versions }} on ${{ matrix.operating-system }}. From 57502c6b795769ca5cf195b0cf2fb2573fd92a67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Wed, 20 Nov 2024 09:22:45 -0500 Subject: [PATCH 18/18] update workflows --- .github/workflows/test.yml | 100 +++++++++--------- ...test_plugin_legacy.yml => test_legacy.yml} | 0 .github/workflows/test_php8.yml | 69 ------------ 3 files changed, 50 insertions(+), 119 deletions(-) rename .github/workflows/{test_plugin_legacy.yml => test_legacy.yml} (100%) delete mode 100644 .github/workflows/test_php8.yml diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index d9f3b478..9cecbe07 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,69 +1,69 @@ name: Unit/Integration tests on: - pull_request: - branches: - - trunk - - develop - - branch-* + pull_request: + branches: + - trunk + - develop + - branch-* jobs: - run: - runs-on: ${{ matrix.operating-system }} + run: + runs-on: ${{ matrix.operating-system }} - strategy: - fail-fast: false - matrix: - operating-system: [ubuntu-latest] - php-versions: ['7.4'] - wp-versions: ['latest'] + strategy: + fail-fast: false + matrix: + operating-system: [ubuntu-latest] + php-versions: ['8.0', '8.1', '8.2'] + wp-versions: ['latest'] - name: WP ${{ matrix.wp-versions }} with PHP ${{ matrix.php-versions }} on ${{ matrix.operating-system }}. + name: WP ${{ matrix.wp-versions }} with PHP ${{ matrix.php-versions }} on ${{ matrix.operating-system }}. - env: - WP_TESTS_DIR: "/tmp/tests/phpunit" - WP_CORE_DIR: "/tmp/wordpress-develop" - IMAGIFY_TESTS_API_KEY: ${{ secrets.IMAGIFY_TESTS_API_KEY }} + env: + WP_TESTS_DIR: "/tmp/tests/phpunit" + WP_CORE_DIR: "/tmp/wordpress-develop" + IMAGIFY_TESTS_API_KEY: ${{ secrets.IMAGIFY_TESTS_API_KEY }} - steps: - - name: Checkout - uses: actions/checkout@v2 + steps: + - name: Checkout + uses: actions/checkout@v2 - - name: Setup PHP - uses: shivammathur/setup-php@v2 - with: - php-version: ${{ matrix.php-versions }} - coverage: none # XDebug can be enabled here 'coverage: xdebug' - tools: composer:v2, phpunit + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php-versions }} + coverage: none # XDebug can be enabled here 'coverage: xdebug' + tools: composer:v2, phpunit - - name: Start mysql service - run: sudo /etc/init.d/mysql start + - name: Start mysql service + run: sudo /etc/init.d/mysql start - - name: Setup problem matchers for PHP - run: echo "::add-matcher::${{ runner.tool_cache }}/php.json" + - name: Setup problem matchers for PHP + run: echo "::add-matcher::${{ runner.tool_cache }}/php.json" - - name: Setup problem matchers for PHPUnit - run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json" + - name: Setup problem matchers for PHPUnit + run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json" - - name: Get composer cache directory - id: composercache - run: echo "::set-output name=dir::$(composer config cache-files-dir)" + - name: Get composer cache directory + id: composercache + run: echo "::set-output name=dir::$(composer config cache-files-dir)" - - name: Cache dependencies - uses: actions/cache@v2 - with: - path: ${{ steps.composercache.outputs.dir }} - key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }} - restore-keys: ${{ runner.os }}-composer- + - name: Cache dependencies + uses: actions/cache@v2 + with: + path: ${{ steps.composercache.outputs.dir }} + key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }} + restore-keys: ${{ runner.os }}-composer- - - name: Install dependencies - run: composer install --prefer-dist --no-interaction --no-scripts + - name: Install dependencies + run: composer install --prefer-dist --no-interaction --no-scripts --ignore-platform-reqs - - name: Install tests - run: bash bin/install-wp-tests.sh wordpress_test root root 127.0.0.1:3306 ${{ matrix.wp-versions }} + - name: Install tests + run: bash bin/install-wp-tests.sh wordpress_test root root 127.0.0.1:3306 ${{ matrix.wp-versions }} - - name: Mysql8 auth plugin workaround - run: sudo mysql -u root -proot -e "ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root';" + - name: Mysql8 auth plugin workaround + run: sudo mysql -u root -proot -e "ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root';" - - name: Test - run: composer run-tests + - name: Test + run: composer run-tests diff --git a/.github/workflows/test_plugin_legacy.yml b/.github/workflows/test_legacy.yml similarity index 100% rename from .github/workflows/test_plugin_legacy.yml rename to .github/workflows/test_legacy.yml diff --git a/.github/workflows/test_php8.yml b/.github/workflows/test_php8.yml deleted file mode 100644 index 53ebdefb..00000000 --- a/.github/workflows/test_php8.yml +++ /dev/null @@ -1,69 +0,0 @@ -name: Unit/Integration tests for PHP 8 - -on: - pull_request: - branches: - - trunk - - develop - - branch-* - -jobs: - run: - runs-on: ${{ matrix.operating-system }} - - strategy: - fail-fast: false - matrix: - operating-system: [ubuntu-latest] - php-versions: ['8.0', '8.1', '8.2'] - wp-versions: ['latest'] - - name: WP ${{ matrix.wp-versions }} with PHP ${{ matrix.php-versions }} on ${{ matrix.operating-system }}. - - env: - WP_TESTS_DIR: "/tmp/tests/phpunit" - WP_CORE_DIR: "/tmp/wordpress-develop" - IMAGIFY_TESTS_API_KEY: ${{ secrets.IMAGIFY_TESTS_API_KEY }} - - steps: - - name: Checkout - uses: actions/checkout@v2 - - - name: Setup PHP - uses: shivammathur/setup-php@v2 - with: - php-version: ${{ matrix.php-versions }} - coverage: none # XDebug can be enabled here 'coverage: xdebug' - tools: composer:v2, phpunit - - - name: Start mysql service - run: sudo /etc/init.d/mysql start - - - name: Setup problem matchers for PHP - run: echo "::add-matcher::${{ runner.tool_cache }}/php.json" - - - name: Setup problem matchers for PHPUnit - run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json" - - - name: Get composer cache directory - id: composercache - run: echo "::set-output name=dir::$(composer config cache-files-dir)" - - - name: Cache dependencies - uses: actions/cache@v2 - with: - path: ${{ steps.composercache.outputs.dir }} - key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }} - restore-keys: ${{ runner.os }}-composer- - - - name: Install dependencies - run: composer install --prefer-dist --no-interaction --no-scripts --ignore-platform-reqs - - - name: Install tests - run: bash bin/install-wp-tests.sh wordpress_test root root 127.0.0.1:3306 ${{ matrix.wp-versions }} - - - name: Mysql8 auth plugin workaround - run: sudo mysql -u root -proot -e "ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root';" - - - name: Test - run: composer run-tests