Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests/AbstractMethodUnitTest: new expectRunTimeException() helper method #210

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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