Skip to content

Commit a6bed33

Browse files
authored
Merge pull request #210 from PHPCSStandards/feature/abstractmethodunittest-new-expectruntimeexception-method
Tests/AbstractMethodUnitTest: new `expectRunTimeException()` helper method
2 parents eb88812 + 8472309 commit a6bed33

File tree

3 files changed

+27
-33
lines changed

3 files changed

+27
-33
lines changed

tests/Core/AbstractMethodUnitTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,4 +179,28 @@ public function getTargetToken($commentString, $tokenType, $tokenContent=null)
179179
}//end getTargetToken()
180180

181181

182+
/**
183+
* Helper method to tell PHPUnit to expect a PHPCS RuntimeException in a PHPUnit cross-version
184+
* compatible manner.
185+
*
186+
* @param string $message The expected exception message.
187+
*
188+
* @return void
189+
*/
190+
public function expectRunTimeException($message)
191+
{
192+
$exception = 'PHP_CodeSniffer\Exceptions\RuntimeException';
193+
194+
if (method_exists($this, 'expectException') === true) {
195+
// PHPUnit 5+.
196+
$this->expectException($exception);
197+
$this->expectExceptionMessage($message);
198+
} else {
199+
// PHPUnit 4.
200+
$this->setExpectedException($exception, $message);
201+
}
202+
203+
}//end expectRunTimeException()
204+
205+
182206
}//end class

tests/Core/File/GetClassPropertiesTest.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,7 @@ class GetClassPropertiesTest extends AbstractMethodUnitTest
3232
*/
3333
public function testNotAClassException($testMarker, $tokenType)
3434
{
35-
$msg = '$stackPtr must be of type T_CLASS';
36-
$exception = 'PHP_CodeSniffer\Exceptions\RuntimeException';
37-
38-
if (\method_exists($this, 'expectException') === true) {
39-
// PHPUnit 5+.
40-
$this->expectException($exception);
41-
$this->expectExceptionMessage($msg);
42-
} else {
43-
// PHPUnit 4.
44-
$this->setExpectedException($exception, $msg);
45-
}
35+
$this->expectRunTimeException('$stackPtr must be of type T_CLASS');
4636

4737
$target = $this->getTargetToken($testMarker, $tokenType);
4838
self::$phpcsFile->getClassProperties($target);

tests/Core/File/GetMemberPropertiesTest.php

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -870,17 +870,7 @@ public function dataGetMemberProperties()
870870
*/
871871
public function testNotClassPropertyException($identifier)
872872
{
873-
$msg = '$stackPtr is not a class member var';
874-
$exception = 'PHP_CodeSniffer\Exceptions\RuntimeException';
875-
876-
if (\method_exists($this, 'expectException') === true) {
877-
// PHPUnit 5+.
878-
$this->expectException($exception);
879-
$this->expectExceptionMessage($msg);
880-
} else {
881-
// PHPUnit 4.
882-
$this->setExpectedException($exception, $msg);
883-
}
873+
$this->expectRunTimeException('$stackPtr is not a class member var');
884874

885875
$variable = $this->getTargetToken($identifier, T_VARIABLE);
886876
$result = self::$phpcsFile->getMemberProperties($variable);
@@ -917,17 +907,7 @@ public function dataNotClassProperty()
917907
*/
918908
public function testNotAVariableException()
919909
{
920-
$msg = '$stackPtr must be of type T_VARIABLE';
921-
$exception = 'PHP_CodeSniffer\Exceptions\RuntimeException';
922-
923-
if (\method_exists($this, 'expectException') === true) {
924-
// PHPUnit 5+.
925-
$this->expectException($exception);
926-
$this->expectExceptionMessage($msg);
927-
} else {
928-
// PHPUnit 4.
929-
$this->setExpectedException($exception, $msg);
930-
}
910+
$this->expectRunTimeException('$stackPtr must be of type T_VARIABLE');
931911

932912
$next = $this->getTargetToken('/* testNotAVariable */', T_RETURN);
933913
$result = self::$phpcsFile->getMemberProperties($next);

0 commit comments

Comments
 (0)