-
Notifications
You must be signed in to change notification settings - Fork 9
/
TestResponseAssert.php
168 lines (141 loc) · 4.83 KB
/
TestResponseAssert.php
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
namespace Illuminate\Testing;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Arr;
use PHPUnit\Framework\ExpectationFailedException;
use ReflectionProperty;
/**
* @internal
*
* @mixin Assert
*/
class TestResponseAssert
{
/**
* Create a new TestResponse assertion helper.
*/
private function __construct(protected TestResponse $response)
{
//
}
/**
* Create a new TestResponse assertion helper.
*/
public static function withResponse(TestResponse $response): self
{
return new static($response);
}
/**
* Pass method calls to the Assert class and decorate the exception message.
*
* @param string $name
* @param array $arguments
* @return void
*
* @throws \PHPUnit\Framework\ExpectationFailedException
*/
public function __call($name, $arguments)
{
try {
Assert::$name(...$arguments);
} catch (ExpectationFailedException $e) {
throw $this->injectResponseContext($e);
}
}
/**
* Pass static method calls to the Assert class.
*
* @param string $name
* @param array $arguments
* @return void
*
* @throws \PHPUnit\Framework\ExpectationFailedException
*/
public static function __callStatic($name, $arguments)
{
Assert::$name(...$arguments);
}
/**
* Inject additional context from the response into the exception message.
*
* @param \PHPUnit\Framework\ExpectationFailedException $exception
* @return \PHPUnit\Framework\ExpectationFailedException
*/
protected function injectResponseContext($exception)
{
if ($lastException = $this->response->exceptions->last()) {
return $this->appendExceptionToException($lastException, $exception);
}
if ($this->response->baseResponse instanceof RedirectResponse) {
$session = $this->response->baseResponse->getSession();
if (! is_null($session) && $session->has('errors')) {
return $this->appendErrorsToException($session->get('errors')->all(), $exception);
}
}
if ($this->response->baseResponse->headers->get('Content-Type') === 'application/json') {
$testJson = new AssertableJsonString($this->response->getContent());
if (isset($testJson['errors'])) {
return $this->appendErrorsToException($testJson->json(), $exception, true);
}
}
return $exception;
}
/**
* Append an exception to the message of another exception.
*
* @param \Throwable $exceptionToAppend
* @param \PHPUnit\Framework\ExpectationFailedException $exception
* @return \PHPUnit\Framework\ExpectationFailedException
*/
protected function appendExceptionToException($exceptionToAppend, $exception)
{
$exceptionMessage = is_string($exceptionToAppend) ? $exceptionToAppend : $exceptionToAppend->getMessage();
$exceptionToAppend = (string) $exceptionToAppend;
$message = <<<"EOF"
The following exception occurred during the last request:
$exceptionToAppend
----------------------------------------------------------------------------------
$exceptionMessage
EOF;
return $this->appendMessageToException($message, $exception);
}
/**
* Append errors to an exception message.
*
* @param array $errors
* @param \PHPUnit\Framework\ExpectationFailedException $exception
* @param bool $json
* @return \PHPUnit\Framework\ExpectationFailedException
*/
protected function appendErrorsToException($errors, $exception, $json = false)
{
$errors = $json
? json_encode($errors, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)
: implode(PHP_EOL, Arr::flatten($errors));
// JSON error messages may already contain the errors, so we shouldn't duplicate them...
if (str_contains($exception->getMessage(), $errors)) {
return $exception;
}
$message = <<<"EOF"
The following errors occurred during the last request:
$errors
EOF;
return $this->appendMessageToException($message, $exception);
}
/**
* Append a message to an exception.
*
* @param string $message
* @param \PHPUnit\Framework\ExpectationFailedException $exception
* @return \PHPUnit\Framework\ExpectationFailedException
*/
protected function appendMessageToException($message, $exception)
{
$property = new ReflectionProperty($exception, 'message');
$property->setValue(
$exception,
$exception->getMessage().PHP_EOL.PHP_EOL.$message.PHP_EOL
);
return $exception;
}
}