Skip to content

Commit a1d1366

Browse files
committed
Tests/AttributesTest: use named data sets
With non-named data sets, when a test fails, PHPUnit will display the number of the test which failed. With tests which have a _lot_ of data sets, this makes it _interesting_ (and time-consuming) to debug those, as one now has to figure out which of the data sets in the data provider corresponds to that number. Using named data sets makes debugging failing tests more straight forward as PHPUnit will display the data set name instead of the number. Using named data sets also documents what exactly each data set is testing. Aside from adding the data set name, this commit also adds the parameter name for each item in the data set, this time in an effort to make it more straight forward to update and add tests as it will be more obvious what each key in the data set signifies. Includes making the data types in the docblocks more specific.
1 parent e1157df commit a1d1366

File tree

1 file changed

+78
-78
lines changed

1 file changed

+78
-78
lines changed

tests/Core/Tokenizer/AttributesTest.php

Lines changed: 78 additions & 78 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,22 +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-
[
76-
T_STRING,
72+
'class attribute' => [
73+
'testMarker' => '/* testAttribute */',
74+
'length' => 2,
75+
'tokenCodes' => [
76+
T_STRING
7777
],
7878
],
79-
[
80-
'/* testAttributeWithParams */',
81-
7,
82-
[
79+
'class attribute with param' => [
80+
'testMarker' => '/* testAttributeWithParams */',
81+
'length' => 7,
82+
'tokenCodes' => [
8383
T_STRING,
8484
T_OPEN_PARENTHESIS,
8585
T_STRING,
@@ -88,10 +88,10 @@ public static function dataAttribute()
8888
T_CLOSE_PARENTHESIS,
8989
],
9090
],
91-
[
92-
'/* testAttributeWithNamedParam */',
93-
10,
94-
[
91+
'class attribute with named param' => [
92+
'testMarker' => '/* testAttributeWithNamedParam */',
93+
'length' => 10,
94+
'tokenCodes' => [
9595
T_STRING,
9696
T_OPEN_PARENTHESIS,
9797
T_PARAM_NAME,
@@ -103,17 +103,17 @@ public static function dataAttribute()
103103
T_CLOSE_PARENTHESIS,
104104
],
105105
],
106-
[
107-
'/* testAttributeOnFunction */',
108-
2,
109-
[
110-
T_STRING,
106+
'function attribute' => [
107+
'testMarker' => '/* testAttributeOnFunction */',
108+
'length' => 2,
109+
'tokenCodes' => [
110+
T_STRING
111111
],
112112
],
113-
[
114-
'/* testAttributeOnFunctionWithParams */',
115-
17,
116-
[
113+
'function attribute with params' => [
114+
'testMarker' => '/* testAttributeOnFunctionWithParams */',
115+
'length' => 17,
116+
'tokenCodes' => [
117117
T_STRING,
118118
T_OPEN_PARENTHESIS,
119119
T_CONSTANT_ENCAPSED_STRING,
@@ -132,10 +132,10 @@ public static function dataAttribute()
132132
T_CLOSE_PARENTHESIS,
133133
],
134134
],
135-
[
136-
'/* testAttributeWithShortClosureParameter */',
137-
17,
138-
[
135+
'function attribute with arrow function as param' => [
136+
'testMarker' => '/* testAttributeWithShortClosureParameter */',
137+
'length' => 17,
138+
'tokenCodes' => [
139139
T_STRING,
140140
T_OPEN_PARENTHESIS,
141141
T_STATIC,
@@ -154,10 +154,10 @@ public static function dataAttribute()
154154
T_CLOSE_PARENTHESIS,
155155
],
156156
],
157-
[
158-
'/* testAttributeGrouping */',
159-
26,
160-
[
157+
'function attribute; multiple comma separated classes' => [
158+
'testMarker' => '/* testAttributeGrouping */',
159+
'length' => 26,
160+
'tokenCodes' => [
161161
T_STRING,
162162
T_COMMA,
163163
T_WHITESPACE,
@@ -185,10 +185,10 @@ public static function dataAttribute()
185185
T_CLOSE_PARENTHESIS,
186186
],
187187
],
188-
[
189-
'/* testAttributeMultiline */',
190-
31,
191-
[
188+
'function attribute; multiple comma separated classes, one per line' => [
189+
'testMarker' => '/* testAttributeMultiline */',
190+
'length' => 31,
191+
'tokenCodes' => [
192192
T_WHITESPACE,
193193
T_WHITESPACE,
194194
T_STRING,
@@ -221,10 +221,10 @@ public static function dataAttribute()
221221
T_WHITESPACE,
222222
],
223223
],
224-
[
225-
'/* testFqcnAttribute */',
226-
13,
227-
[
224+
'function attribute; using partially qualified and fully qualified class names' => [
225+
'testMarker' => '/* testFqcnAttribute */',
226+
'length' => 13,
227+
'tokenCodes' => [
228228
T_STRING,
229229
T_NS_SEPARATOR,
230230
T_STRING,
@@ -294,10 +294,10 @@ public function testAttributeAndLineComment()
294294
/**
295295
* Test that attributes on function declaration parameters are parsed correctly.
296296
*
297-
* @param string $testMarker The comment which prefaces the target token in the test file.
298-
* @param int $position The token position (starting from T_FUNCTION) of T_ATTRIBUTE token.
299-
* @param int $length The number of tokens between opener and closer.
300-
* @param array $tokenCodes The codes of tokens inside the attributes.
297+
* @param string $testMarker The comment which prefaces the target token in the test file.
298+
* @param int $position The token position (starting from T_FUNCTION) of T_ATTRIBUTE token.
299+
* @param int $length The number of tokens between opener and closer.
300+
* @param array<int|string> $tokenCodes The codes of tokens inside the attributes.
301301
*
302302
* @dataProvider dataAttributeOnParameters
303303
*
@@ -347,24 +347,24 @@ function ($token) use ($attribute, $length) {
347347
*
348348
* @see testAttributeOnParameters()
349349
*
350-
* @return array
350+
* @return array<string, array<string, string|int|array<int|string>>>
351351
*/
352352
public static function dataAttributeOnParameters()
353353
{
354354
return [
355-
[
356-
'/* testSingleAttributeOnParameter */',
357-
4,
358-
2,
359-
[
360-
T_STRING,
355+
'parameter attribute; single, inline' => [
356+
'testMarker' => '/* testSingleAttributeOnParameter */',
357+
'position' => 4,
358+
'length' => 2,
359+
'tokenCodes' => [
360+
T_STRING
361361
],
362362
],
363-
[
364-
'/* testMultipleAttributesOnParameter */',
365-
4,
366-
10,
367-
[
363+
'parameter attribute; multiple comma separated, inline' => [
364+
'testMarker' => '/* testMultipleAttributesOnParameter */',
365+
'position' => 4,
366+
'length' => 10,
367+
'tokenCodes' => [
368368
T_STRING,
369369
T_COMMA,
370370
T_WHITESPACE,
@@ -376,11 +376,11 @@ public static function dataAttributeOnParameters()
376376
T_CLOSE_PARENTHESIS,
377377
],
378378
],
379-
[
380-
'/* testMultilineAttributesOnParameter */',
381-
4,
382-
13,
383-
[
379+
'parameter attribute; single, multiline' => [
380+
'testMarker' => '/* testMultilineAttributesOnParameter */',
381+
'position' => 4,
382+
'length' => 13,
383+
'tokenCodes' => [
384384
T_WHITESPACE,
385385
T_WHITESPACE,
386386
T_STRING,
@@ -403,10 +403,10 @@ public static function dataAttributeOnParameters()
403403
/**
404404
* Test that an attribute containing text which looks like a PHP close tag is tokenized correctly.
405405
*
406-
* @param string $testMarker The comment which prefaces the target token in the test file.
407-
* @param int $length The number of tokens between opener and closer.
408-
* @param array $expectedTokensAttribute The codes of tokens inside the attributes.
409-
* @param array $expectedTokensAfter The codes of tokens after the attributes.
406+
* @param string $testMarker The comment which prefaces the target token in the test file.
407+
* @param int $length The number of tokens between opener and closer.
408+
* @param array<array<string>> $expectedTokensAttribute The codes of tokens inside the attributes.
409+
* @param array<int|string> $expectedTokensAfter The codes of tokens after the attributes.
410410
*
411411
* @covers PHP_CodeSniffer\Tokenizers\PHP::parsePhpAttribute
412412
*
@@ -455,15 +455,15 @@ public function testAttributeContainingTextLookingLikeCloseTag($testMarker, $len
455455
*
456456
* @see dataAttributeOnTextLookingLikeCloseTag()
457457
*
458-
* @return array
458+
* @return array<string, array<string, string|int|array<array<string>>|array<int|string>>>
459459
*/
460460
public static function dataAttributeOnTextLookingLikeCloseTag()
461461
{
462462
return [
463-
[
464-
'/* testAttributeContainingTextLookingLikeCloseTag */',
465-
5,
466-
[
463+
'function attribute; string param with "?>"' => [
464+
'testMarker' => '/* testAttributeContainingTextLookingLikeCloseTag */',
465+
'length' => 5,
466+
'expectedTokensAttribute' => [
467467
[
468468
'T_STRING',
469469
'DeprecationReason',
@@ -485,7 +485,7 @@ public static function dataAttributeOnTextLookingLikeCloseTag()
485485
']',
486486
],
487487
],
488-
[
488+
'expectedTokensAfter' => [
489489
T_WHITESPACE,
490490
T_FUNCTION,
491491
T_WHITESPACE,
@@ -497,10 +497,10 @@ public static function dataAttributeOnTextLookingLikeCloseTag()
497497
T_CLOSE_CURLY_BRACKET,
498498
],
499499
],
500-
[
501-
'/* testAttributeContainingMultilineTextLookingLikeCloseTag */',
502-
8,
503-
[
500+
'function attribute; string param with "?>"; multiline' => [
501+
'testMarker' => '/* testAttributeContainingMultilineTextLookingLikeCloseTag */',
502+
'length' => 8,
503+
'expectedTokensAttribute' => [
504504
[
505505
'T_STRING',
506506
'DeprecationReason',
@@ -534,7 +534,7 @@ public static function dataAttributeOnTextLookingLikeCloseTag()
534534
']',
535535
],
536536
],
537-
[
537+
'expectedTokensAfter' => [
538538
T_WHITESPACE,
539539
T_FUNCTION,
540540
T_WHITESPACE,

0 commit comments

Comments
 (0)