Skip to content

Commit 16c01c2

Browse files
authored
[11.x] Allow using Illuminate\Support\Uri on testing HTTP Requests (#54038)
* [11.x] Allow using `Illuminate\Support\Uri` on testing HTTP Requests 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 720b685 commit 16c01c2

File tree

2 files changed

+54
-16
lines changed

2 files changed

+54
-16
lines changed

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

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Cookie\CookieValuePrefix;
88
use Illuminate\Http\Request;
99
use Illuminate\Support\Collection;
10+
use Illuminate\Support\Uri;
1011
use Illuminate\Testing\LoggedExceptionCollection;
1112
use Illuminate\Testing\TestResponse;
1213
use Symfony\Component\HttpFoundation\File\UploadedFile as SymfonyUploadedFile;
@@ -355,7 +356,7 @@ public function withPrecognition()
355356
/**
356357
* Visit the given URI with a GET request.
357358
*
358-
* @param string $uri
359+
* @param \Illuminate\Support\Uri|string $uri
359360
* @param array $headers
360361
* @return \Illuminate\Testing\TestResponse
361362
*/
@@ -370,7 +371,7 @@ public function get($uri, array $headers = [])
370371
/**
371372
* Visit the given URI with a GET request, expecting a JSON response.
372373
*
373-
* @param string $uri
374+
* @param \Illuminate\Support\Uri|string $uri
374375
* @param array $headers
375376
* @param int $options
376377
* @return \Illuminate\Testing\TestResponse
@@ -383,7 +384,7 @@ public function getJson($uri, array $headers = [], $options = 0)
383384
/**
384385
* Visit the given URI with a POST request.
385386
*
386-
* @param string $uri
387+
* @param \Illuminate\Support\Uri|string $uri
387388
* @param array $data
388389
* @param array $headers
389390
* @return \Illuminate\Testing\TestResponse
@@ -399,7 +400,7 @@ public function post($uri, array $data = [], array $headers = [])
399400
/**
400401
* Visit the given URI with a POST request, expecting a JSON response.
401402
*
402-
* @param string $uri
403+
* @param \Illuminate\Support\Uri|string $uri
403404
* @param array $data
404405
* @param array $headers
405406
* @param int $options
@@ -413,7 +414,7 @@ public function postJson($uri, array $data = [], array $headers = [], $options =
413414
/**
414415
* Visit the given URI with a PUT request.
415416
*
416-
* @param string $uri
417+
* @param \Illuminate\Support\Uri|string $uri
417418
* @param array $data
418419
* @param array $headers
419420
* @return \Illuminate\Testing\TestResponse
@@ -429,7 +430,7 @@ public function put($uri, array $data = [], array $headers = [])
429430
/**
430431
* Visit the given URI with a PUT request, expecting a JSON response.
431432
*
432-
* @param string $uri
433+
* @param \Illuminate\Support\Uri|string $uri
433434
* @param array $data
434435
* @param array $headers
435436
* @param int $options
@@ -443,7 +444,7 @@ public function putJson($uri, array $data = [], array $headers = [], $options =
443444
/**
444445
* Visit the given URI with a PATCH request.
445446
*
446-
* @param string $uri
447+
* @param \Illuminate\Support\Uri|string $uri
447448
* @param array $data
448449
* @param array $headers
449450
* @return \Illuminate\Testing\TestResponse
@@ -459,7 +460,7 @@ public function patch($uri, array $data = [], array $headers = [])
459460
/**
460461
* Visit the given URI with a PATCH request, expecting a JSON response.
461462
*
462-
* @param string $uri
463+
* @param \Illuminate\Support\Uri|string $uri
463464
* @param array $data
464465
* @param array $headers
465466
* @param int $options
@@ -473,7 +474,7 @@ public function patchJson($uri, array $data = [], array $headers = [], $options
473474
/**
474475
* Visit the given URI with a DELETE request.
475476
*
476-
* @param string $uri
477+
* @param \Illuminate\Support\Uri|string $uri
477478
* @param array $data
478479
* @param array $headers
479480
* @return \Illuminate\Testing\TestResponse
@@ -489,7 +490,7 @@ public function delete($uri, array $data = [], array $headers = [])
489490
/**
490491
* Visit the given URI with a DELETE request, expecting a JSON response.
491492
*
492-
* @param string $uri
493+
* @param \Illuminate\Support\Uri|string $uri
493494
* @param array $data
494495
* @param array $headers
495496
* @param int $options
@@ -503,7 +504,7 @@ public function deleteJson($uri, array $data = [], array $headers = [], $options
503504
/**
504505
* Visit the given URI with an OPTIONS request.
505506
*
506-
* @param string $uri
507+
* @param \Illuminate\Support\Uri|string $uri
507508
* @param array $data
508509
* @param array $headers
509510
* @return \Illuminate\Testing\TestResponse
@@ -520,7 +521,7 @@ public function options($uri, array $data = [], array $headers = [])
520521
/**
521522
* Visit the given URI with an OPTIONS request, expecting a JSON response.
522523
*
523-
* @param string $uri
524+
* @param \Illuminate\Support\Uri|string $uri
524525
* @param array $data
525526
* @param array $headers
526527
* @param int $options
@@ -534,7 +535,7 @@ public function optionsJson($uri, array $data = [], array $headers = [], $option
534535
/**
535536
* Visit the given URI with a HEAD request.
536537
*
537-
* @param string $uri
538+
* @param \Illuminate\Support\Uri|string $uri
538539
* @param array $headers
539540
* @return \Illuminate\Testing\TestResponse
540541
*/
@@ -551,7 +552,7 @@ public function head($uri, array $headers = [])
551552
* Call the given URI with a JSON request.
552553
*
553554
* @param string $method
554-
* @param string $uri
555+
* @param \Illuminate\Support\Uri|string $uri
555556
* @param array $data
556557
* @param array $headers
557558
* @param int $options
@@ -584,7 +585,7 @@ public function json($method, $uri, array $data = [], array $headers = [], $opti
584585
* Call the given URI and return the Response.
585586
*
586587
* @param string $method
587-
* @param string $uri
588+
* @param \Illuminate\Support\Uri|string $uri
588589
* @param array $parameters
589590
* @param array $cookies
590591
* @param array $files
@@ -619,11 +620,13 @@ public function call($method, $uri, $parameters = [], $cookies = [], $files = []
619620
/**
620621
* Turn the given URI into a fully qualified URL.
621622
*
622-
* @param string $uri
623+
* @param \Illuminate\Support\Uri|string $uri
623624
* @return string
624625
*/
625626
protected function prepareUrlForRequest($uri)
626627
{
628+
$uri = $uri instanceof Uri ? $uri->value() : $uri;
629+
627630
if (str_starts_with($uri, '/')) {
628631
$uri = substr($uri, 1);
629632
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Integration\Foundation\Testing\Concerns;
4+
5+
use Illuminate\Http\Request;
6+
use Illuminate\Support\Uri;
7+
use Orchestra\Testbench\Attributes\WithConfig;
8+
use Orchestra\Testbench\TestCase;
9+
10+
#[WithConfig('app.key', 'base64:IUHRqAQ99pZ0A1MPjbuv1D6ff3jxv0GIvS2qIW4JNU4=')]
11+
class MakeHttpRequestsTest extends TestCase
12+
{
13+
/** {@inheritDoc} */
14+
protected function defineWebRoutes($router)
15+
{
16+
$router->get('decode', fn (Request $request) => [
17+
'url' => $request->fullUrl(),
18+
'query' => $request->query(),
19+
]);
20+
}
21+
22+
public function test_it_can_use_uri_to_make_request()
23+
{
24+
$this->getJson(Uri::of('decode')->withQuery(['editing' => true, 'editMode' => 'create', 'search' => 'Laravel']))
25+
->assertSuccessful()
26+
->assertJson([
27+
'url' => 'http://localhost/decode?editMode=create&editing=1&search=Laravel',
28+
'query' => [
29+
'editing' => '1',
30+
'editMode' => 'create',
31+
'search' => 'Laravel',
32+
],
33+
]);
34+
}
35+
}

0 commit comments

Comments
 (0)