From 0d6de1605bb14e4d9860b60246369a592b77c59e Mon Sep 17 00:00:00 2001 From: jrfnl Date: Mon, 18 Sep 2023 05:29:19 +0200 Subject: [PATCH 01/15] Files/TestDoubles: remove `$doubles_path` BC-layer The `public` `$doubles_path` property originally expected a string value. This was changed to an array in YoastCS 1.1.0, though string values were still handled via this BC-layer. Enough time has passed by now, so this BC-layer should now be removed. --- Yoast/Sniffs/Files/TestDoublesSniff.php | 9 --------- 1 file changed, 9 deletions(-) diff --git a/Yoast/Sniffs/Files/TestDoublesSniff.php b/Yoast/Sniffs/Files/TestDoublesSniff.php index d3d7156c..255ba97c 100644 --- a/Yoast/Sniffs/Files/TestDoublesSniff.php +++ b/Yoast/Sniffs/Files/TestDoublesSniff.php @@ -98,15 +98,6 @@ public function process( File $phpcsFile, $stackPtr ) { return ( $phpcsFile->numTokens + 1 ); } - /* - * BC-compatibility for when the property was still a string. - * - * {@internal This should be removed in YoastCS 2.0.0.}} - */ - if ( \is_string( $this->doubles_path ) ) { - $this->doubles_path = (array) $this->doubles_path; - } - $tokens = $phpcsFile->getTokens(); $base_path = $this->normalize_directory_separators( $phpcsFile->config->basepath ); $base_path = \rtrim( $base_path, '/' ) . '/'; // Make sure the base_path ends in a single slash. From cee90fcfdd08ead93d48fd5621653dca63d8d535 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Fri, 20 Oct 2023 05:56:23 +0200 Subject: [PATCH 02/15] Files/TestDoubles: remove "one object per file" check This is already checked via the `Generic.Files.OneObjectStructurePerFile` sniff, which is included in the WordPress Coding Standards. No need for doubling this check here. Includes removing the associated unit tests. Includes removing the code sample related to mocks/doubles being in the same file as the test class from the XML docs. --- Yoast/Docs/Files/TestDoublesStandard.xml | 30 ------------------- Yoast/Sniffs/Files/TestDoublesSniff.php | 26 ++-------------- Yoast/Tests/Files/TestDoublesUnitTest.php | 16 ---------- .../tests/doubles/multiple-mocks-in-file.inc | 5 ---- .../multiple-objects-in-file-reverse.inc | 7 ----- .../tests/multiple-objects-in-file.inc | 7 ----- 6 files changed, 2 insertions(+), 89 deletions(-) delete mode 100644 Yoast/Tests/Files/TestDoublesUnitTests/tests/doubles/multiple-mocks-in-file.inc delete mode 100644 Yoast/Tests/Files/TestDoublesUnitTests/tests/multiple-objects-in-file-reverse.inc delete mode 100644 Yoast/Tests/Files/TestDoublesUnitTests/tests/multiple-objects-in-file.inc diff --git a/Yoast/Docs/Files/TestDoublesStandard.xml b/Yoast/Docs/Files/TestDoublesStandard.xml index 9dc05853..36db7a2e 100644 --- a/Yoast/Docs/Files/TestDoublesStandard.xml +++ b/Yoast/Docs/Files/TestDoublesStandard.xml @@ -9,36 +9,6 @@ The name of the dedicated sub-directory is configurable. ]]> - - - - - - - - - - - findNext( $this->register(), ( $stackPtr + 1 ) ); - if ( $more_objects_in_file === false ) { - $more_objects_in_file = $phpcsFile->findPrevious( $this->register(), ( $stackPtr - 1 ) ); - } - - if ( $more_objects_in_file !== false ) { - $data = [ - $tokens[ $stackPtr ]['content'], - $object_name, - $tokens[ $more_objects_in_file ]['content'], - ObjectDeclarations::getName( $phpcsFile, $more_objects_in_file ), - ]; - - $phpcsFile->addError( - 'Double/Mock test helper classes should be in their own file. Found %1$s: %2$s and %3$s: %4$s', - $stackPtr, - 'OneObjectPerFile', - $data - ); - } - } } /** diff --git a/Yoast/Tests/Files/TestDoublesUnitTest.php b/Yoast/Tests/Files/TestDoublesUnitTest.php index 9d9aa151..ea9bc7de 100644 --- a/Yoast/Tests/Files/TestDoublesUnitTest.php +++ b/Yoast/Tests/Files/TestDoublesUnitTest.php @@ -64,16 +64,6 @@ public function getErrorList( string $testFile = '' ): array { 3 => 1, ]; - case 'multiple-objects-in-file.inc': - return [ - 5 => 2, - ]; - - case 'multiple-objects-in-file-reverse.inc': - return [ - 7 => 2, - ]; - case 'non-existant-doubles-dir.inc': return [ 4 => 1, @@ -100,12 +90,6 @@ public function getErrorList( string $testFile = '' ): array { 3 => 1, ]; - case 'multiple-mocks-in-file.inc': - return [ - 3 => 1, - 5 => 1, - ]; - // In tests/doubles-not-correct. case 'not-in-correct-subdir.inc': return [ diff --git a/Yoast/Tests/Files/TestDoublesUnitTests/tests/doubles/multiple-mocks-in-file.inc b/Yoast/Tests/Files/TestDoublesUnitTests/tests/doubles/multiple-mocks-in-file.inc deleted file mode 100644 index a06a57ce..00000000 --- a/Yoast/Tests/Files/TestDoublesUnitTests/tests/doubles/multiple-mocks-in-file.inc +++ /dev/null @@ -1,5 +0,0 @@ - Date: Fri, 20 Oct 2023 08:34:30 +0200 Subject: [PATCH 03/15] Files/TestDoubles: add extra tests ... for a few edge cases previously not covered by tests. --- Yoast/Sniffs/Files/TestDoublesSniff.php | 2 +- Yoast/Tests/Files/TestDoublesUnitTest.php | 11 +++++++---- .../tests/doubles/correct-dir-interface-double.inc | 3 +++ .../tests/doubles/correct-dir-trait-double.inc | 3 +++ .../Files/TestDoublesUnitTests/tests/live-coding.inc | 4 ++++ .../tests/non-existant-doubles-dir-not-double.inc | 6 ++++++ .../tests/non-existant-doubles-dir.inc | 2 +- .../tests/non-existant-doubles-dirs.inc | 6 ++++++ .../tests/not-in-correct-dir-interface-double.inc | 3 +++ .../tests/not-in-correct-dir-trait-double.inc | 3 +++ 10 files changed, 37 insertions(+), 6 deletions(-) create mode 100644 Yoast/Tests/Files/TestDoublesUnitTests/tests/doubles/correct-dir-interface-double.inc create mode 100644 Yoast/Tests/Files/TestDoublesUnitTests/tests/doubles/correct-dir-trait-double.inc create mode 100644 Yoast/Tests/Files/TestDoublesUnitTests/tests/live-coding.inc create mode 100644 Yoast/Tests/Files/TestDoublesUnitTests/tests/non-existant-doubles-dir-not-double.inc create mode 100644 Yoast/Tests/Files/TestDoublesUnitTests/tests/non-existant-doubles-dirs.inc create mode 100644 Yoast/Tests/Files/TestDoublesUnitTests/tests/not-in-correct-dir-interface-double.inc create mode 100644 Yoast/Tests/Files/TestDoublesUnitTests/tests/not-in-correct-dir-trait-double.inc diff --git a/Yoast/Sniffs/Files/TestDoublesSniff.php b/Yoast/Sniffs/Files/TestDoublesSniff.php index c32d2481..6178d36c 100644 --- a/Yoast/Sniffs/Files/TestDoublesSniff.php +++ b/Yoast/Sniffs/Files/TestDoublesSniff.php @@ -75,7 +75,7 @@ public function process( File $phpcsFile, $stackPtr ) { $file = \preg_replace( '`^([\'"])(.*)\1$`Ds', '$2', $phpcsFile->getFileName() ); if ( $file === 'STDIN' ) { - return; + return; // @codeCoverageIgnore } if ( ! isset( $phpcsFile->config->basepath ) ) { diff --git a/Yoast/Tests/Files/TestDoublesUnitTest.php b/Yoast/Tests/Files/TestDoublesUnitTest.php index ea9bc7de..e7759c7d 100644 --- a/Yoast/Tests/Files/TestDoublesUnitTest.php +++ b/Yoast/Tests/Files/TestDoublesUnitTest.php @@ -65,6 +65,7 @@ public function getErrorList( string $testFile = '' ): array { ]; case 'non-existant-doubles-dir.inc': + case 'non-existant-doubles-dirs.inc': return [ 4 => 1, ]; @@ -75,11 +76,9 @@ public function getErrorList( string $testFile = '' ): array { ]; case 'not-in-correct-dir-double.inc': - return [ - 3 => 1, - ]; - case 'not-in-correct-dir-mock.inc': + case 'not-in-correct-dir-interface-double.inc': + case 'not-in-correct-dir-trait-double.inc': return [ 3 => 1, ]; @@ -102,9 +101,13 @@ public function getErrorList( string $testFile = '' ): array { 4 => 1, ]; + case 'live-coding.inc': // In tests. case 'not-double-or-mock.inc': // In tests. + case 'non-existant-doubles-dir-not-double.inc': // In tests. case 'correct-dir-double.inc': // In tests/doubles. case 'correct-dir-mock.inc': // In tests/doubles. + case 'correct-dir-interface.inc': // In tests/doubles. + case 'correct-dir-trait-double.inc': // In tests/doubles. case 'correct-custom-dir.inc': // In tests/mocks. default: return []; diff --git a/Yoast/Tests/Files/TestDoublesUnitTests/tests/doubles/correct-dir-interface-double.inc b/Yoast/Tests/Files/TestDoublesUnitTests/tests/doubles/correct-dir-interface-double.inc new file mode 100644 index 00000000..0893831b --- /dev/null +++ b/Yoast/Tests/Files/TestDoublesUnitTests/tests/doubles/correct-dir-interface-double.inc @@ -0,0 +1,3 @@ + Date: Tue, 31 Oct 2023 03:42:57 +0100 Subject: [PATCH 04/15] Files/TestDoubles: make property `private` As the sniff class is now `final` (since PR 319), there is no need for any `protected` properties, so let's make this `private`. --- Yoast/Sniffs/Files/TestDoublesSniff.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Yoast/Sniffs/Files/TestDoublesSniff.php b/Yoast/Sniffs/Files/TestDoublesSniff.php index 6178d36c..628bf53c 100644 --- a/Yoast/Sniffs/Files/TestDoublesSniff.php +++ b/Yoast/Sniffs/Files/TestDoublesSniff.php @@ -46,7 +46,7 @@ final class TestDoublesSniff implements Sniff { * * @var string[] */ - protected $target_paths; + private $target_paths; /** * Returns an array of tokens this test wants to listen for. From 46549f072686d79789803a6a89932aff40e52005 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Thu, 19 Oct 2023 05:47:21 +0200 Subject: [PATCH 05/15] Files/TestDoubles: minor documentation tweaks --- Yoast/Sniffs/Files/TestDoublesSniff.php | 6 +++--- Yoast/Tests/Files/TestDoublesUnitTest.php | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Yoast/Sniffs/Files/TestDoublesSniff.php b/Yoast/Sniffs/Files/TestDoublesSniff.php index 628bf53c..a557913e 100644 --- a/Yoast/Sniffs/Files/TestDoublesSniff.php +++ b/Yoast/Sniffs/Files/TestDoublesSniff.php @@ -34,17 +34,17 @@ final class TestDoublesSniff implements Sniff { * @since 1.1.0 The property type has changed from string to array. * Use of this property with a string value has been deprecated. * - * @var string[] + * @var array */ public $doubles_path = [ '/tests/doubles', ]; /** - * Validated absolute target paths for test double/mock classes or an empty array + * Validated absolute target paths for test fixture directories or an empty array * if the intended target directory/directories don't exist. * - * @var string[] + * @var array */ private $target_paths; diff --git a/Yoast/Tests/Files/TestDoublesUnitTest.php b/Yoast/Tests/Files/TestDoublesUnitTest.php index e7759c7d..09baf7d8 100644 --- a/Yoast/Tests/Files/TestDoublesUnitTest.php +++ b/Yoast/Tests/Files/TestDoublesUnitTest.php @@ -35,7 +35,7 @@ public function setCliValues( $filename, $config ): void { * * @param string $testFileBase The base path that the unit tests files will have. * - * @return string[] + * @return array */ protected function getTestFiles( $testFileBase ): array { $sep = \DIRECTORY_SEPARATOR; From ddd65b44ea3a3f80b2b600db5ed8f973d537454a Mon Sep 17 00:00:00 2001 From: jrfnl Date: Fri, 20 Oct 2023 06:01:37 +0200 Subject: [PATCH 06/15] Files/TestDoubles: minor code tweaks [1] Lower nesting levels and remove `elseif` when not needed. :point_right: This commit will be easiest to review while ignoring whitespace changes. --- Yoast/Sniffs/Files/TestDoublesSniff.php | 107 +++++++++++++----------- 1 file changed, 56 insertions(+), 51 deletions(-) diff --git a/Yoast/Sniffs/Files/TestDoublesSniff.php b/Yoast/Sniffs/Files/TestDoublesSniff.php index a557913e..1adcfcd6 100644 --- a/Yoast/Sniffs/Files/TestDoublesSniff.php +++ b/Yoast/Sniffs/Files/TestDoublesSniff.php @@ -127,64 +127,69 @@ public function process( File $phpcsFile, $stackPtr ) { } if ( empty( $this->target_paths ) === true ) { - if ( $name_contains_double_or_mock === true ) { - // No valid target paths found. - $data = [ - $phpcsFile->config->basepath, - ]; - - if ( \count( $this->doubles_path ) === 1 ) { - $data[] = 'directory'; - $data[] = \implode( '', $this->doubles_path ); - } - else { - $all_paths = \implode( '", "', $this->doubles_path ); - $all_paths = \substr_replace( $all_paths, ' and', \strrpos( $all_paths, ',' ), 1 ); - - $data[] = 'directories'; - $data[] = $all_paths; - } - - $phpcsFile->addError( - 'Double/Mock test helper class detected, but no test doubles sub-%2$s found in "%1$s". Expected: "%3$s". Please create the sub-%2$s.', - $stackPtr, - 'NoDoublesDirectory', - $data - ); - } - } - else { - $path_to_file = $this->normalize_directory_separators( $file ); - $is_double_dir = false; - - foreach ( $this->target_paths as $target_path ) { - if ( \stripos( $path_to_file, $target_path ) !== false ) { - $is_double_dir = true; - break; - } + if ( $name_contains_double_or_mock === false ) { + return; } + // Mock/Double class found, but no valid target paths found. $data = [ - $tokens[ $stackPtr ]['content'], - $object_name, + $phpcsFile->config->basepath, ]; - if ( $name_contains_double_or_mock === true && $is_double_dir === false ) { - $phpcsFile->addError( - 'Double/Mock test helper classes should be placed in a dedicated test doubles sub-directory. Found %s: %s', - $stackPtr, - 'WrongDirectory', - $data - ); + if ( \count( $this->doubles_path ) === 1 ) { + $data[] = 'directory'; + $data[] = \implode( '', $this->doubles_path ); } - elseif ( $name_contains_double_or_mock === false && $is_double_dir === true ) { - $phpcsFile->addError( - 'Double/Mock test helper classes should contain "Double" or "Mock" in the class name. Found %s: %s', - $stackPtr, - 'InvalidClassName', - $data - ); + else { + $all_paths = \implode( '", "', $this->doubles_path ); + $all_paths = \substr_replace( $all_paths, ' and', \strrpos( $all_paths, ',' ), 1 ); + + $data[] = 'directories'; + $data[] = $all_paths; } + + $phpcsFile->addError( + 'Double/Mock test helper class detected, but no test doubles sub-%2$s found in "%1$s". Expected: "%3$s". Please create the sub-%2$s.', + $stackPtr, + 'NoDoublesDirectory', + $data + ); + + return; + } + + $path_to_file = $this->normalize_directory_separators( $file ); + $is_double_dir = false; + + foreach ( $this->target_paths as $target_path ) { + if ( \stripos( $path_to_file, $target_path ) !== false ) { + $is_double_dir = true; + break; + } + } + + $data = [ + $tokens[ $stackPtr ]['content'], + $object_name, + ]; + + if ( $name_contains_double_or_mock === true && $is_double_dir === false ) { + $phpcsFile->addError( + 'Double/Mock test helper classes should be placed in a dedicated test doubles sub-directory. Found %s: %s', + $stackPtr, + 'WrongDirectory', + $data + ); + return; + } + + if ( $name_contains_double_or_mock === false && $is_double_dir === true ) { + $phpcsFile->addError( + 'Double/Mock test helper classes should contain "Double" or "Mock" in the class name. Found %s: %s', + $stackPtr, + 'InvalidClassName', + $data + ); } } From 34c2b8e295b6b1a457b516d8ed9e197c7c1a7a43 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Tue, 24 Oct 2023 10:42:36 +0200 Subject: [PATCH 07/15] Files/TestDoubles: minor code tweaks [2] Move variable definitions to the point in the code flow where they will be used. No need to define them earlier. --- Yoast/Sniffs/Files/TestDoublesSniff.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Yoast/Sniffs/Files/TestDoublesSniff.php b/Yoast/Sniffs/Files/TestDoublesSniff.php index 1adcfcd6..ad8ef0f5 100644 --- a/Yoast/Sniffs/Files/TestDoublesSniff.php +++ b/Yoast/Sniffs/Files/TestDoublesSniff.php @@ -99,13 +99,12 @@ public function process( File $phpcsFile, $stackPtr ) { return ( $phpcsFile->numTokens + 1 ); } - $tokens = $phpcsFile->getTokens(); - $base_path = $this->normalize_directory_separators( $phpcsFile->config->basepath ); - $base_path = \rtrim( $base_path, '/' ) . '/'; // Make sure the base_path ends in a single slash. - if ( ! isset( $this->target_paths ) || \defined( 'PHP_CODESNIFFER_IN_TESTS' ) ) { $this->target_paths = []; + $base_path = $this->normalize_directory_separators( $phpcsFile->config->basepath ); + $base_path = \rtrim( $base_path, '/' ) . '/'; // Make sure the base_path ends in a single slash. + foreach ( $this->doubles_path as $doubles_path ) { $target_path = $base_path; $target_path .= \trim( $this->normalize_directory_separators( $doubles_path ), '/' ) . '/'; @@ -168,7 +167,8 @@ public function process( File $phpcsFile, $stackPtr ) { } } - $data = [ + $tokens = $phpcsFile->getTokens(); + $data = [ $tokens[ $stackPtr ]['content'], $object_name, ]; From 3749a69ce2196a5620199c05e379e4f268352342 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Fri, 20 Oct 2023 07:58:26 +0200 Subject: [PATCH 08/15] Files/TestDoubles: minor test tweaks Ensure all test files end on a new line. --- .../tests/mocks/correct-custom-dir-not-mock.inc | 2 +- .../TestDoublesUnitTests/tests/mocks/correct-custom-dir.inc | 2 +- .../TestDoublesUnitTests/tests/not-in-correct-custom-dir.inc | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Yoast/Tests/Files/TestDoublesUnitTests/tests/mocks/correct-custom-dir-not-mock.inc b/Yoast/Tests/Files/TestDoublesUnitTests/tests/mocks/correct-custom-dir-not-mock.inc index 89f388c3..1e02aee6 100644 --- a/Yoast/Tests/Files/TestDoublesUnitTests/tests/mocks/correct-custom-dir-not-mock.inc +++ b/Yoast/Tests/Files/TestDoublesUnitTests/tests/mocks/correct-custom-dir-not-mock.inc @@ -3,4 +3,4 @@ phpcs:set Yoast.Files.TestDoubles doubles_path[] /tests/mocks,/tests/assets class Prefix_ClassName {} -// phpcs:set Yoast.Files.TestDoubles doubles_path[] /tests/doubles \ No newline at end of file +// phpcs:set Yoast.Files.TestDoubles doubles_path[] /tests/doubles diff --git a/Yoast/Tests/Files/TestDoublesUnitTests/tests/mocks/correct-custom-dir.inc b/Yoast/Tests/Files/TestDoublesUnitTests/tests/mocks/correct-custom-dir.inc index 26933840..2c7da38f 100644 --- a/Yoast/Tests/Files/TestDoublesUnitTests/tests/mocks/correct-custom-dir.inc +++ b/Yoast/Tests/Files/TestDoublesUnitTests/tests/mocks/correct-custom-dir.inc @@ -3,4 +3,4 @@ phpcs:set Yoast.Files.TestDoubles doubles_path[] /tests/mocks,/tests/assets class Prefix_ClassName_Double {} -// phpcs:set Yoast.Files.TestDoubles doubles_path[] /tests/doubles \ No newline at end of file +// phpcs:set Yoast.Files.TestDoubles doubles_path[] /tests/doubles diff --git a/Yoast/Tests/Files/TestDoublesUnitTests/tests/not-in-correct-custom-dir.inc b/Yoast/Tests/Files/TestDoublesUnitTests/tests/not-in-correct-custom-dir.inc index 26933840..2c7da38f 100644 --- a/Yoast/Tests/Files/TestDoublesUnitTests/tests/not-in-correct-custom-dir.inc +++ b/Yoast/Tests/Files/TestDoublesUnitTests/tests/not-in-correct-custom-dir.inc @@ -3,4 +3,4 @@ phpcs:set Yoast.Files.TestDoubles doubles_path[] /tests/mocks,/tests/assets class Prefix_ClassName_Double {} -// phpcs:set Yoast.Files.TestDoubles doubles_path[] /tests/doubles \ No newline at end of file +// phpcs:set Yoast.Files.TestDoubles doubles_path[] /tests/doubles From a65b8541f4a23438ef3eb9bd121743bba55e4e9a Mon Sep 17 00:00:00 2001 From: jrfnl Date: Wed, 18 Oct 2023 02:50:09 +0200 Subject: [PATCH 09/15] Files/TestDoubles: use PHPCSUtils in more places --- Yoast/Sniffs/Files/TestDoublesSniff.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Yoast/Sniffs/Files/TestDoublesSniff.php b/Yoast/Sniffs/Files/TestDoublesSniff.php index ad8ef0f5..9e17923e 100644 --- a/Yoast/Sniffs/Files/TestDoublesSniff.php +++ b/Yoast/Sniffs/Files/TestDoublesSniff.php @@ -5,6 +5,7 @@ use PHP_CodeSniffer\Files\File; use PHP_CodeSniffer\Sniffs\Sniff; use PHPCSUtils\Utils\ObjectDeclarations; +use PHPCSUtils\Utils\TextStrings; /** * Check that all mock/doubles classes are in a `doubles` directory. @@ -72,7 +73,7 @@ public function register() { */ public function process( File $phpcsFile, $stackPtr ) { // Stripping potential quotes to ensure `stdin_path` passed by IDEs does not include quotes. - $file = \preg_replace( '`^([\'"])(.*)\1$`Ds', '$2', $phpcsFile->getFileName() ); + $file = TextStrings::stripQuotes( $phpcsFile->getFileName() ); if ( $file === 'STDIN' ) { return; // @codeCoverageIgnore From 282d86e6df2daeb82d7e28e24cb333aa1b96a987 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Thu, 19 Oct 2023 05:43:01 +0200 Subject: [PATCH 10/15] Files/TestDoubles: implement use of new PathHelper and PathValidationHelper classes and check case-sensitively Aside from starting to use the new `PathHelper` and `PathValidationHelper` utility function classes, this commit also changes the path comparison from case-INsensitive to case-sensitive, which, what with the change to strict PSR-4 compliance for all test files, including double/mock files, is a change which needs to be made anyway. The tests sub-directory names have been updated to proper case to be in line with this change, the inline property settings in test case files have been updated too. On top of that, a couple of dedicated new test cases have been added to verify the case-sensitive handling of the `double_path` property. Includes updating two pre-existing tests to pass duplicate excluded files in different ways. --- Yoast/Sniffs/Files/TestDoublesSniff.php | 37 +++++-------------- Yoast/Tests/Files/TestDoublesUnitTest.php | 30 +++++++++++---- .../correct-dir-double.inc | 0 .../correct-dir-interface-double.inc | 0 .../{doubles => Doubles}/correct-dir-mock.inc | 0 .../correct-dir-not-double-or-mock.inc | 0 .../correct-dir-trait-double.inc | 0 .../not-in-correct-subdir.inc | 0 .../Mocks/correct-custom-dir-not-mock.inc | 6 +++ .../tests/Mocks/correct-custom-dir.inc | 6 +++ ...correct-custom-dir-not-mock-wrong-case.inc | 6 +++ .../correct-custom-dir-wrong-case.inc | 6 +++ .../correct-custom-lowercase-dir-not-mock.inc | 6 +++ .../correct-custom-lowercase-dir.inc | 6 +++ .../mocks/correct-custom-dir-not-mock.inc | 6 --- .../tests/mocks/correct-custom-dir.inc | 6 --- .../tests/no-doubles-path-property.inc | 2 +- .../non-existant-doubles-dir-not-double.inc | 2 +- .../tests/non-existant-doubles-dir.inc | 4 +- .../tests/non-existant-doubles-dirs.inc | 2 +- .../tests/not-in-correct-custom-dir.inc | 4 +- Yoast/ruleset.xml | 2 +- 22 files changed, 75 insertions(+), 56 deletions(-) rename Yoast/Tests/Files/TestDoublesUnitTests/tests/{doubles => Doubles}/correct-dir-double.inc (100%) rename Yoast/Tests/Files/TestDoublesUnitTests/tests/{doubles => Doubles}/correct-dir-interface-double.inc (100%) rename Yoast/Tests/Files/TestDoublesUnitTests/tests/{doubles => Doubles}/correct-dir-mock.inc (100%) rename Yoast/Tests/Files/TestDoublesUnitTests/tests/{doubles => Doubles}/correct-dir-not-double-or-mock.inc (100%) rename Yoast/Tests/Files/TestDoublesUnitTests/tests/{doubles => Doubles}/correct-dir-trait-double.inc (100%) rename Yoast/Tests/Files/TestDoublesUnitTests/tests/{doubles-not-correct => DoublesNotCorrect}/not-in-correct-subdir.inc (100%) create mode 100644 Yoast/Tests/Files/TestDoublesUnitTests/tests/Mocks/correct-custom-dir-not-mock.inc create mode 100644 Yoast/Tests/Files/TestDoublesUnitTests/tests/Mocks/correct-custom-dir.inc create mode 100644 Yoast/Tests/Files/TestDoublesUnitTests/tests/lowercase/correct-custom-dir-not-mock-wrong-case.inc create mode 100644 Yoast/Tests/Files/TestDoublesUnitTests/tests/lowercase/correct-custom-dir-wrong-case.inc create mode 100644 Yoast/Tests/Files/TestDoublesUnitTests/tests/lowercase/correct-custom-lowercase-dir-not-mock.inc create mode 100644 Yoast/Tests/Files/TestDoublesUnitTests/tests/lowercase/correct-custom-lowercase-dir.inc delete mode 100644 Yoast/Tests/Files/TestDoublesUnitTests/tests/mocks/correct-custom-dir-not-mock.inc delete mode 100644 Yoast/Tests/Files/TestDoublesUnitTests/tests/mocks/correct-custom-dir.inc diff --git a/Yoast/Sniffs/Files/TestDoublesSniff.php b/Yoast/Sniffs/Files/TestDoublesSniff.php index 9e17923e..e8b35089 100644 --- a/Yoast/Sniffs/Files/TestDoublesSniff.php +++ b/Yoast/Sniffs/Files/TestDoublesSniff.php @@ -6,6 +6,8 @@ use PHP_CodeSniffer\Sniffs\Sniff; use PHPCSUtils\Utils\ObjectDeclarations; use PHPCSUtils\Utils\TextStrings; +use YoastCS\Yoast\Utils\PathHelper; +use YoastCS\Yoast\Utils\PathValidationHelper; /** * Check that all mock/doubles classes are in a `doubles` directory. @@ -38,14 +40,14 @@ final class TestDoublesSniff implements Sniff { * @var array */ public $doubles_path = [ - '/tests/doubles', + '/tests/Doubles', ]; /** * Validated absolute target paths for test fixture directories or an empty array * if the intended target directory/directories don't exist. * - * @var array + * @var array */ private $target_paths; @@ -101,19 +103,9 @@ public function process( File $phpcsFile, $stackPtr ) { } if ( ! isset( $this->target_paths ) || \defined( 'PHP_CODESNIFFER_IN_TESTS' ) ) { - $this->target_paths = []; - - $base_path = $this->normalize_directory_separators( $phpcsFile->config->basepath ); - $base_path = \rtrim( $base_path, '/' ) . '/'; // Make sure the base_path ends in a single slash. - - foreach ( $this->doubles_path as $doubles_path ) { - $target_path = $base_path; - $target_path .= \trim( $this->normalize_directory_separators( $doubles_path ), '/' ) . '/'; - - if ( \file_exists( $target_path ) && \is_dir( $target_path ) ) { - $this->target_paths[] = \strtolower( $target_path ); - } - } + $this->target_paths = PathValidationHelper::relative_to_absolute( $phpcsFile, $this->doubles_path ); + $this->target_paths = \array_filter( $this->target_paths, 'file_exists' ); + $this->target_paths = \array_filter( $this->target_paths, 'is_dir' ); } $object_name = ObjectDeclarations::getName( $phpcsFile, $stackPtr ); @@ -158,11 +150,11 @@ public function process( File $phpcsFile, $stackPtr ) { return; } - $path_to_file = $this->normalize_directory_separators( $file ); + $path_to_file = PathHelper::normalize_absolute_path( $file ); $is_double_dir = false; foreach ( $this->target_paths as $target_path ) { - if ( \stripos( $path_to_file, $target_path ) !== false ) { + if ( PathHelper::path_starts_with( $path_to_file, $target_path ) === true ) { $is_double_dir = true; break; } @@ -193,15 +185,4 @@ public function process( File $phpcsFile, $stackPtr ) { ); } } - - /** - * Normalize all directory separators to be a forward slash. - * - * @param string $path Path to normalize. - * - * @return string - */ - private function normalize_directory_separators( $path ) { - return \strtr( $path, '\\', '/' ); - } } diff --git a/Yoast/Tests/Files/TestDoublesUnitTest.php b/Yoast/Tests/Files/TestDoublesUnitTest.php index 09baf7d8..615e2071 100644 --- a/Yoast/Tests/Files/TestDoublesUnitTest.php +++ b/Yoast/Tests/Files/TestDoublesUnitTest.php @@ -83,32 +83,46 @@ public function getErrorList( string $testFile = '' ): array { 3 => 1, ]; - // In tests/doubles. + // In tests/Doubles. case 'correct-dir-not-double-or-mock.inc': return [ 3 => 1, ]; - // In tests/doubles-not-correct. + // In tests/DoublesNotCorrect. case 'not-in-correct-subdir.inc': return [ 3 => 1, ]; - // In tests/mocks. + // In tests/Mocks. case 'correct-custom-dir-not-mock.inc': return [ 4 => 1, ]; + // In tests/lowercase. + case 'correct-custom-dir-wrong-case.inc': + return [ + 4 => 1, + ]; + + // In tests/lowercase. + case 'correct-custom-lowercase-dir-not-mock.inc': + return [ + 4 => 1, + ]; + case 'live-coding.inc': // In tests. case 'not-double-or-mock.inc': // In tests. case 'non-existant-doubles-dir-not-double.inc': // In tests. - case 'correct-dir-double.inc': // In tests/doubles. - case 'correct-dir-mock.inc': // In tests/doubles. - case 'correct-dir-interface.inc': // In tests/doubles. - case 'correct-dir-trait-double.inc': // In tests/doubles. - case 'correct-custom-dir.inc': // In tests/mocks. + case 'correct-dir-double.inc': // In tests/Doubles. + case 'correct-dir-mock.inc': // In tests/Doubles. + case 'correct-dir-interface.inc': // In tests/Doubles. + case 'correct-dir-trait-double.inc': // In tests/Doubles. + case 'correct-custom-dir.inc': // In tests/Mocks. + case 'correct-custom-lowercase-dir.inc': // In tests/lowercase. + case 'correct-custom-dir-not-mock-wrong-case.inc': // In tests/lowercase. default: return []; } diff --git a/Yoast/Tests/Files/TestDoublesUnitTests/tests/doubles/correct-dir-double.inc b/Yoast/Tests/Files/TestDoublesUnitTests/tests/Doubles/correct-dir-double.inc similarity index 100% rename from Yoast/Tests/Files/TestDoublesUnitTests/tests/doubles/correct-dir-double.inc rename to Yoast/Tests/Files/TestDoublesUnitTests/tests/Doubles/correct-dir-double.inc diff --git a/Yoast/Tests/Files/TestDoublesUnitTests/tests/doubles/correct-dir-interface-double.inc b/Yoast/Tests/Files/TestDoublesUnitTests/tests/Doubles/correct-dir-interface-double.inc similarity index 100% rename from Yoast/Tests/Files/TestDoublesUnitTests/tests/doubles/correct-dir-interface-double.inc rename to Yoast/Tests/Files/TestDoublesUnitTests/tests/Doubles/correct-dir-interface-double.inc diff --git a/Yoast/Tests/Files/TestDoublesUnitTests/tests/doubles/correct-dir-mock.inc b/Yoast/Tests/Files/TestDoublesUnitTests/tests/Doubles/correct-dir-mock.inc similarity index 100% rename from Yoast/Tests/Files/TestDoublesUnitTests/tests/doubles/correct-dir-mock.inc rename to Yoast/Tests/Files/TestDoublesUnitTests/tests/Doubles/correct-dir-mock.inc diff --git a/Yoast/Tests/Files/TestDoublesUnitTests/tests/doubles/correct-dir-not-double-or-mock.inc b/Yoast/Tests/Files/TestDoublesUnitTests/tests/Doubles/correct-dir-not-double-or-mock.inc similarity index 100% rename from Yoast/Tests/Files/TestDoublesUnitTests/tests/doubles/correct-dir-not-double-or-mock.inc rename to Yoast/Tests/Files/TestDoublesUnitTests/tests/Doubles/correct-dir-not-double-or-mock.inc diff --git a/Yoast/Tests/Files/TestDoublesUnitTests/tests/doubles/correct-dir-trait-double.inc b/Yoast/Tests/Files/TestDoublesUnitTests/tests/Doubles/correct-dir-trait-double.inc similarity index 100% rename from Yoast/Tests/Files/TestDoublesUnitTests/tests/doubles/correct-dir-trait-double.inc rename to Yoast/Tests/Files/TestDoublesUnitTests/tests/Doubles/correct-dir-trait-double.inc diff --git a/Yoast/Tests/Files/TestDoublesUnitTests/tests/doubles-not-correct/not-in-correct-subdir.inc b/Yoast/Tests/Files/TestDoublesUnitTests/tests/DoublesNotCorrect/not-in-correct-subdir.inc similarity index 100% rename from Yoast/Tests/Files/TestDoublesUnitTests/tests/doubles-not-correct/not-in-correct-subdir.inc rename to Yoast/Tests/Files/TestDoublesUnitTests/tests/DoublesNotCorrect/not-in-correct-subdir.inc diff --git a/Yoast/Tests/Files/TestDoublesUnitTests/tests/Mocks/correct-custom-dir-not-mock.inc b/Yoast/Tests/Files/TestDoublesUnitTests/tests/Mocks/correct-custom-dir-not-mock.inc new file mode 100644 index 00000000..e0bc3cf6 --- /dev/null +++ b/Yoast/Tests/Files/TestDoublesUnitTests/tests/Mocks/correct-custom-dir-not-mock.inc @@ -0,0 +1,6 @@ +phpcs:set Yoast.Files.TestDoubles doubles_path[] /tests/Mocks/,/tests/Assets,tests/Mocks,./tests/Mocks + - + From 80aa65ea8337ad8cd0e9fd2be2663c69c02094bd Mon Sep 17 00:00:00 2001 From: jrfnl Date: Fri, 20 Oct 2023 06:55:41 +0200 Subject: [PATCH 11/15] Files/TestDoubles: make the sniff codebase agnostic The `public $doubles_path` contained an arbitrary default value. While this default value is applicable for some of the Yoast codebases, it still needs to be duplicated in the ruleset due to a bug/missing feature in PHPCS in how array properties containing default values are handled. Having this arbitrary default value in the sniff also codifies some Yoast-specific repo layout logic in the sniff, while sniffs, generally speaking should be code-base agnostic. With that in mind, I'm removing the default value. The `Yoast` ruleset contains this value anyhow (due to the bug/missing feature) and that is the more appropriate place for code-base specific/organisation specific property values. Includes updating the test case files to allow for this change. Refs: * squizlabs/PHP_CodeSniffer 2154 * squizlabs/PHP_CodeSniffer 2228 --- Yoast/Sniffs/Files/TestDoublesSniff.php | 17 +++++------------ Yoast/Tests/Files/TestDoublesUnitTest.php | 8 ++++---- .../tests/Doubles/correct-dir-double.inc | 3 +++ .../Doubles/correct-dir-interface-double.inc | 3 +++ .../tests/Doubles/correct-dir-mock.inc | 3 +++ .../Doubles/correct-dir-not-double-or-mock.inc | 3 +++ .../tests/Doubles/correct-dir-trait-double.inc | 3 +++ .../DoublesNotCorrect/not-in-correct-subdir.inc | 3 +++ .../tests/Mocks/correct-custom-dir-not-mock.inc | 2 +- .../tests/Mocks/correct-custom-dir.inc | 2 +- .../TestDoublesUnitTests/tests/live-coding.inc | 3 +++ .../correct-custom-dir-not-mock-wrong-case.inc | 2 +- .../lowercase/correct-custom-dir-wrong-case.inc | 2 +- .../correct-custom-lowercase-dir-not-mock.inc | 2 +- .../lowercase/correct-custom-lowercase-dir.inc | 2 +- .../tests/mock-not-in-correct-dir.inc | 3 +++ .../tests/no-doubles-path-property.inc | 3 --- .../non-existant-doubles-dir-not-double.inc | 2 +- .../tests/non-existant-doubles-dir.inc | 2 +- .../tests/non-existant-doubles-dirs.inc | 2 +- .../tests/not-double-or-mock.inc | 3 +++ .../tests/not-in-correct-custom-dir.inc | 2 +- .../tests/not-in-correct-dir-double.inc | 3 +++ .../not-in-correct-dir-interface-double.inc | 3 +++ .../tests/not-in-correct-dir-mock.inc | 3 +++ .../tests/not-in-correct-dir-trait-double.inc | 3 +++ Yoast/ruleset.xml | 1 - 27 files changed, 58 insertions(+), 30 deletions(-) diff --git a/Yoast/Sniffs/Files/TestDoublesSniff.php b/Yoast/Sniffs/Files/TestDoublesSniff.php index e8b35089..8dbe0b20 100644 --- a/Yoast/Sniffs/Files/TestDoublesSniff.php +++ b/Yoast/Sniffs/Files/TestDoublesSniff.php @@ -10,9 +10,9 @@ use YoastCS\Yoast\Utils\PathValidationHelper; /** - * Check that all mock/doubles classes are in a `doubles` directory. + * Check that all mock/doubles classes are in a dedicated directory for test fixtures. * - * Additionally, checks that all classes in the `doubles` directory/directories + * Additionally, checks that all classes in this fixtures directory/directories * have `Double` or `Mock` in the class name. * * @since 1.0.0 @@ -26,22 +26,15 @@ final class TestDoublesSniff implements Sniff { * The paths should be relative to the root/basepath of the project and can be * customized from within a custom ruleset. * - * Preferably only one path is provided per project, but in exceptional circumstances - * multiple paths can be allowed. - * - * The new PHPCS 3.4.0 array `extend` feature can be used to add to this list. - * To overrule the list, just set the property. - * {@link https://github.com/squizlabs/PHP_CodeSniffer/pull/2154} - * * @since 1.0.0 * @since 1.1.0 The property type has changed from string to array. * Use of this property with a string value has been deprecated. + * @since 3.0.0 The default value has changed to an empty array. + * This property will now always need to be set from within a ruleset. * * @var array */ - public $doubles_path = [ - '/tests/Doubles', - ]; + public $doubles_path = []; /** * Validated absolute target paths for test fixture directories or an empty array diff --git a/Yoast/Tests/Files/TestDoublesUnitTest.php b/Yoast/Tests/Files/TestDoublesUnitTest.php index 615e2071..de44c8d1 100644 --- a/Yoast/Tests/Files/TestDoublesUnitTest.php +++ b/Yoast/Tests/Files/TestDoublesUnitTest.php @@ -61,7 +61,7 @@ public function getErrorList( string $testFile = '' ): array { // In tests/. case 'mock-not-in-correct-dir.inc': return [ - 3 => 1, + 4 => 1, ]; case 'non-existant-doubles-dir.inc': @@ -80,19 +80,19 @@ public function getErrorList( string $testFile = '' ): array { case 'not-in-correct-dir-interface-double.inc': case 'not-in-correct-dir-trait-double.inc': return [ - 3 => 1, + 4 => 1, ]; // In tests/Doubles. case 'correct-dir-not-double-or-mock.inc': return [ - 3 => 1, + 4 => 1, ]; // In tests/DoublesNotCorrect. case 'not-in-correct-subdir.inc': return [ - 3 => 1, + 4 => 1, ]; // In tests/Mocks. diff --git a/Yoast/Tests/Files/TestDoublesUnitTests/tests/Doubles/correct-dir-double.inc b/Yoast/Tests/Files/TestDoublesUnitTests/tests/Doubles/correct-dir-double.inc index 0ef27d1c..f0fe58b9 100644 --- a/Yoast/Tests/Files/TestDoublesUnitTests/tests/Doubles/correct-dir-double.inc +++ b/Yoast/Tests/Files/TestDoublesUnitTests/tests/Doubles/correct-dir-double.inc @@ -1,3 +1,6 @@ +phpcs:set Yoast.Files.TestDoubles doubles_path[] /tests/Doubles - From 5374572c657c6fd8ff4faf3953ca53afc04c1b75 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Fri, 20 Oct 2023 06:57:17 +0200 Subject: [PATCH 12/15] YoastCS/TestDoubles sniff: update the `doubles_path` property value ... to be in line with the change in the test directory structure in the plugin repos and to be compliant with the change to (case-sensitive) PSR-4 for all test files. --- Yoast/ruleset.xml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Yoast/ruleset.xml b/Yoast/ruleset.xml index ccca0b91..ed2c55eb 100644 --- a/Yoast/ruleset.xml +++ b/Yoast/ruleset.xml @@ -119,6 +119,8 @@ + + From d4634292f411dc8c4b08c5e68cb4240134172e68 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Fri, 20 Oct 2023 09:22:53 +0200 Subject: [PATCH 13/15] Files/TestDoubles: allow for doubling PHP 8.1+ enums Includes tests covering this change. --- Yoast/Sniffs/Files/TestDoublesSniff.php | 16 ++++++++-------- Yoast/Tests/Files/TestDoublesUnitTest.php | 2 ++ .../tests/Doubles/correct-dir-enum-double.inc | 6 ++++++ .../tests/not-in-correct-dir-enum-double.inc | 6 ++++++ 4 files changed, 22 insertions(+), 8 deletions(-) create mode 100644 Yoast/Tests/Files/TestDoublesUnitTests/tests/Doubles/correct-dir-enum-double.inc create mode 100644 Yoast/Tests/Files/TestDoublesUnitTests/tests/not-in-correct-dir-enum-double.inc diff --git a/Yoast/Sniffs/Files/TestDoublesSniff.php b/Yoast/Sniffs/Files/TestDoublesSniff.php index 8dbe0b20..68896439 100644 --- a/Yoast/Sniffs/Files/TestDoublesSniff.php +++ b/Yoast/Sniffs/Files/TestDoublesSniff.php @@ -4,16 +4,17 @@ use PHP_CodeSniffer\Files\File; use PHP_CodeSniffer\Sniffs\Sniff; +use PHP_CodeSniffer\Util\Tokens; use PHPCSUtils\Utils\ObjectDeclarations; use PHPCSUtils\Utils\TextStrings; use YoastCS\Yoast\Utils\PathHelper; use YoastCS\Yoast\Utils\PathValidationHelper; /** - * Check that all mock/doubles classes are in a dedicated directory for test fixtures. + * Check that all mock/doubles OO structures are in a dedicated directory for test fixtures. * - * Additionally, checks that all classes in this fixtures directory/directories - * have `Double` or `Mock` in the class name. + * Additionally, checks that all OO structures in this fixtures directory/directories + * have `Double` or `Mock` in their name. * * @since 1.0.0 * @since 3.0.0 This sniff will no longer check for multiple OO object declarations within one file. @@ -50,11 +51,10 @@ final class TestDoublesSniff implements Sniff { * @return array */ public function register() { - return [ - \T_CLASS, - \T_INTERFACE, - \T_TRAIT, - ]; + $targets = Tokens::$ooScopeTokens; + unset( $targets[ \T_ANON_CLASS ] ); + + return $targets; } /** diff --git a/Yoast/Tests/Files/TestDoublesUnitTest.php b/Yoast/Tests/Files/TestDoublesUnitTest.php index de44c8d1..d8f2d1ac 100644 --- a/Yoast/Tests/Files/TestDoublesUnitTest.php +++ b/Yoast/Tests/Files/TestDoublesUnitTest.php @@ -77,6 +77,7 @@ public function getErrorList( string $testFile = '' ): array { case 'not-in-correct-dir-double.inc': case 'not-in-correct-dir-mock.inc': + case 'not-in-correct-dir-enum-double.inc': case 'not-in-correct-dir-interface-double.inc': case 'not-in-correct-dir-trait-double.inc': return [ @@ -118,6 +119,7 @@ public function getErrorList( string $testFile = '' ): array { case 'non-existant-doubles-dir-not-double.inc': // In tests. case 'correct-dir-double.inc': // In tests/Doubles. case 'correct-dir-mock.inc': // In tests/Doubles. + case 'correct-dir-enum.inc': // In tests/Doubles. case 'correct-dir-interface.inc': // In tests/Doubles. case 'correct-dir-trait-double.inc': // In tests/Doubles. case 'correct-custom-dir.inc': // In tests/Mocks. diff --git a/Yoast/Tests/Files/TestDoublesUnitTests/tests/Doubles/correct-dir-enum-double.inc b/Yoast/Tests/Files/TestDoublesUnitTests/tests/Doubles/correct-dir-enum-double.inc new file mode 100644 index 00000000..f035a55d --- /dev/null +++ b/Yoast/Tests/Files/TestDoublesUnitTests/tests/Doubles/correct-dir-enum-double.inc @@ -0,0 +1,6 @@ +phpcs:set Yoast.Files.TestDoubles doubles_path[] /tests/Doubles + Date: Fri, 20 Oct 2023 09:26:45 +0200 Subject: [PATCH 14/15] Files/TestDoubles: minor improvements to the XML docs ... to more closely match what the sniff is looking for. --- Yoast/Docs/Files/TestDoublesStandard.xml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Yoast/Docs/Files/TestDoublesStandard.xml b/Yoast/Docs/Files/TestDoublesStandard.xml index 36db7a2e..c6b1b9fe 100644 --- a/Yoast/Docs/Files/TestDoublesStandard.xml +++ b/Yoast/Docs/Files/TestDoublesStandard.xml @@ -5,8 +5,9 @@ > From 363c292abb75568527de62908075940f390e22e5 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Fri, 20 Oct 2023 09:34:41 +0200 Subject: [PATCH 15/15] Files/TestDoubles: minor tweaks to the error messages ... to remove the presumption that all test fixtures are classes. --- Yoast/Sniffs/Files/TestDoublesSniff.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Yoast/Sniffs/Files/TestDoublesSniff.php b/Yoast/Sniffs/Files/TestDoublesSniff.php index 68896439..1cd18f34 100644 --- a/Yoast/Sniffs/Files/TestDoublesSniff.php +++ b/Yoast/Sniffs/Files/TestDoublesSniff.php @@ -111,6 +111,7 @@ public function process( File $phpcsFile, $stackPtr ) { $name_contains_double_or_mock = true; } + $tokens = $phpcsFile->getTokens(); if ( empty( $this->target_paths ) === true ) { if ( $name_contains_double_or_mock === false ) { return; @@ -118,6 +119,7 @@ public function process( File $phpcsFile, $stackPtr ) { // Mock/Double class found, but no valid target paths found. $data = [ + $tokens[ $stackPtr ]['content'], $phpcsFile->config->basepath, ]; @@ -134,7 +136,7 @@ public function process( File $phpcsFile, $stackPtr ) { } $phpcsFile->addError( - 'Double/Mock test helper class detected, but no test doubles sub-%2$s found in "%1$s". Expected: "%3$s". Please create the sub-%2$s.', + 'Double/Mock test helper %1$s detected, but no test fixtures sub-%3$s found in "%2$s". Expected: "%4$s". Please create the sub-%3$s.', $stackPtr, 'NoDoublesDirectory', $data @@ -153,15 +155,14 @@ public function process( File $phpcsFile, $stackPtr ) { } } - $tokens = $phpcsFile->getTokens(); - $data = [ + $data = [ $tokens[ $stackPtr ]['content'], $object_name, ]; if ( $name_contains_double_or_mock === true && $is_double_dir === false ) { $phpcsFile->addError( - 'Double/Mock test helper classes should be placed in a dedicated test doubles sub-directory. Found %s: %s', + 'Double/Mock test helpers should be placed in a dedicated test fixtures sub-directory. Found %s: %s', $stackPtr, 'WrongDirectory', $data @@ -171,7 +172,7 @@ public function process( File $phpcsFile, $stackPtr ) { if ( $name_contains_double_or_mock === false && $is_double_dir === true ) { $phpcsFile->addError( - 'Double/Mock test helper classes should contain "Double" or "Mock" in the class name. Found %s: %s', + 'Double/Mock test helpers should contain "Double" or "Mock" in the class name. Found %s: %s', $stackPtr, 'InvalidClassName', $data