Skip to content

Commit e82ee0d

Browse files
committed
Tests/Ruleset: introduce an abstract base TestCase
Introduce an abstract base TestCase with some helper methods specifically for tests testing aspects of the `Ruleset` class.
1 parent 308bc66 commit e82ee0d

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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+
$this->expectException(self::RUNTIME_EXCEPTION);
79+
$this->expectExceptionMessage($message);
80+
81+
}//end expectRuntimeExceptionMessage()
82+
83+
84+
/**
85+
* Helper method to tell PHPUnit to expect a PHPCS RuntimeException which matches a regex patten
86+
* in a PHPUnit cross-version compatible manner.
87+
*
88+
* @param string $regex The regex which should match.
89+
*
90+
* @return void
91+
*/
92+
protected function expectRuntimeExceptionRegex($regex)
93+
{
94+
if (method_exists($this, 'expectExceptionMessageMatches') === true) {
95+
$this->expectException(self::RUNTIME_EXCEPTION);
96+
$this->expectExceptionMessageMatches($regex);
97+
} else {
98+
// PHPUnit < 8.4.0.
99+
$this->expectException(self::RUNTIME_EXCEPTION);
100+
$this->expectExceptionMessageRegExp($regex);
101+
}
102+
103+
}//end expectRuntimeExceptionRegex()
104+
105+
106+
}//end class

0 commit comments

Comments
 (0)