-
-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
6 changed files
with
428 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
tests/Core/Ruleset/Fixtures/PropertyTypeHandlingInline.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
/* | ||
* Testing handling of properties set inline. | ||
*/ | ||
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsString arbitraryvalue | ||
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling emptyStringBecomesNull | ||
|
||
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsIntButAcceptsString 12345 | ||
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsFloatButAcceptsString 12.345 | ||
|
||
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsNull null | ||
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsNullCase NULL | ||
|
||
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsBooleanTrue true | ||
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsBooleanTrueCase True | ||
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsBooleanFalse false | ||
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsBooleanFalseCase fALSe | ||
|
||
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsArrayWithOnlyValues[] string, 10, 1.5, null, true, false | ||
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsArrayWithKeysAndValues[] string=>string,10=>10,float=>1.5,null=>null,true=>true,false=>false | ||
|
||
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsOldSchoolArrayWithOnlyValues[] string, 10, 1.5, null, true, false | ||
// phpcs:set TestStandard.SetProperty.PropertyTypeHandling expectsOldSchoolArrayWithKeysAndValues[] string=>string,10=>10,float=>1.5,null=>null,true=>true,false=>false | ||
|
||
echo 'hello!'; |
125 changes: 125 additions & 0 deletions
125
tests/Core/Ruleset/Fixtures/TestStandard/Sniffs/SetProperty/PropertyTypeHandlingSniff.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
<?php | ||
/** | ||
* Test fixture. | ||
* | ||
* @see \PHP_CodeSniffer\Tests\Core\Ruleset\SetSniffPropertyTest | ||
*/ | ||
|
||
namespace Fixtures\TestStandard\Sniffs\SetProperty; | ||
|
||
use PHP_CodeSniffer\Files\File; | ||
use PHP_CodeSniffer\Sniffs\Sniff; | ||
|
||
final class PropertyTypeHandlingSniff implements Sniff | ||
{ | ||
|
||
/** | ||
* Used to verify that string properties are set as string. | ||
* | ||
* This is the default behaviour. | ||
* | ||
* @var string | ||
*/ | ||
public $expectsString; | ||
|
||
/** | ||
* Used to verify that a string value with only whitespace will end up being set as null. | ||
* | ||
* @var string|null | ||
*/ | ||
public $emptyStringBecomesNull; | ||
|
||
/** | ||
* Used to verify that integer properties do not have special handling and will be set as a string. | ||
* | ||
* @var int | ||
*/ | ||
public $expectsIntButAcceptsString; | ||
|
||
/** | ||
* Used to verify that floating point properties do not have special handling and will be set as a string. | ||
* | ||
* @var float | ||
*/ | ||
public $expectsFloatButAcceptsString; | ||
|
||
/** | ||
* Used to verify that null gets set as a proper null value. | ||
* | ||
* @var null | ||
*/ | ||
public $expectsNull; | ||
|
||
/** | ||
* Used to verify that null gets set as a proper null value. | ||
* | ||
* @var null | ||
*/ | ||
public $expectsNullCase; | ||
|
||
/** | ||
* Used to verify that booleans get set as proper boolean values. | ||
* | ||
* @var bool | ||
*/ | ||
public $expectsBooleanTrue; | ||
|
||
/** | ||
* Used to verify that booleans get set as proper boolean values. | ||
* | ||
* @var bool | ||
*/ | ||
public $expectsBooleanTrueCase; | ||
|
||
/** | ||
* Used to verify that booleans get set as proper boolean values. | ||
* | ||
* @var bool | ||
*/ | ||
public $expectsBooleanFalse; | ||
|
||
/** | ||
* Used to verify that booleans get set as proper boolean values. | ||
* | ||
* @var bool | ||
*/ | ||
public $expectsBooleanFalseCase; | ||
|
||
/** | ||
* Used to verify that array properties get parsed to a proper array. | ||
* | ||
* @var array<mixed> | ||
*/ | ||
public $expectsArrayWithOnlyValues; | ||
|
||
/** | ||
* Used to verify that array properties with keys get parsed to a proper array. | ||
* | ||
* @var array<string, mixed> | ||
*/ | ||
public $expectsArrayWithKeysAndValues; | ||
|
||
/** | ||
* Used to verify that array properties passed as a string get parsed to a proper array. | ||
* | ||
* @var array<mixed> | ||
*/ | ||
public $expectsOldSchoolArrayWithOnlyValues; | ||
|
||
/** | ||
* Used to verify that array properties passed as a string with keys get parsed to a proper array. | ||
* | ||
* @var array<string, mixed> | ||
*/ | ||
public $expectsOldSchoolArrayWithKeysAndValues; | ||
|
||
public function register() | ||
{ | ||
return [T_ECHO]; | ||
} | ||
|
||
public function process(File $phpcsFile, $stackPtr) | ||
{ | ||
// Do something. | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0"?> | ||
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="PropertyTypeHandlingTest" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd"> | ||
|
||
<rule ref="./tests/Core/Ruleset/Fixtures/TestStandard/Sniffs/SetProperty/PropertyTypeHandlingSniff.php"/> | ||
|
||
</ruleset> |
Oops, something went wrong.