Skip to content

Commit

Permalink
Tests/WP: use PSR-4
Browse files Browse the repository at this point in the history
* Move files into appropriate subdirectories
* Namespace all test files and remove file docblocks.
    Includes making all references to global functions and constants fully qualified and importing all used classes.
* Make sure the class name reflects the _class under test_.
* Remove prefixes from the test class names which are not needed in namespaced classes (`WPSEO/Yoast`) and where relevant, update references to those classes in the test files.
* Ensure all test class file names match the name of the test class.
* Change the Composer `autoload-dev` directive to use PSR-4 autoloading for all tests.
  • Loading branch information
jrfnl committed Nov 21, 2023
1 parent e022b67 commit 506c2fb
Show file tree
Hide file tree
Showing 94 changed files with 1,256 additions and 1,340 deletions.
7 changes: 3 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,10 @@
},
"autoload-dev": {
"classmap": [
"config/",
"tests/WP"
"config/"
],
"psr-4": {
"Yoast\\WP\\SEO\\Tests\\Unit\\": "tests/Unit/"
"Yoast\\WP\\SEO\\Tests\\": "tests/"
}
},
"config": {
Expand Down Expand Up @@ -84,7 +83,7 @@
"Yoast\\WP\\SEO\\Composer\\Actions::check_coding_standards"
],
"check-cs-thresholds": [
"@putenv YOASTCS_THRESHOLD_ERRORS=129",
"@putenv YOASTCS_THRESHOLD_ERRORS=127",
"@putenv YOASTCS_THRESHOLD_WARNINGS=202",
"Yoast\\WP\\SEO\\Composer\\Actions::check_cs_thresholds"
],
Expand Down
10 changes: 5 additions & 5 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@

<!-- The unit tests are not run within the context of a WP install, so overwritting globals is fine. -->
<rule ref="WordPress.WP.GlobalVariablesOverride">
<exclude-pattern>/tests/Unit/*</exclude-pattern>
<exclude-pattern>/tests/*</exclude-pattern>
</rule>

<!-- Allow for the double/mock classes to override methods just to change the visibility. -->
Expand Down Expand Up @@ -283,7 +283,7 @@
<include-pattern>/config/*\.php$</include-pattern>
<include-pattern>/lib/*\.php$</include-pattern>
<include-pattern>/src/*\.php$</include-pattern>
<include-pattern>/tests/Unit/*\.php$</include-pattern>
<include-pattern>/tests/*\.php$</include-pattern>
</rule>

<!-- Only enable the prefix all globals rules for the "new" code for now.
Expand All @@ -295,21 +295,21 @@
<include-pattern>*/config/*\.php$</include-pattern>
<include-pattern>*/lib/*\.php$</include-pattern>
<include-pattern>*/src/*\.php$</include-pattern>
<include-pattern>*/tests/Unit/*\.php$</include-pattern>
<include-pattern>*/tests/*\.php$</include-pattern>

<!-- Exclude hooks everywhere for the time being until the prefixes will be fixed. -->
<exclude name="WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound"/>
</rule>

<!-- The below issues will be fixed in YoastCS 3.0, after which these excludes can be removed. -->
<rule ref="Yoast.Files.FileName.InvalidClassFileName">
<exclude-pattern>/tests/Unit/*\.php$</exclude-pattern>
<exclude-pattern>/tests/*\.php$</exclude-pattern>
</rule>
<rule ref="Yoast.Files.FileName.InvalidTraitFileName">
<exclude-pattern>/tests/Unit/*\.php$</exclude-pattern>
</rule>
<rule ref="Yoast.NamingConventions.NamespaceName.Invalid">
<exclude-pattern>/tests/Unit/*_*/*\.php$</exclude-pattern>
<exclude-pattern>/tests/(Unit|WP)/*_*/*\.php$</exclude-pattern>
</rule>
<rule ref="Yoast.Commenting.FileComment.Unnecessary">
<exclude-pattern>/tests/*/bootstrap\.php$</exclude-pattern>
Expand Down
2 changes: 1 addition & 1 deletion phpunit-wp.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
>
<testsuites>
<testsuite name="yoastseo">
<directory prefix="test-" suffix=".php">./tests/WP</directory>
<directory suffix="Test.php">./tests/WP</directory>
</testsuite>
</testsuites>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
<?php
/**
* WPSEO plugin test file.
*
* @package WPSEO\Tests\Admin
*/

namespace Yoast\WP\SEO\Tests\WP\Admin;

use WPSEO_Admin_Asset;
use WPSEO_Admin_Asset_Dev_Server_Location;
use Yoast\WPTestUtils\WPIntegration\TestCase;

/**
* Tests WPSEO_Admin_Asset_Dev_Server_Location.
*/
final class Admin_Asset_Dev_Server_Location_Test extends TestCase {
final class Asset_Dev_Server_Location_Test extends TestCase {

/**
* Default arguments to use for creating a new Admin_Asset.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
<?php
/**
* WPSEO plugin test file.
*
* @package WPSEO\Tests
*/

namespace Yoast\WP\SEO\Tests\WP\Admin;

use WPSEO_Admin_Asset;
use WPSEO_Admin_Asset_Manager;
use Yoast\WP\SEO\Tests\WP\TestCase;

/**
* Unit Test Class.
*/
class WPSEO_Admin_Asset_Manager_Test extends TestCase {
class Asset_Manager_Test extends TestCase {

/**
* Holds the instance of the class being tested.
Expand All @@ -36,7 +35,7 @@ public function set_up() {
*/
public function test_DEFAULT_register() {
$instance = $this
->getMockBuilder( 'WPSEO_Admin_Asset_Manager' )
->getMockBuilder( WPSEO_Admin_Asset_Manager::class )
->setMethods(
[
'register_scripts',
Expand Down Expand Up @@ -88,7 +87,7 @@ public function test_enqueue_script() {
$class_instance->register_assets();

$class_instance->enqueue_script( 'post-edit' );
$this->assertTrue( wp_script_is( WPSEO_Admin_Asset_Manager::PREFIX . 'post-edit', 'enqueued' ) );
$this->assertTrue( \wp_script_is( WPSEO_Admin_Asset_Manager::PREFIX . 'post-edit', 'enqueued' ) );
}

/**
Expand All @@ -101,7 +100,7 @@ public function test_enqueue_style() {
$class_instance->register_assets();

$class_instance->enqueue_style( 'metabox-css' );
$this->assertTrue( wp_style_is( WPSEO_Admin_Asset_Manager::PREFIX . 'metabox-css', 'enqueued' ) );
$this->assertTrue( \wp_style_is( WPSEO_Admin_Asset_Manager::PREFIX . 'metabox-css', 'enqueued' ) );
}

/**
Expand All @@ -114,7 +113,7 @@ public function test_wrong_enqueue_style() {
$class_instance->register_assets();

$class_instance->enqueue_style( 'nonexisting' );
$this->assertFalse( wp_style_is( WPSEO_Admin_Asset_Manager::PREFIX . 'nonexisting', 'enqueued' ) );
$this->assertFalse( \wp_style_is( WPSEO_Admin_Asset_Manager::PREFIX . 'nonexisting', 'enqueued' ) );
}

/**
Expand All @@ -141,7 +140,7 @@ public function test_register_script() {
$result = $wp_scripts->registered[ WPSEO_Admin_Asset_Manager::PREFIX . 'handle' ];

$this->assertEquals( WPSEO_Admin_Asset_Manager::PREFIX . 'handle', $result->handle );
$this->assertEquals( 'http://' . WP_TESTS_DOMAIN . '/wp-content/plugins/wordpress-seo/js/dist/src.js', $result->src );
$this->assertEquals( 'http://' . \WP_TESTS_DOMAIN . '/wp-content/plugins/wordpress-seo/js/dist/src.js', $result->src );
$this->assertEquals( [ 'deps' ], $result->deps );
$this->assertEquals( 'version', $result->ver );
$this->assertEquals( [ 'group' => 1 ], $result->extra );
Expand Down Expand Up @@ -193,7 +192,7 @@ public function test_register_style() {
$result = $wp_styles->registered[ WPSEO_Admin_Asset_Manager::PREFIX . 'handle' ];

$this->assertEquals( WPSEO_Admin_Asset_Manager::PREFIX . 'handle', $result->handle );
$this->assertEquals( 'http://' . WP_TESTS_DOMAIN . '/wp-content/plugins/wordpress-seo/css/dist/src.css', $result->src );
$this->assertEquals( 'http://' . \WP_TESTS_DOMAIN . '/wp-content/plugins/wordpress-seo/css/dist/src.css', $result->src );
$this->assertEquals( [ 'deps' ], $result->deps );
$this->assertEquals( 'version', $result->ver );
$this->assertEquals( 'print', $result->args );
Expand Down Expand Up @@ -241,7 +240,7 @@ public function test_register_scripts() {
],
];

$class_instance = $this->getMockBuilder( 'WPSEO_Admin_Asset_Manager' )
$class_instance = $this->getMockBuilder( WPSEO_Admin_Asset_Manager::class )
->setMethods( [ 'register_script' ] )
->getMock();

Expand Down Expand Up @@ -276,7 +275,7 @@ public function test_register_styles() {
],
];

$class_instance = $this->getMockBuilder( 'WPSEO_Admin_Asset_Manager' )
$class_instance = $this->getMockBuilder( WPSEO_Admin_Asset_Manager::class )
->setMethods( [ 'register_style' ] )
->getMock();

Expand All @@ -298,7 +297,7 @@ public function test_register_styles() {
*/
public function test_register_assets() {

$class_instance = $this->getMockBuilder( 'WPSEO_Admin_Asset_Manager' )
$class_instance = $this->getMockBuilder( WPSEO_Admin_Asset_Manager::class )
->setMethods( [ 'register_scripts', 'register_styles' ] )
->getMock();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
<?php
/**
* WPSEO plugin test file.
*
* @package WPSEO\Tests\Admin
*/

namespace Yoast\WP\SEO\Tests\WP\Admin;

use WPSEO_Admin_Asset;
use WPSEO_Admin_Asset_SEO_Location;
use Yoast\WPTestUtils\WPIntegration\TestCase;

/**
* Tests WPSEO_Admin_Asset.
* Tests WPSEO_Admin_Asset_SEO_Location.
*/
final class WPSEO_Admin_Asset_SEO_Location_Test extends TestCase {
final class Asset_SEO_Location_Test extends TestCase {

/**
* Tests the get_url function.
Expand All @@ -29,11 +28,11 @@ public function test_get_url() {
];
$asset = new WPSEO_Admin_Asset( $asset_args );

$expected_js = home_url() . '/wp-content/plugins/wordpress-seo/js/dist/src.js';
$expected_css = home_url() . '/wp-content/plugins/wordpress-seo/css/dist/src.css';
$expected_js = \home_url() . '/wp-content/plugins/wordpress-seo/js/dist/src.js';
$expected_css = \home_url() . '/wp-content/plugins/wordpress-seo/css/dist/src.css';
$expected_empty = '';

$location = new WPSEO_Admin_Asset_SEO_Location( WPSEO_FILE );
$location = new WPSEO_Admin_Asset_SEO_Location( \WPSEO_FILE );

$actual_js = $location->get_url( $asset, WPSEO_Admin_Asset::TYPE_JS );
$actual_css = $location->get_url( $asset, WPSEO_Admin_Asset::TYPE_CSS );
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
<?php
/**
* WPSEO plugin test file.
*
* @package WPSEO\Tests
*/

namespace Yoast\WP\SEO\Tests\WP\Admin;

use InvalidArgumentException;
use WPSEO_Admin_Asset;
use Yoast\WP\SEO\Tests\WP\TestCase;

/**
* Unit Test Class.
*/
class WPSEO_Admin_Asset_Test extends TestCase {
class Asset_Test extends TestCase {

/**
* Tests the constructor when no name and src are passed.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
<?php
/**
* WPSEO plugin test file.
*
* @package WPSEO\Tests\Admin
*/

namespace Yoast\WP\SEO\Tests\WP\Admin;

use WPSEO_Database_Proxy;
use Yoast\WP\SEO\Tests\WP\TestCase;

/**
* Unit Test Class.
*/
class WPSEO_Database_Proxy_Test extends TestCase {
class Database_Proxy_Test extends TestCase {

/**
* Table name for use by the DB Proxy.
Expand Down Expand Up @@ -434,7 +432,7 @@ public function test_register_table() {

$proxy_table_name = self::$proxy_table_name . '_duplicate';

$this->assertTrue( in_array( $proxy_table_name, $wpdb->tables, true ) );
$this->assertTrue( \in_array( $proxy_table_name, $wpdb->tables, true ) );
}

/**
Expand All @@ -447,6 +445,6 @@ public function test_register_table_global() {

$proxy_table_name = self::$proxy_table_name . '_duplicate';

$this->assertTrue( in_array( $proxy_table_name, $wpdb->ms_global_tables, true ) );
$this->assertTrue( \in_array( $proxy_table_name, $wpdb->ms_global_tables, true ) );
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
<?php
/**
* WPSEO plugin test file.
*
* @package WPSEO\Tests\Admin
*/

namespace Yoast\WP\SEO\Tests\WP\Admin;

use WPSEO_Admin_Editor_Specific_Replace_Vars;
use Yoast\WP\SEO\Tests\WP\Doubles\Admin\Editor_Specific_Replace_Vars_Double;
use Yoast\WP\SEO\Tests\WP\TestCase;

/**
* Unit Test Class.
*/
class WPSEO_Admin_Editor_Specific_Replace_Vars_Test extends TestCase {
class Editor_Specific_Replace_Vars_Test extends TestCase {

/**
* Holds the instance of the class being tested.
Expand Down Expand Up @@ -241,7 +239,7 @@ public function test_determine_for_archive_with_a_custom_fallback() {
*/
public function test_determine_for_archive_with_a_existing_archive() {
$class_instance = $this
->getMockBuilder( 'WPSEO_Admin_Editor_Specific_Replace_Vars' )
->getMockBuilder( WPSEO_Admin_Editor_Specific_Replace_Vars::class )
->setMethods( [ 'has_for_page_type' ] )
->getMock();

Expand Down Expand Up @@ -294,7 +292,7 @@ private function create_and_get_with_post_type( $post_type = 'post' ) {
* @covers WPSEO_Admin_Editor_Specific_Replace_Vars::get
*/
public function test_editor_specific_replacement_variables_filter() {
add_filter( 'wpseo_editor_specific_replace_vars', [ $this, 'filter_editor_specific_replacement_variables' ] );
\add_filter( 'wpseo_editor_specific_replace_vars', [ $this, 'filter_editor_specific_replacement_variables' ] );

$expected_replacement_variables = [
'page' => [ 'pt_single', 'pt_plural', 'parent_title' ],
Expand All @@ -313,7 +311,7 @@ public function test_editor_specific_replacement_variables_filter() {
$this->class_instance->get()
);

remove_filter( 'wpseo_editor_specific_replace_vars', [ $this, 'filter_editor_specific_replacement_variables' ] );
\remove_filter( 'wpseo_editor_specific_replace_vars', [ $this, 'filter_editor_specific_replacement_variables' ] );
}

/**
Expand All @@ -322,7 +320,7 @@ public function test_editor_specific_replacement_variables_filter() {
* @covers WPSEO_Admin_Editor_Specific_Replace_Vars::get
*/
public function test_editor_specific_replacement_variables_filter_with_wrong_return_value() {
add_filter( 'wpseo_editor_specific_replace_vars', '__return_false' );
\add_filter( 'wpseo_editor_specific_replace_vars', '__return_false' );

$expected_replacement_variables = [
// Posts types.
Expand All @@ -342,7 +340,7 @@ public function test_editor_specific_replacement_variables_filter_with_wrong_ret
$this->class_instance->get()
);

remove_filter( 'wpseo_editor_specific_replace_vars', '__return_false' );
\remove_filter( 'wpseo_editor_specific_replace_vars', '__return_false' );
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
<?php
/**
* WPSEO plugin test file.
*
* @package WPSEO\Tests\Admin\Exceptions
*/

namespace Yoast\WP\SEO\Tests\WP\Admin\Exceptions;

use WPSEO_File_Size_Exception;
use Yoast\WP\SEO\Tests\WP\TestCase;

/**
* Unit Test Class.
*/
class WPSEO_File_Size_Exception_Test extends TestCase {
class File_Size_Exception_Test extends TestCase {

/**
* Tests that the externally hosted error message is as expected.
Expand Down
Loading

0 comments on commit 506c2fb

Please sign in to comment.