Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Utilize WordPress PHPUnit Test Library Included By wp-env #41852

Merged
merged 8 commits into from
Jul 15, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I chose 6.5 here because of the stated requirements for the minimum PHP version. I wanted a version that was compatible with more versions of PHP.

"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\"",
ObliviousHarmony marked this conversation as resolved.
Show resolved Hide resolved
"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
4 changes: 2 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
beStrictAboutTestsThatDoNotTestAnything="false"
ObliviousHarmony marked this conversation as resolved.
Show resolved Hide resolved
>
<testsuites>
<testsuite name="default">
Expand All @@ -20,7 +21,6 @@
</exclude>
</groups>
<php>
<env name="WP_PHPUNIT__TESTS_CONFIG" value="/wordpress-phpunit/wp-tests-config.php" />
<env name="WP_TESTS_DIR" value="" force="true" />
<env name="WORDPRESS_TABLE_PREFIX" value="wptests_" />
</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
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
4 changes: 2 additions & 2 deletions phpunit/class-wp-test-rest-users-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ 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();
}

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
8 changes: 6 additions & 2 deletions phpunit/multisite.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
beStrictAboutTestsThatDoNotTestAnything="false"
>
<php>
<env name="WP_MULTISITE" value="1" />
<env name="WORDPRESS_TABLE_PREFIX" value="wptests_" />
</php>
<testsuites>
<testsuite name="default">
<directory suffix="-test.php">./</directory>
Expand All @@ -17,7 +22,6 @@
</exclude>
</groups>
<php>
<env name="WP_PHPUNIT__TESTS_CONFIG" value="/wordpress-phpunit/wp-tests-config.php" />
<env name="WP_TESTS_DIR" value="" force="true" />
<env name="WORDPRESS_TABLE_PREFIX" value="wptests_" />
ObliviousHarmony marked this conversation as resolved.
Show resolved Hide resolved
</php>
</phpunit>
16 changes: 8 additions & 8 deletions phpunit/navigation-page-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,25 @@ class WP_Navigation_Page_Test extends WP_UnitTestCase {
/** @var WP_Scripts */
private static $wp_scripts;

public static function setUpBeforeClass() {
parent::setUpBeforeClass();
public static function set_up_before_class() {
parent::set_up_before_class();
global $wp_scripts;
static::$wp_scripts = clone $wp_scripts;
}

public static function tearDownAfterClass() {
parent::tearDownAfterClass();
public static function tear_down_after_class() {
parent::tear_down_after_class();
global $wp_scripts;
$wp_scripts = static::$wp_scripts;
}

public function setUp() {
parent::setUp();
public function set_up() {
parent::set_up();
$this->callback = $this->createMock( WP_Navigation_Page_Test_Callback::class );
}

public function tearDown() {
parent::tearDown();
public function tear_down() {
parent::tear_down();
remove_filter( 'navigation_editor_preload_paths', array( $this->callback, 'preload_paths_callback' ) );
remove_filter( 'wp_get_nav_menus', array( $this->callback, 'wp_nav_menus_callback' ) );
remove_filter( 'rest_pre_dispatch', array( $this->callback, 'preload_menus_rest_pre_dispatch_callback' ) );
Expand Down