Skip to content
This repository was archived by the owner on May 13, 2024. It is now read-only.

Commit c96ca7b

Browse files
authored
Merge pull request #2 from Potherca/feature/v0.6.1
Improvements to GetCompatibleExceptionName class
2 parents 4d4e619 + 722309d commit c96ca7b

13 files changed

+588
-39
lines changed

composer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
"Potherca\\PhpUnit\\": "src/"
1515
}
1616
},
17+
"autoload-dev": {
18+
"psr-4": {
19+
"Potherca\\PhpUnit\\": "tests/unit/"
20+
}
21+
},
1722
"config": {
1823
"sort-packages": true
1924
},

example/GetCompatibleExceptionNameExample.php

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,28 @@ class ExampleTest extends AbstractTestCase
1313
{
1414
use \Potherca\PhpUnit\Traits\GetCompatibleExceptionNameTrait;
1515

16-
public function testException()
16+
public function testArithmeticError()
1717
{
18-
// Please not that `\TypeError::class` is NOT used, as this will cause an error if `TypeError` does not exist.
19-
$exceptionName = $this->getCompatibleExceptionName('\TypeError');
18+
// Please note that `\ArithmeticError::class` is NOT used, as this will cause an error if `ArithmeticError` does not exist.
19+
$exceptionName = $this->getCompatibleExceptionName('\\ArithmeticError');
20+
21+
if (method_exists($this, 'expectExceptionMessage')) {
22+
/* PHPUnit ^5.2 | ^6.0 */
23+
$this->expectException($exceptionName);
24+
$this->expectExceptionMessage('Bit shift by negative number');
25+
} else {
26+
/* PHPUnit ^4.3 | =< 5.6 */
27+
$this->setExpectedExceptionRegExp($exceptionName, 'Bit shift by negative number');
28+
}
29+
30+
/** @noinspection PhpExpressionResultUnusedInspection */
31+
1 >> -1;
32+
}
33+
34+
public function testArgumentCountError()
35+
{
36+
// Please note that `\ArgumentCountError::class` is NOT used, as this will cause an error if `ArgumentCountError` does not exist.
37+
$exceptionName = $this->getCompatibleExceptionName('\\ArgumentCountError');
2038

2139
if (method_exists($this, 'expectExceptionMessageRegExp')) {
2240
/* PHPUnit ^5.2 | ^6.0 */
@@ -27,8 +45,43 @@ public function testException()
2745
$this->setExpectedExceptionRegExp($exceptionName, '/none given|0 passed/');
2846
}
2947

30-
$example = new Example();
48+
/** @noinspection PhpParamsInspection */
49+
new Example();
50+
}
51+
52+
public function testDivisionByZeroError()
53+
{
54+
// Please note that `\DivisionByZeroError::class` is NOT used, as this will cause an error if `DivisionByZeroError` does not exist.
55+
$exceptionName = $this->getCompatibleExceptionName('\\DivisionByZeroError');
56+
57+
if (method_exists($this, 'expectExceptionMessage')) {
58+
/* PHPUnit ^5.2 | ^6.0 */
59+
$this->expectException($exceptionName);
60+
$this->expectExceptionMessage('Division by zero');
61+
} else {
62+
/* PHPUnit ^4.3 | =< 5.6 */
63+
$this->setExpectedException($exceptionName, 'Division by zero');
64+
}
65+
66+
/** @noinspection PhpExpressionResultUnusedInspection */
67+
0 / 0;
68+
}
69+
70+
public function testTypeError()
71+
{
72+
// Please note that `\TypeError::class` is NOT used, as this will cause an error if `TypeError` does not exist.
73+
$exceptionName = $this->getCompatibleExceptionName('\\TypeError');
74+
75+
if (method_exists($this, 'expectExceptionMessageRegExp')) {
76+
/* PHPUnit ^5.2 | ^6.0 */
77+
$this->expectException($exceptionName);
78+
$this->expectExceptionMessageRegExp('/must be of the type array|must be an array/');
79+
} else {
80+
/* PHPUnit ^4.3 | =< 5.6 */
81+
$this->setExpectedExceptionRegExp($exceptionName, '/must be of the type array|must be an array/');
82+
}
3183

32-
var_dump($example);
84+
/** @noinspection PhpParamsInspection */
85+
new Example(false);
3386
}
3487
}

