Skip to content

Commit

Permalink
Files/TestDoubles: implement use of new PathHelper and PathValidation…
Browse files Browse the repository at this point in the history
…Helper 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.
  • Loading branch information
jrfnl committed Nov 19, 2023
1 parent f07d449 commit 8d695d5
Show file tree
Hide file tree
Showing 22 changed files with 75 additions and 56 deletions.
37 changes: 9 additions & 28 deletions Yoast/Sniffs/Files/TestDoublesSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -38,14 +40,14 @@ final class TestDoublesSniff implements Sniff {
* @var array<string>
*/
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<string>
* @var array<string, string>
*/
private $target_paths;

Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -158,11 +150,11 @@ public function process( File $phpcsFile, $stackPtr ) {
return;
}

$path_to_file = $this->normalize_directory_separators( $file );
$path_to_file = PathHelper::normalize_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;
}
Expand Down Expand Up @@ -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, '\\', '/' );
}
}
30 changes: 22 additions & 8 deletions Yoast/Tests/Files/TestDoublesUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [];
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
phpcs:set Yoast.Files.TestDoubles doubles_path[] /tests/Mocks/,/tests/Assets,tests/Mocks,./tests/Mocks
<?php

class Prefix_ClassName {}

// phpcs:set Yoast.Files.TestDoubles doubles_path[] /tests/Doubles
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
phpcs:set Yoast.Files.TestDoubles doubles_path[] /tests/Mocks,/tests/Assets,tests/Mocks/,./tests/Mocks
<?php

class Prefix_ClassName_Double {}

// phpcs:set Yoast.Files.TestDoubles doubles_path[] /tests/Doubles
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
phpcs:set Yoast.Files.TestDoubles doubles_path[] /tests/Lowercase
<?php

class Prefix_ClassName {}

// phpcs:set Yoast.Files.TestDoubles doubles_path[] /tests/Doubles
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
phpcs:set Yoast.Files.TestDoubles doubles_path[] /tests/Lowercase
<?php

class Prefix_ClassName_Double {}

// phpcs:set Yoast.Files.TestDoubles doubles_path[] /tests/Doubles
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
phpcs:set Yoast.Files.TestDoubles doubles_path[] /tests/lowercase/
<?php

class Prefix_ClassName {}

// phpcs:set Yoast.Files.TestDoubles doubles_path[] /tests/Doubles
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
phpcs:set Yoast.Files.TestDoubles doubles_path[] /tests/lowercase
<?php

class Prefix_ClassName_Double {}

// phpcs:set Yoast.Files.TestDoubles doubles_path[] /tests/Doubles

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

class Prefix_NoDoublesPathProperty_Double {}

// phpcs:set Yoast.Files.TestDoubles doubles_path[] /tests/doubles
// phpcs:set Yoast.Files.TestDoubles doubles_path[] /tests/Doubles
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ phpcs:set Yoast.Files.TestDoubles doubles_path[] /tests/doesnotexist

class Prefix_ClassName {}

// phpcs:set Yoast.Files.TestDoubles doubles_path[] /tests/doubles
// phpcs:set Yoast.Files.TestDoubles doubles_path[] /tests/Doubles
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
phpcs:set Yoast.Files.TestDoubles doubles_path[] /tests/doesnotexist
phpcs:set Yoast.Files.TestDoubles doubles_path[] /tests/Doesnotexist
<?php

class Prefix_ClassName_Double {}

// phpcs:set Yoast.Files.TestDoubles doubles_path[] /tests/doubles
// phpcs:set Yoast.Files.TestDoubles doubles_path[] /tests/Doubles
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ phpcs:set Yoast.Files.TestDoubles doubles_path[] /tests/doesnotexist,/tests/non-

class Prefix_ClassName_Double {}

// phpcs:set Yoast.Files.TestDoubles doubles_path[] /tests/doubles
// phpcs:set Yoast.Files.TestDoubles doubles_path[] /tests/Doubles
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
phpcs:set Yoast.Files.TestDoubles doubles_path[] /tests/mocks,/tests/assets
phpcs:set Yoast.Files.TestDoubles doubles_path[] /tests/Mocks,/tests/Assets
<?php

class Prefix_ClassName_Double {}

// phpcs:set Yoast.Files.TestDoubles doubles_path[] /tests/doubles
// phpcs:set Yoast.Files.TestDoubles doubles_path[] /tests/Doubles
2 changes: 1 addition & 1 deletion Yoast/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
<rule ref="Yoast.Files.TestDoubles">
<properties>
<property name="doubles_path" type="array">
<element value="/tests/doubles"/>
<element value="/tests/Doubles"/>
</property>
</properties>
</rule>
Expand Down

0 comments on commit 8d695d5

Please sign in to comment.