Skip to content

Commit 7e14b7f

Browse files
committed
File/Get*Tests: work round removal of exception related annotations
Expecting exceptions via annotations was deprecated in PHPUnit 8.x and removed in PHPUnit 9.0.0 in favour of using the `expectException*()` methods. This does need a work around for PHPUnit 4.x in which the `expectException*()` methods didn't exist yet, but as this only applies to three tests, that's not a big deal.
1 parent 2bdb734 commit 7e14b7f

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

tests/Core/File/GetClassPropertiesTest.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,22 @@ class GetClassPropertiesTest extends AbstractMethodUnitTest
2323
*
2424
* @dataProvider dataNotAClassException
2525
*
26-
* @expectedException PHP_CodeSniffer\Exceptions\RuntimeException
27-
* @expectedExceptionMessage $stackPtr must be of type T_CLASS
28-
*
2926
* @return void
3027
*/
3128
public function testNotAClassException($testMarker, $tokenType)
3229
{
30+
$msg = '$stackPtr must be of type T_CLASS';
31+
$exception = 'PHP_CodeSniffer\Exceptions\RuntimeException';
32+
33+
if (\method_exists($this, 'expectException') === true) {
34+
// PHPUnit 5+.
35+
$this->expectException($exception);
36+
$this->expectExceptionMessage($msg);
37+
} else {
38+
// PHPUnit 4.
39+
$this->setExpectedException($exception, $msg);
40+
}
41+
3342
$target = $this->getTargetToken($testMarker, $tokenType);
3443
self::$phpcsFile->getClassProperties($target);
3544

tests/Core/File/GetMemberPropertiesTest.php

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -815,15 +815,24 @@ public function dataGetMemberProperties()
815815
*
816816
* @param string $identifier Comment which precedes the test case.
817817
*
818-
* @expectedException PHP_CodeSniffer\Exceptions\RuntimeException
819-
* @expectedExceptionMessage $stackPtr is not a class member var
820-
*
821818
* @dataProvider dataNotClassProperty
822819
*
823820
* @return void
824821
*/
825822
public function testNotClassPropertyException($identifier)
826823
{
824+
$msg = '$stackPtr is not a class member var';
825+
$exception = 'PHP_CodeSniffer\Exceptions\RuntimeException';
826+
827+
if (\method_exists($this, 'expectException') === true) {
828+
// PHPUnit 5+.
829+
$this->expectException($exception);
830+
$this->expectExceptionMessage($msg);
831+
} else {
832+
// PHPUnit 4.
833+
$this->setExpectedException($exception, $msg);
834+
}
835+
827836
$variable = $this->getTargetToken($identifier, T_VARIABLE);
828837
$result = self::$phpcsFile->getMemberProperties($variable);
829838

@@ -855,13 +864,22 @@ public function dataNotClassProperty()
855864
/**
856865
* Test receiving an expected exception when a non variable is passed.
857866
*
858-
* @expectedException PHP_CodeSniffer\Exceptions\RuntimeException
859-
* @expectedExceptionMessage $stackPtr must be of type T_VARIABLE
860-
*
861867
* @return void
862868
*/
863869
public function testNotAVariableException()
864870
{
871+
$msg = '$stackPtr must be of type T_VARIABLE';
872+
$exception = 'PHP_CodeSniffer\Exceptions\RuntimeException';
873+
874+
if (\method_exists($this, 'expectException') === true) {
875+
// PHPUnit 5+.
876+
$this->expectException($exception);
877+
$this->expectExceptionMessage($msg);
878+
} else {
879+
// PHPUnit 4.
880+
$this->setExpectedException($exception, $msg);
881+
}
882+
865883
$next = $this->getTargetToken('/* testNotAVariable */', T_RETURN);
866884
$result = self::$phpcsFile->getMemberProperties($next);
867885

0 commit comments

Comments
 (0)