|
6 | 6 | use Illuminate\Console\Application as Artisan;
|
7 | 7 | use Illuminate\Database\Eloquent\Model;
|
8 | 8 | use Illuminate\Foundation\Bootstrap\HandleExceptions;
|
9 |
| -use Illuminate\Http\RedirectResponse; |
10 | 9 | use Illuminate\Queue\Queue;
|
11 |
| -use Illuminate\Support\Arr; |
12 | 10 | use Illuminate\Support\Carbon;
|
13 | 11 | use Illuminate\Support\Facades\Facade;
|
14 | 12 | use Illuminate\Support\Facades\ParallelTesting;
|
15 | 13 | use Illuminate\Support\Str;
|
16 |
| -use Illuminate\Testing\AssertableJsonString; |
17 | 14 | use Illuminate\View\Component;
|
18 | 15 | use Mockery;
|
19 | 16 | use Mockery\Exception\InvalidCountException;
|
20 |
| -use PHPUnit\Framework\ExpectationFailedException; |
21 | 17 | use PHPUnit\Framework\TestCase as BaseTestCase;
|
22 | 18 | use PHPUnit\Util\Annotation\Registry;
|
23 |
| -use ReflectionProperty; |
24 | 19 | use Throwable;
|
25 | 20 |
|
26 | 21 | abstract class TestCase extends BaseTestCase
|
@@ -301,111 +296,10 @@ protected function callBeforeApplicationDestroyedCallbacks()
|
301 | 296 | */
|
302 | 297 | protected function onNotSuccessfulTest(Throwable $exception): void
|
303 | 298 | {
|
304 |
| - if (! $exception instanceof ExpectationFailedException || is_null(static::$latestResponse)) { |
305 |
| - parent::onNotSuccessfulTest($exception); |
306 |
| - } |
307 |
| - |
308 |
| - if ($lastException = static::$latestResponse->exceptions->last()) { |
309 |
| - parent::onNotSuccessfulTest($this->appendExceptionToException($lastException, $exception)); |
310 |
| - |
311 |
| - return; |
312 |
| - } |
313 |
| - |
314 |
| - if (static::$latestResponse->baseResponse instanceof RedirectResponse) { |
315 |
| - $session = static::$latestResponse->baseResponse->getSession(); |
316 |
| - |
317 |
| - if (! is_null($session) && $session->has('errors')) { |
318 |
| - parent::onNotSuccessfulTest($this->appendErrorsToException($session->get('errors')->all(), $exception)); |
319 |
| - |
320 |
| - return; |
321 |
| - } |
322 |
| - } |
323 |
| - |
324 |
| - if (static::$latestResponse->baseResponse->headers->get('Content-Type') === 'application/json') { |
325 |
| - $testJson = new AssertableJsonString(static::$latestResponse->getContent()); |
326 |
| - |
327 |
| - if (isset($testJson['errors'])) { |
328 |
| - parent::onNotSuccessfulTest($this->appendErrorsToException($testJson->json(), $exception, true)); |
329 |
| - |
330 |
| - return; |
331 |
| - } |
332 |
| - } |
333 |
| - |
334 |
| - parent::onNotSuccessfulTest($exception); |
335 |
| - } |
336 |
| - |
337 |
| - /** |
338 |
| - * Append an exception to the message of another exception. |
339 |
| - * |
340 |
| - * @param \Throwable $exceptionToAppend |
341 |
| - * @param \Throwable $exception |
342 |
| - * @return \Throwable |
343 |
| - */ |
344 |
| - protected function appendExceptionToException($exceptionToAppend, $exception) |
345 |
| - { |
346 |
| - $exceptionMessage = $exceptionToAppend->getMessage(); |
347 |
| - |
348 |
| - $exceptionToAppend = (string) $exceptionToAppend; |
349 |
| - |
350 |
| - $message = <<<"EOF" |
351 |
| - The following exception occurred during the last request: |
352 |
| -
|
353 |
| - $exceptionToAppend |
354 |
| -
|
355 |
| - ---------------------------------------------------------------------------------- |
356 |
| -
|
357 |
| - $exceptionMessage |
358 |
| - EOF; |
359 |
| - |
360 |
| - return $this->appendMessageToException($message, $exception); |
361 |
| - } |
362 |
| - |
363 |
| - /** |
364 |
| - * Append errors to an exception message. |
365 |
| - * |
366 |
| - * @param array $errors |
367 |
| - * @param \Throwable $exception |
368 |
| - * @param bool $json |
369 |
| - * @return \Throwable |
370 |
| - */ |
371 |
| - protected function appendErrorsToException($errors, $exception, $json = false) |
372 |
| - { |
373 |
| - $errors = $json |
374 |
| - ? json_encode($errors, JSON_PRETTY_PRINT) |
375 |
| - : implode(PHP_EOL, Arr::flatten($errors)); |
376 |
| - |
377 |
| - // JSON error messages may already contain the errors, so we shouldn't duplicate them... |
378 |
| - if (str_contains($exception->getMessage(), $errors)) { |
379 |
| - return $exception; |
380 |
| - } |
381 |
| - |
382 |
| - $message = <<<"EOF" |
383 |
| - The following errors occurred during the last request: |
384 |
| -
|
385 |
| - $errors |
386 |
| - EOF; |
387 |
| - |
388 |
| - return $this->appendMessageToException($message, $exception); |
389 |
| - } |
390 |
| - |
391 |
| - /** |
392 |
| - * Append a message to an exception. |
393 |
| - * |
394 |
| - * @param string $message |
395 |
| - * @param \Throwable $exception |
396 |
| - * @return \Throwable |
397 |
| - */ |
398 |
| - protected function appendMessageToException($message, $exception) |
399 |
| - { |
400 |
| - $property = new ReflectionProperty($exception, 'message'); |
401 |
| - |
402 |
| - $property->setAccessible(true); |
403 |
| - |
404 |
| - $property->setValue( |
405 |
| - $exception, |
406 |
| - $exception->getMessage().PHP_EOL.PHP_EOL.$message.PHP_EOL |
| 299 | + parent::onNotSuccessfulTest( |
| 300 | + is_null(static::$latestResponse) |
| 301 | + ? $exception |
| 302 | + : static::$latestResponse->transformNotSuccessfulException($exception) |
407 | 303 | );
|
408 |
| - |
409 |
| - return $exception; |
410 | 304 | }
|
411 | 305 | }
|
0 commit comments