Skip to content

Commit

Permalink
Tests/Ruleset: start using the new AbstractRulesetTestCase
Browse files Browse the repository at this point in the history
Start using the new `AbstractRulesetTestCase` in pre-existing `Ruleset` tests.
  • Loading branch information
jrfnl committed Nov 22, 2024
1 parent a25d8cf commit db02f0d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 42 deletions.
15 changes: 4 additions & 11 deletions tests/Core/Ruleset/RuleInclusionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@

use PHP_CodeSniffer\Ruleset;
use PHP_CodeSniffer\Tests\ConfigDouble;
use PHPUnit\Framework\TestCase;
use ReflectionObject;
use PHP_CodeSniffer\Tests\Core\Ruleset\AbstractRulesetTestCase;

/**
* Tests for the \PHP_CodeSniffer\Ruleset class.
*
* @covers \PHP_CodeSniffer\Ruleset
*/
final class RuleInclusionTest extends TestCase
final class RuleInclusionTest extends AbstractRulesetTestCase
{

/**
Expand Down Expand Up @@ -352,10 +351,7 @@ public static function dataRegisteredSniffCodes()
public function testSettingProperties($sniffClass, $propertyName, $expectedValue)
{
$this->assertArrayHasKey($sniffClass, self::$ruleset->sniffs);

$hasProperty = (new ReflectionObject(self::$ruleset->sniffs[$sniffClass]))->hasProperty($propertyName);
$errorMsg = sprintf('Property %s does not exist on sniff class %s', $propertyName, $sniffClass);
$this->assertTrue($hasProperty, $errorMsg);
$this->assertXObjectHasProperty($propertyName, self::$ruleset->sniffs[$sniffClass]);

$actualValue = self::$ruleset->sniffs[$sniffClass]->$propertyName;
$this->assertSame($expectedValue, $actualValue);
Expand Down Expand Up @@ -444,10 +440,7 @@ public static function dataSettingProperties()
public function testSettingInvalidPropertiesOnStandardsAndCategoriesSilentlyFails($sniffClass, $propertyName)
{
$this->assertArrayHasKey($sniffClass, self::$ruleset->sniffs, 'Sniff class '.$sniffClass.' not listed in registered sniffs');

$hasProperty = (new ReflectionObject(self::$ruleset->sniffs[$sniffClass]))->hasProperty($propertyName);
$errorMsg = sprintf('Property %s registered for sniff %s which does not support it', $propertyName, $sniffClass);
$this->assertFalse($hasProperty, $errorMsg);
$this->assertXObjectNotHasProperty($propertyName, self::$ruleset->sniffs[$sniffClass]);

}//end testSettingInvalidPropertiesOnStandardsAndCategoriesSilentlyFails()

Expand Down
26 changes: 6 additions & 20 deletions tests/Core/Ruleset/SetSniffPropertyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@

use PHP_CodeSniffer\Ruleset;
use PHP_CodeSniffer\Tests\ConfigDouble;
use PHPUnit\Framework\TestCase;
use PHP_CodeSniffer\Tests\Core\Ruleset\AbstractRulesetTestCase;
use ReflectionObject;

/**
* These tests specifically focus on the changes made to work around the PHP 8.2 dynamic properties deprecation.
*
* @covers \PHP_CodeSniffer\Ruleset::setSniffProperty
*/
final class SetSniffPropertyTest extends TestCase
final class SetSniffPropertyTest extends AbstractRulesetTestCase
{


Expand Down Expand Up @@ -135,15 +135,8 @@ public function testSetPropertyAppliesPropertyToMultipleSniffsInCategory()
*/
public function testSetPropertyThrowsErrorOnInvalidProperty()
{
$exceptionClass = 'PHP_CodeSniffer\Exceptions\RuntimeException';
$exceptionMsg = 'Ruleset invalid. Property "indentation" does not exist on sniff Generic.Arrays.ArrayIndent';
if (method_exists($this, 'expectException') === true) {
$this->expectException($exceptionClass);
$this->expectExceptionMessage($exceptionMsg);
} else {
// PHPUnit < 5.2.0.
$this->setExpectedException($exceptionClass, $exceptionMsg);
}
$exceptionMsg = 'Ruleset invalid. Property "indentation" does not exist on sniff Generic.Arrays.ArrayIndent';
$this->expectRuntimeExceptionMessage($exceptionMsg);

// Set up the ruleset.
$standard = __DIR__.'/SetPropertyThrowsErrorOnInvalidPropertyTest.xml';
Expand All @@ -162,15 +155,8 @@ public function testSetPropertyThrowsErrorOnInvalidProperty()
*/
public function testSetPropertyThrowsErrorWhenPropertyOnlyAllowedViaAttribute()
{
$exceptionClass = 'PHP_CodeSniffer\Exceptions\RuntimeException';
$exceptionMsg = 'Ruleset invalid. Property "arbitrarystring" does not exist on sniff TestStandard.SetProperty.NotAllowedViaAttribute';
if (method_exists($this, 'expectException') === true) {
$this->expectException($exceptionClass);
$this->expectExceptionMessage($exceptionMsg);
} else {
// PHPUnit < 5.2.0.
$this->setExpectedException($exceptionClass, $exceptionMsg);
}
$exceptionMsg = 'Ruleset invalid. Property "arbitrarystring" does not exist on sniff TestStandard.SetProperty.NotAllowedViaAttribute';
$this->expectRuntimeExceptionMessage($exceptionMsg);

// Set up the ruleset.
$standard = __DIR__.'/SetPropertyNotAllowedViaAttributeTest.xml';
Expand Down
14 changes: 3 additions & 11 deletions tests/Core/Ruleset/ShowSniffDeprecationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@

use PHP_CodeSniffer\Ruleset;
use PHP_CodeSniffer\Tests\ConfigDouble;
use PHPUnit\Framework\TestCase;
use PHP_CodeSniffer\Tests\Core\Ruleset\AbstractRulesetTestCase;

/**
* Tests PHPCS native handling of sniff deprecations.
*
* @covers \PHP_CodeSniffer\Ruleset::hasSniffDeprecations
* @covers \PHP_CodeSniffer\Ruleset::showSniffDeprecations
*/
final class ShowSniffDeprecationsTest extends TestCase
final class ShowSniffDeprecationsTest extends AbstractRulesetTestCase
{


Expand Down Expand Up @@ -452,15 +452,7 @@ public function testDeprecatedSniffsAreListedAlphabetically()
*/
public function testExceptionIsThrownOnIncorrectlyImplementedInterface($standard, $exceptionMessage)
{
$exception = 'PHP_CodeSniffer\Exceptions\RuntimeException';
if (method_exists($this, 'expectException') === true) {
// PHPUnit 5+.
$this->expectException($exception);
$this->expectExceptionMessage($exceptionMessage);
} else {
// PHPUnit 4.
$this->setExpectedException($exception, $exceptionMessage);
}
$this->expectRuntimeExceptionMessage($exceptionMessage);

// Set up the ruleset.
$standard = __DIR__.'/'.$standard;
Expand Down

0 comments on commit db02f0d

Please sign in to comment.