Skip to content

Commit 332c959

Browse files
committed
Tests/Tokenizer: tests for T_CASE and T_IF scope condition
This commit copies, with some non-structural modifications, a sniff test to the tokenizer tests. Two new tokenizer tests are created as the result of this copy. One to ensure that the scope of `T_CASE` is correctly set when the case shares the scope closer with `T_SWITCH` (no `T_BREAK`). And another to ensure that the scope of `T_IF` is correctly set when there is a nested `T_SWITCH` and `T_CASE` without a `T_BREAK`. The original sniff test was added in fddc61a, which is part of squizlabs/PHP_CodeSniffer#497. fddc61a introduced a test to InlineControlStructureUnitTest.php as there were no Tokenizer tests back then. The test was added to ensure that Tokenizer::recurseScopeMap() correctly adds scope information to the `T_CASE` token when it shares the closer with `T_SWITCH` and to `T_IF` when a nested `T_CASE` shares the scope closer with T_SWITCH`. Since the InlineControlStructure sniff doesn't listen for the `T_SWITCH` token anymore (see baa4f65), the original test added in fddc61a became useless and thus was refactored to use a different control structure. This new version of the sniff test was kept as testing code that uses nested alternative syntax control structures is still valid and is not covered in other tests.
1 parent 3a19186 commit 332c959

File tree

6 files changed

+109
-12
lines changed

6 files changed

+109
-12
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,17 @@ if ($error === ERROR_INVALID_GENDER): ?>
8888

8989

9090

91-
if ($this->allowShopping !== true):
92-
if ($this->status != Shop_Cart :: OK):
93-
switch ($this->status):
94-
case Shop_Cart :: NOT_FOUND:
95-
echo 'foo';
96-
endswitch;
91+
if ($value):
92+
if ($anotherValue):
93+
foreach ($array as $element):
94+
echo $element;
95+
endforeach;
9796
endif;
9897
else:
9998
echo 'foo';
10099
endif;
101100

101+
102102
// ELSE IF split over multiple lines (not inline)
103103
if ($test) {
104104
} else

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,17 +100,17 @@ if ($error === ERROR_INVALID_GENDER): ?>
100100

101101

102102

103-
if ($this->allowShopping !== true):
104-
if ($this->status != Shop_Cart :: OK):
105-
switch ($this->status):
106-
case Shop_Cart :: NOT_FOUND:
107-
echo 'foo';
108-
endswitch;
103+
if ($value):
104+
if ($anotherValue):
105+
foreach ($array as $element):
106+
echo $element;
107+
endforeach;
109108
endif;
110109
else:
111110
echo 'foo';
112111
endif;
113112

113+
114114
// ELSE IF split over multiple lines (not inline)
115115
if ($test) {
116116
} else

tests/Core/Tokenizers/Tokenizer/RecurseScopeMapCaseKeywordConditionsTest.inc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,10 @@ enum Foo: string {
9393
/* testKeywordAsEnumCaseNameShouldBeString7 */
9494
case ARRAY = 'array';
9595
}
96+
97+
// Test for https://github.com/squizlabs/PHP_CodeSniffer/issues/497
98+
switch ($value):
99+
/* testSwitchCaseScopeCloserSharedWithSwitch */
100+
case 1:
101+
echo 'one';
102+
endswitch;

tests/Core/Tokenizers/Tokenizer/RecurseScopeMapCaseKeywordConditionsTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ public static function dataNotEnumCases()
106106
'switch case with constant, keyword in mixed case' => ['/* testIsNotEnumCaseIsCaseInsensitive */'],
107107
'switch case, body in curlies declares enum' => ['/* testCaseInSwitchWhenCreatingEnumInSwitch1 */'],
108108
'switch case, body after semicolon declares enum' => ['/* testCaseInSwitchWhenCreatingEnumInSwitch2 */'],
109+
'switch case, shared closer with switch' => ['/* testSwitchCaseScopeCloserSharedWithSwitch */'],
109110
];
110111

