Skip to content

Commit 8fefd80

Browse files
authored
[9.x] Fixes memory leak on TestCase::$latestResponse (#44306)
* Fixes memory leak on `$latestResponse` * Fixes tests
1 parent 9a3ad1f commit 8fefd80

File tree

3 files changed

+30
-15
lines changed

3 files changed

+30
-15
lines changed

src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ trait MakesHttpRequests
6464
protected $withCredentials = false;
6565

6666
/**
67-
* The latest test response.
67+
* The latest test response (if any).
6868
*
6969
* @var \Illuminate\Testing\TestResponse|null
7070
*/
71-
public $latestResponse;
71+
public static $latestResponse;
7272

7373
/**
7474
* Define additional headers to be sent with the request.
@@ -551,7 +551,7 @@ public function call($method, $uri, $parameters = [], $cookies = [], $files = []
551551
$response = $this->followRedirects($response);
552552
}
553553

554-
return $this->latestResponse = $this->createTestResponse($response);
554+
return static::$latestResponse = $this->createTestResponse($response);
555555
}
556556

557557
/**

src/Illuminate/Foundation/Testing/TestCase.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ abstract public function createApplication();
8686
*/
8787
protected function setUp(): void
8888
{
89-
$this->latestResponse = null;
89+
static::$latestResponse = null;
9090

9191
Facade::clearResolvedInstances();
9292

@@ -226,6 +226,16 @@ protected function tearDown(): void
226226
}
227227
}
228228

229+
/**
230+
* Clean up the testing environment before the next test case.
231+
*
232+
* @return void
233+
*/
234+
public static function tearDownAfterClass(): void
235+
{
236+
static::$latestResponse = null;
237+
}
238+
229239
/**
230240
* Register a callback to be run after the application is created.
231241
*
@@ -278,18 +288,18 @@ protected function callBeforeApplicationDestroyedCallbacks()
278288
*/
279289
protected function onNotSuccessfulTest(Throwable $exception): void
280290
{
281-
if (! $exception instanceof ExpectationFailedException || is_null($this->latestResponse)) {
291+
if (! $exception instanceof ExpectationFailedException || is_null(static::$latestResponse)) {
282292
parent::onNotSuccessfulTest($exception);
283293
}
284294

285-
if ($lastException = $this->latestResponse->exceptions->last()) {
295+
if ($lastException = static::$latestResponse->exceptions->last()) {
286296
parent::onNotSuccessfulTest($this->appendExceptionToException($lastException, $exception));
287297

288298
return;
289299
}
290300

291-
if ($this->latestResponse->baseResponse instanceof RedirectResponse) {
292-
$session = $this->latestResponse->baseResponse->getSession();
301+
if (static::$latestResponse->baseResponse instanceof RedirectResponse) {
302+
$session = static::$latestResponse->baseResponse->getSession();
293303

294304
if (! is_null($session) && $session->has('errors')) {
295305
parent::onNotSuccessfulTest($this->appendErrorsToException($session->get('errors')->all(), $exception));
@@ -298,8 +308,8 @@ protected function onNotSuccessfulTest(Throwable $exception): void
298308
}
299309
}
300310

301-
if ($this->latestResponse->baseResponse->headers->get('Content-Type') === 'application/json') {
302-
$testJson = new AssertableJsonString($this->latestResponse->getContent());
311+
if (static::$latestResponse->baseResponse->headers->get('Content-Type') === 'application/json') {
312+
$testJson = new AssertableJsonString(static::$latestResponse->getContent());
303313

304314
if (isset($testJson['errors'])) {
305315
parent::onNotSuccessfulTest($this->appendErrorsToException($testJson->json(), $exception, true));

tests/Foundation/Testing/TestCaseTest.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class TestCaseTest extends BaseTestCase
1717
public function test_it_includes_response_exceptions_on_test_failures()
1818
{
1919
$testCase = new ExampleTestCase();
20-
$testCase->latestResponse = TestResponse::fromBaseResponse(new Response())
20+
$testCase::$latestResponse = TestResponse::fromBaseResponse(new Response())
2121
->withExceptions(collect([new Exception('Unexpected exception.')]));
2222

2323
$this->expectException(ExpectationFailedException::class);
@@ -29,7 +29,7 @@ public function test_it_includes_response_exceptions_on_test_failures()
2929
public function test_it_includes_validation_errors_on_test_failures()
3030
{
3131
$testCase = new ExampleTestCase();
32-
$testCase->latestResponse = TestResponse::fromBaseResponse(
32+
$testCase::$latestResponse = TestResponse::fromBaseResponse(
3333
tap(new RedirectResponse('/'))
3434
->setSession(new Store('test-session', new NullSessionHandler()))
3535
->withErrors([
@@ -45,7 +45,7 @@ public function test_it_includes_validation_errors_on_test_failures()
4545
public function test_it_includes_json_validation_errors_on_test_failures()
4646
{
4747
$testCase = new ExampleTestCase();
48-
$testCase->latestResponse = TestResponse::fromBaseResponse(
48+
$testCase::$latestResponse = TestResponse::fromBaseResponse(
4949
new Response(['errors' => ['first_name' => 'The first name field is required.']])
5050
);
5151

@@ -57,7 +57,7 @@ public function test_it_includes_json_validation_errors_on_test_failures()
5757
public function test_it_doesnt_fail_with_false_json()
5858
{
5959
$testCase = new ExampleTestCase();
60-
$testCase->latestResponse = TestResponse::fromBaseResponse(
60+
$testCase::$latestResponse = TestResponse::fromBaseResponse(
6161
new Response(false, 200, ['Content-Type' => 'application/json'])
6262
);
6363

@@ -69,7 +69,7 @@ public function test_it_doesnt_fail_with_false_json()
6969
public function test_it_doesnt_fail_with_encoded_json()
7070
{
7171
$testCase = new ExampleTestCase();
72-
$testCase->latestResponse = TestResponse::fromBaseResponse(
72+
$testCase::$latestResponse = TestResponse::fromBaseResponse(
7373
tap(new Response, function ($response) {
7474
$response->header('Content-Type', 'application/json');
7575
$response->header('Content-Encoding', 'gzip');
@@ -81,6 +81,11 @@ public function test_it_doesnt_fail_with_encoded_json()
8181
$this->expectExceptionMessageMatches('/Assertion message/s');
8282
$testCase->onNotSuccessfulTest(new ExpectationFailedException('Assertion message.'));
8383
}
84+
85+
public function tearDown(): void
86+
{
87+
ExampleTestCase::$latestResponse = null;
88+
}
8489
}
8590

8691
class ExampleTestCase extends TestCase

0 commit comments

Comments
 (0)