1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cerbero\OctaneTestbench; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Response; |
6
|
|
|
use Illuminate\Testing\Assert; |
7
|
|
|
use Illuminate\Testing\Fluent\Concerns\Debugging; |
8
|
|
|
use Illuminate\Testing\TestResponse; |
9
|
|
|
use PHPUnit\Framework\TestCase; |
|
|
|
|
10
|
|
|
use ReflectionMethod; |
11
|
|
|
use Symfony\Component\HttpFoundation\Response as SymfonyResponse; |
12
|
|
|
use Throwable; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* The response test case. |
16
|
|
|
* |
17
|
|
|
*/ |
18
|
|
|
class ResponseTestCase extends TestResponse |
19
|
|
|
{ |
20
|
|
|
use Debugging; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The exception thrown in the response. |
24
|
|
|
* |
25
|
|
|
* @var Throwable |
26
|
|
|
*/ |
27
|
|
|
public $exception; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Instantiate the class. |
31
|
|
|
* |
32
|
|
|
* @param TestCase $testCase |
33
|
|
|
* @param SymfonyResponse|Throwable $response |
34
|
|
|
*/ |
35
|
17 |
|
public function __construct(protected TestCase $testCase, SymfonyResponse|Throwable $response) |
36
|
|
|
{ |
37
|
17 |
|
if ($response instanceof Throwable) { |
38
|
4 |
|
$this->exception = $response; |
39
|
|
|
} else { |
40
|
13 |
|
parent::__construct($this->toIlluminateResponse($response)); |
41
|
|
|
} |
42
|
17 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Turn the given Symfony response into an Illuminate response |
46
|
|
|
* |
47
|
|
|
* @param SymfonyResponse $response |
48
|
|
|
* @return Response |
49
|
|
|
*/ |
50
|
13 |
|
protected function toIlluminateResponse(SymfonyResponse $response): Response |
51
|
|
|
{ |
52
|
13 |
|
if ($response instanceof Response) { |
53
|
4 |
|
return $response; |
54
|
|
|
} |
55
|
|
|
|
56
|
11 |
|
return new Response( |
57
|
11 |
|
$response->getContent(), |
58
|
11 |
|
$response->getStatusCode(), |
59
|
11 |
|
$response->headers->allPreserveCase(), |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Assert that the thrown exception matches the given exception |
65
|
|
|
* |
66
|
|
|
* @param Throwable|string $exception |
67
|
|
|
* @return static |
68
|
|
|
*/ |
69
|
3 |
|
public function assertException(Throwable|string $exception): static |
70
|
|
|
{ |
71
|
3 |
|
$class = is_string($exception) ? $exception : $exception::class; |
72
|
|
|
|
73
|
3 |
|
Assert::assertInstanceOf($class, $this->exception); |
74
|
|
|
|
75
|
3 |
|
return is_string($exception) ? $this : $this->assertExceptionMessage($exception->getMessage()); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Assert that the thrown exception message matches the given message |
80
|
|
|
* |
81
|
|
|
* @param string $message |
82
|
|
|
* @return static |
83
|
|
|
*/ |
84
|
2 |
|
public function assertExceptionMessage(string $message): static |
85
|
|
|
{ |
86
|
2 |
|
Assert::assertSame($message, $this->exception->getMessage()); |
87
|
|
|
|
88
|
2 |
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Retrieve the response to inspect. |
93
|
|
|
* |
94
|
|
|
* @param string|null $key |
95
|
|
|
* @return mixed |
96
|
|
|
*/ |
97
|
2 |
|
protected function prop(string $key = null) |
98
|
|
|
{ |
99
|
2 |
|
$target = $this->exception ?: $this->baseResponse; |
100
|
|
|
|
101
|
2 |
|
return data_get($target, $key); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Handle dynamic calls into macros or pass missing methods to the response or test case. |
106
|
|
|
* |
107
|
|
|
* @param string $method |
108
|
|
|
* @param array $args |
109
|
|
|
* @return mixed |
110
|
|
|
*/ |
111
|
10 |
|
public function __call($method, $args) |
112
|
|
|
{ |
113
|
10 |
|
if (static::hasMacro($method)) { |
114
|
1 |
|
parent::__call($method, $args); |
115
|
9 |
|
} elseif ($this->baseResponse && method_exists($this->baseResponse, $method)) { |
116
|
8 |
|
return $this->baseResponse->$method(...$args); |
117
|
|
|
} else { |
118
|
2 |
|
$reflection = new ReflectionMethod($this->testCase, $method); |
119
|
2 |
|
$reflection->setAccessible(true); |
120
|
2 |
|
$reflection->invokeArgs($this->testCase, $args); |
121
|
|
|
} |
122
|
|
|
|
123
|
3 |
|
return $this; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
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: