Skip to content

Commit 5f94ae4

Browse files
committed
Tests: remove work-arounds for expecting exceptions
1 parent b29854b commit 5f94ae4

File tree

8 files changed

+28
-101
lines changed

8 files changed

+28
-101
lines changed

tests/Core/AbstractMethodUnitTest.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -246,16 +246,8 @@ public static function getTargetTokenFromFile(File $phpcsFile, $commentString, $
246246
*/
247247
public function expectRunTimeException($message)
248248
{
249-
$exception = 'PHP_CodeSniffer\Exceptions\RuntimeException';
250-
251-
if (method_exists($this, 'expectException') === true) {
252-
// PHPUnit 5+.
253-
$this->expectException($exception);
254-
$this->expectExceptionMessage($message);
255-
} else {
256-
// PHPUnit 4.
257-
$this->setExpectedException($exception, $message);
258-
}
249+
$this->expectException('PHP_CodeSniffer\Exceptions\RuntimeException');
250+
$this->expectExceptionMessage($message);
259251

260252
}//end expectRunTimeException()
261253

tests/Core/Config/GeneratorArgTest.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,8 @@ public function testInvalidGenerator($generatorName)
112112
$exception = 'PHP_CodeSniffer\Exceptions\DeepExitException';
113113
$message = 'ERROR: "'.$generatorName.'" is not a valid generator. The following generators are supported: Text, HTML and Markdown.';
114114

115-
if (method_exists($this, 'expectException') === true) {
116-
// PHPUnit 5+.
117-
$this->expectException($exception);
118-
$this->expectExceptionMessage($message);
119-
} else {
120-
// PHPUnit 4.
121-
$this->setExpectedException($exception, $message);
122-
}
115+
$this->expectException($exception);
116+
$this->expectExceptionMessage($message);
123117

124118
new ConfigDouble(["--generator={$generatorName}"]);
125119

tests/Core/Config/SniffsExcludeArgsTest.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,8 @@ public function testInvalid($argument, $value, $errors, $suggestion)
5050
$message .= 'Run "phpcs --help" for usage information'.PHP_EOL;
5151
$message .= PHP_EOL;
5252

53-
if (method_exists($this, 'expectException') === true) {
54-
// PHPUnit 5+.
55-
$this->expectException($exception);
56-
$this->expectExceptionMessage($message);
57-
} else {
58-
// PHPUnit 4.
59-
$this->setExpectedException($exception, $message);
60-
}
53+
$this->expectException($exception);
54+
$this->expectExceptionMessage($message);
6155

6256
new ConfigDouble(["--$argument=$value"]);
6357

tests/Core/Generators/GeneratorTest.php

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -106,24 +106,13 @@ public function testGeneratingInvalidDocsResultsInException()
106106
$ruleset = new Ruleset($config);
107107

108108
if (PHP_VERSION_ID >= 80000) {
109-
$exception = 'TypeError';
110-
$message = 'processSniff(): Argument #1 ($doc) must be of type DOMNode, null given';
111-
} else if (PHP_VERSION_ID >= 70000) {
112-
$exception = 'TypeError';
113-
$message = 'processSniff() must be an instance of DOMNode, null given';
109+
$message = 'processSniff(): Argument #1 ($doc) must be of type DOMNode, null given';
114110
} else {
115-
$exception = 'PHPUnit_Framework_Error';
116-
$message = 'processSniff() must be an instance of DOMNode, null given';
111+
$message = 'processSniff() must be an instance of DOMNode, null given';
117112
}
118113

119-
if (method_exists($this, 'expectExceptionMessage') === true) {
120-
// PHPUnit 5.2.0+.
121-
$this->expectException($exception);
122-
$this->expectExceptionMessage($message);
123-
} else {
124-
// Ancient PHPUnit.
125-
$this->setExpectedException($exception, $message);
126-
}
114+
$this->expectException('TypeError');
115+
$this->expectExceptionMessage($message);
127116

128117
$generator = new MockGenerator($ruleset);
129118
$generator->generate();

tests/Core/Ruleset/AbstractRulesetTestCase.php

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,8 @@ protected function assertXObjectNotHasProperty($propertyName, $object, $message=
7575
*/
7676
protected function expectRuntimeExceptionMessage($message)
7777
{
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-
}
78+
$this->expectException(self::RUNTIME_EXCEPTION);
79+
$this->expectExceptionMessage($message);
8680

8781
}//end expectRuntimeExceptionMessage()
8882

@@ -100,13 +94,10 @@ protected function expectRuntimeExceptionRegex($regex)
10094
if (method_exists($this, 'expectExceptionMessageMatches') === true) {
10195
$this->expectException(self::RUNTIME_EXCEPTION);
10296
$this->expectExceptionMessageMatches($regex);
103-
} else if (method_exists($this, 'expectExceptionMessageRegExp') === true) {
97+
} else {
10498
// PHPUnit < 8.4.0.
10599
$this->expectException(self::RUNTIME_EXCEPTION);
106100
$this->expectExceptionMessageRegExp($regex);
107-
} else {
108-
// PHPUnit < 5.2.0.
109-
$this->setExpectedExceptionRegExp(self::RUNTIME_EXCEPTION, $regex);
110101
}
111102

112103
}//end expectRuntimeExceptionRegex()

