From e0f2f6c9be8805579787d5bcfb77fd6baa42a16e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Potock=C3=BD?= Date: Tue, 9 Apr 2024 16:13:11 +0200 Subject: [PATCH] [11.x] Introduce method `Http::createPendingRequest()` (#50980) * Introduce method `\Illuminate\Http\Client\Factory::createPendingRequest()` * Fix passing stubs & stray requests * Update comments --- src/Illuminate/Http/Client/Factory.php | 16 +++++++++++++--- tests/Http/HttpClientTest.php | 7 +++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Http/Client/Factory.php b/src/Illuminate/Http/Client/Factory.php index 4f8a48a50696..b6679f200671 100644 --- a/src/Illuminate/Http/Client/Factory.php +++ b/src/Illuminate/Http/Client/Factory.php @@ -418,6 +418,18 @@ public function recorded($callback = null) * * @return \Illuminate\Http\Client\PendingRequest */ + public function createPendingRequest() + { + return tap($this->newPendingRequest(), function ($request) { + $request->stub($this->stubCallbacks)->preventStrayRequests($this->preventStrayRequests); + }); + } + + /** + * Instantiate a new pending request instance for this factory. + * + * @return \Illuminate\Http\Client\PendingRequest + */ protected function newPendingRequest() { return (new PendingRequest($this, $this->globalMiddleware))->withOptions(value($this->globalOptions)); @@ -456,8 +468,6 @@ public function __call($method, $parameters) return $this->macroCall($method, $parameters); } - return tap($this->newPendingRequest(), function ($request) { - $request->stub($this->stubCallbacks)->preventStrayRequests($this->preventStrayRequests); - })->{$method}(...$parameters); + return $this->createPendingRequest()->{$method}(...$parameters); } } diff --git a/tests/Http/HttpClientTest.php b/tests/Http/HttpClientTest.php index 6590bf78f143..75fd630a418f 100644 --- a/tests/Http/HttpClientTest.php +++ b/tests/Http/HttpClientTest.php @@ -3025,6 +3025,13 @@ public function testItCanHaveGlobalDefaultValues() $this->assertSame(['false'], $headers['X-Foo']); $this->assertSame(['true'], $headers['X-Bar']); } + + public function testItCanCreatePendingRequest() + { + $factory = new Factory(); + + $this->assertInstanceOf(PendingRequest::class, $factory->createPendingRequest()); + } } class CustomFactory extends Factory