Skip to content

Commit e0508be

Browse files
committed
Tests/Ruleset: start using the new AbstractRulesetTestCase
Start using the new `AbstractRulesetTestCase` in pre-existing `Ruleset` tests.
1 parent e82ee0d commit e0508be

File tree

3 files changed

+13
-29
lines changed

3 files changed

+13
-29
lines changed

tests/Core/Ruleset/RuleInclusionTest.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@
1111

1212
use PHP_CodeSniffer\Ruleset;
1313
use PHP_CodeSniffer\Tests\ConfigDouble;
14-
use PHPUnit\Framework\TestCase;
15-
use ReflectionObject;
14+
use PHP_CodeSniffer\Tests\Core\Ruleset\AbstractRulesetTestCase;
1615

1716
/**
1817
* Tests for the \PHP_CodeSniffer\Ruleset class.
1918
*
2019
* @covers \PHP_CodeSniffer\Ruleset
2120
*/
22-
final class RuleInclusionTest extends TestCase
21+
final class RuleInclusionTest extends AbstractRulesetTestCase
2322
{
2423

2524
/**
@@ -348,10 +347,7 @@ public static function dataRegisteredSniffCodes()
348347
public function testSettingProperties($sniffClass, $propertyName, $expectedValue)
349348
{
350349
$this->assertArrayHasKey($sniffClass, self::$ruleset->sniffs);
351-
352-
$hasProperty = (new ReflectionObject(self::$ruleset->sniffs[$sniffClass]))->hasProperty($propertyName);
353-
$errorMsg = sprintf('Property %s does not exist on sniff class %s', $propertyName, $sniffClass);
354-
$this->assertTrue($hasProperty, $errorMsg);
350+
$this->assertXObjectHasProperty($propertyName, self::$ruleset->sniffs[$sniffClass]);
355351

356352
$actualValue = self::$ruleset->sniffs[$sniffClass]->$propertyName;
357353
$this->assertSame($expectedValue, $actualValue);
@@ -440,10 +436,7 @@ public static function dataSettingProperties()
440436
public function testSettingInvalidPropertiesOnStandardsAndCategoriesSilentlyFails($sniffClass, $propertyName)
441437
{
442438
$this->assertArrayHasKey($sniffClass, self::$ruleset->sniffs, 'Sniff class '.$sniffClass.' not listed in registered sniffs');
443-
444-
$hasProperty = (new ReflectionObject(self::$ruleset->sniffs[$sniffClass]))->hasProperty($propertyName);
445-
$errorMsg = sprintf('Property %s registered for sniff %s which does not support it', $propertyName, $sniffClass);
446-
$this->assertFalse($hasProperty, $errorMsg);
439+
$this->assertXObjectNotHasProperty($propertyName, self::$ruleset->sniffs[$sniffClass]);
447440

448441
}//end testSettingInvalidPropertiesOnStandardsAndCategoriesSilentlyFails()
449442

tests/Core/Ruleset/SetSniffPropertyTest.php

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111

1212
use PHP_CodeSniffer\Ruleset;
1313
use PHP_CodeSniffer\Tests\ConfigDouble;
14-
use PHPUnit\Framework\TestCase;
14+
use PHP_CodeSniffer\Tests\Core\Ruleset\AbstractRulesetTestCase;
1515
use ReflectionObject;
1616

1717
/**
1818
* These tests specifically focus on the changes made to work around the PHP 8.2 dynamic properties deprecation.
1919
*
2020
* @covers \PHP_CodeSniffer\Ruleset::setSniffProperty
2121
*/
22-
final class SetSniffPropertyTest extends TestCase
22+
final class SetSniffPropertyTest extends AbstractRulesetTestCase
2323
{
2424

2525

@@ -135,12 +135,8 @@ public function testSetPropertyAppliesPropertyToMultipleSniffsInCategory()
135135
*/
136136
public function testSetPropertyThrowsErrorOnInvalidProperty()
137137
{
138-
$exceptionClass = 'PHP_CodeSniffer\Exceptions\RuntimeException';
139-
$exceptionMsg = 'Ruleset invalid. Property "indentation" does not exist on sniff Generic.Arrays.ArrayIndent';
140-
if (method_exists($this, 'expectException') === true) {
141-
$this->expectException($exceptionClass);
142-
$this->expectExceptionMessage($exceptionMsg);
143-
}
138+
$exceptionMsg = 'Ruleset invalid. Property "indentation" does not exist on sniff Generic.Arrays.ArrayIndent';
139+
$this->expectRuntimeExceptionMessage($exceptionMsg);
144140

145141
// Set up the ruleset.
146142
$standard = __DIR__.'/SetPropertyThrowsErrorOnInvalidPropertyTest.xml';
@@ -159,12 +155,8 @@ public function testSetPropertyThrowsErrorOnInvalidProperty()
159155
*/
160156
public function testSetPropertyThrowsErrorWhenPropertyOnlyAllowedViaAttribute()
161157
{
162-
$exceptionClass = 'PHP_CodeSniffer\Exceptions\RuntimeException';
163-
$exceptionMsg = 'Ruleset invalid. Property "arbitrarystring" does not exist on sniff TestStandard.SetProperty.NotAllowedViaAttribute';
164-
if (method_exists($this, 'expectException') === true) {
165-
$this->expectException($exceptionClass);
166-
$this->expectExceptionMessage($exceptionMsg);
167-
}
158+
$exceptionMsg = 'Ruleset invalid. Property "arbitrarystring" does not exist on sniff TestStandard.SetProperty.NotAllowedViaAttribute';
159+
$this->expectRuntimeExceptionMessage($exceptionMsg);
168160

169161
// Set up the ruleset.
170162
$standard = __DIR__.'/SetPropertyNotAllowedViaAttributeTest.xml';

tests/Core/Ruleset/ShowSniffDeprecationsTest.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
use PHP_CodeSniffer\Exceptions\RuntimeException;
1313
use PHP_CodeSniffer\Ruleset;
1414
use PHP_CodeSniffer\Tests\ConfigDouble;
15-
use PHPUnit\Framework\TestCase;
15+
use PHP_CodeSniffer\Tests\Core\Ruleset\AbstractRulesetTestCase;
1616

1717
/**
1818
* Tests PHPCS native handling of sniff deprecations.
1919
*
2020
* @covers \PHP_CodeSniffer\Ruleset::hasSniffDeprecations
2121
* @covers \PHP_CodeSniffer\Ruleset::showSniffDeprecations
2222
*/
23-
final class ShowSniffDeprecationsTest extends TestCase
23+
final class ShowSniffDeprecationsTest extends AbstractRulesetTestCase
2424
{
2525

2626

@@ -491,8 +491,7 @@ public function testDeprecatedSniffsAreListedAlphabetically()
491491
*/
492492
public function testExceptionIsThrownOnIncorrectlyImplementedInterface($standard, $exceptionMessage)
493493
{
494-
$this->expectException(RuntimeException::class);
495-
$this->expectExceptionMessage($exceptionMessage);
494+
$this->expectRuntimeExceptionMessage($exceptionMessage);
496495

497496
// Set up the ruleset.
498497
$standard = __DIR__.'/'.$standard;

0 commit comments

Comments
 (0)