Skip to content

Commit

Permalink
Ruleset: add tests for custom property type handling
Browse files Browse the repository at this point in the history
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
jrfnl committed Nov 22, 2024
1 parent f91bda8 commit d1433ce
Show file tree
Hide file tree
Showing 6 changed files with 428 additions and 4 deletions.
9 changes: 5 additions & 4 deletions tests/Core/Ruleset/ExplainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,10 @@ public function testExplainWithDeprecatedSniffs()
$ruleset = new Ruleset($config);

$expected = PHP_EOL;
$expected .= 'The ShowSniffDeprecationsTest standard contains 9 sniffs'.PHP_EOL.PHP_EOL;
$expected .= 'The ShowSniffDeprecationsTest standard contains 10 sniffs'.PHP_EOL.PHP_EOL;

$expected .= 'TestStandard (9 sniffs)'.PHP_EOL;
$expected .= '-----------------------'.PHP_EOL;
$expected .= 'TestStandard (10 sniffs)'.PHP_EOL;
$expected .= '------------------------'.PHP_EOL;
$expected .= ' TestStandard.Deprecated.WithLongReplacement *'.PHP_EOL;
$expected .= ' TestStandard.Deprecated.WithoutReplacement *'.PHP_EOL;
$expected .= ' TestStandard.Deprecated.WithReplacement *'.PHP_EOL;
Expand All @@ -197,7 +197,8 @@ public function testExplainWithDeprecatedSniffs()
$expected .= ' TestStandard.SetProperty.AllowedAsDeclared'.PHP_EOL;
$expected .= ' TestStandard.SetProperty.AllowedViaMagicMethod'.PHP_EOL;
$expected .= ' TestStandard.SetProperty.AllowedViaStdClass'.PHP_EOL;
$expected .= ' TestStandard.SetProperty.NotAllowedViaAttribute'.PHP_EOL.PHP_EOL;
$expected .= ' TestStandard.SetProperty.NotAllowedViaAttribute'.PHP_EOL;
$expected .= ' TestStandard.SetProperty.PropertyTypeHandling'.PHP_EOL.PHP_EOL;

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

Expand Down
26 changes: 26 additions & 0 deletions tests/Core/Ruleset/Fixtures/PropertyTypeHandlingInline.inc
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!';
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.
}
}
6 changes: 6 additions & 0 deletions tests/Core/Ruleset/PropertyTypeHandlingInlineTest.xml
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>
Loading

0 comments on commit d1433ce

Please sign in to comment.