Skip to content

Commit

Permalink
BackfillFnTokenTest: use data providers when appropriate
Browse files Browse the repository at this point in the history
These two tests were testing multiple test cases, but not using a data provider, which means that if one of the test cases would fail, the ones after the failing test case wouldn't even be tested.

By using a data provider, that issue is avoided.

It also allows for simplifying some of the test code for the `testKeywordReturnTypes()` test which duplicated the logic for the `scopePositionTestHelper()` just to allow for custom error messages mentioning the test marker. This is no longer needed when each marker is tested individually via the data provider.
  • Loading branch information
jrfnl committed Apr 22, 2024
1 parent 5f38ce0 commit 46b883d
Showing 1 changed file with 66 additions and 34 deletions.
100 changes: 66 additions & 34 deletions tests/Core/Tokenizer/BackfillFnTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,43 @@ final class BackfillFnTokenTest extends AbstractTokenizerTestCase
/**
* Test simple arrow functions.
*
* @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
* @param string $testMarker The comment prefacing the target token.
*
* @dataProvider dataSimple
* @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
*
* @return void
*/
public function testSimple()
public function testSimple($testMarker)
{
foreach (['/* testStandard */', '/* testMixedCase */'] as $comment) {
$token = $this->getTargetToken($comment, T_FN);
$this->backfillHelper($token);
$this->scopePositionTestHelper($token, 5, 12);
}
$token = $this->getTargetToken($testMarker, T_FN);
$this->backfillHelper($token);
$this->scopePositionTestHelper($token, 5, 12);

}//end testSimple()


/**
* Data provider.
*
* @see testSimple()
*
* @return array<string, array<string, string>>
*/
public static function dataSimple()
{
return [
'standard' => [
'testMarker' => '/* testStandard */',
],
'mixed case' => [
'testMarker' => '/* testMixedCase */',
],
];

}//end dataSimple()


/**
* Test whitespace inside arrow function definitions.
*
Expand Down Expand Up @@ -370,44 +392,54 @@ public function testNamespaceOperatorInTypes()


/**
* Test arrow functions that use self/parent/callable/array/static return types.
* Test arrow functions that use keyword return types.
*
* @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
* @param string $testMarker The comment prefacing the target token.
*
* @dataProvider dataKeywordReturnTypes
* @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
*
* @return void
*/
public function testKeywordReturnTypes()
public function testKeywordReturnTypes($testMarker)
{
$tokens = $this->phpcsFile->getTokens();

$testMarkers = [
'Self',
'Parent',
'Callable',
'Array',
'Static',
];

foreach ($testMarkers as $marker) {
$token = $this->getTargetToken('/* test'.$marker.'ReturnType */', T_FN);
$this->backfillHelper($token);

$expectedScopeOpener = ($token + 11);
$expectedScopeCloser = ($token + 14);
$token = $this->getTargetToken($testMarker, T_FN);
$this->backfillHelper($token);
$this->scopePositionTestHelper($token, 11, 14);

$this->assertSame($expectedScopeOpener, $tokens[$token]['scope_opener'], "Scope opener is not the arrow token (for $marker)");
$this->assertSame($expectedScopeCloser, $tokens[$token]['scope_closer'], "Scope closer is not the semicolon token(for $marker)");
}//end testKeywordReturnTypes()

$opener = $tokens[$token]['scope_opener'];
$this->assertSame($expectedScopeOpener, $tokens[$opener]['scope_opener'], "Opener scope opener is not the arrow token(for $marker)");
$this->assertSame($expectedScopeCloser, $tokens[$opener]['scope_closer'], "Opener scope closer is not the semicolon token(for $marker)");

$closer = $tokens[$token]['scope_closer'];
$this->assertSame($expectedScopeOpener, $tokens[$closer]['scope_opener'], "Closer scope opener is not the arrow token(for $marker)");
$this->assertSame($expectedScopeCloser, $tokens[$closer]['scope_closer'], "Closer scope closer is not the semicolon token(for $marker)");
}
/**
* Data provider.
*
* @see testKeywordReturnTypes()
*
* @return array<string, array<string, string>>
*/
public static function dataKeywordReturnTypes()
{
return [
'self' => [
'testMarker' => '/* testSelfReturnType */',
],
'parent' => [
'testMarker' => '/* testParentReturnType */',
],
'callable' => [
'testMarker' => '/* testCallableReturnType */',
],
'array' => [
'testMarker' => '/* testArrayReturnType */',
],
'static' => [
'testMarker' => '/* testStaticReturnType */',
],
];

}//end testKeywordReturnTypes()
}//end dataKeywordReturnTypes()


/**
Expand Down

0 comments on commit 46b883d

Please sign in to comment.