Skip to content

Commit abad7a0

Browse files
committed
Tests\Core\AbstractMethodUnitTest: add new getTargetToken() method
This adds a new `getTargetToken()` method which can retrieve the target token for testing with higher precision than the (duplicate) code which was so far used in the individual test methods. The improvements this method offers are: * Avoid test leaking/contamination. If/when the token to start the test from was retrieved by doing a `findNext()` from the delimiter comment, a typo could cause a token from the *next* test to be used for the testing instead of the target token. If the expected results would be the same for both tests, this would go completely unnoticed. This is now no longer possible. The only requirement is that the delimiter comments start with `/* test...`. * No more token counting when setting up the unit tests, just pass the target token type constant to this method and it will get you the correct token. This also allows for not having to jump through hoops when deciding where to place the delimiter comment for the test. * If the token a test looks for is a `T_STRING` or text based token, the optional `$tokenContent` allows for selecting the correct token, even when there are several of the same type in the test case line.
1 parent 511a538 commit abad7a0

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

tests/Core/AbstractMethodUnitTest.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,64 @@ public static function tearDownAfterClass()
7777
}//end tearDownAfterClass()
7878

7979

80+
/**
81+
* Get the token pointer for a target token based on a specific comment found on the line before.
82+
*
83+
* Note: the test delimiter comment MUST start with "/* test" to allow this function to
84+
* distinguish between comments used *in* a test and test delimiters.
85+
*
86+
* @param string $commentString The delimiter comment to look for.
87+
* @param int|string|array $tokenType The type of token(s) to look for.
88+
* @param string $tokenContent Optional. The token content for the target token.
89+
*
90+
* @return int
91+
*/
92+
public function getTargetToken($commentString, $tokenType, $tokenContent=null)
93+
{
94+
$start = (self::$phpcsFile->numTokens - 1);
95+
$comment = self::$phpcsFile->findPrevious(
96+
T_COMMENT,
97+
$start,
98+
null,
99+
false,
100+
$commentString
101+
);
102+
103+
$tokens = self::$phpcsFile->getTokens();
104+
$end = ($start + 1);
105+
106+
// Limit the token finding to between this and the next delimiter comment.
107+
for ($i = ($comment + 1); $i < $end; $i++) {
108+
if ($tokens[$i]['code'] !== T_COMMENT) {
109+
continue;
110+
}
111+
112+
if (stripos($tokens[$i]['content'], '/* test') === 0) {
113+
$end = $i;
114+
break;
115+
}
116+
}
117+
118+
$target = self::$phpcsFile->findNext(
119+
$tokenType,
120+
($comment + 1),
121+
$end,
122+
false,
123+
$tokenContent
124+
);
125+
126+
if ($target === false) {
127+
$msg = 'Failed to find test target token for comment string: '.$commentString;
128+
if ($tokenContent !== null) {
129+
$msg .= ' With token content: '.$tokenContent;
130+
}
131+
132+
$this->assertFalse(true, $msg);
133+
}
134+
135+
return $target;
136+
137+
}//end getTargetToken()
138+
139+
80140
}//end class

0 commit comments

Comments
 (0)