Skip to content

Commit e715707

Browse files
committed
AbstractMethodUnitTest: improve finding of target token
These changes are similar to changes previously made in the same method in PHPCSUtils. As things were, there could be a situation where the `getTargetTokenFromFile()` method did not find the delimiter comment. In that case, the method would search for the target token starting at token 0, which would generally lead to an incorrect token being identified as the target token. This has now been fixed by verifying the outcome of the `findPrevious()` call and throwing an exception (causing the test to fail) when the delimiter comment was not found. Along the same lines, when the target token would not be found, an exception will now be thrown as well.
1 parent e245088 commit e715707

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

tests/Core/AbstractMethodUnitTest.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace PHP_CodeSniffer\Tests\Core;
1111

12+
use Exception;
1213
use PHP_CodeSniffer\Files\DummyFile;
1314
use PHP_CodeSniffer\Files\File;
1415
use PHP_CodeSniffer\Ruleset;
@@ -110,6 +111,9 @@ public function getTargetToken($commentString, $tokenType, $tokenContent=null)
110111
* @param string $tokenContent Optional. The token content for the target token.
111112
*
112113
* @return int
114+
*
115+
* @throws Exception When the test delimiter comment is not found.
116+
* @throws Exception When the test target token is not found.
113117
*/
114118
public static function getTargetTokenFromFile(File $phpcsFile, $commentString, $tokenType, $tokenContent=null)
115119
{
@@ -122,6 +126,12 @@ public static function getTargetTokenFromFile(File $phpcsFile, $commentString, $
122126
$commentString
123127
);
124128

129+
if ($comment === false) {
130+
throw new Exception(
131+
sprintf('Failed to find the test marker: %s in test case file %s', $commentString, $phpcsFile->getFilename())
132+
);
133+
}
134+
125135
$tokens = $phpcsFile->getTokens();
126136
$end = ($start + 1);
127137

@@ -148,10 +158,10 @@ public static function getTargetTokenFromFile(File $phpcsFile, $commentString, $
148158
if ($target === false) {
149159
$msg = 'Failed to find test target token for comment string: '.$commentString;
150160
if ($tokenContent !== null) {
151-
$msg .= ' With token content: '.$tokenContent;
161+
$msg .= ' with token content: '.$tokenContent;
152162
}
153163

154-
self::assertFalse(true, $msg);
164+
throw new Exception($msg);
155165
}
156166

157167
return $target;

0 commit comments

Comments
 (0)