Skip to content

Commit

Permalink
[11.x] Introduce method Http::createPendingRequest() (#50980)
Browse files Browse the repository at this point in the history
* Introduce method `\Illuminate\Http\Client\Factory::createPendingRequest()`

* Fix passing stubs & stray requests

* Update comments
  • Loading branch information
Jacobs63 authored Apr 9, 2024
1 parent eda7068 commit e0f2f6c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/Illuminate/Http/Client/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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);
}
}
7 changes: 7 additions & 0 deletions tests/Http/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e0f2f6c

Please sign in to comment.