Skip to content

Commit 5b7f4af

Browse files
committed
Tests/Tokenizer/ArrayKeywordTest: 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 these classes.
1 parent b659067 commit 5b7f4af

File tree

4 files changed

+273
-16
lines changed

4 files changed

+273
-16
lines changed

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

Lines changed: 3 additions & 16 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 ArrayKeywordTest extends AbstractTokenizerTestCase
1315
{
@@ -21,7 +23,6 @@ final class ArrayKeywordTest extends AbstractTokenizerTestCase
2123
*
2224
* @dataProvider dataArrayKeyword
2325
* @covers PHP_CodeSniffer\Tokenizers\PHP::tokenize
24-
* @covers PHP_CodeSniffer\Tokenizers\Tokenizer::createTokenMap
2526
*
2627
* @return void
2728
*/
@@ -35,10 +36,6 @@ public function testArrayKeyword($testMarker, $testContent='array')
3536
$this->assertSame(T_ARRAY, $tokenArray['code'], 'Token tokenized as '.$tokenArray['type'].', not T_ARRAY (code)');
3637
$this->assertSame('T_ARRAY', $tokenArray['type'], 'Token tokenized as '.$tokenArray['type'].', not T_ARRAY (type)');
3738

38-
$this->assertArrayHasKey('parenthesis_owner', $tokenArray, 'Parenthesis owner is not set');
39-
$this->assertArrayHasKey('parenthesis_opener', $tokenArray, 'Parenthesis opener is not set');
40-
$this->assertArrayHasKey('parenthesis_closer', $tokenArray, 'Parenthesis closer is not set');
41-
4239
}//end testArrayKeyword()
4340

4441

@@ -84,7 +81,6 @@ public static function dataArrayKeyword()
8481
*
8582
* @dataProvider dataArrayType
8683
* @covers PHP_CodeSniffer\Tokenizers\PHP::tokenize
87-
* @covers PHP_CodeSniffer\Tokenizers\Tokenizer::createTokenMap
8884
*
8985
* @return void
9086
*/
@@ -98,10 +94,6 @@ public function testArrayType($testMarker, $testContent='array')
9894
$this->assertSame(T_STRING, $tokenArray['code'], 'Token tokenized as '.$tokenArray['type'].', not T_STRING (code)');
9995
$this->assertSame('T_STRING', $tokenArray['type'], 'Token tokenized as '.$tokenArray['type'].', not T_STRING (type)');
10096

101-
$this->assertArrayNotHasKey('parenthesis_owner', $tokenArray, 'Parenthesis owner is set');
102-
$this->assertArrayNotHasKey('parenthesis_opener', $tokenArray, 'Parenthesis opener is set');
103-
$this->assertArrayNotHasKey('parenthesis_closer', $tokenArray, 'Parenthesis closer is set');
104-
10597
}//end testArrayType()
10698

10799

@@ -163,7 +155,6 @@ public static function dataArrayType()
163155
*
164156
* @dataProvider dataNotArrayKeyword
165157
* @covers PHP_CodeSniffer\Tokenizers\PHP::tokenize
166-
* @covers PHP_CodeSniffer\Tokenizers\Tokenizer::createTokenMap
167158
*
168159
* @return void
169160
*/
@@ -177,10 +168,6 @@ public function testNotArrayKeyword($testMarker, $testContent='array')
177168
$this->assertSame(T_STRING, $tokenArray['code'], 'Token tokenized as '.$tokenArray['type'].', not T_STRING (code)');
178169
$this->assertSame('T_STRING', $tokenArray['type'], 'Token tokenized as '.$tokenArray['type'].', not T_STRING (type)');
179170

180-
$this->assertArrayNotHasKey('parenthesis_owner', $tokenArray, 'Parenthesis owner is set');
181-
$this->assertArrayNotHasKey('parenthesis_opener', $tokenArray, 'Parenthesis opener is set');
182-
$this->assertArrayNotHasKey('parenthesis_closer', $tokenArray, 'Parenthesis closer is set');
183-
184171
}//end testNotArrayKeyword()
185172

