Skip to content

Commit fa96dd3

Browse files
authored
Merge pull request #702 from PHPCSStandards/feature/ruleset-tests-baseclass
Tests/Ruleset: introduce an abstract base TestCase and start using it
2 parents 40c7a43 + db02f0d commit fa96dd3

File tree

4 files changed

+128
-42
lines changed

4 files changed

+128
-42
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
/**
3+
* Test case with helper methods for tests for the Ruleset class.
4+
*
5+
* @author Juliette Reinders Folmer <[email protected]>
6+
* @copyright 2024 PHPCSStandards and contributors
7+
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
8+
*/
9+
10+
namespace PHP_CodeSniffer\Tests\Core\Ruleset;
11+
12+
use PHPUnit\Framework\TestCase;
13+
14+
abstract class AbstractRulesetTestCase extends TestCase
15+
{
16+
17+
/**
18+
* The fully qualified name of the PHPCS runtime exception class.
19+
*
20+
* @var string
21+
*/
22+
const RUNTIME_EXCEPTION = 'PHP_CodeSniffer\Exceptions\RuntimeException';
23+
24+
25+
/**
26+
* Asserts that an object has a specified property in a PHPUnit cross-version compatible manner.
27+
*
28+
* @param string $propertyName The name of the property.
29+
* @param object $object The object on which to check whether the property exists.
30+
* @param string $message Optional failure message to display.
31+
*
32+
* @return void
33+
*/
34+
protected function assertXObjectHasProperty($propertyName, $object, $message='')
35+
{
36+
if (method_exists($this, 'assertObjectHasProperty') === true) {
37+
$this->assertObjectHasProperty($propertyName, $object, $message);
38+
} else {
39+
// PHPUnit < 9.6.11.
40+
$this->assertObjectHasAttribute($propertyName, $object, $message);
41+
}
42+
43+
}//end assertXObjectHasProperty()
44+
45+
46+
/**
47+
* Asserts that an object does not have a specified property
48+
* in a PHPUnit cross-version compatible manner.
49+
*
50+
* @param string $propertyName The name of the property.
51+
* @param object $object The object on which to check whether the property exists.
52+
* @param string $message Optional failure message to display.
53+
*
54+
* @return void
55+
*/
56+
protected function assertXObjectNotHasProperty($propertyName, $object, $message='')
57+
{
58+
if (method_exists($this, 'assertObjectNotHasProperty') === true) {
59+
$this->assertObjectNotHasProperty($propertyName, $object, $message);
60+
} else {
61+
// PHPUnit < 9.6.11.
62+
$this->assertObjectNotHasAttribute($propertyName, $object, $message);
63+
}
64+
65+
}//end assertXObjectNotHasProperty()
66+
67+
68+
/**
69+
* Helper method to tell PHPUnit to expect a PHPCS RuntimeException with a certain message
70+
* in a PHPUnit cross-version compatible manner.
71+
*
72+
* @param string $message The expected exception message.
73+
*
74+
* @return void
75+
*/
76+
protected function expectRuntimeExceptionMessage($message)
77+
{
78+
if (method_exists($this, 'expectException') === true) {
79+
// PHPUnit 5+.
80+
$this->expectException(self::RUNTIME_EXCEPTION);
81+
$this->expectExceptionMessage($message);
82+
} else {
83+
// PHPUnit 4.
84+
$this->setExpectedException(self::RUNTIME_EXCEPTION, $message);
85+
}
86+
87+
}//end expectRuntimeExceptionMessage()
88+
89+
90+
/**
91+
* Helper method to tell PHPUnit to expect a PHPCS RuntimeException which matches a regex patten
92+
* in a PHPUnit cross-version compatible manner.
93+
*
94+
* @param string $regex The regex which should match.
95+
*
96+
* @return void
97+
*/
98+
protected function expectRuntimeExceptionRegex($regex)
99+
{
100+
if (method_exists($this, 'expectExceptionMessageMatches') === true) {
101+
$this->expectException(self::RUNTIME_EXCEPTION);
102+
$this->expectExceptionMessageMatches($regex);
103+
} else if (method_exists($this, 'expectExceptionMessageRegExp') === true) {
104+
// PHPUnit < 8.4.0.
105+
$this->expectException(self::RUNTIME_EXCEPTION);
106+
$this->expectExceptionMessageRegExp($regex);
107+
} else {
108+
// PHPUnit < 5.2.0.
109+
$this->setExpectedExceptionRegExp(self::RUNTIME_EXCEPTION, $regex);
110+
}
111+
112+
}//end expectRuntimeExceptionRegex()
113+
114+
115+
}//end class

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
/**
@@ -352,10 +351,7 @@ public static function dataRegisteredSniffCodes()
352351
public function testSettingProperties($sniffClass, $propertyName, $expectedValue)
353352
{
354353
$this->assertArrayHasKey($sniffClass, self::$ruleset->sniffs);
355-
356-
$hasProperty = (new ReflectionObject(self::$ruleset->sniffs[$sniffClass]))->hasProperty($propertyName);
357-
$errorMsg = sprintf('Property %s does not exist on sniff class %s', $propertyName, $sniffClass);
358-
$this->assertTrue($hasProperty, $errorMsg);
354+
$this->assertXObjectHasProperty($propertyName, self::$ruleset->sniffs[$sniffClass]);
359355

360356
$actualValue = self::$ruleset->sniffs[$sniffClass]->$propertyName;
361357
$this->assertSame($expectedValue, $actualValue);
@@ -444,10 +440,7 @@ public static function dataSettingProperties()
444440
public function testSettingInvalidPropertiesOnStandardsAndCategoriesSilentlyFails($sniffClass, $propertyName)
445441
{
446442
$this->assertArrayHasKey($sniffClass, self::$ruleset->sniffs, 'Sniff class '.$sniffClass.' not listed in registered sniffs');
447-
448-
$hasProperty = (new ReflectionObject(self::$ruleset->sniffs[$sniffClass]))->hasProperty($propertyName);
449-
$errorMsg = sprintf('Property %s registered for sniff %s which does not support it', $propertyName, $sniffClass);
450-
$this->assertFalse($hasProperty, $errorMsg);
443+
$this->assertXObjectNotHasProperty($propertyName, self::$ruleset->sniffs[$sniffClass]);
451444

452445
}//end testSettingInvalidPropertiesOnStandardsAndCategoriesSilentlyFails()
453446

tests/Core/Ruleset/SetSniffPropertyTest.php

Lines changed: 6 additions & 20 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,15 +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-
} else {
144-
// PHPUnit < 5.2.0.
145-
$this->setExpectedException($exceptionClass, $exceptionMsg);
146-
}
138+
$exceptionMsg = 'Ruleset invalid. Property "indentation" does not exist on sniff Generic.Arrays.ArrayIndent';
139+
$this->expectRuntimeExceptionMessage($exceptionMsg);
147140

148141
// Set up the ruleset.
149142
$standard = __DIR__.'/SetPropertyThrowsErrorOnInvalidPropertyTest.xml';
@@ -162,15 +155,8 @@ public function testSetPropertyThrowsErrorOnInvalidProperty()
162155
*/
163156
public function testSetPropertyThrowsErrorWhenPropertyOnlyAllowedViaAttribute()
164157
{
165-
$exceptionClass = 'PHP_CodeSniffer\Exceptions\RuntimeException';
166-
$exceptionMsg = 'Ruleset invalid. Property "arbitrarystring" does not exist on sniff TestStandard.SetProperty.NotAllowedViaAttribute';
167-
if (method_exists($this, 'expectException') === true) {
168-
$this->expectException($exceptionClass);
169-
$this->expectExceptionMessage($exceptionMsg);
170-
} else {
171-
// PHPUnit < 5.2.0.
172-
$this->setExpectedException($exceptionClass, $exceptionMsg);
173-
}
158+
$exceptionMsg = 'Ruleset invalid. Property "arbitrarystring" does not exist on sniff TestStandard.SetProperty.NotAllowedViaAttribute';
159+
$this->expectRuntimeExceptionMessage($exceptionMsg);
174160

175161
// Set up the ruleset.
176162
$standard = __DIR__.'/SetPropertyNotAllowedViaAttributeTest.xml';

tests/Core/Ruleset/ShowSniffDeprecationsTest.php

Lines changed: 3 additions & 11 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

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

2525

@@ -490,15 +490,7 @@ public function testDeprecatedSniffsAreListedAlphabetically()
490490
*/
491491
public function testExceptionIsThrownOnIncorrectlyImplementedInterface($standard, $exceptionMessage)
492492
{
493-
$exception = 'PHP_CodeSniffer\Exceptions\RuntimeException';
494-
if (method_exists($this, 'expectException') === true) {
495-
// PHPUnit 5+.
496-
$this->expectException($exception);
497-
$this->expectExceptionMessage($exceptionMessage);
498-
} else {
499-
// PHPUnit 4.
500-
$this->setExpectedException($exception, $exceptionMessage);
501-
}
493+
$this->expectRuntimeExceptionMessage($exceptionMessage);
502494

503495
// Set up the ruleset.
504496
$standard = __DIR__.'/'.$standard;

0 commit comments

Comments
 (0)