Skip to content

Commit 9fd6cc2

Browse files
committed
Ruleset: add tests to document trimming behaviour
Document that both the property names as well as the value get trimmed off surrounding whitespace before being passed to the sniff object. Includes fixing two incorrect property docs in the test sniff.
1 parent 384f8e8 commit 9fd6cc2

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed

tests/Core/Ruleset/Fixtures/PropertyTypeHandlingInline.inc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* Testing handling of properties set inline.
55
*/
66
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsString arbitraryvalue
7+
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsTrimmedString some value
78
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling emptyStringBecomesNull
89

910
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsIntButAcceptsString 12345
@@ -14,8 +15,10 @@
1415

1516
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsBooleanTrue true
1617
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsBooleanTrueCase True
18+
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsBooleanTrueTrimmed true
1719
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsBooleanFalse false
1820
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsBooleanFalseCase fALSe
21+
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsBooleanFalseTrimmed false
1922

2023
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsArrayWithOnlyValues[] string, 10, 1.5, null, true, false
2124
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsArrayWithKeysAndValues[] string=>string,10=>10,float=>1.5,null=>null,true=>true,false=>false

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

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ final class PropertyTypeHandlingSniff implements Sniff
2222
*/
2323
public $expectsString;
2424

25+
/**
26+
* Used to verify that string properties are set as string, with surrounding whitespace trimmed.
27+
*
28+
* This is the default behaviour.
29+
*
30+
* @var string
31+
*/
32+
public $expectsTrimmedString;
33+
2534
/**
2635
* Used to verify that a string value with only whitespace will end up being set as null.
2736
*
@@ -44,14 +53,14 @@ final class PropertyTypeHandlingSniff implements Sniff
4453
public $expectsFloatButAcceptsString;
4554

4655
/**
47-
* Used to verify that null gets set as a proper null value.
56+
* Used to verify that null gets set as a string.
4857
*
4958
* @var null
5059
*/
5160
public $expectsNull;
5261

5362
/**
54-
* Used to verify that null gets set as a proper null value.
63+
* Used to verify that null gets set as a string.
5564
*
5665
* @var null
5766
*/
@@ -71,6 +80,13 @@ final class PropertyTypeHandlingSniff implements Sniff
7180
*/
7281
public $expectsBooleanTrueCase;
7382

83+
/**
84+
* Used to verify that booleans get set as proper boolean values.
85+
*
86+
* @var bool
87+
*/
88+
public $expectsBooleanTrueTrimmed;
89+
7490
/**
7591
* Used to verify that booleans get set as proper boolean values.
7692
*
@@ -85,6 +101,13 @@ final class PropertyTypeHandlingSniff implements Sniff
85101
*/
86102
public $expectsBooleanFalseCase;
87103

104+
/**
105+
* Used to verify that booleans get set as proper boolean values.
106+
*
107+
* @var bool
108+
*/
109+
public $expectsBooleanFalseTrimmed;
110+
88111
/**
89112
* Used to verify that array properties get parsed to a proper array.
90113
*

tests/Core/Ruleset/PropertyTypeHandlingTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ public static function dataTypeHandling()
119119
'propertyName' => 'expectsString',
120120
'expected' => 'arbitraryvalue',
121121
],
122+
'String value with whitespace gets trimmed' => [
123+
'propertyName' => 'expectsTrimmedString',
124+
'expected' => 'some value',
125+
],
122126
'String with whitespace only value becomes null' => [
123127
'propertyName' => 'emptyStringBecomesNull',
124128
'expected' => null,
@@ -147,6 +151,10 @@ public static function dataTypeHandling()
147151
'propertyName' => 'expectsBooleanTrueCase',
148152
'expected' => 'True',
149153
],
154+
'True (with spaces) value gets set as boolean' => [
155+
'propertyName' => 'expectsBooleanTrueTrimmed',
156+
'expected' => true,
157+
],
150158
'False value gets set as boolean' => [
151159
'propertyName' => 'expectsBooleanFalse',
152160
'expected' => false,
@@ -155,6 +163,10 @@ public static function dataTypeHandling()
155163
'propertyName' => 'expectsBooleanFalseCase',
156164
'expected' => 'fALSe',
157165
],
166+
'False (with spaces) value gets set as boolean' => [
167+
'propertyName' => 'expectsBooleanFalseTrimmed',
168+
'expected' => false,
169+
],
158170
'Array with only values (new style)' => [
159171
'propertyName' => 'expectsArrayWithOnlyValues',
160172
'expected' => $expectedArrayOnlyValues,

tests/Core/Ruleset/PropertyTypeHandlingTest.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<rule ref="./tests/Core/Ruleset/Fixtures/TestStandard/Sniffs/SetProperty/PropertyTypeHandlingSniff.php">
55
<properties>
66
<property name="expectsString" value="arbitraryvalue"/>
7+
<property name="expectsTrimmedString" value=" some value "/>
78
<property name="emptyStringBecomesNull" value=" "/>
89

910
<property name="expectsIntButAcceptsString" value="12345"/>
@@ -12,10 +13,13 @@
1213
<property name="expectsNull" value="null"/>
1314
<property name="expectsNullCase" value="NULL"/>
1415

15-
<property name="expectsBooleanTrue" value="true"/>
16+
<!-- Also tests that property names get cleaned of surrounding whitespace. -->
17+
<property name=" expectsBooleanTrue " value="true"/>
1618
<property name="expectsBooleanTrueCase" value="True"/>
19+
<property name="expectsBooleanTrueTrimmed" value="true "/>
1720
<property name="expectsBooleanFalse" value="false"/>
1821
<property name="expectsBooleanFalseCase" value="fALSe"/>
22+
<property name="expectsBooleanFalseTrimmed" value=" false "/>
1923

2024
<property name="expectsArrayWithOnlyValues" type="array">
2125
<element value="string"/>

0 commit comments

Comments
 (0)