Skip to content

Commit 0702f55

Browse files
authored
[9.x] Move reusable onNotSuccessfulTest functionality to TestResponse (#44827)
* [9.x] Move reusable `onNotSuccessfulTest` to `TestResponse` This allows it to be reusable on Testbench without having redundant code. Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> Signed-off-by: Mior Muhammad Zaki <[email protected]>
1 parent 912a409 commit 0702f55

File tree

2 files changed

+117
-110
lines changed

2 files changed

+117
-110
lines changed

src/Illuminate/Foundation/Testing/TestCase.php

Lines changed: 4 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,16 @@
66
use Illuminate\Console\Application as Artisan;
77
use Illuminate\Database\Eloquent\Model;
88
use Illuminate\Foundation\Bootstrap\HandleExceptions;
9-
use Illuminate\Http\RedirectResponse;
109
use Illuminate\Queue\Queue;
11-
use Illuminate\Support\Arr;
1210
use Illuminate\Support\Carbon;
1311
use Illuminate\Support\Facades\Facade;
1412
use Illuminate\Support\Facades\ParallelTesting;
1513
use Illuminate\Support\Str;
16-
use Illuminate\Testing\AssertableJsonString;
1714
use Illuminate\View\Component;
1815
use Mockery;
1916
use Mockery\Exception\InvalidCountException;
20-
use PHPUnit\Framework\ExpectationFailedException;
2117
use PHPUnit\Framework\TestCase as BaseTestCase;
2218
use PHPUnit\Util\Annotation\Registry;
23-
use ReflectionProperty;
2419
use Throwable;
2520

2621
abstract class TestCase extends BaseTestCase
@@ -301,111 +296,10 @@ protected function callBeforeApplicationDestroyedCallbacks()
301296
*/
302297
protected function onNotSuccessfulTest(Throwable $exception): void
303298
{
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)
407303
);
408-
409-
return $exception;
410304
}
411305
}

src/Illuminate/Testing/TestResponse.php

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Illuminate\Cookie\CookieValuePrefix;
1010
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
1111
use Illuminate\Database\Eloquent\Model;
12+
use Illuminate\Http\RedirectResponse;
1213
use Illuminate\Http\Request;
1314
use Illuminate\Support\Arr;
1415
use Illuminate\Support\Carbon;
@@ -21,6 +22,8 @@
2122
use Illuminate\Testing\Constraints\SeeInOrder;
2223
use Illuminate\Testing\Fluent\AssertableJson;
2324
use LogicException;
25+
use PHPUnit\Framework\ExpectationFailedException;
26+
use ReflectionProperty;
2427
use Symfony\Component\HttpFoundation\Cookie;
2528
use Symfony\Component\HttpFoundation\StreamedResponse;
2629

@@ -1549,6 +1552,116 @@ public function withExceptions(Collection $exceptions)
15491552
return $this;
15501553
}
15511554

1555+
/**
1556+
* This method is called when test method did not execute successfully.
1557+
*
1558+
* @param \Throwable $exception
1559+
* @return \Throwable
1560+
*/
1561+
public function transformNotSuccessfulException($exception)
1562+
{
1563+
if (! $exception instanceof ExpectationFailedException) {
1564+
return $exception;
1565+
}
1566+
1567+
if ($lastException = $this->exceptions->last()) {
1568+
return $this->appendExceptionToException($lastException, $exception);
1569+
}
1570+
1571+
if ($this->baseResponse instanceof RedirectResponse) {
1572+
$session = $this->baseResponse->getSession();
1573+
1574+
if (! is_null($session) && $session->has('errors')) {
1575+
return $this->appendErrorsToException($session->get('errors')->all(), $exception);
1576+
}
1577+
}
1578+
1579+
if ($this->baseResponse->headers->get('Content-Type') === 'application/json') {
1580+
$testJson = new AssertableJsonString($this->getContent());
1581+
1582+
if (isset($testJson['errors'])) {
1583+
return $this->appendErrorsToException($testJson->json(), $exception, true);
1584+
}
1585+
}
1586+
1587+
return $exception;
1588+
}
1589+
1590+
/**
1591+
* Append an exception to the message of another exception.
1592+
*
1593+
* @param \Throwable $exceptionToAppend
1594+
* @param \Throwable $exception
1595+
* @return \Throwable
1596+
*/
1597+
protected function appendExceptionToException($exceptionToAppend, $exception)
1598+
{
1599+
$exceptionMessage = $exceptionToAppend->getMessage();
1600+
1601+
$exceptionToAppend = (string) $exceptionToAppend;
1602+
1603+
$message = <<<"EOF"
1604+
The following exception occurred during the last request:
1605+
1606+
$exceptionToAppend
1607+
1608+
----------------------------------------------------------------------------------
1609+
1610+
$exceptionMessage
1611+
EOF;
1612+
1613+
return $this->appendMessageToException($message, $exception);
1614+
}
1615+
1616+
/**
1617+
* Append errors to an exception message.
1618+
*
1619+
* @param array $errors
1620+
* @param \Throwable $exception
1621+
* @param bool $json
1622+
* @return \Throwable
1623+
*/
1624+
protected function appendErrorsToException($errors, $exception, $json = false)
1625+
{
1626+
$errors = $json
1627+
? json_encode($errors, JSON_PRETTY_PRINT)
1628+
: implode(PHP_EOL, Arr::flatten($errors));
1629+
1630+
// JSON error messages may already contain the errors, so we shouldn't duplicate them...
1631+
if (str_contains($exception->getMessage(), $errors)) {
1632+
return $exception;
1633+
}
1634+
1635+
$message = <<<"EOF"
1636+
The following errors occurred during the last request:
1637+
1638+
$errors
1639+
EOF;
1640+
1641+
return $this->appendMessageToException($message, $exception);
1642+
}
1643+
1644+
/**
1645+
* Append a message to an exception.
1646+
*
1647+
* @param string $message
1648+
* @param \Throwable $exception
1649+
* @return \Throwable
1650+
*/
1651+
protected function appendMessageToException($message, $exception)
1652+
{
1653+
$property = new ReflectionProperty($exception, 'message');
1654+
1655+
$property->setAccessible(true);
1656+
1657+
$property->setValue(
1658+
$exception,
1659+
$exception->getMessage().PHP_EOL.PHP_EOL.$message.PHP_EOL
1660+
);
1661+
1662+
return $exception;
1663+
}
1664+
15521665
/**
15531666
* Dynamically access base response parameters.
15541667
*

0 commit comments

Comments
 (0)