Skip to content

Commit 7df3c90

Browse files
authored
[11.x] Allow TestResponse to accept Illuminate\Http\Request (#47418)
* [11.x] Allow `TestResponse` to accept `Illuminate\Http\Request` This allows use to make assertion on the request from `TestResponse` Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip 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 72b5017 commit 7df3c90

File tree

7 files changed

+58
-13
lines changed

7 files changed

+58
-13
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ public function call($method, $uri, $parameters = [], $cookies = [], $files = []
570570
$response = $this->followRedirects($response);
571571
}
572572

573-
return static::$latestResponse = $this->createTestResponse($response);
573+
return static::$latestResponse = $this->createTestResponse($response, $request);
574574
}
575575

576576
/**
@@ -692,11 +692,12 @@ protected function followRedirects($response)
692692
* Create the test response instance from the given response.
693693
*
694694
* @param \Illuminate\Http\Response $response
695+
* @param \Illuminate\Http\Request $request
695696
* @return \Illuminate\Testing\TestResponse
696697
*/
697-
protected function createTestResponse($response)
698+
protected function createTestResponse($response, $request)
698699
{
699-
return tap(TestResponse::fromBaseResponse($response), function ($response) {
700+
return tap(TestResponse::fromBaseResponse($response, $request), function ($response) {
700701
$response->withExceptions(
701702
$this->app->bound(LoggedExceptionCollection::class)
702703
? $this->app->make(LoggedExceptionCollection::class)

src/Illuminate/Testing/TestResponse.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ class TestResponse implements ArrayAccess
3737
__call as macroCall;
3838
}
3939

40+
/**
41+
* The original request.
42+
*
43+
* @var \Illuminate\Http\Request|null
44+
*/
45+
public $baseRequest;
46+
4047
/**
4148
* The response to delegate to.
4249
*
@@ -62,23 +69,26 @@ class TestResponse implements ArrayAccess
6269
* Create a new test response instance.
6370
*
6471
* @param \Illuminate\Http\Response $response
72+
* @param \Illuminate\Http\Request|null $request
6573
* @return void
6674
*/
67-
public function __construct($response)
75+
public function __construct($response, $request = null)
6876
{
6977
$this->baseResponse = $response;
78+
$this->baseRequest = $request;
7079
$this->exceptions = new Collection;
7180
}
7281

7382
/**
7483
* Create a new TestResponse from another response.
7584
*
7685
* @param \Illuminate\Http\Response $response
86+
* @param \Illuminate\Http\Request|null $request
7787
* @return static
7888
*/
79-
public static function fromBaseResponse($response)
89+
public static function fromBaseResponse($response, $request = null)
8090
{
81-
return new static($response);
91+
return new static($response, $request);
8292
}
8393

8494
/**

tests/Integration/Routing/FallbackRouteTest.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,13 @@ public function testFallbackWithWildcards()
5555
})->where('any', '.*');
5656

5757
$this->assertStringContainsString('one', $this->get('/one')->getContent());
58-
$this->assertStringContainsString('wildcard', $this->get('/non-existing')->getContent());
59-
$this->assertEquals(200, $this->get('/non-existing')->getStatusCode());
58+
59+
tap($this->get('/non-existing'), function ($response) {
60+
$this->assertStringContainsString('wildcard', $response->getContent());
61+
$this->assertEquals(200, $response->getStatusCode());
62+
63+
$this->assertSame('non-existing', $response->baseRequest->route('any'));
64+
});
6065
}
6166

6267
public function testNoRoutes()

tests/Integration/Routing/ImplicitModelRouteBindingTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ public function testWithoutRouteCachingEnabled()
107107
'id' => $user->id,
108108
'name' => $user->name,
109109
]);
110+
111+
$this->assertTrue($user->is($response->baseRequest->route('user')));
110112
}
111113

112114
public function testSoftDeletedModelsAreNotRetrieved()
@@ -144,6 +146,8 @@ public function testSoftDeletedModelsCanBeRetrievedUsingWithTrashedMethod()
144146
'id' => $user->id,
145147
'name' => $user->name,
146148
]);
149+
150+
$this->assertTrue($user->is($response->baseRequest->route('user')));
147151
}
148152

149153
public function testEnforceScopingImplicitRouteBindings()

tests/Integration/Routing/RouteViewTest.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,17 @@ public function testRouteViewWithParams()
2727
$this->assertStringContainsString('Test bar', $this->get('/route/value1/value2')->getContent());
2828
$this->assertStringContainsString('Test bar', $this->get('/route/value1')->getContent());
2929

30-
$this->assertEquals('value1', $this->get('/route/value1/value2')->viewData('param'));
31-
$this->assertEquals('value2', $this->get('/route/value1/value2')->viewData('param2'));
30+
tap($this->get('/route/value1/value2'), function ($response) {
31+
$this->assertEquals('value1', $response->viewData('param'));
32+
$this->assertEquals('value1', $response->baseRequest->route('param'));
33+
$this->assertEquals('value2', $response->baseRequest->route('param2'));
34+
});
35+
36+
tap($this->get('/route/value1/value2'), function ($response) {
37+
$this->assertEquals('value2', $response->viewData('param2'));
38+
$this->assertEquals('value1', $response->baseRequest->route('param'));
39+
$this->assertEquals('value2', $response->baseRequest->route('param2'));
40+
});
3241
}
3342

3443
public function testRouteViewWithStatus()

tests/Integration/Routing/SimpleRouteTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,11 @@ public function testSimpleRouteThroughTheFramework()
1616
$response = $this->get('/');
1717

1818
$this->assertSame('Hello World', $response->content());
19+
20+
$response = $this->get('/?foo=bar');
21+
22+
$this->assertSame('Hello World', $response->content());
23+
24+
$this->assertSame('bar', $response->baseRequest->query('foo'));
1925
}
2026
}

tests/Integration/Routing/UrlSigningTest.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,12 @@ public function testSigningUrl()
3333
})->name('foo');
3434

3535
$this->assertIsString($url = URL::signedRoute('foo', ['id' => 1]));
36-
$this->assertSame('valid', $this->get($url)->original);
36+
37+
tap($this->get($url), function ($response) {
38+
$this->assertSame('valid', $response->original);
39+
40+
$this->assertIsString($response->baseRequest->query('signature'));
41+
});
3742
}
3843

3944
public function testSigningUrlWithCustomRouteSlug()
@@ -46,8 +51,13 @@ public function testSigningUrlWithCustomRouteSlug()
4651
$model->routable = 'routable-slug';
4752

4853
$this->assertIsString($url = URL::signedRoute('foo', ['post' => $model]));
49-
$this->assertSame('valid', $this->get($url)->original['valid']);
50-
$this->assertSame('routable-slug', $this->get($url)->original['slug']);
54+
55+
tap($this->get($url), function ($response) {
56+
$this->assertSame('valid', $response->original['valid']);
57+
$this->assertSame('routable-slug', $response->original['slug']);
58+
59+
$this->assertSame('routable-slug', $response->baseRequest->route('post'));
60+
});
5161
}
5262

5363
public function testTemporarySignedUrls()

0 commit comments

Comments
 (0)