Skip to content

Commit 841f909

Browse files
committed
Tests/Tokenizer/DefaultKeywordTest: split the test class
... into two test classes, one targetting the `Tokenizer\PHP` class, one targetting the `Tokenizer\Tokenizer` class. While this does mean there is now some duplication between these test classes, I don't think that's problematic. It also allows for these tests to diverge based on the specific test needs for each of the classes under test.
1 parent 5b7f4af commit 841f909

File tree

4 files changed

+438
-8
lines changed

4 files changed

+438
-8
lines changed
Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
<?php
2+
/**
3+
* Tests the retokenization of the `default` keyword to T_MATCH_DEFAULT for PHP 8.0 match structures
4+
* and makes sure that the tokenization of switch `T_DEFAULT` structures is not aversely affected.
5+
*
6+
* @author Juliette Reinders Folmer <[email protected]>
7+
* @copyright 2020-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\Tokenizer\PHP;
12+
13+
use PHP_CodeSniffer\Tests\Core\Tokenizer\AbstractTokenizerTestCase;
14+
15+
final class DefaultKeywordTest extends AbstractTokenizerTestCase
16+
{
17+
18+
19+
/**
20+
* Test the retokenization of the `default` keyword for match structure to `T_MATCH_DEFAULT`.
21+
*
22+
* Note: Cases and default structures within a match structure do *NOT* get case/default scope
23+
* conditions, in contrast to case and default structures in switch control structures.
24+
*
25+
* @param string $testMarker The comment prefacing the target token.
26+
* @param string $testContent The token content to look for.
27+
*
28+
* @dataProvider dataMatchDefault
29+
* @covers PHP_CodeSniffer\Tokenizers\PHP::tokenize
30+
*
31+
* @return void
32+
*/
33+
public function testMatchDefault($testMarker, $testContent='default')
34+
{
35+
$tokens = $this->phpcsFile->getTokens();
36+
37+
$token = $this->getTargetToken($testMarker, [T_MATCH_DEFAULT, T_DEFAULT, T_STRING], $testContent);
38+
$tokenArray = $tokens[$token];
39+
40+
$this->assertSame(T_MATCH_DEFAULT, $tokenArray['code'], 'Token tokenized as '.$tokenArray['type'].', not T_MATCH_DEFAULT (code)');
41+
$this->assertSame('T_MATCH_DEFAULT', $tokenArray['type'], 'Token tokenized as '.$tokenArray['type'].', not T_MATCH_DEFAULT (type)');
42+
43+
}//end testMatchDefault()
44+
45+
46+
/**
47+
* Data provider.
48+
*
49+
* @see testMatchDefault()
50+
*
51+
* @return array<string, array<string, string>>
52+
*/
53+
public static function dataMatchDefault()
54+
{
55+
return [
56+
'simple_match_default' => [
57+
'testMarker' => '/* testSimpleMatchDefault */',
58+
],
59+
'match_default_in_switch_case_1' => [
60+
'testMarker' => '/* testMatchDefaultNestedInSwitchCase1 */',
61+
],
62+
'match_default_in_switch_case_2' => [
63+
'testMarker' => '/* testMatchDefaultNestedInSwitchCase2 */',
64+
],
65+
'match_default_in_switch_default' => [
66+
'testMarker' => '/* testMatchDefaultNestedInSwitchDefault */',
67+
],
68+
'match_default_containing_switch' => [
69+
'testMarker' => '/* testMatchDefault */',
70+
],
71+
72+
'match_default_with_nested_long_array_and_default_key' => [
73+
'testMarker' => '/* testMatchDefaultWithNestedLongArrayWithClassConstantKey */',
74+
'testContent' => 'DEFAULT',
75+
],
76+
'match_default_with_nested_long_array_and_default_key_2' => [
77+
'testMarker' => '/* testMatchDefaultWithNestedLongArrayWithClassConstantKeyLevelDown */',
78+
'testContent' => 'DEFAULT',
79+
],
80+
'match_default_with_nested_short_array_and_default_key' => [
81+
'testMarker' => '/* testMatchDefaultWithNestedShortArrayWithClassConstantKey */',
82+
'testContent' => 'DEFAULT',
83+
],
84+
'match_default_with_nested_short_array_and_default_key_2' => [
85+
'testMarker' => '/* testMatchDefaultWithNestedShortArrayWithClassConstantKeyLevelDown */',
86+
'testContent' => 'DEFAULT',
87+
],
88+
'match_default_in_long_array' => [
89+
'testMarker' => '/* testMatchDefaultNestedInLongArray */',
90+
'testContent' => 'DEFAULT',
91+
],
92+
'match_default_in_short_array' => [
93+
'testMarker' => '/* testMatchDefaultNestedInShortArray */',
94+
'testContent' => 'DEFAULT',
95+
],
96+
];
97+
98+
}//end dataMatchDefault()
99+
100+
101+
/**
102+
* Verify that the retokenization of `T_DEFAULT` tokens in match constructs, doesn't negatively
103+
* impact the tokenization of `T_DEFAULT` tokens in switch control structures.
104+
*
105+
* @param string $testMarker The comment prefacing the target token.
106+
* @param string $testContent The token content to look for.
107+
*
108+
* @dataProvider dataSwitchDefault
109+
* @covers PHP_CodeSniffer\Tokenizers\PHP::tokenize
110+
*
111+
* @return void
112+
*/
113+
public function testSwitchDefault($testMarker, $testContent='default')
114+
{
115+
$tokens = $this->phpcsFile->getTokens();
116+
117+
$token = $this->getTargetToken($testMarker, [T_MATCH_DEFAULT, T_DEFAULT, T_STRING], $testContent);
118+
$tokenArray = $tokens[$token];
119+
120+
$this->assertSame(T_DEFAULT, $tokenArray['code'], 'Token tokenized as '.$tokenArray['type'].', not T_DEFAULT (code)');
121+
$this->assertSame('T_DEFAULT', $tokenArray['type'], 'Token tokenized as '.$tokenArray['type'].', not T_DEFAULT (type)');
122+
123+
}//end testSwitchDefault()
124+
125+
126+
/**
127+
* Data provider.
128+
*
129+
* @see testSwitchDefault()
130+
*
131+
* @return array<string, array<string, string|int>>
132+
*/
133+
public static function dataSwitchDefault()
134+
{
135+
return [
136+
'simple_switch_default' => [
137+
'testMarker' => '/* testSimpleSwitchDefault */',
138+
],
139+
'simple_switch_default_with_curlies' => [
140+
'testMarker' => '/* testSimpleSwitchDefaultWithCurlies */',
141+
],
142+
'switch_default_toplevel' => [
143+
'testMarker' => '/* testSwitchDefault */',
144+
],
145+
'switch_default_nested_in_match_case' => [
146+
'testMarker' => '/* testSwitchDefaultNestedInMatchCase */',
147+
],
148+
'switch_default_nested_in_match_default' => [
149+
'testMarker' => '/* testSwitchDefaultNestedInMatchDefault */',
150+
],
151+
];
152+
153+
}//end dataSwitchDefault()
154+
155+
156+
/**
157+
* Verify that the retokenization of `T_DEFAULT` tokens in match constructs, doesn't negatively
158+
* impact the tokenization of `T_STRING` tokens with the contents 'default' which aren't in
159+
* actual fact the default keyword.
160+
*
161+
* @param string $testMarker The comment prefacing the target token.
162+
* @param string $testContent The token content to look for.
163+
*
164+
* @dataProvider dataNotDefaultKeyword
165+
* @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
166+
*
167+
* @return void
168+
*/
169+
public function testNotDefaultKeyword($testMarker, $testContent='DEFAULT')
170+
{
171+
$tokens = $this->phpcsFile->getTokens();
172+
173+
$token = $this->getTargetToken($testMarker, [T_MATCH_DEFAULT, T_DEFAULT, T_STRING], $testContent);
174+
$tokenArray = $tokens[$token];
175+
176+
$this->assertSame(T_STRING, $tokenArray['code'], 'Token tokenized as '.$tokenArray['type'].', not T_STRING (code)');
177+
$this->assertSame('T_STRING', $tokenArray['type'], 'Token tokenized as '.$tokenArray['type'].', not T_STRING (type)');
178+
179+
}//end testNotDefaultKeyword()
180+
181+
182+
/**
183+
* Data provider.
184+
*
185+
* @see testNotDefaultKeyword()
186+
*
187+
* @return array<string, array<string, string>>
188+
*/
189+
public static function dataNotDefaultKeyword()
190+
{
191+
return [
192+
'class-constant-as-short-array-key' => [
193+
'testMarker' => '/* testClassConstantAsShortArrayKey */',
194+
],
195+
'class-property-as-short-array-key' => [
196+
'testMarker' => '/* testClassPropertyAsShortArrayKey */',
197+
],
198+
'class-constant-as-long-array-key' => [
199+
'testMarker' => '/* testClassConstantAsLongArrayKey */',
200+
],
201+
'class-constant-as-yield-key' => [
202+
'testMarker' => '/* testClassConstantAsYieldKey */',
203+
],
204+
205+
'class-constant-as-long-array-key-nested-in-match' => [
206+
'testMarker' => '/* testClassConstantAsLongArrayKeyNestedInMatch */',
207+
],
208+
'class-constant-as-long-array-key-nested-in-match-2' => [
209+
'testMarker' => '/* testClassConstantAsLongArrayKeyNestedInMatchLevelDown */',
210+
],
211+
'class-constant-as-short-array-key-nested-in-match' => [
212+
'testMarker' => '/* testClassConstantAsShortArrayKeyNestedInMatch */',
213+
],
214+
'class-constant-as-short-array-key-nested-in-match-2' => [
215+
'testMarker' => '/* testClassConstantAsShortArrayKeyNestedInMatchLevelDown */',
216+
],
217+
'class-constant-as-long-array-key-with-nested-match' => [
218+
'testMarker' => '/* testClassConstantAsLongArrayKeyWithNestedMatch */',
219+
],
220+
'class-constant-as-short-array-key-with-nested-match' => [
221+
'testMarker' => '/* testClassConstantAsShortArrayKeyWithNestedMatch */',
222+
],
223+
224+
'class-constant-in-switch-case' => [
225+
'testMarker' => '/* testClassConstantInSwitchCase */',
226+
],
227+
'class-property-in-switch-case' => [
228+
'testMarker' => '/* testClassPropertyInSwitchCase */',
229+
],
230+
231+
'class-constant-declaration' => [
232+
'testMarker' => '/* testClassConstant */',
233+
],
234+
'class-method-declaration' => [
235+
'testMarker' => '/* testMethodDeclaration */',
236+
'testContent' => 'default',
237+
],
238+
];
239+
240+
}//end dataNotDefaultKeyword()
241+
242+
243+
}//end class

0 commit comments

Comments
 (0)