Skip to content

Commit 7aeaca8

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 866f7cf commit 7aeaca8

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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 Ruleset class.
19+
*
20+
* @var string
21+
*/
22+
const RULESET_CLASS = 'PHP_CodeSniffer\Ruleset';
23+
24+
/**
25+
* The fully qualified name of the PHPCS runtime exception class.
26+
*
27+
* @var string
28+
*/
29+
const RUNTIME_EXCEPTION = 'PHP_CodeSniffer\Exceptions\RuntimeException';
30+
31+
32+
/**
33+
* Asserts that an object has a specified property in a PHPUnit cross-version compatible manner.
34+
*
35+
* @param string $propertyName The name of the property.
36+
* @param object $object The object on which to check whether the property exists.
37+
* @param string $message Optional failure message to display.
38+
*
39+
* @return void
40+
*/
41+
protected function assertXObjectHasProperty($propertyName, $object, $message='')
42+
{
43+
if (method_exists($this, 'assertObjectHasProperty') === true) {
44+
$this->assertObjectHasProperty($propertyName, $object, $message);
45+
} else {
46+
// PHPUnit < 9.6.11.
47+
$this->assertObjectHasAttribute($propertyName, $object, $message);
48+
}
49+
50+
}//end assertXObjectHasProperty()
51+
52+
53+
/**
54+
* Asserts that an object does not have a specified property
55+
* in a PHPUnit cross-version compatible manner.
56+
*
57+
* @param string $propertyName The name of the property.
58+
* @param object $object The object on which to check whether the property exists.
59+
* @param string $message Optional failure message to display.
60+
*
61+
* @return void
62+
*/
63+
protected function assertXObjectNotHasProperty($propertyName, $object, $message='')
64+
{
65+
if (method_exists($this, 'assertObjectNotHasProperty') === true) {
66+
$this->assertObjectNotHasProperty($propertyName, $object, $message);
67+
} else {
68+
// PHPUnit < 9.6.11.
69+
$this->assertObjectNotHasAttribute($propertyName, $object, $message);
70+
}
71+
72+
}//end assertXObjectNotHasProperty()
73+
74+
75+
/**
76+
* Helper method to tell PHPUnit to expect a PHPCS RuntimeException with a certain message
77+
* in a PHPUnit cross-version compatible manner.
78+
*
79+
* @param string $message The expected exception message.
80+
*
81+
* @return void
82+
*/
83+
protected function expectRuntimeExceptionMessage($message)
84+
{
85+
if (method_exists($this, 'expectException') === true) {
86+
// PHPUnit 5+.
87+
$this->expectException(self::RUNTIME_EXCEPTION);
88+
$this->expectExceptionMessage($message);
89+
} else {
90+
// PHPUnit 4.
91+
$this->setExpectedException(self::RUNTIME_EXCEPTION, $message);
92+
}
93+
94+
}//end expectRuntimeExceptionMessage()
95+
96+
97+
/**
98+
* Helper method to tell PHPUnit to expect a PHPCS RuntimeException which matches a regex patten
99+
* in a PHPUnit cross-version compatible manner.
100+
*
101+
* @param string $regex The regex which should match.
102+
*
103+
* @return void
104+
*/
105+
protected function expectRuntimeExceptionRegex($regex)
106+
{
107+
if (method_exists($this, 'expectExceptionMessageMatches') === true) {
108+
$this->expectException(self::RUNTIME_EXCEPTION);
109+
$this->expectExceptionMessageMatches($regex);
110+
} else if (method_exists($this, 'expectExceptionMessageRegExp') === true) {
111+
// PHPUnit < 8.4.0.
112+
$this->expectException(self::RUNTIME_EXCEPTION);
113+
$this->expectExceptionMessageRegExp($regex);
114+
} else {
115+
// PHPUnit < 5.2.0.
116+
$this->setExpectedExceptionRegExp(self::RUNTIME_EXCEPTION, $regex);
117+
}
118+
119+
}//end expectRuntimeExceptionRegex()
120+
121+
122+
}//end class

0 commit comments

Comments
 (0)