Skip to content

Commit d1433ce

Browse files
committed
Ruleset: add tests for custom property type handling
Add tests specifically aimed at testing the type handling of sniff properties set via a ruleset and via inline annotations. The current tests document the existing behaviour, which contains some oddities, but fixing these could be considered a breaking change, so should wait until PHPCS 4.0.
1 parent f91bda8 commit d1433ce

File tree

6 files changed

+428
-4
lines changed

6 files changed

+428
-4
lines changed

tests/Core/Ruleset/ExplainTest.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,10 @@ public function testExplainWithDeprecatedSniffs()
185185
$ruleset = new Ruleset($config);
186186

187187
$expected = PHP_EOL;
188-
$expected .= 'The ShowSniffDeprecationsTest standard contains 9 sniffs'.PHP_EOL.PHP_EOL;
188+
$expected .= 'The ShowSniffDeprecationsTest standard contains 10 sniffs'.PHP_EOL.PHP_EOL;
189189

190-
$expected .= 'TestStandard (9 sniffs)'.PHP_EOL;
191-
$expected .= '-----------------------'.PHP_EOL;
190+
$expected .= 'TestStandard (10 sniffs)'.PHP_EOL;
191+
$expected .= '------------------------'.PHP_EOL;
192192
$expected .= ' TestStandard.Deprecated.WithLongReplacement *'.PHP_EOL;
193193
$expected .= ' TestStandard.Deprecated.WithoutReplacement *'.PHP_EOL;
194194
$expected .= ' TestStandard.Deprecated.WithReplacement *'.PHP_EOL;
@@ -197,7 +197,8 @@ public function testExplainWithDeprecatedSniffs()
197197
$expected .= ' TestStandard.SetProperty.AllowedAsDeclared'.PHP_EOL;
198198
$expected .= ' TestStandard.SetProperty.AllowedViaMagicMethod'.PHP_EOL;
199199
$expected .= ' TestStandard.SetProperty.AllowedViaStdClass'.PHP_EOL;
200-
$expected .= ' TestStandard.SetProperty.NotAllowedViaAttribute'.PHP_EOL.PHP_EOL;
200+
$expected .= ' TestStandard.SetProperty.NotAllowedViaAttribute'.PHP_EOL;
201+
$expected .= ' TestStandard.SetProperty.PropertyTypeHandling'.PHP_EOL.PHP_EOL;
201202

202203
$expected .= '* Sniffs marked with an asterix are deprecated.'.PHP_EOL;
203204

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
/*
4+
* Testing handling of properties set inline.
5+
*/
6+
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsString arbitraryvalue
7+
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling emptyStringBecomesNull
8+
9+
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsIntButAcceptsString 12345
10+
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsFloatButAcceptsString 12.345
11+
12+
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsNull null
13+
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsNullCase NULL
14+
15+
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsBooleanTrue true
16+
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsBooleanTrueCase True
17+
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsBooleanFalse false
18+
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsBooleanFalseCase fALSe
19+
20+
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsArrayWithOnlyValues[] string, 10, 1.5, null, true, false
21+
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsArrayWithKeysAndValues[] string=>string,10=>10,float=>1.5,null=>null,true=>true,false=>false
22+
23+
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsOldSchoolArrayWithOnlyValues[] string, 10, 1.5, null, true, false
24+
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsOldSchoolArrayWithKeysAndValues[] string=>string,10=>10,float=>1.5,null=>null,true=>true,false=>false
25+
26+
echo 'hello!';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
<?php
2+
/**
3+
* Test fixture.
4+
*
5+
* @see \PHP_CodeSniffer\Tests\Core\Ruleset\SetSniffPropertyTest
6+
*/
7+
8+
namespace Fixtures\TestStandard\Sniffs\SetProperty;
9+
10+
use PHP_CodeSniffer\Files\File;
11+
use PHP_CodeSniffer\Sniffs\Sniff;
12+
13+
final class PropertyTypeHandlingSniff implements Sniff
14+
{
15+
16+
/**
17+
* Used to verify that string properties are set as string.
18+
*
19+
* This is the default behaviour.
20+
*
21+
* @var string
22+
*/
23+
public $expectsString;
24+
25+
/**
26+
* Used to verify that a string value with only whitespace will end up being set as null.
27+
*
28+
* @var string|null
29+
*/
30+
public $emptyStringBecomesNull;
31+
32+
/**
33+
* Used to verify that integer properties do not have special handling and will be set as a string.
34+
*
35+
* @var int
36+
*/
37+
public $expectsIntButAcceptsString;
38+
39+
/**
40+
* Used to verify that floating point properties do not have special handling and will be set as a string.
41+
*
42+
* @var float
43+
*/
44+
public $expectsFloatButAcceptsString;
45+
46+
/**
47+
* Used to verify that null gets set as a proper null value.
48+
*
49+
* @var null
50+
*/
51+
public $expectsNull;
52+
53+
/**
54+
* Used to verify that null gets set as a proper null value.
55+
*
56+
* @var null
57+
*/
58+
public $expectsNullCase;
59+
60+
/**
61+
* Used to verify that booleans get set as proper boolean values.
62+
*
63+
* @var bool
64+
*/
65+
public $expectsBooleanTrue;
66+
67+
/**
68+
* Used to verify that booleans get set as proper boolean values.
69+
*
70+
* @var bool
71+
*/
72+
public $expectsBooleanTrueCase;
73+
74+
/**
75+
* Used to verify that booleans get set as proper boolean values.
76+
*
77+
* @var bool
78+
*/
79+
public $expectsBooleanFalse;
80+
81+
/**
82+
* Used to verify that booleans get set as proper boolean values.
83+
*
84+
* @var bool
85+
*/
86+
public $expectsBooleanFalseCase;
87+
88+
/**
89+
* Used to verify that array properties get parsed to a proper array.
90+
*
91+
* @var array<mixed>
92+
*/
93+
public $expectsArrayWithOnlyValues;
94+
95+
/**
96+
* Used to verify that array properties with keys get parsed to a proper array.
97+
*
98+
* @var array<string, mixed>
99+
*/
100+
public $expectsArrayWithKeysAndValues;
101+
102+
/**
103+
* Used to verify that array properties passed as a string get parsed to a proper array.
104+
*
105+
* @var array<mixed>
106+
*/
107+
public $expectsOldSchoolArrayWithOnlyValues;
108+
109+
/**
110+
* Used to verify that array properties passed as a string with keys get parsed to a proper array.
111+
*
112+
* @var array<string, mixed>
113+
*/
114+
public $expectsOldSchoolArrayWithKeysAndValues;
115+
116+
public function register()
117+
{
118+
return [T_ECHO];
119+
}
120+
121+
public function process(File $phpcsFile, $stackPtr)
122+
{
123+
// Do something.
124+
}
125+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0"?>
2+
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="PropertyTypeHandlingTest" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd">
3+
4+
<rule ref="./tests/Core/Ruleset/Fixtures/TestStandard/Sniffs/SetProperty/PropertyTypeHandlingSniff.php"/>
5+
6+
</ruleset>

0 commit comments

Comments
 (0)