-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHandlesOwnErrorsTestCases.php
More file actions
89 lines (82 loc) · 2.78 KB
/
HandlesOwnErrorsTestCases.php
File metadata and controls
89 lines (82 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
declare(strict_types=1);
namespace Firehed\API\Traits;
use Firehed\API\Interfaces\HandlesOwnErrorsInterface;
use Psr\Http\Message\ResponseInterface;
use Throwable;
trait HandlesOwnErrorsTestCases
{
private $handleExceptionMayRethrow = false;
abstract protected function getEndpoint(): HandlesOwnErrorsInterface;
public function setAllowHandleExceptionToRethrow(bool $allowed)
{
$this->handleExceptionMayRethrow = $allowed;
}
/**
* @covers ::handleException
* @dataProvider exceptionsToHandle
*/
public function testHandleException(Throwable $e)
{
try {
$response = $this->getEndpoint()->handleException($e);
$this->assertInstanceOf(
ResponseInterface::class,
$response,
'handleException() did not return a PSR7 response'
);
} catch (Throwable $thrown) {
if ($this->handleExceptionMayRethrow) {
$this->assertSame($e, $thrown, 'A different exception was thrown');
} else {
throw $thrown;
}
}
}
/**
* Data Provider for testHandleException. To test additional exceptons,
* alias this method during import and extend in the using class; i.e.:
*
* ```php
* class MyTest extends PHPUnit\Framework\TestCase {
* use Firehed\API\EndpointTestTrait {
* exceptionsToTest as baseExceptions;
* }
* public function exceptionsToTest() {
* $cases = $this->baseExceptions();
* $cases[] = [new SomeOtherException()];
* return $cases;
* }
* }
* ```
*
* @return array<array<Exception>>
*/
public static function exceptionsToHandle(): array
{
return [
[new \Exception()],
[new \ErrorException()],
[new \LogicException()],
[new \BadFunctionCallException()],
[new \BadMethodCallException()],
[new \DomainException()],
[new \InvalidArgumentException()],
[new \LengthException()],
[new \OutOfRangeException()],
[new \RuntimeException()],
[new \OutOfBoundsException()],
[new \OverflowException()],
[new \RangeException()],
[new \UnderflowException()],
[new \UnexpectedValueException()],
// PHP7: Add new Error exceptions
[new \Error()],
[new \ArithmeticError()],
[new \AssertionError()],
[new \DivisionByZeroError()],
[new \ParseError()],
[new \TypeError()],
];
}
}