Skip to content

Commit

Permalink
[11.x] Allow using Illuminate\Support\Uri on testing HTTP Requests (#…
Browse files Browse the repository at this point in the history
…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]>
  • Loading branch information
crynobone authored Dec 30, 2024
1 parent 720b685 commit 16c01c2
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 16 deletions.
35 changes: 19 additions & 16 deletions src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Cookie\CookieValuePrefix;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Illuminate\Support\Uri;
use Illuminate\Testing\LoggedExceptionCollection;
use Illuminate\Testing\TestResponse;
use Symfony\Component\HttpFoundation\File\UploadedFile as SymfonyUploadedFile;
Expand Down Expand Up @@ -355,7 +356,7 @@ public function withPrecognition()
/**
* Visit the given URI with a GET request.
*
* @param string $uri
* @param \Illuminate\Support\Uri|string $uri
* @param array $headers
* @return \Illuminate\Testing\TestResponse
*/
Expand All @@ -370,7 +371,7 @@ public function get($uri, array $headers = [])
/**
* Visit the given URI with a GET request, expecting a JSON response.
*
* @param string $uri
* @param \Illuminate\Support\Uri|string $uri
* @param array $headers
* @param int $options
* @return \Illuminate\Testing\TestResponse
Expand All @@ -383,7 +384,7 @@ public function getJson($uri, array $headers = [], $options = 0)
/**
* Visit the given URI with a POST request.
*
* @param string $uri
* @param \Illuminate\Support\Uri|string $uri
* @param array $data
* @param array $headers
* @return \Illuminate\Testing\TestResponse
Expand All @@ -399,7 +400,7 @@ public function post($uri, array $data = [], array $headers = [])
/**
* Visit the given URI with a POST request, expecting a JSON response.
*
* @param string $uri
* @param \Illuminate\Support\Uri|string $uri
* @param array $data
* @param array $headers
* @param int $options
Expand All @@ -413,7 +414,7 @@ public function postJson($uri, array $data = [], array $headers = [], $options =
/**
* Visit the given URI with a PUT request.
*
* @param string $uri
* @param \Illuminate\Support\Uri|string $uri
* @param array $data
* @param array $headers
* @return \Illuminate\Testing\TestResponse
Expand All @@ -429,7 +430,7 @@ public function put($uri, array $data = [], array $headers = [])
/**
* Visit the given URI with a PUT request, expecting a JSON response.
*
* @param string $uri
* @param \Illuminate\Support\Uri|string $uri
* @param array $data
* @param array $headers
* @param int $options
Expand All @@ -443,7 +444,7 @@ public function putJson($uri, array $data = [], array $headers = [], $options =
/**
* Visit the given URI with a PATCH request.
*
* @param string $uri
* @param \Illuminate\Support\Uri|string $uri
* @param array $data
* @param array $headers
* @return \Illuminate\Testing\TestResponse
Expand All @@ -459,7 +460,7 @@ public function patch($uri, array $data = [], array $headers = [])
/**
* Visit the given URI with a PATCH request, expecting a JSON response.
*
* @param string $uri
* @param \Illuminate\Support\Uri|string $uri
* @param array $data
* @param array $headers
* @param int $options
Expand All @@ -473,7 +474,7 @@ public function patchJson($uri, array $data = [], array $headers = [], $options
/**
* Visit the given URI with a DELETE request.
*
* @param string $uri
* @param \Illuminate\Support\Uri|string $uri
* @param array $data
* @param array $headers
* @return \Illuminate\Testing\TestResponse
Expand All @@ -489,7 +490,7 @@ public function delete($uri, array $data = [], array $headers = [])
/**
* Visit the given URI with a DELETE request, expecting a JSON response.
*
* @param string $uri
* @param \Illuminate\Support\Uri|string $uri
* @param array $data
* @param array $headers
* @param int $options
Expand All @@ -503,7 +504,7 @@ public function deleteJson($uri, array $data = [], array $headers = [], $options
/**
* Visit the given URI with an OPTIONS request.
*
* @param string $uri
* @param \Illuminate\Support\Uri|string $uri
* @param array $data
* @param array $headers
* @return \Illuminate\Testing\TestResponse
Expand All @@ -520,7 +521,7 @@ public function options($uri, array $data = [], array $headers = [])
/**
* Visit the given URI with an OPTIONS request, expecting a JSON response.
*
* @param string $uri
* @param \Illuminate\Support\Uri|string $uri
* @param array $data
* @param array $headers
* @param int $options
Expand All @@ -534,7 +535,7 @@ public function optionsJson($uri, array $data = [], array $headers = [], $option
/**
* Visit the given URI with a HEAD request.
*
* @param string $uri
* @param \Illuminate\Support\Uri|string $uri
* @param array $headers
* @return \Illuminate\Testing\TestResponse
*/
Expand All @@ -551,7 +552,7 @@ public function head($uri, array $headers = [])
* Call the given URI with a JSON request.
*
* @param string $method
* @param string $uri
* @param \Illuminate\Support\Uri|string $uri
* @param array $data
* @param array $headers
* @param int $options
Expand Down Expand Up @@ -584,7 +585,7 @@ public function json($method, $uri, array $data = [], array $headers = [], $opti
* Call the given URI and return the Response.
*
* @param string $method
* @param string $uri
* @param \Illuminate\Support\Uri|string $uri
* @param array $parameters
* @param array $cookies
* @param array $files
Expand Down Expand Up @@ -619,11 +620,13 @@ public function call($method, $uri, $parameters = [], $cookies = [], $files = []
/**
* Turn the given URI into a fully qualified URL.
*
* @param string $uri
* @param \Illuminate\Support\Uri|string $uri
* @return string
*/
protected function prepareUrlForRequest($uri)
{
$uri = $uri instanceof Uri ? $uri->value() : $uri;

if (str_starts_with($uri, '/')) {
$uri = substr($uri, 1);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Illuminate\Tests\Integration\Foundation\Testing\Concerns;

use Illuminate\Http\Request;
use Illuminate\Support\Uri;
use Orchestra\Testbench\Attributes\WithConfig;
use Orchestra\Testbench\TestCase;

#[WithConfig('app.key', 'base64:IUHRqAQ99pZ0A1MPjbuv1D6ff3jxv0GIvS2qIW4JNU4=')]
class MakeHttpRequestsTest extends TestCase
{
/** {@inheritDoc} */
protected function defineWebRoutes($router)
{
$router->get('decode', fn (Request $request) => [
'url' => $request->fullUrl(),
'query' => $request->query(),
]);
}

public function test_it_can_use_uri_to_make_request()
{
$this->getJson(Uri::of('decode')->withQuery(['editing' => true, 'editMode' => 'create', 'search' => 'Laravel']))
->assertSuccessful()
->assertJson([
'url' => 'http://localhost/decode?editMode=create&editing=1&search=Laravel',
'query' => [
'editing' => '1',
'editMode' => 'create',
'search' => 'Laravel',
],
]);
}
}

0 comments on commit 16c01c2

Please sign in to comment.