test: migrate to Yoast WP test utils and work around incompatibilities

This commit is contained in:
Stefan Kalscheuer 2024-03-24 11:27:40 +01:00
parent 5f1e1a7879
commit fc957fd3b4
Signed by: stefan
GPG Key ID: 3887EC2A53B55430
4 changed files with 22 additions and 32 deletions

View File

@ -13,9 +13,9 @@ jobs:
- php: '8.1'
wordpress: '6.1'
- php: '8.0'
wordpress: '6.0'
- php: '7.4'
wordpress: '5.9'
- php: '7.2'
wordpress: '5.0'
steps:
- name: Checkout
uses: actions/checkout@v4
@ -25,7 +25,7 @@ jobs:
php-version: ${{ matrix.php }}
tools: composer
- name: Setup DB
run: sudo /etc/init.d/mysql start
run: sudo sudo systemctl start mysql.service
- name: Setup WP
run: bash bin/install-wp-tests.sh wordpress root root localhost "${{ matrix.wordpress }}"
- name: Install

View File

@ -24,11 +24,10 @@
"dealerdirect/phpcodesniffer-composer-installer": "^v1.0",
"matthiasmullie/minify": "^1.3",
"phpcompatibility/phpcompatibility-wp": "^2.1",
"phpunit/phpunit": "^8|^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": [

View File

@ -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();

View File

@ -13,12 +13,12 @@ use DateInterval;
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.
*