111112
}//end dataNotEnumCases()
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
// Test for https://github.com/squizlabs/PHP_CodeSniffer/issues/497
4+
/* testIfElseWithNestedCaseMissingBreak */
5+
if ($foo === true):
6+
switch ($bar):
7+
case 1:
8+
echo 'one';
9+
endswitch;
10+
/* testIfElseWithNestedCaseMissingBreakCloser */
11+
else:
12+
echo 'other';
13+
endif;
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
/**
3+
* Tests setting the scope for T_IF token.
4+
*
5+
* @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
6+
* @author Rodrigo Primo <rodrigosprimo@gmail.com>
7+
* @copyright 2021 Squiz Pty Ltd (ABN 77 084 670 600)
8+
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
9+
*/
10+
11+
namespace PHP_CodeSniffer\Tests\Core\Tokenizers\Tokenizer;
12+
13+
use PHP_CodeSniffer\Tests\Core\Tokenizers\AbstractTokenizerTestCase;
14+
15+
final class RecurseScopeMapIfKeywordConditionsTest extends AbstractTokenizerTestCase
16+
{
17+
18+
19+
/**
20+
* Tests setting the scope for T_IF token with nested case statement missing break statement.
21+
*
22+
* @link https://github.com/squizlabs/PHP_CodeSniffer/issues/497#ref-commit-fddc61a
23+
*
24+
* @covers \PHP_CodeSniffer\Tokenizers\Tokenizer::recurseScopeMap
25+
*
26+
* @return void
27+
*/
28+
public function testIfElseWithNestedCaseMissingBreakSharedClosers()
29+
{
30+
31+
$tokens = $this->phpcsFile->getTokens();
32+
$ifTestMarker = '/* testIfElseWithNestedCaseMissingBreak */';
33+
$ifCloserTestMarker = '/* testIfElseWithNestedCaseMissingBreakCloser */';
34+
$ifTokenIndex = $this->getTargetToken($ifTestMarker, T_IF);
35+
$tokenArray = $tokens[$ifTokenIndex];
36+
37+
$expectedScopeCondition = $ifTokenIndex;
38+
$expectedScopeOpener = $this->getTargetToken($ifTestMarker, T_COLON);
39+
$expectedScopeCloser = $this->getTargetToken($ifCloserTestMarker, T_ELSE);
40+
41+
$this->assertArrayHasKey('scope_condition', $tokenArray, 'Scope condition not set');
42+
$this->assertSame(
43+
$expectedScopeCondition,
44+
$tokenArray['scope_condition'],
45+
sprintf(
46+
'Scope condition not set correctly; expected T_IF, found %s',
47+
$tokens[$tokenArray['scope_condition']]['type']
48+
)
49+
);
50+
51+
$this->assertArrayHasKey('scope_opener', $tokenArray, 'Scope opener not set');
52+
$this->assertSame(
53+
$expectedScopeOpener,
54+
$tokenArray['scope_opener'],
55+
sprintf(
56+
'Scope opener not set correctly; expected %s, found %s',
57+
$tokens[$expectedScopeOpener]['type'],
58+
$tokens[$tokenArray['scope_opener']]['type']
59+
)
60+
);
61+
62+
$this->assertArrayHasKey('scope_closer', $tokenArray, 'Scope closer not set');
63+
$this->assertSame(
64+
$expectedScopeCloser,
65+
$tokenArray['scope_closer'],
66+
sprintf(
67+
'Scope closer not set correctly; expected %s, found %s',
68+
$tokens[$expectedScopeCloser]['type'],
69+
$tokens[$tokenArray['scope_closer']]['type']
70+
)
71+
);
72+
73+
}//end testIfElseWithNestedCaseMissingBreakSharedClosers()
74+
75+
76+
}//end class

0 commit comments

Comments
 (0)