186173

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
/* testEmptyArray */
4+
$var = array();
5+
6+
/* testArrayWithSpace */
7+
$var = array (1 => 10);
8+
9+
/* testArrayWithComment */
10+
$var = Array /*comment*/ (1 => 10);
11+
12+
/* testNestingArray */
13+
$var = array(
14+
/* testNestedArray */
15+
array(
16+
'key' => 'value',
17+
18+
/* testClosureReturnType */
19+
'closure' => function($a) use($global) : Array {},
20+
),
21+
);
22+
23+
/* testFunctionDeclarationParamType */
24+
function typedParam(array $a) {}
25+
26+
/* testFunctionDeclarationReturnType */
27+
function returnType($a) : int|array|null {}
28+
29+
class Bar {
30+
/* testClassConst */
31+
const ARRAY = [];
32+
33+
/* testClassMethod */
34+
public function array() {}
35+
36+
/* testOOConstType */
37+
const array /* testTypedOOConstName */ ARRAY = /* testOOConstDefault */ array();
38+
39+
/* testOOPropertyType */
40+
protected array $property;
41+
}
42+
43+
class DNFTypes {
44+
/* testOOConstDNFType */
45+
const (A&B)|array|(C&D) NAME = [];
46+
47+
/* testOOPropertyDNFType */
48+
protected (A&B)|ARRAY|null $property;
49+
50+
/* testFunctionDeclarationParamDNFType */
51+
public function name(null|array|(A&B) $param) {
52+
/* testClosureDeclarationParamDNFType */
53+
$cl = function ( array|(A&B) $param) {};
54+
55+
/* testArrowDeclarationReturnDNFType */
56+
$arrow = fn($a): (A&B)|Array => new $a;
57+
}
58+
}
Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
<?php
2+
/**
3+
* Tests that the array keyword is tokenized correctly.
4+
*
5+
* @author Juliette Reinders Folmer <[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 CreateTokenMapArrayParenthesesTest extends AbstractTokenizerTestCase
15+
{
16+
17+
18+
/**
19+
* Test that the array keyword is correctly tokenized as `T_ARRAY`.
20+
*
21+
* @param string $testMarker The comment prefacing the target token.
22+
* @param string $testContent Optional. The token content to look for.
23+
*
24+
* @dataProvider dataArrayKeyword
25+
* @covers PHP_CodeSniffer\Tokenizers\Tokenizer::createTokenMap
26+
*
27+
* @return void
28+
*/
29+
public function testArrayKeyword($testMarker, $testContent='array')
30+
{
31+
$tokens = $this->phpcsFile->getTokens();
32+
33+
$token = $this->getTargetToken($testMarker, [T_ARRAY, T_STRING], $testContent);
34+
$tokenArray = $tokens[$token];
35+
36+
// Make sure we're looking at the right token.
37+
$this->assertSame(T_ARRAY, $tokenArray['code'], 'Token tokenized as '.$tokenArray['type'].', not T_ARRAY (code)');
38+
39+
$this->assertArrayHasKey('parenthesis_owner', $tokenArray, 'Parenthesis owner is not set');
40+
$this->assertArrayHasKey('parenthesis_opener', $tokenArray, 'Parenthesis opener is not set');
41+
$this->assertArrayHasKey('parenthesis_closer', $tokenArray, 'Parenthesis closer is not set');
42+
43+
}//end testArrayKeyword()
44+
45+
46+
/**
47+
* Data provider.
48+
*
49+
* @see testArrayKeyword()
50+
*
51+
* @return array<string, array<string, string>>
52+
*/
53+
public static function dataArrayKeyword()
54+
{
55+
return [
56+
'empty array' => [
57+
'testMarker' => '/* testEmptyArray */',
58+
],
59+
'array with space before parenthesis' => [
60+
'testMarker' => '/* testArrayWithSpace */',
61+
],
62+
'array with comment before parenthesis' => [
63+
'testMarker' => '/* testArrayWithComment */',
64+
'testContent' => 'Array',
65+
],
66+
'nested: outer array' => [
67+
'testMarker' => '/* testNestingArray */',
68+
],
69+
'nested: inner array' => [
70+
'testMarker' => '/* testNestedArray */',
71+
],
72+
'OO constant default value' => [
73+
'testMarker' => '/* testOOConstDefault */',
74+
],
75+
];
76+
77+
}//end dataArrayKeyword()
78+
79+
80+
/**
81+
* Test that the array keyword when used in a type declaration is correctly tokenized as `T_STRING`.
82+
*
83+
* @param string $testMarker The comment prefacing the target token.
84+
* @param string $testContent Optional. The token content to look for.
85+
*
86+
* @dataProvider dataArrayType
87+
* @covers PHP_CodeSniffer\Tokenizers\Tokenizer::createTokenMap
88+
*
89+
* @return void
90+
*/
91+
public function testArrayType($testMarker, $testContent='array')
92+
{
93+
$tokens = $this->phpcsFile->getTokens();
94+
95+
$token = $this->getTargetToken($testMarker, [T_ARRAY, T_STRING], $testContent);
96+
$tokenArray = $tokens[$token];
97+
98+
// Make sure we're looking at the right token.
99+
$this->assertSame(T_STRING, $tokenArray['code'], 'Token tokenized as '.$tokenArray['type'].', not T_STRING (code)');
100+
101+
$this->assertArrayNotHasKey('parenthesis_owner', $tokenArray, 'Parenthesis owner is set');
102+
$this->assertArrayNotHasKey('parenthesis_opener', $tokenArray, 'Parenthesis opener is set');
103+
$this->assertArrayNotHasKey('parenthesis_closer', $tokenArray, 'Parenthesis closer is set');
104+
105+
}//end testArrayType()
106+
107+
108+
/**
109+
* Data provider.
110+
*
111+
* @see testArrayType()
112+
*
113+
* @return array<string, array<string, string>>
114+
*/
115+
public static function dataArrayType()
116+
{
117+
return [
118+
'closure return type' => [
119+
'testMarker' => '/* testClosureReturnType */',
120+
'testContent' => 'Array',
121+
],
122+
'function param type' => [
123+
'testMarker' => '/* testFunctionDeclarationParamType */',
124+
],
125+
'function union return type' => [
126+
'testMarker' => '/* testFunctionDeclarationReturnType */',
127+
],
128+
'OO constant type' => [
129+
'testMarker' => '/* testOOConstType */',
130+
],
131+
'OO property type' => [
132+
'testMarker' => '/* testOOPropertyType */',
133+
],
134+
135+
'OO constant DNF type' => [
136+
'testMarker' => '/* testOOConstDNFType */',
137+
],
138+
'OO property DNF type' => [
139+
'testMarker' => '/* testOOPropertyDNFType */',
140+
'testContent' => 'ARRAY',
141+
],
142+
'function param DNF type' => [
143+
'testMarker' => '/* testFunctionDeclarationParamDNFType */',
144+
],
145+
'closure param DNF type' => [
146+
'testMarker' => '/* testClosureDeclarationParamDNFType */',
147+
],
148+
'arrow return DNF type' => [
149+
'testMarker' => '/* testArrowDeclarationReturnDNFType */',
150+
'testContent' => 'Array',
151+
],
152+
];
153+
154+
}//end dataArrayType()
155+
156+
157+
/**
158+
* Verify that the retokenization of `T_ARRAY` tokens to `T_STRING` is handled correctly
159+
* for tokens with the contents 'array' which aren't in actual fact the array 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 dataNotArrayKeyword
165+
* @covers PHP_CodeSniffer\Tokenizers\Tokenizer::createTokenMap
166+
*
167+
* @return void
168+
*/
169+
public function testNotArrayKeyword($testMarker, $testContent='array')
170+
{
171+
$tokens = $this->phpcsFile->getTokens();
172+
173+
$token = $this->getTargetToken($testMarker, [T_ARRAY, T_STRING], $testContent);
174+
$tokenArray = $tokens[$token];
175+
176+
// Make sure we're looking at the right token.
177+
$this->assertSame(T_STRING, $tokenArray['code'], 'Token tokenized as '.$tokenArray['type'].', not T_STRING (code)');
178+
179+
$this->assertArrayNotHasKey('parenthesis_owner', $tokenArray, 'Parenthesis owner is set');
180+
$this->assertArrayNotHasKey('parenthesis_opener', $tokenArray, 'Parenthesis opener is set');
181+
$this->assertArrayNotHasKey('parenthesis_closer', $tokenArray, 'Parenthesis closer is set');
182+
183+
}//end testNotArrayKeyword()
184+
185+
186+
/**
187+
* Data provider.
188+
*
189+
* @see testNotArrayKeyword()
190+
*
191+
* @return array<string, array<string, string>>
192+
*/
193+
public static function dataNotArrayKeyword()
194+
{
195+
return [
196+
'class-constant-name' => [
197+
'testMarker' => '/* testClassConst */',
198+
'testContent' => 'ARRAY',
199+
],
200+
'class-method-name' => [
201+
'testMarker' => '/* testClassMethod */',
202+
],
203+
'class-constant-name-after-type' => [
204+
'testMarker' => '/* testTypedOOConstName */',
205+
'testContent' => 'ARRAY',
206+
],
207+
];
208+
209+
}//end dataNotArrayKeyword()
210+
211+
212+
}//end class

0 commit comments

Comments
 (0)