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

View File

@ -24,11 +24,10 @@
"dealerdirect/phpcodesniffer-composer-installer": "^v1.0", "dealerdirect/phpcodesniffer-composer-installer": "^v1.0",
"matthiasmullie/minify": "^1.3", "matthiasmullie/minify": "^1.3",
"phpcompatibility/phpcompatibility-wp": "^2.1", "phpcompatibility/phpcompatibility-wp": "^2.1",
"phpunit/phpunit": "^8|^9",
"slowprog/composer-copy-file": "~0.3", "slowprog/composer-copy-file": "~0.3",
"squizlabs/php_codesniffer": "^3.9", "squizlabs/php_codesniffer": "^3.9",
"wp-coding-standards/wpcs": "^3.0", "wp-coding-standards/wpcs": "^3.0",
"yoast/phpunit-polyfills": "^2.0" "yoast/wp-test-utils": "^1.2"
}, },
"scripts": { "scripts": {
"post-install-cmd": [ "post-install-cmd": [

View File

@ -5,34 +5,25 @@
* @package SCLiveticker * @package SCLiveticker
*/ */
$_tests_dir = getenv( 'WP_TESTS_DIR' ); use Yoast\WPTestUtils\WPIntegration;
if ( ! $_tests_dir ) { require_once dirname( __DIR__ ) . '/vendor/yoast/wp-test-utils/src/WPIntegration/bootstrap-functions.php';
$_tests_dir = rtrim( sys_get_temp_dir(), '/\\' ) . '/wordpress-tests-lib';
$_tests_dir = WPIntegration\get_path_to_wp_test_dir();
// Get 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';
} }
);
// Forward custom PHPUnit Polyfills configuration to PHPUnit bootstrap file. /*
$_phpunit_polyfills_path = getenv( 'WP_TESTS_PHPUNIT_POLYFILLS_PATH' ); * Bootstrap WordPress. This will also load the Composer autoload file, the PHPUnit Polyfills
if ( false !== $_phpunit_polyfills_path ) { * and the custom autoloader for the TestCase and the mock object classes.
define( 'WP_TESTS_PHPUNIT_POLYFILLS_PATH', $_phpunit_polyfills_path );
}
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 );
}
// Give access to tests_add_filter() function.
require_once "{$_tests_dir}/includes/functions.php";
/**
* Manually load the plugin being tested.
*/ */
function _manually_load_plugin(): void { WPIntegration\bootstrap_it();
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";

View File

@ -13,12 +13,12 @@ use DateInterval;
use DateTime; use DateTime;
use WP_REST_Request; use WP_REST_Request;
use WP_REST_Server; use WP_REST_Server;
use WP_UnitTestCase; use Yoast\WPTestUtils\WPIntegration\TestCase;
/** /**
* Class Test_API. * Class Test_API.
*/ */
class Test_API extends WP_UnitTestCase { class Test_API extends TestCase {
/** /**
* Initialize WP REST API for tests. * Initialize WP REST API for tests.
* *