Skip to content

Commit 06a8f6d

Browse files
committed
Ruleset/PropertyTypeHandlingTest: add tests with extended arrays
Add some extra tests to verify that extending a (new style) array property declaration works as expected. While this is also tested via the `ProcessRuleShouldProcessElementTest`, some extra tests seemed warranted.
1 parent bfdc6ea commit 06a8f6d

File tree

4 files changed

+99
-14
lines changed

4 files changed

+99
-14
lines changed

tests/Core/Ruleset/Fixtures/PropertyTypeHandlingInline.inc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@
1919

2020
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsArrayWithOnlyValues[] string, 10, 1.5, null, true, false
2121
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsArrayWithKeysAndValues[] string=>string,10=>10,float=>1.5,null=>null,true=>true,false=>false
22+
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsArrayWithExtendedValues[] string, 15, another string
23+
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsArrayWithExtendedKeysAndValues[] 10=>10,string=>string,15=>15,another string=>another string
2224

2325
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsOldSchoolArrayWithOnlyValues[] string, 10, 1.5, null, true, false
2426
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsOldSchoolArrayWithKeysAndValues[] string=>string,10=>10,float=>1.5,null=>null,true=>true,false=>false
27+
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsOldSchoolArrayWithExtendedValues[] string, 15, another string
28+
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsOldSchoolArrayWithExtendedKeysAndValues[] 10=>10,string=>string,15=>15,another string=>another string
2529

2630
echo 'hello!';

tests/Core/Ruleset/Fixtures/TestStandard/Sniffs/SetProperty/PropertyTypeHandlingSniff.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,20 @@ final class PropertyTypeHandlingSniff implements Sniff
9999
*/
100100
public $expectsArrayWithKeysAndValues;
101101

102+
/**
103+
* Used to verify that array properties can get extended.
104+
*
105+
* @var array<mixed>
106+
*/
107+
public $expectsArrayWithExtendedValues;
108+
109+
/**
110+
* Used to verify that array properties can get extended.
111+
*
112+
* @var array<mixed>
113+
*/
114+
public $expectsArrayWithExtendedKeysAndValues;
115+
102116
/**
103117
* Used to verify that array properties passed as a string get parsed to a proper array.
104118
*
@@ -113,6 +127,20 @@ final class PropertyTypeHandlingSniff implements Sniff
113127
*/
114128
public $expectsOldSchoolArrayWithKeysAndValues;
115129

130+
/**
131+
* Used to verify that array properties passed as a string can get extended.
132+
*
133+
* @var array<string, mixed>
134+
*/
135+
public $expectsOldSchoolArrayWithExtendedValues;
136+
137+
/**
138+
* Used to verify that array properties passed as a string can get extended.
139+
*
140+
* @var array<string, mixed>
141+
*/
142+
public $expectsOldSchoolArrayWithExtendedKeysAndValues;
143+
116144
public function register()
117145
{
118146
return [T_ECHO];

tests/Core/Ruleset/PropertyTypeHandlingTest.php

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -102,63 +102,91 @@ public static function dataTypeHandling()
102102
'false' => 'false',
103103
];
104104

