1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cerbero\OctaneTestbench; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Response; |
6
|
|
|
use Illuminate\Testing\Assert; |
7
|
|
|
use Illuminate\Testing\TestResponse; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
|
|
|
|
9
|
|
|
use ReflectionMethod; |
10
|
|
|
use Symfony\Component\HttpFoundation\Response as SymfonyResponse; |
11
|
|
|
use Throwable; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* The response test case. |
15
|
|
|
* |
16
|
|
|
*/ |
17
|
|
|
class ResponseTestCase extends TestResponse |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* The exception thrown in the response. |
21
|
|
|
* |
22
|
|
|
* @var Throwable |
23
|
|
|
*/ |
24
|
|
|
public $exception; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Instantiate the class. |
28
|
|
|
* |
29
|
|
|
* @param TestCase $testCase |
30
|
|
|
* @param SymfonyResponse|Throwable $response |
31
|
|
|
*/ |
32
|
15 |
|
public function __construct(protected TestCase $testCase, SymfonyResponse|Throwable $response) |
33
|
|
|
{ |
34
|
15 |
|
if ($response instanceof Throwable) { |
35
|
3 |
|
$this->exception = $response; |
36
|
|
|
} else { |
37
|
12 |
|
parent::__construct($this->toIlluminateResponse($response)); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Turn the given Symfony response into an Illuminate response |
43
|
|
|
* |
44
|
|
|
* @param SymfonyResponse $response |
45
|
|
|
* @return Response |
46
|
|
|
*/ |
47
|
12 |
|
protected function toIlluminateResponse(SymfonyResponse $response): Response |
48
|
|
|
{ |
49
|
12 |
|
if ($response instanceof Response) { |
50
|
3 |
|
return $response; |
51
|
|
|
} |
52
|
|
|
|
53
|
11 |
|
return new Response( |
54
|
11 |
|
$response->getContent(), |
55
|
11 |
|
$response->getStatusCode(), |
56
|
11 |
|
$response->headers->allPreserveCase(), |
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Assert that the thrown exception matches the given exception |
62
|
|
|
* |
63
|
|
|
* @param Throwable|string $exception |
64
|
|
|
* @return static |
65
|
|
|
*/ |
66
|
3 |
|
public function assertException(Throwable|string $exception): static |
67
|
|
|
{ |
68
|
3 |
|
$class = is_string($exception) ? $exception : $exception::class; |
69
|
|
|
|
70
|
3 |
|
Assert::assertInstanceOf($class, $this->exception); |
71
|
|
|
|
72
|
3 |
|
return is_string($exception) ? $this : $this->assertExceptionMessage($exception->getMessage()); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Assert that the thrown exception message matches the given message |
77
|
|
|
* |
78
|
|
|
* @param string $message |
79
|
|
|
* @return static |
80
|
|
|
*/ |
81
|
2 |
|
public function assertExceptionMessage(string $message): static |
82
|
|
|
{ |
83
|
2 |
|
Assert::assertSame($message, $this->exception->getMessage()); |
84
|
|
|
|
85
|
2 |
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Handle dynamic calls into macros or pass missing methods to the response or test case. |
90
|
|
|
* |
91
|
|
|
* @param string $method |
92
|
|
|
* @param array $args |
93
|
|
|
* @return mixed |
94
|
|
|
*/ |
95
|
10 |
|
public function __call($method, $args) |
96
|
|
|
{ |
97
|
10 |
|
if (static::hasMacro($method)) { |
98
|
1 |
|
parent::__call($method, $args); |
99
|
9 |
|
} elseif ($this->baseResponse && method_exists($this->baseResponse, $method)) { |
100
|
8 |
|
return $this->baseResponse->$method(...$args); |
101
|
|
|
} else { |
102
|
2 |
|
$reflection = new ReflectionMethod($this->testCase, $method); |
103
|
2 |
|
$reflection->setAccessible(true); |
104
|
2 |
|
$reflection->invokeArgs($this->testCase, $args); |
105
|
|
|
} |
106
|
|
|
|
107
|
3 |
|
return $this; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: