Skip to content

Commit 41570d2

Browse files
Expand and improve tests
1 parent 748c118 commit 41570d2

9 files changed

+174
-39
lines changed

tests/Core/Tokenizers/PHP/BackfillAsymmetricVisibilityTest.inc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class PropertyDemo {
1616
public /* testPublicPrivateSetProperty */ private(set) mixed $priv3;
1717
public /* testPublicPrivateSetPropertyUC */ PRIVATE(SET) mixed $priv4;
1818

19-
/* testInvalidUnsetProperty */ public mixed $invalid1;
19+
/* testInvalidUnsetProperty */ public(unset) mixed $invalid1;
2020
/* testInvalidSpaceProperty */ public (set) mixed $invalid2;
2121
/* testInvalidCommentProperty */ protected/* foo */(set) mixed $invalid3;
2222
/* testInvalidGetProperty */ private(get) mixed $invalid4;
@@ -48,5 +48,12 @@ class ConstructorPromotionDemo {
4848
) {}
4949
}
5050

51+
class NonVisibilityCases {
52+
function /* testProtectedFunctionName */ protected() {}
53+
function /* testPublicFunctionName */ public(
54+
/* testSetParameterType */ Set $setter
55+
) {}
56+
}
57+
5158
class LiveCodingDemo {
5259
/* testLiveCoding */ private(set

tests/Core/Tokenizers/PHP/BackfillAsymmetricVisibilityTest.php

Lines changed: 102 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,7 @@ public function testAsymmetricVisibility($testMarker, $testType, $testContent)
3535
T_PUBLIC_SET,
3636
T_PROTECTED_SET,
3737
T_PRIVATE_SET,
38-
// For error cases.
39-
constant($testType),
40-
],
41-
$testContent
38+
]
4239
);
4340
$tokenArray = $tokens[$target];
4441

@@ -52,10 +49,52 @@ public function testAsymmetricVisibility($testMarker, $testType, $testContent)
5249
$tokenArray['code'],
5350
'Token tokenized as '.$tokenArray['type'].' (code)'
5451
);
52+
$this->assertSame(
53+
$testContent,
54+
$tokenArray['content'],
55+
'Token tokenized as '.$tokenArray['type'].' (content)'
56+
);
5557

5658
}//end testAsymmetricVisibility()
5759

5860

61+
/**
62+
* Test that things that are not asymmetric visibility keywords are not
63+
* tokenized as such.
64+
*
65+
* @param string $testMarker The comment which prefaces the target token in the test file.
66+
* @param string $testType The expected token type
67+
* @param string $testContent The token content to look for
68+
*
69+
* @dataProvider dataNotAsymmetricVisibility
70+
* @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
71+
*
72+
* @return void
73+
*/
74+
public function testNotAsymmetricVisibility($testMarker, $testType, $testContent)
75+
{
76+
$tokens = $this->phpcsFile->getTokens();
77+
$target = $this->getTargetToken(
78+
$testMarker,
79+
[ constant($testType) ],
80+
$testContent
81+
);
82+
$tokenArray = $tokens[$target];
83+
84+
$this->assertSame(
85+
$testType,
86+
$tokenArray['type'],
87+
'Token tokenized as '.$tokenArray['type'].' (type)'
88+
);
89+
$this->assertSame(
90+
constant($testType),
91+
$tokenArray['code'],
92+
'Token tokenized as '.$tokenArray['type'].' (code)'
93+
);
94+
95+
}//end testNotAsymmetricVisibility()
96+
97+
5998
/**
6099
* Data provider.
61100
*
@@ -127,31 +166,6 @@ public static function dataAsymmetricVisibility()
127166
'testType' => 'T_PRIVATE_SET',
128167
'testContent' => 'PRIVATE(SET)',
129168
],
130-
'property, invalid case 1' => [
131-
'testMarker' => '/* testInvalidUnsetProperty */',
132-
'testType' => 'T_PUBLIC',
133-
'testContent' => 'public',
134-
],
135-
'property, invalid case 2' => [
136-
'testMarker' => '/* testInvalidSpaceProperty */',
137-
'testType' => 'T_PUBLIC',
138-
'testContent' => 'public',
139-
],
140-
'property, invalid case 3' => [
141-
'testMarker' => '/* testInvalidCommentProperty */',
142-
'testType' => 'T_PROTECTED',
143-
'testContent' => 'protected',
144-
],
145-
'property, invalid case 4' => [
146-
'testMarker' => '/* testInvalidGetProperty */',
147-
'testType' => 'T_PRIVATE',
148-
'testContent' => 'private',
149-
],
150-
'property, invalid case 5' => [
151-
'testMarker' => '/* testInvalidNoParenProperty */',
152-
'testType' => 'T_PRIVATE',
153-
'testContent' => 'private',
154-
],
155169

