Skip to content

Commit 4c92408

Browse files
rodrigoprimojrfnl
authored andcommitted
Tests/Tokenizer: tests to ensure scope is set correctly for T_SWITCH
This commit copies, with some non-structural modifications, a sniff test to the tokenizer tests. The original test was added in b24b96b, part of squizlabs/PHP_CodeSniffer#497. b24b96b introduced a test to InlineControlStructureUnitTest.php as there were no Tokenizer tests back then. It was added to ensure that Tokenizer::recurseScopeMap() would correctly add scope information to the `T_SWITCH` token when handling a `switch` that uses the alternative syntax. This commit also adds a test to ensure the scope is correctly set when using the normal switch syntax. This was not part of b24b96b, but it is relevant anyway, as no other tokenizer test covers this part. Since the InlineControlStructure sniff doesn't listen for the `T_SWITCH` token anymore (see baa4f65), the original test added in b24b96b became useless and thus was refactored to use a different control structure. The sniff test was kept as testing code that uses a control structure, and opening and closing PHP tags is an interesting case not covered in other tests.
1 parent a9b6fa0 commit 4c92408

File tree

4 files changed

+153
-16
lines changed

4 files changed

+153
-16
lines changed

src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.1.inc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,14 @@ if ($a)
7979
</div>
8080

8181
<?php
82-
switch ($this->error):
83-
case Shop_Customer :: ERROR_INVALID_GENDER: ?>
84-
Ung&uuml;ltiges Geschlecht!
85-
<?php break;
86-
case Shop_Customer :: ERROR_EMAIL_IN_USE: ?>
87-
Die eingetragene E-Mail-Adresse ist bereits registriert.
88-
<?php break;
89-
endswitch;
82+
83+
if ($error === ERROR_ONE): ?>
84+
Error one!
85+
<?php elseif ($error === ERROR_TWO): ?>
86+
Error two!
87+
<?php endif;
88+
89+
9090

9191
if ($this->allowShopping !== true):
9292
if ($this->status != Shop_Cart :: OK):