tests/Core/Util/Common/GetSniffCodeTest.php

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,8 @@ public function testGetSniffCodeThrowsExceptionOnInvalidInput($input)
3535
$exception = 'InvalidArgumentException';
3636
$message = 'The $sniffClass parameter must be a non-empty string';
3737

38-
if (method_exists($this, 'expectException') === true) {
39-
// PHPUnit 5+.
40-
$this->expectException($exception);
41-
$this->expectExceptionMessage($message);
42-
} else {
43-
// PHPUnit 4.
44-
$this->setExpectedException($exception, $message);
45-
}
38+
$this->expectException($exception);
39+
$this->expectExceptionMessage($message);
4640

4741
Common::getSniffCode($input);
4842

@@ -81,14 +75,8 @@ public function testGetSniffCodeThrowsExceptionOnInputWhichIsNotASniffTestClass(
8175
$exception = 'InvalidArgumentException';
8276
$message = 'The $sniffClass parameter was not passed a fully qualified sniff(test) class name. Received:';
8377

84-
if (method_exists($this, 'expectException') === true) {
85-
// PHPUnit 5+.
86-
$this->expectException($exception);
87-
$this->expectExceptionMessage($message);
88-
} else {
89-
// PHPUnit 4.
90-
$this->setExpectedException($exception, $message);
91-
}
78+
$this->expectException($exception);
79+
$this->expectExceptionMessage($message);
9280

9381
Common::getSniffCode($input);
9482

tests/Core/Util/Help/HelpTest.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -150,14 +150,8 @@ public function testConstructorInvalidArgumentException()
150150
$exception = 'InvalidArgumentException';
151151
$message = 'The $shortOptions parameter must be a string';
152152

153-
if (method_exists($this, 'expectException') === true) {
154-
// PHPUnit 5+.
155-
$this->expectException($exception);
156-
$this->expectExceptionMessage($message);
157-
} else {
158-
// PHPUnit 4.
159-
$this->setExpectedException($exception, $message);
160-
}
153+
$this->expectException($exception);
154+
$this->expectExceptionMessage($message);
161155

162156
new Help(new ConfigDouble(), [], []);
163157

tests/Core/Util/MessageCollector/MessageCollectorTest.php

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,9 @@ public function testAddingNonStringMessageResultsInException($message)
3333
{
3434
$exception = 'InvalidArgumentException';
3535
$exceptionMsg = 'The $message should be of type string. Received: ';
36-
if (method_exists($this, 'expectException') === true) {
37-
// PHPUnit 5+.
38-
$this->expectException($exception);
39-
$this->expectExceptionMessage($exceptionMsg);
40-
} else {
41-
// PHPUnit 4.
42-
$this->setExpectedException($exception, $exceptionMsg);
43-
}
36+
37+
$this->expectException($exception);
38+
$this->expectExceptionMessage($exceptionMsg);
4439

4540
$msgCollector = new MessageCollector();
4641
$msgCollector->add($message);
@@ -80,14 +75,9 @@ public function testAddingMessageWithUnsupportedMessageTypeResultsInException($t
8075
{
8176
$exception = 'InvalidArgumentException';
8277
$exceptionMsg = 'The message $type should be one of the predefined MessageCollector constants. Received: ';
83-
if (method_exists($this, 'expectException') === true) {
84-
// PHPUnit 5+.
85-
$this->expectException($exception);
86-
$this->expectExceptionMessage($exceptionMsg);
87-
} else {
88-
// PHPUnit 4.
89-
$this->setExpectedException($exception, $exceptionMsg);
90-
}
78+
79+
$this->expectException($exception);
80+
$this->expectExceptionMessage($exceptionMsg);
9181

9282
$msgCollector = new MessageCollector();
9383
$msgCollector->add('Message', $type);
@@ -309,14 +299,9 @@ public static function dataDisplayingNonBlockingMessages()
309299
public function testDisplayingBlockingErrors($messages, $expected)
310300
{
311301
$exception = 'PHP_CodeSniffer\Exceptions\RuntimeException';
312-
if (method_exists($this, 'expectException') === true) {
313-
// PHPUnit 5+.
314-
$this->expectException($exception);
315-
$this->expectExceptionMessage($expected);
316-
} else {
317-
// PHPUnit 4.
318-
$this->setExpectedException($exception, $expected);
319-
}
302+
303+
$this->expectException($exception);
304+
$this->expectExceptionMessage($expected);
320305

321306
$msgCollector = new MessageCollector();
322307
$this->createErrorCache($msgCollector, $messages);

0 commit comments

Comments
 (0)