105+
$expectedArrayOnlyValuesExtended = [
106+
'string',
107+
'15',
108+
'another string',
109+
];
110+
$expectedArrayKeysAndValuesExtended = [
111+
10 => '10',
112+
'string' => 'string',
113+
15 => '15',
114+
'another string' => 'another string',
115+
];
116+
105117
return [
106-
'String value (default)' => [
118+
'String value (default)' => [
107119
'propertyName' => 'expectsString',
108120
'expected' => 'arbitraryvalue',
109121
],
110-
'String with whitespace only value becomes null' => [
122+
'String with whitespace only value becomes null' => [
111123
'propertyName' => 'emptyStringBecomesNull',
112124
'expected' => null,
113125
],
114-
'Integer value gets set as string' => [
126+
'Integer value gets set as string' => [
115127
'propertyName' => 'expectsIntButAcceptsString',
116128
'expected' => '12345',
117129
],
118-
'Float value gets set as string' => [
130+
'Float value gets set as string' => [
119131
'propertyName' => 'expectsFloatButAcceptsString',
120132
'expected' => '12.345',
121133
],
122-
'Null value gets set as string' => [
134+
'Null value gets set as string' => [
123135
'propertyName' => 'expectsNull',
124136
'expected' => 'null',
125137
],
126-
'Null (uppercase) value gets set as string' => [
138+
'Null (uppercase) value gets set as string' => [
127139
'propertyName' => 'expectsNullCase',
128140
'expected' => 'NULL',
129141
],
130-
'True value gets set as boolean' => [
142+
'True value gets set as boolean' => [
131143
'propertyName' => 'expectsBooleanTrue',
132144
'expected' => true,
133145
],
134-
'True (mixed case) value gets set as string' => [
146+
'True (mixed case) value gets set as string' => [
135147
'propertyName' => 'expectsBooleanTrueCase',
136148
'expected' => 'True',
137149
],
138-
'False value gets set as boolean' => [
150+
'False value gets set as boolean' => [
139151
'propertyName' => 'expectsBooleanFalse',
140152
'expected' => false,
141153
],
142-
'False (mixed case) value gets set as string' => [
154+
'False (mixed case) value gets set as string' => [
143155
'propertyName' => 'expectsBooleanFalseCase',
144156
'expected' => 'fALSe',
145157
],
146-
'Array with only values (new style)' => [
158+
'Array with only values (new style)' => [
147159
'propertyName' => 'expectsArrayWithOnlyValues',
148160
'expected' => $expectedArrayOnlyValues,
149161
],
150-
'Array with keys and values (new style)' => [
162+
'Array with keys and values (new style)' => [
151163
'propertyName' => 'expectsArrayWithKeysAndValues',
152164
'expected' => $expectedArrayKeysAndValues,
153165
],
154-
'Array with only values (old style)' => [
166+
'Array with only values extended (new style)' => [
167+
'propertyName' => 'expectsArrayWithExtendedValues',
168+
'expected' => $expectedArrayOnlyValuesExtended,
169+
],
170+
'Array with keys and values extended (new style)' => [
171+
'propertyName' => 'expectsArrayWithExtendedKeysAndValues',
172+
'expected' => $expectedArrayKeysAndValuesExtended,
173+
],
174+
'Array with only values (old style)' => [
155175
'propertyName' => 'expectsOldSchoolArrayWithOnlyValues',
156176
'expected' => $expectedArrayOnlyValues,
157177
],
158-
'Array with keys and values (old style)' => [
178+
'Array with keys and values (old style)' => [
159179
'propertyName' => 'expectsOldSchoolArrayWithKeysAndValues',
160180
'expected' => $expectedArrayKeysAndValues,
161181
],
182+
'Array with only values extended (old style)' => [
183+
'propertyName' => 'expectsOldSchoolArrayWithExtendedValues',
184+
'expected' => $expectedArrayOnlyValuesExtended,
185+
],
186+
'Array with keys and values extended (old style)' => [
187+
'propertyName' => 'expectsOldSchoolArrayWithExtendedKeysAndValues',
188+
'expected' => $expectedArrayKeysAndValuesExtended,
189+
],
162190
];
163191

164192
}//end dataTypeHandling()

tests/Core/Ruleset/PropertyTypeHandlingTest.xml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,34 @@
3535
<element key="false" value="false"/>
3636
</property>
3737

38+
<property name="expectsArrayWithExtendedValues" type="array">
39+
<element value="string"/>
40+
</property>
41+
42+
<property name="expectsArrayWithExtendedValues" type="array" extend="true">
43+
<element value="15"/>
44+
<element value="another string"/>
45+
</property>
46+
47+
<property name="expectsArrayWithExtendedKeysAndValues" type="array">
48+
<element key="10" value="10"/>
49+
<element key="string" value="string"/>
50+
</property>
51+
52+
<property name="expectsArrayWithExtendedKeysAndValues" type="array" extend="true">
53+
<element key="15" value="15"/>
54+
<element key="another string" value="another string"/>
55+
</property>
56+
3857
<property name="expectsOldSchoolArrayWithOnlyValues" type="array" value="string, 10, 1.5, null, true, false" />
3958

4059
<property name="expectsOldSchoolArrayWithKeysAndValues" type="array" value="string=>string,10=>10,float=>1.5,null=>null,true=>true,false=>false" />
60+
61+
<property name="expectsOldSchoolArrayWithExtendedValues" type="array" value="string" />
62+
<property name="expectsOldSchoolArrayWithExtendedValues" type="array" extend="true" value="15,another string" />
63+
64+
<property name="expectsOldSchoolArrayWithExtendedKeysAndValues" type="array" value="10=>10,string=>string" />
65+
<property name="expectsOldSchoolArrayWithExtendedKeysAndValues" type="array" extend="true" value="15=>15,another string=>another string" />
4166
</properties>
4267
</rule>
4368

0 commit comments

Comments
 (0)