Skip to content

Commit

Permalink
test: migrate to Yoast WP test utils and work around incompatibilities
Browse files Browse the repository at this point in the history
  • Loading branch information
stklcode committed Mar 24, 2024
1 parent 5f1e1a7 commit 9421cac
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
41 changes: 16 additions & 25 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
4 changes: 2 additions & 2 deletions tests/test-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down

0 comments on commit 9421cac

Please sign in to comment.