Skip to content

Commit c9c2c27

Browse files
committed
Tests/Tokenizer: tests to ensure scope is set correctly for T_SWITCH
This commit moves a sniff test to the tokenizer tests. Originally, the moved test was added in b24b96b which is part of squizlabs/PHP_CodeSniffer#497. b24b96b introduce a test to a sniff test (InlineControlStructureUnitTest.php) as back then there were no Tokenizer tests. 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. The commit also adds a test to unsure scope is set correctly when using the normal switch syntax. This was not part of b24b96b, but relevant anyway as there are no previous tokenizer test covering this part. Now that the InlineControlStructure sniff doesn't listen for the T_SWITCH token anymore, the original test added in b24b96b became useless and thus was refactored to use different control structure.
1 parent 87bd8d0 commit c9c2c27

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_INVALID_GENDER): ?>
84+
Invalid gender!
85+
<?php elseif ($error === ERROR_EMAIL_IN_USE): ?>
86+
Invalid email!
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_INVALID_GENDER): ?>
96+
Invalid gender!
97+
<?php elseif ($error === ERROR_EMAIL_IN_USE): ?>
98+
Invalid email!
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+
/* testEnsureTestWillNotPickUpWrongColon */
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 2021 Squiz Pty Ltd (ABN 77 084 670 600)
7+
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
8+
*/
9+
10+
namespace PHP_CodeSniffer\Tests\Core\Tokenizer\Tokenizer;
11+
12+
use PHP_CodeSniffer\Tests\Core\Tokenizer\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 dataSwitch
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 dataSwitch()
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 dataSwitch()
111+
112+
113+
}//end class

0 commit comments

Comments
 (0)