Skip to content

Commit

Permalink
Files/TestDoubles: minor tweaks to the error messages
Browse files Browse the repository at this point in the history
... to remove the presumption that all test fixtures are classes.
  • Loading branch information
jrfnl committed Nov 20, 2023
1 parent fa5a494 commit 363c292
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions Yoast/Sniffs/Files/TestDoublesSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,15 @@ 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;
}

// Mock/Double class found, but no valid target paths found.
$data = [
$tokens[ $stackPtr ]['content'],
$phpcsFile->config->basepath,
];

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 363c292

Please sign in to comment.