Skip to content

Commit 5456208

Browse files
authored
Merge pull request #264 from PHPCSStandards/feature/tests-attributes-various-improvements
Tests/AttributesTest: use named data sets + activate a test
2 parents d333624 + 4920451 commit 5456208

File tree

1 file changed

+120
-75
lines changed

1 file changed

+120
-75
lines changed

tests/Core/Tokenizer/AttributesTest.php

Lines changed: 120 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ final class AttributesTest extends AbstractMethodUnitTest
1818
/**
1919
* Test that attributes are parsed correctly.
2020
*
21-
* @param string $testMarker The comment which prefaces the target token in the test file.
22-
* @param int $length The number of tokens between opener and closer.
23-
* @param array $tokenCodes The codes of tokens inside the attributes.
21+
* @param string $testMarker The comment which prefaces the target token in the test file.
22+
* @param int $length The number of tokens between opener and closer.
23+
* @param array<int|string> $tokenCodes The codes of tokens inside the attributes.
2424
*
2525
* @dataProvider dataAttribute
2626
* @covers PHP_CodeSniffer\Tokenizers\PHP::tokenize
@@ -64,20 +64,22 @@ function ($token) use ($attribute, $length) {
6464
*
6565
* @see testAttribute()
6666
*
67-
* @return array
67+
* @return array<string, array<string, string|int|array<int|string>>>
6868
*/
6969
public static function dataAttribute()
7070
{
7171
return [
72-
[
73-
'/* testAttribute */',
74-
2,
75-
[ T_STRING ],
72+
'class attribute' => [
73+
'testMarker' => '/* testAttribute */',
74+
'length' => 2,
75+
'tokenCodes' => [
76+
T_STRING
77+
],
7678
],
77-
[
78-
'/* testAttributeWithParams */',
79-
7,
80-
[
79+
'class attribute with param' => [
80+
'testMarker' => '/* testAttributeWithParams */',
81+
'length' => 7,
82+
'tokenCodes' => [
8183
T_STRING,
8284
T_OPEN_PARENTHESIS,
8385
T_STRING,
@@ -86,10 +88,10 @@ public static function dataAttribute()
8688
T_CLOSE_PARENTHESIS,
8789
],
8890
],
89-
[
90-
'/* testAttributeWithNamedParam */',
91-
10,
92-
[
91+
'class attribute with named param' => [
92+
'testMarker' => '/* testAttributeWithNamedParam */',
93+
'length' => 10,
94+
'tokenCodes' => [
9395
T_STRING,
9496
T_OPEN_PARENTHESIS,
9597
T_PARAM_NAME,
@@ -101,15 +103,17 @@ public static function dataAttribute()
101103
T_CLOSE_PARENTHESIS,
102104
],
103105
],
104-
[
105-
'/* testAttributeOnFunction */',
106-
2,
107-
[ T_STRING ],
106+
'function attribute' => [
107+
'testMarker' => '/* testAttributeOnFunction */',
108+
'length' => 2,
109+
'tokenCodes' => [
110+
T_STRING
111+
],
108112
],
109-
[
110-
'/* testAttributeOnFunctionWithParams */',
111-
17,
112-
[
113+
'function attribute with params' => [
114+
'testMarker' => '/* testAttributeOnFunctionWithParams */',
115+
'length' => 17,
116+
'tokenCodes' => [
113117
T_STRING,
114118
T_OPEN_PARENTHESIS,
115119
T_CONSTANT_ENCAPSED_STRING,
@@ -128,10 +132,10 @@ public static function dataAttribute()
128132
T_CLOSE_PARENTHESIS,
129133
],
130134
],
131-
[
132-
'/* testAttributeWithShortClosureParameter */',
133-
17,
134-
[
135+
'function attribute with arrow function as param' => [
136+
'testMarker' => '/* testAttributeWithShortClosureParameter */',
137+
'length' => 17,
138+
'tokenCodes' => [
135139
T_STRING,
136140
T_OPEN_PARENTHESIS,
137141
T_STATIC,
@@ -150,10 +154,10 @@ public static function dataAttribute()
150154
T_CLOSE_PARENTHESIS,
151155
],
152156
],
153-
[
154-
'/* testAttributeGrouping */',
155-
26,
156-
[
157+
'function attribute; multiple comma separated classes' => [
158+
'testMarker' => '/* testAttributeGrouping */',
159+
'length' => 26,
160+
'tokenCodes' => [
157161
T_STRING,
158162
T_COMMA,
159163
T_WHITESPACE,
@@ -181,10 +185,10 @@ public static function dataAttribute()
181185
T_CLOSE_PARENTHESIS,
182186
],
183187
],
184-
[
185-
'/* testAttributeMultiline */',
186-
31,
187-
[
188+
'function attribute; multiple comma separated classes, one per line' => [
189+
'testMarker' => '/* testAttributeMultiline */',
190+
'length' => 31,
191+
'tokenCodes' => [
188192
T_WHITESPACE,
189193
T_WHITESPACE,
190194
T_STRING,
@@ -217,10 +221,49 @@ public static function dataAttribute()
217221
T_WHITESPACE,
218222
],
219223
],
220-
[
221-
'/* testFqcnAttribute */',
222-
13,
223-
[
224+
'function attribute; multiple comma separated classes, one per line, with comments' => [
225+
'testMarker' => '/* testAttributeMultilineWithComment */',
226+
'length' => 34,
227+
'tokenCodes' => [
228+
T_WHITESPACE,
229+
T_WHITESPACE,
230+
T_STRING,
231+
T_COMMA,
232+
T_WHITESPACE,
233+
T_COMMENT,
234+
T_WHITESPACE,
235+
T_STRING,
236+
T_OPEN_PARENTHESIS,
237+
T_COMMENT,
238+
T_WHITESPACE,
239+
T_CONSTANT_ENCAPSED_STRING,
240+
T_CLOSE_PARENTHESIS,
241+
T_COMMA,
242+
T_WHITESPACE,
243+
T_WHITESPACE,
244+
T_STRING,
245+
T_OPEN_PARENTHESIS,
246+
T_CONSTANT_ENCAPSED_STRING,
247+
T_COMMA,
248+
T_WHITESPACE,
249+
T_PARAM_NAME,
250+
T_COLON,
251+
T_WHITESPACE,
252+
T_OPEN_SHORT_ARRAY,
253+
T_CONSTANT_ENCAPSED_STRING,
254+
T_WHITESPACE,
255+
T_DOUBLE_ARROW,
256+
T_WHITESPACE,
257+
T_CONSTANT_ENCAPSED_STRING,
258+
T_CLOSE_SHORT_ARRAY,
259+
T_CLOSE_PARENTHESIS,
260+
T_WHITESPACE,
261+
],
262+
],
263+
'function attribute; using partially qualified and fully qualified class names' => [
264+
'testMarker' => '/* testFqcnAttribute */',
265+
'length' => 13,
266+
'tokenCodes' => [
224267
T_STRING,
225268
T_NS_SEPARATOR,
226269
T_STRING,
@@ -290,10 +333,10 @@ public function testAttributeAndLineComment()
290333
/**
291334
* Test that attributes on function declaration parameters are parsed correctly.
292335
*
293-
* @param string $testMarker The comment which prefaces the target token in the test file.
294-
* @param int $position The token position (starting from T_FUNCTION) of T_ATTRIBUTE token.
295-
* @param int $length The number of tokens between opener and closer.
296-
* @param array $tokenCodes The codes of tokens inside the attributes.
336+
* @param string $testMarker The comment which prefaces the target token in the test file.
337+
* @param int $position The token position (starting from T_FUNCTION) of T_ATTRIBUTE token.
338+
* @param int $length The number of tokens between opener and closer.
339+
* @param array<int|string> $tokenCodes The codes of tokens inside the attributes.
297340
*
298341
* @dataProvider dataAttributeOnParameters
299342
*
@@ -343,22 +386,24 @@ function ($token) use ($attribute, $length) {
343386
*
344387
* @see testAttributeOnParameters()
345388
*
346-
* @return array
389+
* @return array<string, array<string, string|int|array<int|string>>>
347390
*/
348391
public static function dataAttributeOnParameters()
349392
{
350393
return [
351-
[
352-
'/* testSingleAttributeOnParameter */',
353-
4,
354-
2,
355-
[T_STRING],
394+
'parameter attribute; single, inline' => [
395+
'testMarker' => '/* testSingleAttributeOnParameter */',
396+
'position' => 4,
397+
'length' => 2,
398+
'tokenCodes' => [
399+
T_STRING
400+
],
356401
],
357-
[
358-
'/* testMultipleAttributesOnParameter */',
359-
4,
360-
10,
361-
[
402+
'parameter attribute; multiple comma separated, inline' => [
403+
'testMarker' => '/* testMultipleAttributesOnParameter */',
404+
'position' => 4,
405+
'length' => 10,
406+
'tokenCodes' => [
362407
T_STRING,
363408
T_COMMA,
364409
T_WHITESPACE,
@@ -370,11 +415,11 @@ public static function dataAttributeOnParameters()
370415
T_CLOSE_PARENTHESIS,
371416
],
372417
],
373-
[
374-
'/* testMultilineAttributesOnParameter */',
375-
4,
376-
13,
377-
[
418+
'parameter attribute; single, multiline' => [
419+
'testMarker' => '/* testMultilineAttributesOnParameter */',
420+
'position' => 4,
421+
'length' => 13,
422+
'tokenCodes' => [
378423
T_WHITESPACE,
379424
T_WHITESPACE,
380425
T_STRING,
@@ -397,10 +442,10 @@ public static function dataAttributeOnParameters()
397442
/**
398443
* Test that an attribute containing text which looks like a PHP close tag is tokenized correctly.
399444
*
400-
* @param string $testMarker The comment which prefaces the target token in the test file.
401-
* @param int $length The number of tokens between opener and closer.
402-
* @param array $expectedTokensAttribute The codes of tokens inside the attributes.
403-
* @param array $expectedTokensAfter The codes of tokens after the attributes.
445+
* @param string $testMarker The comment which prefaces the target token in the test file.
446+
* @param int $length The number of tokens between opener and closer.
447+
* @param array<array<string>> $expectedTokensAttribute The codes of tokens inside the attributes.
448+
* @param array<int|string> $expectedTokensAfter The codes of tokens after the attributes.
404449
*
405450
* @covers PHP_CodeSniffer\Tokenizers\PHP::parsePhpAttribute
406451
*
@@ -449,15 +494,15 @@ public function testAttributeContainingTextLookingLikeCloseTag($testMarker, $len
449494
*
450495
* @see dataAttributeOnTextLookingLikeCloseTag()
451496
*
452-
* @return array
497+
* @return array<string, array<string, string|int|array<array<string>>|array<int|string>>>
453498
*/
454499
public static function dataAttributeOnTextLookingLikeCloseTag()
455500
{
456501
return [
457-
[
458-
'/* testAttributeContainingTextLookingLikeCloseTag */',
459-
5,
460-
[
502+
'function attribute; string param with "?>"' => [
503+
'testMarker' => '/* testAttributeContainingTextLookingLikeCloseTag */',
504+
'length' => 5,
505+
'expectedTokensAttribute' => [
461506
[
462507
'T_STRING',
463508
'DeprecationReason',
@@ -479,7 +524,7 @@ public static function dataAttributeOnTextLookingLikeCloseTag()
479524
']',
480525
],
481526
],
482-
[
527+
'expectedTokensAfter' => [
483528
T_WHITESPACE,
484529
T_FUNCTION,
485530
T_WHITESPACE,
@@ -491,10 +536,10 @@ public static function dataAttributeOnTextLookingLikeCloseTag()
491536
T_CLOSE_CURLY_BRACKET,
492537
],
493538
],
494-
[
495-
'/* testAttributeContainingMultilineTextLookingLikeCloseTag */',
496-
8,
497-
[
539+
'function attribute; string param with "?>"; multiline' => [
540+
'testMarker' => '/* testAttributeContainingMultilineTextLookingLikeCloseTag */',
541+
'length' => 8,
542+
'expectedTokensAttribute' => [
498543
[
499544
'T_STRING',
500545
'DeprecationReason',
@@ -528,7 +573,7 @@ public static function dataAttributeOnTextLookingLikeCloseTag()
528573
']',
529574
],
530575
],
531-
[
576+
'expectedTokensAfter' => [
532577
T_WHITESPACE,
533578
T_FUNCTION,
534579
T_WHITESPACE,

0 commit comments

Comments
 (0)