From b0695e66eb089050fd7a74b2a9fc37a8b4da24f7 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Wed, 4 Oct 2023 10:34:29 +0200 Subject: [PATCH 1/2] Tests: make sure all files are namespaced ... including the test bootstrap. Includes ensuring that all calls to global functions and global constants are now fully qualified. --- tests/unit/bootstrap.php | 16 +++++++++------- tests/wp/bootstrap.php | 16 +++++++++------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/tests/unit/bootstrap.php b/tests/unit/bootstrap.php index 7bd3fe8b5..7fe2b245d 100644 --- a/tests/unit/bootstrap.php +++ b/tests/unit/bootstrap.php @@ -5,15 +5,17 @@ * @package Yoast\WP\Duplicate_Post\Tests */ -define( 'OBJECT', 'OBJECT' ); -define( 'ARRAY_A', 'ARRAY_A' ); -define( 'ARRAY_N', 'ARRAY_N' ); +namespace Yoast\WP\Duplicate_Post\Tests\Unit; -define( 'DUPLICATE_POST_FILE', '/var/www/html/wp-content/plugins/duplicate-post/duplicate-post.php' ); -define( 'DUPLICATE_POST_CURRENT_VERSION', '4.0' ); +\define( 'OBJECT', 'OBJECT' ); +\define( 'ARRAY_A', 'ARRAY_A' ); +\define( 'ARRAY_N', 'ARRAY_N' ); -if ( file_exists( dirname( __DIR__, 2 ) . '/vendor/autoload.php' ) === false ) { - echo PHP_EOL, 'ERROR: Run `composer install` to generate the autoload files before running the unit tests.', PHP_EOL; +\define( 'DUPLICATE_POST_FILE', '/var/www/html/wp-content/plugins/duplicate-post/duplicate-post.php' ); +\define( 'DUPLICATE_POST_CURRENT_VERSION', '4.0' ); + +if ( \file_exists( \dirname( __DIR__, 2 ) . '/vendor/autoload.php' ) === false ) { + echo \PHP_EOL, 'ERROR: Run `composer install` to generate the autoload files before running the unit tests.', \PHP_EOL; exit( 1 ); } diff --git a/tests/wp/bootstrap.php b/tests/wp/bootstrap.php index 0bc1ce523..2785db2a3 100644 --- a/tests/wp/bootstrap.php +++ b/tests/wp/bootstrap.php @@ -5,28 +5,30 @@ * @package Yoast\WP\Duplicate_Post\Tests */ +namespace Yoast\WP\Duplicate_Post\Tests\WP; + use Yoast\WPTestUtils\WPIntegration; // Disable xdebug backtrace. -if ( function_exists( 'xdebug_disable' ) ) { - xdebug_disable(); +if ( \function_exists( 'xdebug_disable' ) ) { + \xdebug_disable(); } -echo 'Welcome to the Duplicate Post Test Suite' . PHP_EOL; -echo 'Version: 1.0' . PHP_EOL . PHP_EOL; +echo 'Welcome to the Duplicate Post Test Suite' . \PHP_EOL; +echo 'Version: 1.0' . \PHP_EOL . \PHP_EOL; /* * Load the plugin(s). */ -require_once dirname( __DIR__, 2 ) . '/vendor/yoast/wp-test-utils/src/WPIntegration/bootstrap-functions.php'; +require_once \dirname( __DIR__, 2 ) . '/vendor/yoast/wp-test-utils/src/WPIntegration/bootstrap-functions.php'; // Get access to tests_add_filter() function. require_once WPIntegration\get_path_to_wp_test_dir() . 'includes/functions.php'; -tests_add_filter( +\tests_add_filter( 'muplugins_loaded', function() { - require_once dirname( __DIR__, 2 ) . '/duplicate-post.php'; + require_once \dirname( __DIR__, 2 ) . '/duplicate-post.php'; } ); From 97eceea7ae9ab42dee562eb111f5b43037ce268b Mon Sep 17 00:00:00 2001 From: jrfnl Date: Wed, 4 Oct 2023 16:31:20 +0200 Subject: [PATCH 2/2] Tests: use PSR-4 * Ensure subdirectory names use "ProperCase". * Ensure all test class file names match the name of the test class. * 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. --- .phpcs.xml.dist | 8 ++++++++ composer.json | 6 +++--- phpunit-wp.xml.dist | 4 ++-- phpunit.xml.dist | 4 ++-- .../Admin/Options_Form_Generator_Test.php} | 0 .../Admin/Options_Inputs_Test.php} | 0 .../Admin/Options_Page_Test.php} | 0 .../options-test.php => Unit/Admin/Options_Test.php} | 0 .../Handlers/Check_Changes_Handler_Test.php} | 0 .../Permissions_Helper_Test.php} | 0 .../Post_Duplicator_Test.php} | 0 .../Post_Republisher_Test.php} | 0 .../Revisions_Migrator_Test.php} | 0 tests/{unit/testcase.php => Unit/TestCase.php} | 0 .../ui/admin-bar-test.php => Unit/UI/Admin_Bar_Test.php} | 0 .../UI/Asset_Manager_Test.php} | 0 .../UI/Block_Editor_Test.php} | 0 .../UI/Bulk_Actions_Test.php} | 0 .../UI/Classic_Editor_Test.php} | 0 .../{unit/ui/column-test.php => Unit/UI/Column_Test.php} | 0 .../UI/Link_Builder_Test.php} | 0 .../ui/metabox-test.php => Unit/UI/Metabox_Test.php} | 0 .../post-states-test.php => Unit/UI/Post_States_Test.php} | 0 .../row-actions-test.php => Unit/UI/Row_Actions_Test.php} | 0 tests/{unit/utils-test.php => Unit/Utils_Test.php} | 0 .../Watchers/Bulk_Actions_Watcher_Test.php} | 0 .../Watchers/Copied_Post_Watcher_Test.php} | 0 .../Watchers/Link_Actions_Watcher_Test.php} | 0 .../Watchers/Original_Post_Watcher_Test.php} | 0 .../Watchers/Republished_Post_Watcher_Test.php} | 0 tests/{unit => Unit}/bootstrap.php | 0 .../Post_Duplicator_Test.php} | 0 tests/{wp => WP}/bootstrap.php | 0 33 files changed, 15 insertions(+), 7 deletions(-) rename tests/{unit/admin/options-form-generator-test.php => Unit/Admin/Options_Form_Generator_Test.php} (100%) rename tests/{unit/admin/options-inputs-test.php => Unit/Admin/Options_Inputs_Test.php} (100%) rename tests/{unit/admin/options-page-test.php => Unit/Admin/Options_Page_Test.php} (100%) rename tests/{unit/admin/options-test.php => Unit/Admin/Options_Test.php} (100%) rename tests/{unit/handlers/check-changes-handler-test.php => Unit/Handlers/Check_Changes_Handler_Test.php} (100%) rename tests/{unit/permissions-helper-test.php => Unit/Permissions_Helper_Test.php} (100%) rename tests/{unit/post-duplicator-test.php => Unit/Post_Duplicator_Test.php} (100%) rename tests/{unit/post-republisher-test.php => Unit/Post_Republisher_Test.php} (100%) rename tests/{unit/revisions-migrator-test.php => Unit/Revisions_Migrator_Test.php} (100%) rename tests/{unit/testcase.php => Unit/TestCase.php} (100%) rename tests/{unit/ui/admin-bar-test.php => Unit/UI/Admin_Bar_Test.php} (100%) rename tests/{unit/ui/asset-manager-test.php => Unit/UI/Asset_Manager_Test.php} (100%) rename tests/{unit/ui/block-editor-test.php => Unit/UI/Block_Editor_Test.php} (100%) rename tests/{unit/ui/bulk-actions-test.php => Unit/UI/Bulk_Actions_Test.php} (100%) rename tests/{unit/ui/classic-editor-test.php => Unit/UI/Classic_Editor_Test.php} (100%) rename tests/{unit/ui/column-test.php => Unit/UI/Column_Test.php} (100%) rename tests/{unit/ui/link-builder-test.php => Unit/UI/Link_Builder_Test.php} (100%) rename tests/{unit/ui/metabox-test.php => Unit/UI/Metabox_Test.php} (100%) rename tests/{unit/ui/post-states-test.php => Unit/UI/Post_States_Test.php} (100%) rename tests/{unit/ui/row-actions-test.php => Unit/UI/Row_Actions_Test.php} (100%) rename tests/{unit/utils-test.php => Unit/Utils_Test.php} (100%) rename tests/{unit/watchers/bulk-actions-watcher-test.php => Unit/Watchers/Bulk_Actions_Watcher_Test.php} (100%) rename tests/{unit/watchers/copied-post-watcher-test.php => Unit/Watchers/Copied_Post_Watcher_Test.php} (100%) rename tests/{unit/watchers/link-actions-watcher-test.php => Unit/Watchers/Link_Actions_Watcher_Test.php} (100%) rename tests/{unit/watchers/original-post-watcher-test.php => Unit/Watchers/Original_Post_Watcher_Test.php} (100%) rename tests/{unit/watchers/republished-post-watcher-test.php => Unit/Watchers/Republished_Post_Watcher_Test.php} (100%) rename tests/{unit => Unit}/bootstrap.php (100%) rename tests/{wp/post-duplicator-test.php => WP/Post_Duplicator_Test.php} (100%) rename tests/{wp => WP}/bootstrap.php (100%) diff --git a/.phpcs.xml.dist b/.phpcs.xml.dist index be004ec90..bf5594e74 100644 --- a/.phpcs.xml.dist +++ b/.phpcs.xml.dist @@ -163,4 +163,12 @@ /src/watchers/republished-post-watcher\.php$ + + + /tests/*/bootstrap\.php$ + + + /tests/*\.php$ + + diff --git a/composer.json b/composer.json index 9a8613e50..e2d4605b1 100644 --- a/composer.json +++ b/composer.json @@ -37,9 +37,9 @@ ] }, "autoload-dev": { - "classmap": [ - "tests/" - ] + "psr-4": { + "Yoast\\WP\\Duplicate_Post\\Tests\\": "tests/" + } }, "config": { "allow-plugins": { diff --git a/phpunit-wp.xml.dist b/phpunit-wp.xml.dist index fd042a8fe..bfa74d8b2 100644 --- a/phpunit-wp.xml.dist +++ b/phpunit-wp.xml.dist @@ -4,7 +4,7 @@ xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/8.5/phpunit.xsd" backupGlobals="false" backupStaticAttributes="false" - bootstrap="tests/wp/bootstrap.php" + bootstrap="tests/WP/bootstrap.php" colors="true" convertDeprecationsToExceptions="true" convertErrorsToExceptions="true" @@ -20,7 +20,7 @@ > - ./tests/wp + ./tests/WP diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 2a4179710..bddc0b1f9 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -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" @@ -20,7 +20,7 @@ > - ./tests/unit + ./tests/Unit diff --git a/tests/unit/admin/options-form-generator-test.php b/tests/Unit/Admin/Options_Form_Generator_Test.php similarity index 100% rename from tests/unit/admin/options-form-generator-test.php rename to tests/Unit/Admin/Options_Form_Generator_Test.php diff --git a/tests/unit/admin/options-inputs-test.php b/tests/Unit/Admin/Options_Inputs_Test.php similarity index 100% rename from tests/unit/admin/options-inputs-test.php rename to tests/Unit/Admin/Options_Inputs_Test.php diff --git a/tests/unit/admin/options-page-test.php b/tests/Unit/Admin/Options_Page_Test.php similarity index 100% rename from tests/unit/admin/options-page-test.php rename to tests/Unit/Admin/Options_Page_Test.php diff --git a/tests/unit/admin/options-test.php b/tests/Unit/Admin/Options_Test.php similarity index 100% rename from tests/unit/admin/options-test.php rename to tests/Unit/Admin/Options_Test.php diff --git a/tests/unit/handlers/check-changes-handler-test.php b/tests/Unit/Handlers/Check_Changes_Handler_Test.php similarity index 100% rename from tests/unit/handlers/check-changes-handler-test.php rename to tests/Unit/Handlers/Check_Changes_Handler_Test.php diff --git a/tests/unit/permissions-helper-test.php b/tests/Unit/Permissions_Helper_Test.php similarity index 100% rename from tests/unit/permissions-helper-test.php rename to tests/Unit/Permissions_Helper_Test.php diff --git a/tests/unit/post-duplicator-test.php b/tests/Unit/Post_Duplicator_Test.php similarity index 100% rename from tests/unit/post-duplicator-test.php rename to tests/Unit/Post_Duplicator_Test.php diff --git a/tests/unit/post-republisher-test.php b/tests/Unit/Post_Republisher_Test.php similarity index 100% rename from tests/unit/post-republisher-test.php rename to tests/Unit/Post_Republisher_Test.php diff --git a/tests/unit/revisions-migrator-test.php b/tests/Unit/Revisions_Migrator_Test.php similarity index 100% rename from tests/unit/revisions-migrator-test.php rename to tests/Unit/Revisions_Migrator_Test.php diff --git a/tests/unit/testcase.php b/tests/Unit/TestCase.php similarity index 100% rename from tests/unit/testcase.php rename to tests/Unit/TestCase.php diff --git a/tests/unit/ui/admin-bar-test.php b/tests/Unit/UI/Admin_Bar_Test.php similarity index 100% rename from tests/unit/ui/admin-bar-test.php rename to tests/Unit/UI/Admin_Bar_Test.php diff --git a/tests/unit/ui/asset-manager-test.php b/tests/Unit/UI/Asset_Manager_Test.php similarity index 100% rename from tests/unit/ui/asset-manager-test.php rename to tests/Unit/UI/Asset_Manager_Test.php diff --git a/tests/unit/ui/block-editor-test.php b/tests/Unit/UI/Block_Editor_Test.php similarity index 100% rename from tests/unit/ui/block-editor-test.php rename to tests/Unit/UI/Block_Editor_Test.php diff --git a/tests/unit/ui/bulk-actions-test.php b/tests/Unit/UI/Bulk_Actions_Test.php similarity index 100% rename from tests/unit/ui/bulk-actions-test.php rename to tests/Unit/UI/Bulk_Actions_Test.php diff --git a/tests/unit/ui/classic-editor-test.php b/tests/Unit/UI/Classic_Editor_Test.php similarity index 100% rename from tests/unit/ui/classic-editor-test.php rename to tests/Unit/UI/Classic_Editor_Test.php diff --git a/tests/unit/ui/column-test.php b/tests/Unit/UI/Column_Test.php similarity index 100% rename from tests/unit/ui/column-test.php rename to tests/Unit/UI/Column_Test.php diff --git a/tests/unit/ui/link-builder-test.php b/tests/Unit/UI/Link_Builder_Test.php similarity index 100% rename from tests/unit/ui/link-builder-test.php rename to tests/Unit/UI/Link_Builder_Test.php diff --git a/tests/unit/ui/metabox-test.php b/tests/Unit/UI/Metabox_Test.php similarity index 100% rename from tests/unit/ui/metabox-test.php rename to tests/Unit/UI/Metabox_Test.php diff --git a/tests/unit/ui/post-states-test.php b/tests/Unit/UI/Post_States_Test.php similarity index 100% rename from tests/unit/ui/post-states-test.php rename to tests/Unit/UI/Post_States_Test.php diff --git a/tests/unit/ui/row-actions-test.php b/tests/Unit/UI/Row_Actions_Test.php similarity index 100% rename from tests/unit/ui/row-actions-test.php rename to tests/Unit/UI/Row_Actions_Test.php diff --git a/tests/unit/utils-test.php b/tests/Unit/Utils_Test.php similarity index 100% rename from tests/unit/utils-test.php rename to tests/Unit/Utils_Test.php diff --git a/tests/unit/watchers/bulk-actions-watcher-test.php b/tests/Unit/Watchers/Bulk_Actions_Watcher_Test.php similarity index 100% rename from tests/unit/watchers/bulk-actions-watcher-test.php rename to tests/Unit/Watchers/Bulk_Actions_Watcher_Test.php diff --git a/tests/unit/watchers/copied-post-watcher-test.php b/tests/Unit/Watchers/Copied_Post_Watcher_Test.php similarity index 100% rename from tests/unit/watchers/copied-post-watcher-test.php rename to tests/Unit/Watchers/Copied_Post_Watcher_Test.php diff --git a/tests/unit/watchers/link-actions-watcher-test.php b/tests/Unit/Watchers/Link_Actions_Watcher_Test.php similarity index 100% rename from tests/unit/watchers/link-actions-watcher-test.php rename to tests/Unit/Watchers/Link_Actions_Watcher_Test.php diff --git a/tests/unit/watchers/original-post-watcher-test.php b/tests/Unit/Watchers/Original_Post_Watcher_Test.php similarity index 100% rename from tests/unit/watchers/original-post-watcher-test.php rename to tests/Unit/Watchers/Original_Post_Watcher_Test.php diff --git a/tests/unit/watchers/republished-post-watcher-test.php b/tests/Unit/Watchers/Republished_Post_Watcher_Test.php similarity index 100% rename from tests/unit/watchers/republished-post-watcher-test.php rename to tests/Unit/Watchers/Republished_Post_Watcher_Test.php diff --git a/tests/unit/bootstrap.php b/tests/Unit/bootstrap.php similarity index 100% rename from tests/unit/bootstrap.php rename to tests/Unit/bootstrap.php diff --git a/tests/wp/post-duplicator-test.php b/tests/WP/Post_Duplicator_Test.php similarity index 100% rename from tests/wp/post-duplicator-test.php rename to tests/WP/Post_Duplicator_Test.php diff --git a/tests/wp/bootstrap.php b/tests/WP/bootstrap.php similarity index 100% rename from tests/wp/bootstrap.php rename to tests/WP/bootstrap.php