src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.1.inc.fixed

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,14 @@ if ($a) {
9191
</div>
9292

9393
<?php
94-
switch ($this->error):
95-
case Shop_Customer :: ERROR_INVALID_GENDER: ?>
96-
Ung&uuml;ltiges Geschlecht!
97-
<?php break;
98-
case Shop_Customer :: ERROR_EMAIL_IN_USE: ?>
99-
Die eingetragene E-Mail-Adresse ist bereits registriert.
100-
<?php break;
101-
endswitch;
94+
95+
if ($error === ERROR_ONE): ?>
96+
Error one!
97+
<?php elseif ($error === ERROR_TWO): ?>
98+
Error two!
99+
<?php endif;
100+
101+
102102

103103
if ($this->allowShopping !== true):
104104
if ($this->status != Shop_Cart :: OK):
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
/* testSwitchNormalSyntax */
4+
switch ($value) {
5+
case 1:
6+
echo 'one';
7+
break;
8+
default :
9+
echo 'other';
10+
break;
11+
}
12+
13+
// Test for https://github.com/squizlabs/PHP_CodeSniffer/issues/497
14+
/* testSwitchAlternativeSyntax */
15+
switch ($value):
16+
/* testSwitchAlternativeSyntaxEnsureTestWillNotPickUpWrongColon */
17+
case 1:
18+
echo 'one';
19+
break;
20+
default:
21+
echo 'other';
22+
break;
23+
/* testSwitchAlternativeSyntaxEnd */
24+
endswitch;
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
/**
3+
* Tests setting the scope for T_SWITCH token (normal and alternative syntax).
4+
*
5+
* @author Rodrigo Primo <[email protected]>
6+
* @copyright 2024 PHPCSStandards and contributors
7+
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
8+
*/
9+
10+
namespace PHP_CodeSniffer\Tests\Core\Tokenizers\Tokenizer;
11+
12+
use PHP_CodeSniffer\Tests\Core\Tokenizers\AbstractTokenizerTestCase;
13+
14+
final class RecurseScopeMapSwitchTokenScopeTest extends AbstractTokenizerTestCase
15+
{
16+
17+
18+
/**
19+
* Tests setting the scope for T_SWITCH token (normal and alternative syntax).
20+
*
21+
* @param string $testMarker The comment which prefaces the target token in the test file.
22+
* @param array<string, int|string> $expectedTokens The expected token codes for the scope opener/closer.
23+
* @param string|null $testCloserMarker Optional. The comment which prefaces the scope closer if different
24+
* from the test marker.
25+
*
26+
* @link https://github.com/squizlabs/PHP_CodeSniffer/issues/497#ref-commit-b24b96b
27+
*
28+
* @dataProvider dataSwitchScope
29+
* @covers \PHP_CodeSniffer\Tokenizers\Tokenizer::recurseScopeMap
30+
*
31+
* @return void
32+
*/
33+
public function testSwitchScope($testMarker, $expectedTokens, $testCloserMarker=null)
34+
{
35+
$tokens = $this->phpcsFile->getTokens();
36+
$switchIndex = $this->getTargetToken($testMarker, [T_SWITCH]);
37+
$tokenArray = $tokens[$switchIndex];
38+
39+
$scopeCloserMarker = $testMarker;
40+
if (isset($testCloserMarker) === true) {
41+
$scopeCloserMarker = $testCloserMarker;
42+
}
43+
44+
$expectedScopeCondition = $switchIndex;
45+
$expectedScopeOpener = $this->getTargetToken($testMarker, $expectedTokens['scope_opener']);
46+
$expectedScopeCloser = $this->getTargetToken($scopeCloserMarker, $expectedTokens['scope_closer']);
47+
48+
$this->assertArrayHasKey('scope_condition', $tokenArray, 'Scope condition not set');
49+
$this->assertSame(
50+
$expectedScopeCondition,
51+
$tokenArray['scope_condition'],
52+
sprintf(
53+
'Scope condition not set correctly; expected T_SWITCH, found %s',
54+
$tokens[$tokenArray['scope_condition']]['type']
55+
)
56+
);
57+
58+
$this->assertArrayHasKey('scope_opener', $tokenArray, 'Scope opener not set');
59+
$this->assertSame(
60+
$expectedScopeOpener,
61+
$tokenArray['scope_opener'],
62+
sprintf(
63+
'Scope opener not set correctly; expected %s, found %s',
64+
$tokens[$expectedScopeOpener]['type'],
65+
$tokens[$tokenArray['scope_opener']]['type']
66+
)
67+
);
68+
69+
$this->assertArrayHasKey('scope_closer', $tokenArray, 'Scope closer not set');
70+
$this->assertSame(
71+
$expectedScopeCloser,
72+
$tokenArray['scope_closer'],
73+
sprintf(
74+
'Scope closer not set correctly; expected %s, found %s',
75+
$tokens[$expectedScopeCloser]['type'],
76+
$tokens[$tokenArray['scope_closer']]['type']
77+
)
78+
);
79+
80+
}//end testSwitchScope()
81+
82+
83+
/**
84+
* Data provider.
85+
*
86+
* @see testSwitchScope()
87+
*
88+
* @return array<string, array<string, string|array<string, int|string>>>
89+
*/
90+
public static function dataSwitchScope()
91+
{
92+
return [
93+
'switch normal syntax' => [
94+
'testMarker' => '/* testSwitchNormalSyntax */',
95+
'expectedTokens' => [
96+
'scope_opener' => T_OPEN_CURLY_BRACKET,
97+
'scope_closer' => T_CLOSE_CURLY_BRACKET,
98+
],
99+
],
100+
'switch alternative syntax' => [
101+
'testMarker' => '/* testSwitchAlternativeSyntax */',
102+
'expectedTokens' => [
103+
'scope_opener' => T_COLON,
104+
'scope_closer' => T_ENDSWITCH,
105+
],
106+
'testCloserMarker' => '/* testSwitchAlternativeSyntaxEnd */',
107+
],
108+
];
109+
110+
}//end dataSwitchScope()
111+
112+
113+
}//end class

0 commit comments

Comments
 (0)