example/PHP5.3/GetCompatibleExceptionNameExample.php

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class ExampleTest extends AbstractTestCase
1414
/**
1515
* As PHP5.3 does not support traits, __call is (a)bused instead of the trait.
1616
*
17-
use \Potherca\PhpUnit\Traits\GetCompatibleExceptionNameTrait;
17+
use \Potherca\PhpUnit\Traits\GetCompatibleExceptionNameTrait;
1818
*
1919
* @param string $name
2020
* @param array $parameters
@@ -26,10 +26,28 @@ final public function __call($name, array $parameters)
2626
return \Potherca\PhpUnit\Shim\Util::traitShim($this, $name, $parameters);
2727
}
2828

29-
public function testException()
29+
public function testArithmeticError()
3030
{
31-
// Please not that `\TypeError::class` is NOT used, as this will cause an error if `TypeError` does not exist.
32-
$exceptionName = $this->getCompatibleExceptionName('\TypeError');
31+
// Please note that `\ArithmeticError::class` is NOT used, as this will cause an error if `ArithmeticError` does not exist.
32+
$exceptionName = $this->getCompatibleExceptionName('\\ArithmeticError');
33+
34+
if (method_exists($this, 'expectExceptionMessage')) {
35+
/* PHPUnit ^5.2 | ^6.0 */
36+
$this->expectException($exceptionName);
37+
$this->expectExceptionMessage('Bit shift by negative number');
38+
} else {
39+
/* PHPUnit ^4.3 | =< 5.6 */
40+
$this->setExpectedExceptionRegExp($exceptionName, 'Bit shift by negative number');
41+
}
42+
43+
/** @noinspection PhpExpressionResultUnusedInspection */
44+
1 >> -1;
45+
}
46+
47+
public function testArgumentCountError()
48+
{
49+
// Please note that `\ArgumentCountError::class` is NOT used, as this will cause an error if `ArgumentCountError` does not exist.
50+
$exceptionName = $this->getCompatibleExceptionName('\\ArgumentCountError');
3351

3452
if (method_exists($this, 'expectExceptionMessageRegExp')) {
3553
/* PHPUnit ^5.2 | ^6.0 */
@@ -40,8 +58,43 @@ public function testException()
4058
$this->setExpectedExceptionRegExp($exceptionName, '/none given|0 passed/');
4159
}
4260

43-
$example = new Example();
61+
/** @noinspection PhpParamsInspection */
62+
new Example();
63+
}
64+
65+
public function testDivisionByZeroError()
66+
{
67+
// Please note that `\DivisionByZeroError::class` is NOT used, as this will cause an error if `DivisionByZeroError` does not exist.
68+
$exceptionName = $this->getCompatibleExceptionName('\\DivisionByZeroError');
69+
70+
if (method_exists($this, 'expectExceptionMessage')) {
71+
/* PHPUnit ^5.2 | ^6.0 */
72+
$this->expectException($exceptionName);
73+
$this->expectExceptionMessage('Division by zero');
74+
} else {
75+
/* PHPUnit ^4.3 | =< 5.6 */
76+
$this->setExpectedException($exceptionName, 'Division by zero');
77+
}
78+
79+
/** @noinspection PhpExpressionResultUnusedInspection */
80+
0 / 0;
81+
}
82+
83+
public function testTypeError()
84+
{
85+
// Please note that `\TypeError::class` is NOT used, as this will cause an error if `TypeError` does not exist.
86+
$exceptionName = $this->getCompatibleExceptionName('\\TypeError');
87+
88+
if (method_exists($this, 'expectExceptionMessageRegExp')) {
89+
/* PHPUnit ^5.2 | ^6.0 */
90+
$this->expectException($exceptionName);
91+
$this->expectExceptionMessageRegExp('/must be of the type array|must be an array/');
92+
} else {
93+
/* PHPUnit ^4.3 | =< 5.6 */
94+
$this->setExpectedExceptionRegExp($exceptionName, '/must be of the type array|must be an array/');
95+
}
4496

45-
var_dump($example);
97+
/** @noinspection PhpParamsInspection */
98+
new Example(false);
4699
}
47100
}

