Skip to content

Commit

Permalink
Tests/Unit: use PSR-4
Browse files Browse the repository at this point in the history
* Ensure the `tests/Unit` directory and the subdirectory names use "ProperCase".
* Removing prefixes from the test class names which are not needed in namespaced classes (`WPSEO/Yoast`) and where relevant, update references in other test files.
* Ensure all test class file names match the name of the test class. (best effort, bit too many files to be 100% sure)
* Change the Composer `autoload-dev` directive to use PSR-4 autoloading.
* Update the `phpunit.xml.dist` file to match.
* Update the `phpcs.xml.dist` file to match.
  • Loading branch information
jrfnl committed Nov 20, 2023
1 parent 0004f3b commit 507bc2e
Show file tree
Hide file tree
Showing 566 changed files with 57 additions and 45 deletions.
9 changes: 6 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,12 @@
},
"autoload-dev": {
"classmap": [
"tests/",
"config/"
]
"config/",
"tests/integration"
],
"psr-4": {
"Yoast\\WP\\SEO\\Tests\\Unit\\": "tests/Unit/"
}
},
"config": {
"allow-plugins": {
Expand Down
31 changes: 20 additions & 11 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
<element value="admin/views/tool-bulk-editor.php"/>
<!-- Don't trigger on test bootstrap files. -->
<element value="tests/integration/bootstrap.php"/>
<element value="tests/unit/bootstrap.php"/>
<element value="tests/Unit/bootstrap.php"/>
</property>

<!-- Remove the following prefixes from the names of object structures. -->
Expand All @@ -107,7 +107,7 @@
<properties>
<property name="doubles_path" type="array" extend="true">
<element value="/tests/integration/doubles"/>
<element value="/tests/unit/doubles"/>
<element value="/tests/Unit/Doubles"/>
</property>
</properties>
</rule>
Expand All @@ -126,7 +126,7 @@
<property name="src_directory" type="array">
<element value="config"/>
<element value="src"/>
<element value="tests/unit"/>
<element value="tests/Unit"/>
<element value="src/deprecated/src"/>
</property>

Expand Down Expand Up @@ -240,33 +240,33 @@

<!-- 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/Unit/*</exclude-pattern>
</rule>

<!-- Allow for the double/mock classes to override methods just to change the visibility. -->
<rule ref="Generic.CodeAnalysis.UselessOverridingMethod">
<exclude-pattern>/tests/integration/doubles/*</exclude-pattern>
<exclude-pattern>/tests/unit/doubles/*</exclude-pattern>
<exclude-pattern>/tests/Unit/Doubles/*</exclude-pattern>
</rule>

<!-- Test mock/double classes do not have to be prefixed. -->
<rule ref="WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound">
<exclude-pattern>/tests/integration/doubles/*</exclude-pattern>
<exclude-pattern>/tests/unit/doubles/*</exclude-pattern>
<exclude-pattern>/tests/Unit/Doubles/*</exclude-pattern>
</rule>

<!-- For documentation of the double/mock classes, please refer to the _real_ classes. -->
<rule ref="Squiz.Commenting.FunctionComment.Missing">
<exclude-pattern>/tests/integration/doubles/*</exclude-pattern>
<exclude-pattern>/tests/unit/doubles/*</exclude-pattern>
<exclude-pattern>/tests/Unit/Doubles/*</exclude-pattern>
</rule>
<rule ref="Squiz.Commenting.FunctionComment.MissingParamTag">
<exclude-pattern>/tests/integration/doubles/*</exclude-pattern>
<exclude-pattern>/tests/unit/doubles/*</exclude-pattern>
<exclude-pattern>/tests/Unit/Doubles/*</exclude-pattern>
</rule>
<rule ref="Squiz.Commenting.VariableComment.Missing">
<exclude-pattern>/tests/integration/doubles/*</exclude-pattern>
<exclude-pattern>/tests/unit/doubles/*</exclude-pattern>
<exclude-pattern>/tests/Unit/Doubles/*</exclude-pattern>
</rule>


Expand All @@ -286,7 +286,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/Unit/*\.php$</include-pattern>
</rule>

<!-- Only enable the prefix all globals rules for the "new" code for now.
Expand All @@ -298,13 +298,22 @@
<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/Unit/*\.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>
</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>
</rule>
<rule ref="Yoast.Commenting.FileComment.Unnecessary">
<exclude-pattern>/tests/Unit/bootstrap\.php$</exclude-pattern>
</rule>
Expand Down
4 changes: 2 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/8.5/phpunit.xsd"
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="tests/unit/bootstrap.php"
bootstrap="tests/Unit/bootstrap.php"
colors="true"
convertDeprecationsToExceptions="true"
convertErrorsToExceptions="true"
Expand All @@ -20,7 +20,7 @@
>
<testsuites>
<testsuite name="yoastseo">
<directory suffix="-test.php">./tests/unit/</directory>
<directory suffix="Test.php">./tests/Unit/</directory>
</testsuite>
</testsuites>

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* @phpcs:disable Yoast.Files.FileName.InvalidClassFileName
* @phpcs:disable Yoast.NamingConventions.ObjectNameDepth.MaxExceeded
*/
final class Admin_Asset_Analysis_Worker_Location_Test extends TestCase {
final class Asset_Analysis_Worker_Location_Test extends TestCase {

/**
* Prepare the tests.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Mockery;
use WPSEO_Admin_Gutenberg_Compatibility_Notification;
use WPSEO_Gutenberg_Compatibility;
use Yoast\WP\SEO\Tests\Unit\Doubles\Admin\WPSEO_Admin_Gutenberg_Compatibility_Notification_Double;
use Yoast\WP\SEO\Tests\Unit\Doubles\Admin\Gutenberg_Compatibility_Notification_Double;
use Yoast\WP\SEO\Tests\Unit\TestCase;
use Yoast_Notification;
use Yoast_Notification_Center;
Expand All @@ -20,7 +20,7 @@
*
* @phpcs:disable Yoast.NamingConventions.ObjectNameDepth.MaxExceeded
*/
class WPSEO_Admin_Gutenberg_Compatibility_Notification_Test extends TestCase {
class Gutenberg_Compatibility_Notification_Test extends TestCase {

/**
* Holds the admin user mock instance.
Expand Down Expand Up @@ -64,7 +64,7 @@ public function set_up() {
$this->gutenberg_compatibility_mock = Mockery::mock( WPSEO_Gutenberg_Compatibility::class )->makePartial();
$this->notification_center_mock = Mockery::mock( Yoast_Notification_Center::class );

$this->gutenberg_notification = new WPSEO_Admin_Gutenberg_Compatibility_Notification_Double();
$this->gutenberg_notification = new Gutenberg_Compatibility_Notification_Double();
$this->gutenberg_notification->set_dependencies( $this->gutenberg_compatibility_mock, $this->notification_center_mock );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* @coversDefaultClass WPSEO_Import_Settings
*/
class WPSEO_Import_Settings_Test extends TestCase {
class Import_Settings_Test extends TestCase {

/**
* Holds the test instance.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@

use Mockery;
use wpdb;
use Yoast\WP\SEO\Tests\Unit\Doubles\Admin\Import\Plugins\WPSEO_Import_AIOSEO_V4_Double;
use Yoast\WP\SEO\Tests\Unit\Doubles\Admin\Import\Plugins\Import_AIOSEO_V4_Double;
use Yoast\WP\SEO\Tests\Unit\TestCase;

/**
* Tests for the All-in-One SEO V4 import feature.
*
* @coversDefaultClass WPSEO_Import_AIOSEO_V4
*/
class WPSEO_Import_AIOSEO_V4_Test extends TestCase {
class Import_AIOSEO_V4_Test extends TestCase {

/**
* The instance under test.
*
* @var WPSEO_Import_AIOSEO_V4_Double
* @var Import_AIOSEO_V4_Double
*/
private $instance;

Expand All @@ -27,7 +27,7 @@ class WPSEO_Import_AIOSEO_V4_Test extends TestCase {
protected function set_up() {
parent::set_up();

$this->instance = new WPSEO_Import_AIOSEO_V4_Double();
$this->instance = new Import_AIOSEO_V4_Double();
}

/**
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*
* @group Metabox
*/
class Metabox_Collapsibles_Section_Test extends TestCase {
class Metabox_Collapsibles_Sections_Test extends TestCase {

/**
* Set up function stubs.
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*
* @coversDefaultClass WPSEO_Product_Upsell_Notice
*/
class WPSEO_Product_Upsell_Notice_Test extends TestCase {
class Product_Upsell_Notice_Test extends TestCase {

/**
* Test the dismiss_notice_listener function.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*
* @group tracking
*/
class WPSEO_Tracking_Test extends TestCase {
class Tracking_Test extends TestCase {

/**
* Tests the constructor on a non-production setup.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*
* @coversDefaultClass WPSEO_Admin_User_Profile
*/
class WPSEO_Admin_User_Profile_Test extends TestCase {
class User_Profile_Test extends TestCase {

/**
* Holds the instance to test.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*
* @covers \Yoast_Feature_Toggles
*/
class Yoast_Feature_Toggles_Test extends TestCase {
class Feature_Toggles_Test extends TestCase {

/**
* Limited data copy of the `$feature_toggles` array from the `Yoast_Feature_Toggles::load_toggles() method.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* @covers \Yoast_Integration_Toggles
*/
class Yoast_Integration_Toggles_Test extends TestCase {
class Integration_Toggles_Test extends TestCase {

/**
* Test the basic functionality of the Yoast_Integration_Toggles class.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Brain\Monkey;
use Yoast\WP\SEO\Analytics\Application\Missing_Indexables_Collector;
use Yoast\WP\SEO\Tests\Unit\Doubles\Actions\indexing\Abstract_Indexing_Action_Double;
use Yoast\WP\SEO\Tests\Unit\Doubles\Actions\Indexing\Abstract_Indexing_Action_Double;
use Yoast\WP\SEO\Tests\Unit\TestCase;

/**
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
use Yoast\WP\SEO\Tests\Unit\TestCase;

/**
* Class Yoast_Admin_Tools_Page_Conditional_Test.
* Class Yoast_Tools_Page_Conditional_Test.
*
* @group indexables
* @group conditionals
*
* @coversDefaultClass \Yoast\WP\SEO\Conditionals\Yoast_Tools_Page_Conditional
*/
class Yoast_Admin_Tools_Page_Conditional_Test extends TestCase {
class Yoast_Tools_Page_Conditional_Test extends TestCase {

/**
* Holds the Yoast admin tools page conditional.
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Yoast\WP\SEO\Tests\Unit\Doubles\Actions\indexing;
namespace Yoast\WP\SEO\Tests\Unit\Doubles\Actions\Indexing;

use Yoast\WP\SEO\Actions\Indexing\Abstract_Indexing_Action;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
/**
* Test helper class.
*
* Class WPSEO_Gutenberg_Compatibility_Double.
* Class Gutenberg_Compatibility_Notification_Double.
*
* @phpcs:disable Yoast.NamingConventions.ObjectNameDepth.MaxExceeded
*/
class WPSEO_Admin_Gutenberg_Compatibility_Notification_Double extends WPSEO_Admin_Gutenberg_Compatibility_Notification {
class Gutenberg_Compatibility_Notification_Double extends WPSEO_Admin_Gutenberg_Compatibility_Notification {

/**
* Sets the dependency instances.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/**
* Double for the WPSEO_Import_AIOSEO_V4 class, to be able to test its protected methods.
*/
class WPSEO_Import_AIOSEO_V4_Double extends WPSEO_Import_AIOSEO_V4 {
class Import_AIOSEO_V4_Double extends WPSEO_Import_AIOSEO_V4 {

/**
* Replaces the AiOSEO variables in our temporary table with Yoast variables (replace vars).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
use WPSEO_Health_Check;

/**
* Class WPSEO_Health_Check_Double exposes protected properties and methods needed by the unit tests.
* Class Health_Check_Double exposes protected properties and methods needed by the unit tests.
*/
class WPSEO_Health_Check_Double extends WPSEO_Health_Check {
class Health_Check_Double extends WPSEO_Health_Check {

/**
* Indicates that the test has executed.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace doubles\lib;
namespace Yoast\WP\SEO\Tests\Unit\Doubles\Lib;

use Yoast\WP\Lib\ORM;

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*
* @group sitemaps
*/
class WPSEO_Sitemaps_Admin_Test extends TestCase {
class Sitemaps_Admin_Test extends TestCase {

/**
* Class instance to use for the test.
Expand Down
Loading

0 comments on commit 507bc2e

Please sign in to comment.