Skip to content

Commit 5ed168c

Browse files
committed
Tests/Tokenizer/EnumCaseTest: 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 613e1c9 commit 5ed168c

File tree

4 files changed

+260
-11
lines changed

4 files changed

+260
-11
lines changed

tests/Core/Tokenizer/EnumCaseTest.php renamed to tests/Core/Tokenizer/PHP/EnumCaseTest.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
88
*/
99

10-
namespace PHP_CodeSniffer\Tests\Core\Tokenizer;
10+
namespace PHP_CodeSniffer\Tests\Core\Tokenizer\PHP;
11+
12+
use PHP_CodeSniffer\Tests\Core\Tokenizer\AbstractTokenizerTestCase;
1113

1214
final class EnumCaseTest extends AbstractTokenizerTestCase
1315
{
@@ -20,7 +22,6 @@ final class EnumCaseTest extends AbstractTokenizerTestCase
2022
*
2123
* @dataProvider dataEnumCases
2224
* @covers PHP_CodeSniffer\Tokenizers\PHP::tokenize
23-
* @covers PHP_CodeSniffer\Tokenizers\Tokenizer::recurseScopeMap
2425
*
2526
* @return void
2627
*/
@@ -33,10 +34,6 @@ public function testEnumCases($testMarker)
3334
$this->assertSame(T_ENUM_CASE, $tokenArray['code'], 'Token tokenized as '.$tokenArray['type'].', not T_ENUM_CASE (code)');
3435
$this->assertSame('T_ENUM_CASE', $tokenArray['type'], 'Token tokenized as '.$tokenArray['type'].', not T_ENUM_CASE (type)');
3536

36-
$this->assertArrayNotHasKey('scope_condition', $tokenArray, 'Scope condition is set');
37-
$this->assertArrayNotHasKey('scope_opener', $tokenArray, 'Scope opener is set');
38-
$this->assertArrayNotHasKey('scope_closer', $tokenArray, 'Scope closer is set');
39-
4037
}//end testEnumCases()
4138

4239

@@ -69,7 +66,6 @@ public static function dataEnumCases()
6966
*
7067
* @dataProvider dataNotEnumCases
7168
* @covers PHP_CodeSniffer\Tokenizers\PHP::tokenize
72-
* @covers PHP_CodeSniffer\Tokenizers\Tokenizer::recurseScopeMap
7369
*
7470
* @return void
7571
*/
@@ -82,10 +78,6 @@ public function testNotEnumCases($testMarker)
8278
$this->assertSame(T_CASE, $tokenArray['code'], 'Token tokenized as '.$tokenArray['type'].', not T_CASE (code)');
8379
$this->assertSame('T_CASE', $tokenArray['type'], 'Token tokenized as '.$tokenArray['type'].', not T_CASE (type)');
8480

85-
$this->assertArrayHasKey('scope_condition', $tokenArray, 'Scope condition is not set');
86-
$this->assertArrayHasKey('scope_opener', $tokenArray, 'Scope opener is not set');
87-
$this->assertArrayHasKey('scope_closer', $tokenArray, 'Scope closer is not set');
88-
8981
}//end testNotEnumCases()
9082