156170
// Constructor property promotion.
157171
'promotion, public set, no read visibility, lowercase' => [
@@ -214,6 +228,48 @@ public static function dataAsymmetricVisibility()
214228
'testType' => 'T_PRIVATE_SET',
215229
'testContent' => 'PRIVATE(SET)',
216230
],
231+
];
232+
233+
}//end dataAsymmetricVisibility()
234+
235+
236+
/**
237+
* Data provider.
238+
*
239+
* @see testNotAsymmetricVisibility()
240+
*
241+
* @return array<string, array<string, string>>
242+
*/
243+
public static function dataNotAsymmetricVisibility()
244+
{
245+
return [
246+
'property, invalid case 1' => [
247+
'testMarker' => '/* testInvalidUnsetProperty */',
248+
'testType' => 'T_PUBLIC',
249+
'testContent' => 'public',
250+
],
251+
'property, invalid case 2' => [
252+
'testMarker' => '/* testInvalidSpaceProperty */',
253+
'testType' => 'T_PUBLIC',
254+
'testContent' => 'public',
255+
],
256+
'property, invalid case 3' => [
257+
'testMarker' => '/* testInvalidCommentProperty */',
258+
'testType' => 'T_PROTECTED',
259+
'testContent' => 'protected',
260+
],
261+
'property, invalid case 4' => [
262+
'testMarker' => '/* testInvalidGetProperty */',
263+
'testType' => 'T_PRIVATE',
264+
'testContent' => 'private',
265+
],
266+
'property, invalid case 5' => [
267+
'testMarker' => '/* testInvalidNoParenProperty */',
268+
'testType' => 'T_PRIVATE',
269+
'testContent' => 'private',
270+
],
271+
272+
// Constructor property promotion.
217273
'promotion, invalid case 1' => [
218274
'testMarker' => '/* testInvalidUnsetCPP */',
219275
'testType' => 'T_PUBLIC',
@@ -240,6 +296,23 @@ public static function dataAsymmetricVisibility()
240296
'testContent' => 'private',
241297
],
242298

299+
// Context sensitivitiy.
300+
'protected as function name' => [
301+
'testMarker' => '/* testProtectedFunctionName */',
302+
'testType' => 'T_STRING',
303+
'testContent' => 'protected',
304+
],
305+
'public as function name' => [
306+
'testMarker' => '/* testPublicFunctionName */',
307+
'testType' => 'T_STRING',
308+
'testContent' => 'public',
309+
],
310+
'set as parameter type' => [
311+
'testMarker' => '/* testSetParameterType */',
312+
'testType' => 'T_STRING',
313+
'testContent' => 'Set',
314+
],
315+
243316
// Live coding.
244317
'live coding' => [
245318
'testMarker' => '/* testLiveCoding */',

tests/Core/Tokenizers/PHP/BitwiseOrTest.inc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,18 @@ class TypeUnion
8484
/* testTypeUnionWithPHP84FinalKeywordAndFQN */
8585
final \MyClass|false $finalKeywordC;
8686

87+
/* testTypeUnionPropertyPrivateSet */
88+
private(set) Foo|Bar $asym1;
89+
90+
/* testTypeUnionPropertyPublicPrivateSet */
91+
public private(set) Foo|Bar $asym2;
92+
93+
/* testTypeUnionPropertyProtected */
94+
protected(set) Foo|Bar $asym3;
95+
96+
/* testTypeUnionPropertyPublicProtected */
97+
public protected(set) Foo|Bar $asym4;
98+
8799
public function paramTypes(
88100
/* testTypeUnionParam1 */
89101
int|float $paramA /* testBitwiseOrParamDefaultValue */ = CONSTANT_A | CONSTANT_B,

tests/Core/Tokenizers/PHP/BitwiseOrTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ public static function dataTypeUnion()
128128
'type for final property, no visibility' => ['/* testTypeUnionWithPHP84FinalKeyword */'],
129129
'type for final property, reversed modifier order' => ['/* testTypeUnionWithPHP84FinalKeywordFirst */'],
130130
'type for final property, no visibility, FQN type' => ['/* testTypeUnionWithPHP84FinalKeywordAndFQN */'],
131+
'type for private(set) property' => ['/* testTypeUnionPropertyPrivateSet */'],
132+
'type for public private(set) property' => ['/* testTypeUnionPropertyPublicPrivateSet */'],
133+
'type for protected(set) property' => ['/* testTypeUnionPropertyProtected */'],
134+
'type for public protected(set) property' => ['/* testTypeUnionPropertyPublicProtected */'],
131135
'type for method parameter' => ['/* testTypeUnionParam1 */'],
132136
'type for method parameter, first in multi-union' => ['/* testTypeUnionParam2 */'],
133137
'type for method parameter, last in multi-union' => ['/* testTypeUnionParam3 */'],

tests/Core/Tokenizers/PHP/ContextSensitiveKeywordsTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -236,22 +236,22 @@ public static function dataKeywords()
236236
'testMarker' => '/* testPrivateIsKeyword */',
237237
'expectedTokenType' => 'T_PRIVATE',
238238
],
239-
'private(set): property declaration' => [
240-
'testMarker' => '/* testPrivateSetIsKeyword */',
241-
'expectedTokenType' => 'T_PRIVATE_SET',
242-
],
243239
'protected: property declaration' => [
244240
'testMarker' => '/* testProtectedIsKeyword */',
245241
'expectedTokenType' => 'T_PROTECTED',
246242
],
247-
'protected(set): property declaration' => [
248-
'testMarker' => '/* testProtectedSetIsKeyword */',
249-
'expectedTokenType' => 'T_PROTECTED_SET',
250-
],
251243
'public: property declaration' => [
252244
'testMarker' => '/* testPublicIsKeyword */',
253245
'expectedTokenType' => 'T_PUBLIC',
254246
],
247+
'private(set): property declaration' => [
248+
'testMarker' => '/* testPrivateSetIsKeyword */',
249+
'expectedTokenType' => 'T_PRIVATE_SET',
250+
],
251+
'protected(set): property declaration' => [
252+
'testMarker' => '/* testProtectedSetIsKeyword */',
253+
'expectedTokenType' => 'T_PROTECTED_SET',
254+
],
255255
'public(set): property declaration' => [
256256
'testMarker' => '/* testPublicSetIsKeyword */',
257257
'expectedTokenType' => 'T_PUBLIC_SET',

tests/Core/Tokenizers/PHP/DNFTypesTest.inc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,18 @@ abstract class DNFTypes {
181181
/* testDNFTypeWithPHP84FinalKeywordAndStatic */
182182
final static (\className&\InterfaceName)|false $finalKeywordB;
183183

184+
/* testDNFTypePropertyWithPrivateSet */
185+
private(set) (A&B&C)|true $asym1;
186+
187+
/* testDNFTypePropertyWithPublicPrivateSet */
188+
public private(set) (A&B&C)|true $asym2;
189+
190+
/* testDNFTypePropertyWithProtectedSet */
191+
protected(set) (A&B&C)|true $asym3;
192+
193+
/* testDNFTypePropertyWithPublicProtectedSet */
194+
public protected(set) (A&B&C)|true $asym4;
195+
184196
public function paramTypes(
185197
/* testDNFTypeParam1WithAttribute */
186198
#[MyAttribute]

tests/Core/Tokenizers/PHP/DNFTypesTest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,18 @@ public static function dataDNFTypeParentheses()
450450
'OO property type: with final and static keyword' => [
451451
'testMarker' => '/* testDNFTypeWithPHP84FinalKeywordAndStatic */',
452452
],
453-
453+
'OO property type: asymmetric visibility, private(set)' => [
454+
'testMarker' => '/* testDNFTypePropertyWithPrivateSet */',
455+
],
456+
'OO property type: asymmetric vis, public private(set)' => [
457+
'testMarker' => '/* testDNFTypePropertyWithPublicPrivateSet */',
458+
],
459+
'OO property type: asymmetric visibility, protected(set)' => [
460+
'testMarker' => '/* testDNFTypePropertyWithProtectedSet */',
461+
],
462+
'OO property type: asymmetric vis, public protected(set)' => [
463+
'testMarker' => '/* testDNFTypePropertyWithPublicProtectedSet */',
464+
],
454465
'OO method param type: first param' => [
455466
'testMarker' => '/* testDNFTypeParam1WithAttribute */',
456467
],

tests/Core/Tokenizers/PHP/TypeIntersectionTest.inc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,18 @@ class TypeIntersection
6969
/* testTypeIntersectionWithPHP84FinalKeywordFirst */
7070
final private \className&InterfaceName $finalKeywordB;
7171

72+
/* testTypeIntersectionPropertyWithPrivateSet */
73+
private(set) Foo&Bar $asym1;
74+
75+
/* testTypeIntersectionPropertyWithPublicPrivateSet */
76+
public private(set) Foo&Bar $asym2;
77+
78+
/* testTypeIntersectionPropertyWithProtectedSet */
79+
protected(set) Foo&Bar $asym3;
80+
81+
/* testTypeIntersectionPropertyWithPublicProtectedSet */
82+
public protected(set) Foo&Bar $asym4;
83+
7284
public function paramTypes(
7385
/* testTypeIntersectionParam1 */
7486
Foo&Bar $paramA /* testBitwiseAndParamDefaultValue */ = CONSTANT_A & CONSTANT_B,

tests/Core/Tokenizers/PHP/TypeIntersectionTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ public static function dataTypeIntersection()
126126
'type for static readonly property' => ['/* testTypeIntersectionPropertyWithStaticKeyword */'],
127127
'type for final property' => ['/* testTypeIntersectionWithPHP84FinalKeyword */'],
128128
'type for final property reversed modifier order' => ['/* testTypeIntersectionWithPHP84FinalKeywordFirst */'],
129+
'type for asymmetric visibility (private(set)) property' => ['/* testTypeIntersectionPropertyWithPrivateSet */'],
130+
'type for asymmetric visibility (public private(set)) prop' => ['/* testTypeIntersectionPropertyWithPublicPrivateSet */'],
131+
'type for asymmetric visibility (protected(set)) property' => ['/* testTypeIntersectionPropertyWithProtectedSet */'],
132+
'type for asymmetric visibility (public protected(set)) prop' => ['/* testTypeIntersectionPropertyWithPublicProtectedSet */'],
129133
'type for method parameter' => ['/* testTypeIntersectionParam1 */'],
130134
'type for method parameter, first in multi-intersect' => ['/* testTypeIntersectionParam2 */'],
131135
'type for method parameter, last in multi-intersect' => ['/* testTypeIntersectionParam3 */'],

0 commit comments

Comments
 (0)