From 9421cac4cd4f93f0da2b319ce5e2232bb5984867 Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Sun, 24 Mar 2024 11:27:40 +0100 Subject: [PATCH] test: migrate to Yoast WP test utils and work around incompatibilities --- .github/workflows/test.yml | 2 +- composer.json | 4 ++-- tests/bootstrap.php | 41 +++++++++++++++----------------------- tests/test-api.php | 4 ++-- 4 files changed, 21 insertions(+), 30 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 9c9db30..2cf6e6b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -15,7 +15,7 @@ jobs: - php: '8.0' wordpress: '5.9' - php: '7.2' - wordpress: '5.0' + wordpress: '5.2' steps: - name: Checkout uses: actions/checkout@v4 diff --git a/composer.json b/composer.json index 61d65bc..486e240 100644 --- a/composer.json +++ b/composer.json @@ -24,11 +24,11 @@ "dealerdirect/phpcodesniffer-composer-installer": "^v1.0", "matthiasmullie/minify": "^1.3", "phpcompatibility/phpcompatibility-wp": "^2.1", - "phpunit/phpunit": "^8|^9", + "phpunit/phpunit": "^7|^9", "slowprog/composer-copy-file": "~0.3", "squizlabs/php_codesniffer": "^3.9", "wp-coding-standards/wpcs": "^3.0", - "yoast/phpunit-polyfills": "^2.0" + "yoast/wp-test-utils": "^1.2" }, "scripts": { "post-install-cmd": [ diff --git a/tests/bootstrap.php b/tests/bootstrap.php index ede8667..6b764df 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -5,34 +5,25 @@ * @package SCLiveticker */ -$_tests_dir = getenv( 'WP_TESTS_DIR' ); +use Yoast\WPTestUtils\WPIntegration; -if ( ! $_tests_dir ) { - $_tests_dir = rtrim( sys_get_temp_dir(), '/\\' ) . '/wordpress-tests-lib'; -} +require_once dirname( __DIR__ ) . '/vendor/yoast/wp-test-utils/src/WPIntegration/bootstrap-functions.php'; -// Forward custom PHPUnit Polyfills configuration to PHPUnit bootstrap file. -$_phpunit_polyfills_path = getenv( 'WP_TESTS_PHPUNIT_POLYFILLS_PATH' ); -if ( false !== $_phpunit_polyfills_path ) { - define( 'WP_TESTS_PHPUNIT_POLYFILLS_PATH', $_phpunit_polyfills_path ); -} +$_tests_dir = WPIntegration\get_path_to_wp_test_dir(); -if ( ! file_exists( "{$_tests_dir}/includes/functions.php" ) ) { - echo "Could not find {$_tests_dir}/includes/functions.php, have you run bin/install-wp-tests.sh ?" . PHP_EOL; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped - exit( 1 ); -} +// Get access to tests_add_filter() function. +require_once $_tests_dir . 'includes/functions.php'; -// Give access to tests_add_filter() function. -require_once "{$_tests_dir}/includes/functions.php"; +// Add plugin to active mu-plugins to make sure it gets loaded. +tests_add_filter( + 'muplugins_loaded', + function() { + require dirname( __DIR__ ) . '/stklcode-liveticker.php'; + } +); -/** - * Manually load the plugin being tested. +/* + * Bootstrap WordPress. This will also load the Composer autoload file, the PHPUnit Polyfills + * and the custom autoloader for the TestCase and the mock object classes. */ -function _manually_load_plugin(): void { - require dirname( dirname( __FILE__ ) ) . '/stklcode-liveticker.php'; -} - -tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' ); - -// Start up the WP testing environment. -require "{$_tests_dir}/includes/bootstrap.php"; +WPIntegration\bootstrap_it(); diff --git a/tests/test-api.php b/tests/test-api.php index f78f0cb..274c498 100644 --- a/tests/test-api.php +++ b/tests/test-api.php @@ -13,12 +13,12 @@ use DateTime; use WP_REST_Request; use WP_REST_Server; -use WP_UnitTestCase; +use Yoast\WPTestUtils\WPIntegration\TestCase; /** * Class Test_API. */ -class Test_API extends WP_UnitTestCase { +class Test_API extends TestCase { /** * Initialize WP REST API for tests. *