9183

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
enum Foo
4+
{
5+
/* testPureEnumCase */
6+
case SOME_CASE;
7+
}
8+
9+
enum Boo: int {
10+
/* testBackingIntegerEnumCase */
11+
case ONE = 1;
12+
}
13+
14+
enum Hoo: string
15+
{
16+
/* testBackingStringEnumCase */
17+
case ONE = 'one';
18+
}
19+
20+
enum ComplexEnum: int implements SomeInterface
21+
{
22+
use SomeTrait {
23+
traitMethod as enumMethod;
24+
}
25+
26+
const SOME_CONSTANT = true;
27+
28+
/* testEnumCaseInComplexEnum */
29+
case ONE = 1;
30+
31+
/* testEnumCaseIsCaseInsensitive */
32+
CaSe TWO = 2;
33+
34+
public function someMethod(): bool
35+
{
36+
switch (true) {
37+
/* testCaseWithSemicolonIsNotEnumCase */
38+
case CONSTANT;
39+
}
40+
}
41+
42+
/* testEnumCaseAfterSwitch */
43+
case THREE = 3;
44+
45+
public function someOtherMethod(): bool
46+
{
47+
switch (true):
48+
case false:
49+
endswitch;
50+
}
51+
52+
/* testEnumCaseAfterSwitchWithEndSwitch */
53+
case FOUR = 4;
54+
}
55+
56+
switch (true) {
57+
/* testCaseWithConstantIsNotEnumCase */
58+
case CONSTANT:
59+
/* testCaseWithConstantAndIdenticalIsNotEnumCase */
60+
case CONSTANT === 1:
61+
/* testCaseWithAssigmentToConstantIsNotEnumCase */
62+
case CONSTANT = 1:
63+
/* testIsNotEnumCaseIsCaseInsensitive */
64+
cAsE CONSTANT:
65+
}
66+
67+
switch ($x) {
68+
/* testCaseInSwitchWhenCreatingEnumInSwitch1 */
69+
case 'a': {
70+
enum Foo {}
71+
break;
72+
}
73+
74+
/* testCaseInSwitchWhenCreatingEnumInSwitch2 */
75+
case 'b';
76+
enum Bar {}
77+
break;
78+
}
79+
80+
enum Foo: string {
81+
/* testKeywordAsEnumCaseNameShouldBeString1 */
82+
case INTERFACE = 'interface';
83+
/* testKeywordAsEnumCaseNameShouldBeString2 */
84+
case TRAIT = 'trait';
85+
/* testKeywordAsEnumCaseNameShouldBeString3 */
86+
case ENUM = 'enum';
87+
/* testKeywordAsEnumCaseNameShouldBeString4 */
88+
case FUNCTION = 'function';
89+
/* testKeywordAsEnumCaseNameShouldBeString5 */
90+
case FALSE = 'false';
91+
/* testKeywordAsEnumCaseNameShouldBeString6 */
92+
case DEFAULT = 'default';
93+
/* testKeywordAsEnumCaseNameShouldBeString7 */
94+
case ARRAY = 'array';
95+
}
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
<?php
2+
/**
3+
* Tests converting enum "case" to T_ENUM_CASE.
4+
*
5+
* @author Jaroslav Hanslík <[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 RecurseScopeMapCaseKeywordConditionsTest extends AbstractTokenizerTestCase
15+
{
16+
17+
18+
/**
19+
* Test that the enum "case" is converted to T_ENUM_CASE.
20+
*
21+
* @param string $testMarker The comment which prefaces the target token in the test file.
22+
*
23+
* @dataProvider dataEnumCases
24+
* @covers PHP_CodeSniffer\Tokenizers\Tokenizer::recurseScopeMap
25+
*
26+
* @return void
27+
*/
28+
public function testEnumCases($testMarker)
29+
{
30+
$tokens = $this->phpcsFile->getTokens();
31+
$enumCase = $this->getTargetToken($testMarker, [T_ENUM_CASE, T_CASE]);
32+
$tokenArray = $tokens[$enumCase];
33+
34+
// Make sure we're looking at the right token.
35+
$this->assertSame(T_ENUM_CASE, $tokenArray['code'], 'Token tokenized as '.$tokenArray['type'].', not T_ENUM_CASE (code)');
36+
37+
$this->assertArrayNotHasKey('scope_condition', $tokenArray, 'Scope condition is set');
38+
$this->assertArrayNotHasKey('scope_opener', $tokenArray, 'Scope opener is set');
39+
$this->assertArrayNotHasKey('scope_closer', $tokenArray, 'Scope closer is set');
40+
41+
}//end testEnumCases()
42+
43+
44+
/**
45+
* Data provider.
46+
*
47+
* @see testEnumCases()
48+
*
49+
* @return array<string, array<string>>
50+
*/
51+
public static function dataEnumCases()
52+
{
53+
return [
54+
'enum case, no value' => ['/* testPureEnumCase */'],
55+
'enum case, integer value' => ['/* testBackingIntegerEnumCase */'],
56+
'enum case, string value' => ['/* testBackingStringEnumCase */'],
57+
'enum case, integer value in more complex enum' => ['/* testEnumCaseInComplexEnum */'],
58+
'enum case, keyword in mixed case' => ['/* testEnumCaseIsCaseInsensitive */'],
59+
'enum case, after switch statement' => ['/* testEnumCaseAfterSwitch */'],
60+
'enum case, after switch statement using alternative syntax' => ['/* testEnumCaseAfterSwitchWithEndSwitch */'],
61+
];
62+
63+
}//end dataEnumCases()
64+
65+
66+
/**
67+
* Test that "case" that is not enum case is still tokenized as `T_CASE`.
68+
*
69+
* @param string $testMarker The comment which prefaces the target token in the test file.
70+
*
71+
* @dataProvider dataNotEnumCases
72+
* @covers PHP_CodeSniffer\Tokenizers\Tokenizer::recurseScopeMap
73+
*
74+
* @return void
75+
*/
76+
public function testNotEnumCases($testMarker)
77+
{
78+
$tokens = $this->phpcsFile->getTokens();
79+
$case = $this->getTargetToken($testMarker, [T_ENUM_CASE, T_CASE]);
80+
$tokenArray = $tokens[$case];
81+
82+
// Make sure we're looking at the right token.
83+
$this->assertSame(T_CASE, $tokenArray['code'], 'Token tokenized as '.$tokenArray['type'].', not T_CASE (code)');
84+
85+
$this->assertArrayHasKey('scope_condition', $tokenArray, 'Scope condition is not set');
86+
$this->assertArrayHasKey('scope_opener', $tokenArray, 'Scope opener is not set');
87+
$this->assertArrayHasKey('scope_closer', $tokenArray, 'Scope closer is not set');
88+
89+
}//end testNotEnumCases()
90+
91+
92+
/**
93+
* Data provider.
94+
*
95+
* @see testNotEnumCases()
96+
*
97+
* @return array<string, array<string>>
98+
*/
99+
public static function dataNotEnumCases()
100+
{
101+
return [
102+
'switch case with constant, semicolon condition end' => ['/* testCaseWithSemicolonIsNotEnumCase */'],
103+
'switch case with constant, colon condition end' => ['/* testCaseWithConstantIsNotEnumCase */'],
104+
'switch case with constant, comparison' => ['/* testCaseWithConstantAndIdenticalIsNotEnumCase */'],
105+
'switch case with constant, assignment' => ['/* testCaseWithAssigmentToConstantIsNotEnumCase */'],
106+
'switch case with constant, keyword in mixed case' => ['/* testIsNotEnumCaseIsCaseInsensitive */'],
107+
'switch case, body in curlies declares enum' => ['/* testCaseInSwitchWhenCreatingEnumInSwitch1 */'],
108+
'switch case, body after semicolon declares enum' => ['/* testCaseInSwitchWhenCreatingEnumInSwitch2 */'],
109+
];
110+
111+
}//end dataNotEnumCases()
112+
113+
114+
/**
115+
* Test that "case" that is not enum case is still tokenized as `T_CASE`.
116+
*
117+
* @param string $testMarker The comment which prefaces the target token in the test file.
118+
*
119+
* @dataProvider dataKeywordAsEnumCaseNameShouldBeString
120+
* @covers PHP_CodeSniffer\Tokenizers\Tokenizer::recurseScopeMap
121+
*
122+
* @return void
123+
*/
124+
public function testKeywordAsEnumCaseNameShouldBeString($testMarker)
125+
{
126+
$tokens = $this->phpcsFile->getTokens();
127+
$enumCaseName = $this->getTargetToken($testMarker, [T_STRING, T_INTERFACE, T_TRAIT, T_ENUM, T_FUNCTION, T_FALSE, T_DEFAULT, T_ARRAY]);
128+
$tokenArray = $tokens[$enumCaseName];
129+
130+
// Make sure we're looking at the right token.
131+
$this->assertSame(T_STRING, $tokenArray['code'], 'Token tokenized as '.$tokenArray['type'].', not T_STRING (code)');
132+
133+
$this->assertArrayNotHasKey('scope_condition', $tokenArray, 'Scope condition is set');
134+
$this->assertArrayNotHasKey('scope_opener', $tokenArray, 'Scope opener is set');
135+
$this->assertArrayNotHasKey('scope_closer', $tokenArray, 'Scope closer is set');
136+
137+
}//end testKeywordAsEnumCaseNameShouldBeString()
138+
139+
140+
/**
141+
* Data provider.
142+
*
143+
* @see testKeywordAsEnumCaseNameShouldBeString()
144+
*
145+
* @return array<string, array<string>>
146+
*/
147+
public static function dataKeywordAsEnumCaseNameShouldBeString()
148+
{
149+
return [
150+
'"interface" as case name' => ['/* testKeywordAsEnumCaseNameShouldBeString1 */'],
151+
'"trait" as case name' => ['/* testKeywordAsEnumCaseNameShouldBeString2 */'],
152+
'"enum" as case name' => ['/* testKeywordAsEnumCaseNameShouldBeString3 */'],
153+
'"function" as case name' => ['/* testKeywordAsEnumCaseNameShouldBeString4 */'],
154+
'"false" as case name' => ['/* testKeywordAsEnumCaseNameShouldBeString5 */'],
155+
'"default" as case name' => ['/* testKeywordAsEnumCaseNameShouldBeString6 */'],
156+
'"array" as case name' => ['/* testKeywordAsEnumCaseNameShouldBeString7 */'],
157+
];
158+
159+
}//end dataKeywordAsEnumCaseNameShouldBeString()
160+
161+
162+
}//end class

0 commit comments

Comments
 (0)