Skip to content

Commit

Permalink
Merge pull request #210 from PHPCSStandards/feature/abstractmethoduni…
Browse files Browse the repository at this point in the history
…ttest-new-expectruntimeexception-method

Tests/AbstractMethodUnitTest: new `expectRunTimeException()` helper method
  • Loading branch information
jrfnl authored Jan 2, 2024
2 parents eb88812 + 8472309 commit a6bed33
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 33 deletions.
24 changes: 24 additions & 0 deletions tests/Core/AbstractMethodUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 1 addition & 11 deletions tests/Core/File/GetClassPropertiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
24 changes: 2 additions & 22 deletions tests/Core/File/GetMemberPropertiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit a6bed33

Please sign in to comment.