Skip to content

Commit

Permalink
Utilize WordPress PHPUnit Test Library Included By wp-env (#41852)
Browse files Browse the repository at this point in the history
* Removed `wp-phpunit` Dependency

We're going to be running PHPUnit tests
using the in-container test files. We will
no longer need `wp-phpunit/wp-phpunit`.

* Utilized `Yoast/PHPUnit-Polyfills` In Tests

In order to work on PHP 8, we must use the methods
for setUp(), tearDown(), setUpBeforeClass(), tearDownAfterClass().
This commit replaces those with polyfill versions.

* Stopped Using `phpunit` Container For Tests

This commit replaces the `run phpunit` unit test scripts
with ones that run using the project's PHPUnit.

* Marked Risky Tests Incomplete

Rather than being heavy-handed and disabling the
strict handling of tests that do nothing, we should
mark those tests as incomplete.
  • Loading branch information
ObliviousHarmony authored Jul 15, 2022
1 parent 9b13f84 commit 1eb1ba1
Show file tree
Hide file tree
Showing 21 changed files with 566 additions and 633 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ node_modules
gutenberg.zip
coverage
*-performance-results.json
.phpunit.result.cache

# Directories/files that may appear in your environment
*.log
Expand Down
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@
"phpcompatibility/php-compatibility": "^9.3",
"wp-coding-standards/wpcs": "^2.2",
"sirbrillig/phpcs-variable-analysis": "^2.8",
"wp-phpunit/wp-phpunit": "^5.4",
"phpunit/phpunit": "^8.5",
"phpunit/phpunit": "^6.5",
"spatie/phpunit-watcher": "^1.23",
"yoast/phpunit-polyfills": "^1.0"
},
Expand Down
987 changes: 440 additions & 547 deletions composer.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,9 @@
"test-unit:update": "npm run test-unit -- --updateSnapshot",
"test-unit:watch": "npm run test-unit -- --watch",
"pretest-unit-php": "wp-env start",
"test-unit-php": "wp-env run phpunit \"phpunit -c /var/www/html/wp-content/plugins/gutenberg/phpunit.xml.dist --verbose\"",
"test-unit-php": "wp-env run tests-wordpress /var/www/html/wp-content/plugins/gutenberg/vendor/bin/phpunit -c /var/www/html/wp-content/plugins/gutenberg/phpunit.xml.dist --verbose",
"pretest-unit-php-multisite": "wp-env start",
"test-unit-php-multisite": "wp-env run phpunit \"WP_MULTISITE=1 phpunit -c /var/www/html/wp-content/plugins/gutenberg/phpunit/multisite.xml --verbose\"",
"test-unit-php-multisite": "wp-env run tests-wordpress /var/www/html/wp-content/plugins/gutenberg/vendor/bin/phpunit -c /var/www/html/wp-content/plugins/gutenberg/phpunit/multisite.xml --verbose",
"prestorybook:build": "npm run build:packages",
"storybook:build": "build-storybook -c ./storybook -o ./storybook/build",
"prestorybook:dev": "npm run build:packages",
Expand Down
7 changes: 3 additions & 4 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
>
<php>
<env name="WORDPRESS_TABLE_PREFIX" value="wptests_" />
</php>
<testsuites>
<testsuite name="default">
<directory suffix="-test.php">./phpunit/</directory>
Expand All @@ -19,8 +22,4 @@
<group>ms-required</group>
</exclude>
</groups>
<php>
<env name="WP_PHPUNIT__TESTS_CONFIG" value="/wordpress-phpunit/wp-tests-config.php" />
<env name="WP_TESTS_DIR" value="" force="true" />
</php>
</phpunit>
8 changes: 4 additions & 4 deletions phpunit/block-supports/border-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ class WP_Block_Supports_Border_Test extends WP_UnitTestCase {
*/
private $test_block_name;

function setUp() {
parent::setUp();
function set_up() {
parent::set_up();
$this->test_block_name = null;
}

function tearDown() {
function tear_down() {
unregister_block_type( $this->test_block_name );
$this->test_block_name = null;
parent::tearDown();
parent::tear_down();
}

/**
Expand Down
8 changes: 4 additions & 4 deletions phpunit/block-supports/colors-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ class WP_Block_Supports_Colors_Test extends WP_UnitTestCase {
*/
private $test_block_name;

function setUp() {
parent::setUp();
function set_up() {
parent::set_up();
$this->test_block_name = null;
}

function tearDown() {
function tear_down() {
unregister_block_type( $this->test_block_name );
$this->test_block_name = null;
parent::tearDown();
parent::tear_down();
}

function test_color_slugs_with_numbers_are_kebab_cased_properly() {
Expand Down
8 changes: 4 additions & 4 deletions phpunit/block-supports/layout-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
*/

class WP_Block_Supports_Layout_Test extends WP_UnitTestCase {
function setUp() {
parent::setUp();
function set_up() {
parent::set_up();
$this->theme_root = realpath( __DIR__ . '/../data/themedir1' );
$this->orig_theme_dir = $GLOBALS['wp_theme_directories'];

Expand All @@ -24,11 +24,11 @@ function setUp() {
unset( $GLOBALS['wp_themes'] );
}

function tearDown() {
function tear_down() {
$GLOBALS['wp_theme_directories'] = $this->orig_theme_dir;
wp_clean_themes_cache();
unset( $GLOBALS['wp_themes'] );
parent::tearDown();
parent::tear_down();
}

function filter_set_theme_root() {
Expand Down
8 changes: 4 additions & 4 deletions phpunit/block-supports/spacing-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ class WP_Block_Supports_Spacing_Test extends WP_UnitTestCase {
*/
private $test_block_name;

function setUp() {
parent::setUp();
function set_up() {
parent::set_up();
$this->test_block_name = null;
}

function tearDown() {
function tear_down() {
unregister_block_type( $this->test_block_name );
$this->test_block_name = null;
parent::tearDown();
parent::tear_down();
}

function test_spacing_style_is_applied() {
Expand Down
8 changes: 4 additions & 4 deletions phpunit/block-supports/typography-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ class WP_Block_Supports_Typography_Test extends WP_UnitTestCase {
*/
private $test_block_name;

function setUp() {
parent::setUp();
function set_up() {
parent::set_up();
$this->test_block_name = null;
}

function tearDown() {
function tear_down() {
unregister_block_type( $this->test_block_name );
$this->test_block_name = null;
parent::tearDown();
parent::tear_down();
}

function test_font_size_slug_with_numbers_is_kebab_cased_properly() {
Expand Down
5 changes: 0 additions & 5 deletions phpunit/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@
// Try the WP_TESTS_DIR environment variable first.
$_tests_dir = getenv( 'WP_TESTS_DIR' );

// Next, try the WP_PHPUNIT composer package.
if ( ! $_tests_dir ) {
$_tests_dir = getenv( 'WP_PHPUNIT__DIR' );
}

// See if we're installed inside an existing WP dev instance.
if ( ! $_tests_dir ) {
$_try_tests_dir = __DIR__ . '/../../../../../tests/phpunit';
Expand Down
8 changes: 4 additions & 4 deletions phpunit/class-block-context-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ class Block_Context_Test extends WP_UnitTestCase {
/**
* Sets up each test method.
*/
public function setUp() {
public function set_up() {
global $post;

parent::setUp();
parent::set_up();

$args = array(
'post_content' => 'example',
Expand All @@ -34,8 +34,8 @@ public function setUp() {
/**
* Tear down each test method.
*/
public function tearDown() {
parent::tearDown();
public function tear_down() {
parent::tear_down();

while ( ! empty( $this->registered_block_names ) ) {
$block_name = array_pop( $this->registered_block_names );
Expand Down
28 changes: 21 additions & 7 deletions phpunit/class-gutenberg-rest-block-patterns-controller-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,25 @@ public function test_get_items() {
/**
* Abstract methods that we must implement.
*/
public function test_context_param() {}
public function test_get_item() {}
public function test_create_item() {}
public function test_update_item() {}
public function test_delete_item() {}
public function test_prepare_item() {}
public function test_get_item_schema() {}
public function test_context_param() {
$this->markTestIncomplete();
}
public function test_get_item() {
$this->markTestIncomplete();
}
public function test_create_item() {
$this->markTestIncomplete();
}
public function test_update_item() {
$this->markTestIncomplete();
}
public function test_delete_item() {
$this->markTestIncomplete();
}
public function test_prepare_item() {
$this->markTestIncomplete();
}
public function test_get_item_schema() {
$this->markTestIncomplete();
}
}
8 changes: 4 additions & 4 deletions phpunit/class-override-script-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
*/

class Override_Script_Test extends WP_UnitTestCase {
function setUp() {
parent::setUp();
function set_up() {
parent::set_up();

wp_register_script(
'gutenberg-dummy-script',
Expand All @@ -18,8 +18,8 @@ function setUp() {
);
}

function tearDown() {
parent::tearDown();
function tear_down() {
parent::tear_down();

wp_deregister_script( 'gutenberg-dummy-script' );
}
Expand Down
28 changes: 21 additions & 7 deletions phpunit/class-wp-rest-block-pattern-categories-controller-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,25 @@ public function test_get_items() {
/**
* Abstract methods that we must implement.
*/
public function test_context_param() {}
public function test_get_item() {}
public function test_create_item() {}
public function test_update_item() {}
public function test_delete_item() {}
public function test_prepare_item() {}
public function test_get_item_schema() {}
public function test_context_param() {
$this->markTestIncomplete();
}
public function test_get_item() {
$this->markTestIncomplete();
}
public function test_create_item() {
$this->markTestIncomplete();
}
public function test_update_item() {
$this->markTestIncomplete();
}
public function test_delete_item() {
$this->markTestIncomplete();
}
public function test_prepare_item() {
$this->markTestIncomplete();
}
public function test_get_item_schema() {
$this->markTestIncomplete();
}
}
40 changes: 29 additions & 11 deletions phpunit/class-wp-test-rest-users-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,24 +148,42 @@ public static function wpTearDownAfterClass() {
/**
* This function is run before each method
*/
public function setUp() {
parent::setUp();
public function set_up() {
parent::set_up();
$this->endpoint = new WP_REST_Users_Controller();
}

/**
* The following methods are implemented in core and tested.
* We need to define them here because they exist in the abstract parent.
*/
public function test_register_routes() {}
public function test_context_param() {}
public function test_get_item() {}
public function test_prepare_item() {}
public function test_create_item() {}
public function test_update_item() {}
public function test_delete_item() {}
public function test_get_items() {}
public function test_get_item_schema() {}
public function test_register_routes() {
$this->markTestIncomplete();
}
public function test_context_param() {
$this->markTestIncomplete();
}
public function test_get_item() {
$this->markTestIncomplete();
}
public function test_prepare_item() {
$this->markTestIncomplete();
}
public function test_create_item() {
$this->markTestIncomplete();
}
public function test_update_item() {
$this->markTestIncomplete();
}
public function test_delete_item() {
$this->markTestIncomplete();
}
public function test_get_items() {
$this->markTestIncomplete();
}
public function test_get_item_schema() {
$this->markTestIncomplete();
}

public function test_registered_query_params() {
$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/users' );
Expand Down
8 changes: 4 additions & 4 deletions phpunit/class-wp-theme-json-resolver-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

class WP_Theme_JSON_Resolver_Gutenberg_Test extends WP_UnitTestCase {

function setUp() {
parent::setUp();
function set_up() {
parent::set_up();
$this->theme_root = realpath( __DIR__ . '/data/themedir1' );

$this->orig_theme_dir = $GLOBALS['wp_theme_directories'];
Expand All @@ -26,11 +26,11 @@ function setUp() {
unset( $GLOBALS['wp_themes'] );
}

function tearDown() {
function tear_down() {
$GLOBALS['wp_theme_directories'] = $this->orig_theme_dir;
wp_clean_themes_cache();
unset( $GLOBALS['wp_themes'] );
parent::tearDown();
parent::tear_down();
}

function filter_set_theme_root() {
Expand Down
4 changes: 2 additions & 2 deletions phpunit/class-wp-webfonts-local-provider-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ class WP_Webfonts_Provider_Local_Test extends WP_UnitTestCase {
private $theme_root;
private $orig_theme_dir;

public function setUp() {
parent::setUp();
public function set_up() {
parent::set_up();

$this->provider = new WP_Webfonts_Provider_Local();

Expand Down
4 changes: 2 additions & 2 deletions phpunit/class-wp-webfonts-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ public function test_generate_and_enqueue_styles() {
@font-face{font-family:"Source Serif Pro";font-style:normal;font-weight:200 900;font-display:fallback;font-stretch:normal;src:local("Source Serif Pro"), url('https://example.com/assets/fonts/source-serif-pro/SourceSerif4Variable-Roman.ttf.woff2') format('woff2');}
EOF;

$this->assertContains(
$this->assertStringContainsString(
$expected,
get_echo( 'wp_print_styles' )
);
Expand Down Expand Up @@ -443,7 +443,7 @@ public function test_generate_and_enqueue_editor_styles() {
@font-face{font-family:Roboto;font-style:normal;font-weight:400;font-display:fallback;font-stretch:normal;src:local(Roboto), url('https://example.com/assets/fonts/roboto/Roboto-Regular.ttf') format('truetype');}@font-face{font-family:"Source Serif Pro";font-style:normal;font-weight:200 900;font-display:fallback;font-stretch:normal;src:local("Source Serif Pro"), url('https://example.com/assets/fonts/source-serif-pro/SourceSerif4Variable-Roman.ttf.woff2') format('woff2');}
EOF;

$this->assertContains(
$this->assertStringContainsString(
$expected,
get_echo( 'wp_print_styles' )
);
Expand Down
Loading

0 comments on commit 1eb1ba1

Please sign in to comment.