src/Shim/AbstractTraitShim.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ final public function __construct($testcase)
4343
'Argument 1 passed to %s must be an instance of %s, %s given',
4444
array(
4545
__METHOD__,
46-
'"\PHPUnit_Framework_TestCase" or "\PHPUnit\Framework\TestCase"',
46+
'"\\PHPUnit_Framework_TestCase" or "\\PHPUnit\\Framework\\TestCase"',
4747
$type,
4848
)
4949
);

src/Shim/Compatibility/TestCase/PHPUnit_Framework_TestCase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
* This file makes sure that `\PHPUnit_Framework_TestCase` always exist.
1717
*/
1818
namespace {
19-
if (class_exists('\PHPUnit\Framework\TestCase') === true
20-
&& class_exists('\PHPUnit_Framework_TestCase') === false
19+
if (class_exists('\\PHPUnit\\Framework\\TestCase') === true
20+
&& class_exists('\\PHPUnit_Framework_TestCase') === false
2121
) {
2222
abstract class PHPUnit_Framework_TestCase extends \PHPUnit\Framework\TestCase {}
2323
}

src/Shim/Compatibility/TestCase/TestCase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
* This file makes sure that `\PHPUnit\Framework\TestCase` always exist.
1717
*/
1818
namespace PHPUnit\Framework {
19-
if (class_exists('\PHPUnit\Framework\TestCase') === false
20-
&& class_exists('\PHPUnit_Framework_TestCase') === true
19+
if (class_exists('\\PHPUnit\\Framework\\TestCase') === false
20+
&& class_exists('\\PHPUnit_Framework_TestCase') === true
2121
) {
2222
abstract class TestCase extends \PHPUnit_Framework_TestCase {}
2323
}

src/Shim/GetCompatibleExceptionName.php

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
use Potherca\PhpUnit\InvalidArgumentException;
66

77
/**
8-
* @TODO: Add support for `ArithmeticError`
9-
*
108
* @method expectExceptionMessage($message)
119
* @method fail($message)
1210
* @method markTestSkipped($message)
@@ -21,13 +19,15 @@ class GetCompatibleExceptionName extends AbstractTraitShim
2119
* @return string
2220
*
2321
* @throws InvalidArgumentException
24-
* @throws \PHPUnit_Framework_Exception | \PHPUnit\Framework\Exception
22+
*
23+
* @throws \PHPUnit_Framework_Exception|\PHPUnit\Framework\Exception
2524
* @throws \PHPUnit_Framework_AssertionFailedError|\PHPUnit\Framework\AssertionFailedError
2625
* @throws \PHPUnit_Framework_SkippedTestError|\PHPUnit\Framework\SkippedTestError
2726
*/
2827
final public function getCompatibleExceptionName($exceptionName)
2928
{
3029
$matchingExceptionName = '';
30+
$alternative = '';
3131

3232
try {
3333
$exceptionName = $this->getExistingClassName($exceptionName);
@@ -47,27 +47,27 @@ final public function getCompatibleExceptionName($exceptionName)
4747
} else {
4848
if ($exceptionName === 'ParseError') {
4949
$this->getTestcase()->markTestSkipped('Parse errors can not be caught in PHP5');
50+
} elseif ($exceptionName === 'ArithmeticError') {
51+
/* PHP 7.0 thrown when an error occurs while performing
52+
* mathematical operations. As I have not been able to find the
53+
* PHP5 equivalent, marking as skipped until a working example
54+
* is available
55+
*/
56+
$this->getTestcase()->markTestSkipped('There are no equivalent for Arithmetic errors in PHP5 ');
5057
} elseif ($exceptionName === 'ArgumentCountError') {
5158
// PHP 7.1 thrown when too few arguments are passed to a user-defined function or method.
52-
$alternative = '\\PHPUnit_Framework_Error_Warning';
53-
54-
$matchingExceptionName = '\\PHPUnit\\Framework\\Error\\Warning';
55-
if (class_exists($matchingExceptionName) === false) {
56-
$matchingExceptionName = $alternative;
57-
}
59+
// PHP 7.0 throws a TypeError with message 'none given'
60+
$matchingExceptionName = $this->getCompatibleExceptionName('\\TypeError');
5861
} else {
59-
$exceptionName = $this->getMatchingExceptionName($exceptionName);
62+
$matchingExceptionName = $this->getMatchingExceptionName($exceptionName);
6063

61-
$alternative = '';
62-
if ($exceptionName === '\\PHPUnit_Framework_Error') {
63-
$alternative = 'PHPUnit\\Framework\\Error\\Error';
64+
if ($matchingExceptionName === '\\PHPUnit_Framework_Error') {
65+
$alternative = '\\PHPUnit\\Framework\\Error\\Error';
6466
}
65-
66-
$matchingExceptionName = $this->getExistingClassName($exceptionName, $alternative);
6767
}
6868
}
6969

70-
return $matchingExceptionName;
70+
return $this->getExistingClassName($matchingExceptionName, $alternative);
7171
}
7272

7373
////////////////////////////// UTILITY METHODS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\
@@ -79,17 +79,23 @@ final public function getCompatibleExceptionName($exceptionName)
7979
*/
8080
private function isPhpUnitExceptionNeeded($exceptionName)
8181
{
82-
return class_exists('\\' . $exceptionName) === false
83-
/* @NOTE: The line below validates that the Exception does not extend the PHP7 "Throwable" interface */
84-
|| class_implements('\\' . $exceptionName) === array();
82+
$exists = class_exists('\\' . $exceptionName);
83+
84+
$extends = false;
85+
if ($exists === true) {
86+
/* @NOTE: This validates the Exception does not extend the PHP7 "Throwable" interface */
87+
$extends = class_implements('\\' . $exceptionName) !== array();
88+
}
89+
90+
return $exists === false && $extends === false;
8591
}
8692

8793
/**
8894
* @param $exceptionName
8995
*
9096
* @return string
9197
*
92-
* @throws \PHPUnit\Framework_AssertionFailedError | \PHPUnit\Framework\AssertionFailedError
98+
* @throws \PHPUnit_Framework_AssertionFailedError | \PHPUnit\Framework\AssertionFailedError
9399
*/
94100
private function getMatchingExceptionName($exceptionName)
95101
{

src/Shim/Util.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ final public static function traitShim($testcase, $functionName, array $paramete
5050
'Argument 1 passed to %s must be an instance of %s, %s given',
5151
array(
5252
__METHOD__,
53-
'"\PHPUnit_Framework_TestCase" or "\PHPUnit\Framework\TestCase"',
53+
'"\\PHPUnit_Framework_TestCase" or "\\PHPUnit\\Framework\\TestCase"',
5454
$type,
5555
)
5656
);
@@ -149,7 +149,7 @@ final public static function createShimForTrait($testCase, $methodName, $traitNa
149149
}
150150

151151
$implements = class_implements($shimClass);
152-
$interface = 'Potherca\PhpUnit\Shim\TraitShimInterface';
152+
$interface = 'Potherca\\PhpUnit\\Shim\\TraitShimInterface';
153153

154154
if (in_array($interface, $implements, true) === false) {
155155
$message = vsprintf(

src/Traits/GetCompatibleExceptionNameTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
* public function testException()
4848
* {
4949
* // Please not that `\TypeError::class` is NOT used, as this will cause an error if `TypeError` does not exist.
50-
* $exceptionName = $this->getCompatibleExceptionName('\TypeError');
50+
* $exceptionName = $this->getCompatibleExceptionName('\\TypeError');
5151
*
5252
* $this->expectException($exceptionName);
5353
* $this->expectExceptionMessageRegExp('/none given|0 passed/');

0 commit comments

Comments
 (0)