forked from PHPCSStandards/PHP_CodeSniffer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecurseScopeMapSwitchTokenScopeTest.php
More file actions
160 lines (142 loc) · 7.07 KB
/
Copy pathRecurseScopeMapSwitchTokenScopeTest.php
File metadata and controls
160 lines (142 loc) · 7.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
/**
* Tests setting the scope for T_SWITCH token (normal and alternative syntax).
*
* @author Rodrigo Primo <rodrigosprimo@gmail.com>
* @copyright 2024 PHPCSStandards and contributors
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/
namespace PHP_CodeSniffer\Tests\Core\Tokenizers\Tokenizer;
use PHP_CodeSniffer\Tests\Core\Tokenizers\AbstractTokenizerTestCase;
final class RecurseScopeMapSwitchTokenScopeTest extends AbstractTokenizerTestCase
{
/**
* Tests setting the scope for T_SWITCH token (normal and alternative syntax).
*
* @param string $testMarker The comment which prefaces the target token in the test file.
* @param array<string, int|string> $expectedTokens The expected token codes for the scope opener/closer.
* @param string|null $testOpenerMarker Optional. The comment which prefaces the scope opener if different
* from the test marker.
* @param string|null $testCloserMarker Optional. The comment which prefaces the scope closer if different
* from the test marker.
*
* @link https://github.com/squizlabs/PHP_CodeSniffer/issues/497#ref-commit-b24b96b
*
* @dataProvider dataSwitchScope
* @covers \PHP_CodeSniffer\Tokenizers\Tokenizer::recurseScopeMap
*
* @return void
*/
public function testSwitchScope($testMarker, $expectedTokens, $testOpenerMarker=null, $testCloserMarker=null)
{
$tokens = $this->phpcsFile->getTokens();
$switchIndex = $this->getTargetToken($testMarker, [T_SWITCH]);
$tokenArray = $tokens[$switchIndex];
$scopeOpenerMarker = $testMarker;
if (isset($testOpenerMarker) === true) {
$scopeOpenerMarker = $testOpenerMarker;
}
$scopeCloserMarker = $testMarker;
if (isset($testCloserMarker) === true) {
$scopeCloserMarker = $testCloserMarker;
}
$expectedScopeCondition = $switchIndex;
$expectedScopeOpener = $this->getTargetToken($scopeOpenerMarker, $expectedTokens['scope_opener']);
$expectedScopeCloser = $this->getTargetToken($scopeCloserMarker, $expectedTokens['scope_closer']);
$this->assertArrayHasKey('scope_condition', $tokenArray, 'Scope condition not set');
$this->assertSame(
$expectedScopeCondition,
$tokenArray['scope_condition'],
sprintf(
'Scope condition not set correctly; expected T_SWITCH, found %s',
$tokens[$tokenArray['scope_condition']]['type']
)
);
$this->assertArrayHasKey('scope_opener', $tokenArray, 'Scope opener not set');
$this->assertSame(
$expectedScopeOpener,
$tokenArray['scope_opener'],
sprintf(
'Scope opener not set correctly; expected %s, found %s',
$tokens[$expectedScopeOpener]['type'],
$tokens[$tokenArray['scope_opener']]['type']
)
);
$this->assertArrayHasKey('scope_closer', $tokenArray, 'Scope closer not set');
$this->assertSame(
$expectedScopeCloser,
$tokenArray['scope_closer'],
sprintf(
'Scope closer not set correctly; expected %s, found %s',
$tokens[$expectedScopeCloser]['type'],
$tokens[$tokenArray['scope_closer']]['type']
)
);
}//end testSwitchScope()
/**
* Data provider.
*
* @see testSwitchScope()
*
* @return array<string, array<string, string|array<string, int|string>>>
*/
public static function dataSwitchScope()
{
return [
'switch normal syntax' => [
'testMarker' => '/* testSwitchNormalSyntax */',
'expectedTokens' => [
'scope_opener' => T_OPEN_CURLY_BRACKET,
'scope_closer' => T_CLOSE_CURLY_BRACKET,
],
'testOpenerMarker' => null,
'testCloserMarker' => '/* testSwitchNormalSyntaxScopeCloser */',
],
'switch alternative syntax' => [
'testMarker' => '/* testSwitchAlternativeSyntax */',
'expectedTokens' => [
'scope_opener' => T_COLON,
'scope_closer' => T_ENDSWITCH,
],
'testOpenerMarker' => null,
'testCloserMarker' => '/* testSwitchAlternativeSyntaxScopeCloser */',
],
'switch with closure in the condition' => [
'testMarker' => '/* testSwitchClosureWithinCondition */',
'expectedTokens' => [
'scope_opener' => T_OPEN_CURLY_BRACKET,
'scope_closer' => T_CLOSE_CURLY_BRACKET,
],
'testOpenerMarker' => '/* testSwitchClosureWithinConditionScopeOpener */',
'testCloserMarker' => '/* testSwitchClosureWithinConditionScopeCloser */',
],
'switch alternative syntax with closure containing return type in the condition' => [
'testMarker' => '/* testSwitchClosureWithReturnTypeWithinCondition */',
'expectedTokens' => [
'scope_opener' => T_COLON,
'scope_closer' => T_ENDSWITCH,
],
'testOpenerMarker' => '/* testSwitchClosureWithReturnTypeWithinConditionScopeOpener */',
'testCloserMarker' => '/* testSwitchClosureWithReturnTypeWithinConditionScopeCloser */',
],
'switch with arrow function in the condition' => [
'testMarker' => '/* testSwitchArrowFunctionWithinCondition */',
'expectedTokens' => [
'scope_opener' => T_OPEN_CURLY_BRACKET,
'scope_closer' => T_CLOSE_CURLY_BRACKET,
],
'testOpenerMarker' => '/* testSwitchArrowFunctionWithinConditionScopeOpener */',
'testCloserMarker' => '/* testSwitchArrowFunctionWithinConditionScopeCloser */',
],
'switch alternative syntax with arrow function containing return type in the condition' => [
'testMarker' => '/* testSwitchArrowFunctionWithReturnTypeWithinCondition */',
'expectedTokens' => [
'scope_opener' => T_COLON,
'scope_closer' => T_ENDSWITCH,
],
'testOpenerMarker' => '/* testSwitchArrowFunctionWithReturnTypeWithinConditionScopeOpener */',
'testCloserMarker' => '/* testSwitchArrowFunctionWithReturnTypeWithinConditionScopeCloser */',
],
];
}//end dataSwitchScope()
}//end class