diff --git a/tests/Core/AbstractMethodUnitTest.php b/tests/Core/AbstractMethodUnitTest.php index 21216fb684..ced92062f6 100644 --- a/tests/Core/AbstractMethodUnitTest.php +++ b/tests/Core/AbstractMethodUnitTest.php @@ -179,4 +179,28 @@ public function getTargetToken($commentString, $tokenType, $tokenContent=null) }//end getTargetToken() + /** + * Helper method to tell PHPUnit to expect a PHPCS RuntimeException in a PHPUnit cross-version + * compatible manner. + * + * @param string $message The expected exception message. + * + * @return void + */ + public function expectRunTimeException($message) + { + $exception = 'PHP_CodeSniffer\Exceptions\RuntimeException'; + + if (method_exists($this, 'expectException') === true) { + // PHPUnit 5+. + $this->expectException($exception); + $this->expectExceptionMessage($message); + } else { + // PHPUnit 4. + $this->setExpectedException($exception, $message); + } + + }//end expectRunTimeException() + + }//end class diff --git a/tests/Core/File/GetClassPropertiesTest.php b/tests/Core/File/GetClassPropertiesTest.php index d7dfce8585..17d5216040 100644 --- a/tests/Core/File/GetClassPropertiesTest.php +++ b/tests/Core/File/GetClassPropertiesTest.php @@ -32,17 +32,7 @@ class GetClassPropertiesTest extends AbstractMethodUnitTest */ public function testNotAClassException($testMarker, $tokenType) { - $msg = '$stackPtr must be of type T_CLASS'; - $exception = 'PHP_CodeSniffer\Exceptions\RuntimeException'; - - if (\method_exists($this, 'expectException') === true) { - // PHPUnit 5+. - $this->expectException($exception); - $this->expectExceptionMessage($msg); - } else { - // PHPUnit 4. - $this->setExpectedException($exception, $msg); - } + $this->expectRunTimeException('$stackPtr must be of type T_CLASS'); $target = $this->getTargetToken($testMarker, $tokenType); self::$phpcsFile->getClassProperties($target); diff --git a/tests/Core/File/GetMemberPropertiesTest.php b/tests/Core/File/GetMemberPropertiesTest.php index 4ef08b168b..36bf4d7d3e 100644 --- a/tests/Core/File/GetMemberPropertiesTest.php +++ b/tests/Core/File/GetMemberPropertiesTest.php @@ -870,17 +870,7 @@ public function dataGetMemberProperties() */ public function testNotClassPropertyException($identifier) { - $msg = '$stackPtr is not a class member var'; - $exception = 'PHP_CodeSniffer\Exceptions\RuntimeException'; - - if (\method_exists($this, 'expectException') === true) { - // PHPUnit 5+. - $this->expectException($exception); - $this->expectExceptionMessage($msg); - } else { - // PHPUnit 4. - $this->setExpectedException($exception, $msg); - } + $this->expectRunTimeException('$stackPtr is not a class member var'); $variable = $this->getTargetToken($identifier, T_VARIABLE); $result = self::$phpcsFile->getMemberProperties($variable); @@ -917,17 +907,7 @@ public function dataNotClassProperty() */ public function testNotAVariableException() { - $msg = '$stackPtr must be of type T_VARIABLE'; - $exception = 'PHP_CodeSniffer\Exceptions\RuntimeException'; - - if (\method_exists($this, 'expectException') === true) { - // PHPUnit 5+. - $this->expectException($exception); - $this->expectExceptionMessage($msg); - } else { - // PHPUnit 4. - $this->setExpectedException($exception, $msg); - } + $this->expectRunTimeException('$stackPtr must be of type T_VARIABLE'); $next = $this->getTargetToken('/* testNotAVariable */', T_RETURN); $result = self::$phpcsFile->getMemberProperties($next);