Skip to content

Commit b9203fc

Browse files
authored
Share HTTP client when faking (#40771)
1 parent 64366c9 commit b9203fc

File tree

2 files changed

+93
-2
lines changed

2 files changed

+93
-2
lines changed

src/Illuminate/Support/Facades/Http.php

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
/**
88
* @method static \GuzzleHttp\Promise\PromiseInterface response($body = null, $status = 200, $headers = [])
9-
* @method static \Illuminate\Http\Client\Factory fake($callback = null)
109
* @method static \Illuminate\Http\Client\PendingRequest accept(string $contentType)
1110
* @method static \Illuminate\Http\Client\PendingRequest acceptJson()
1211
* @method static \Illuminate\Http\Client\PendingRequest asForm()
@@ -43,7 +42,6 @@
4342
* @method static \Illuminate\Http\Client\Response post(string $url, array $data = [])
4443
* @method static \Illuminate\Http\Client\Response put(string $url, array $data = [])
4544
* @method static \Illuminate\Http\Client\Response send(string $method, string $url, array $options = [])
46-
* @method static \Illuminate\Http\Client\ResponseSequence fakeSequence(string $urlPattern = '*')
4745
* @method static void assertSent(callable $callback)
4846
* @method static void assertSentInOrder(array $callbacks)
4947
* @method static void assertNotSent(callable $callback)
@@ -64,4 +62,46 @@ protected static function getFacadeAccessor()
6462
{
6563
return Factory::class;
6664
}
65+
66+
/**
67+
* Register a stub callable that will intercept requests and be able to return stub responses.
68+
*
69+
* @param \Closure|array $callback
70+
* @return \Illuminate\Http\Client\Factory
71+
*/
72+
public static function fake($callback = null)
73+
{
74+
return tap(static::getFacadeRoot(), function ($fake) use ($callback) {
75+
static::swap($fake->fake($callback));
76+
});
77+
}
78+
79+
/**
80+
* Register a response sequence for the given URL pattern.
81+
*
82+
* @param string $urlPattern
83+
* @return \Illuminate\Http\Client\ResponseSequence
84+
*/
85+
public static function fakeSequence(string $urlPattern = '*')
86+
{
87+
$fake = tap(static::getFacadeRoot(), function ($fake) {
88+
static::swap($fake);
89+
});
90+
91+
return $fake->fakeSequence($urlPattern);
92+
}
93+
94+
/**
95+
* Stub the given URL using the given callback.
96+
*
97+
* @param string $url
98+
* @param \Illuminate\Http\Client\Response|\GuzzleHttp\Promise\PromiseInterface|callable $callback
99+
* @return \Illuminate\Http\Client\Factory
100+
*/
101+
public static function stubUrl($url, $callback)
102+
{
103+
return tap(static::getFacadeRoot(), function ($fake) use ($url, $callback) {
104+
static::swap($fake->stubUrl($url, $callback));
105+
});
106+
}
67107
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Support;
4+
5+
use Illuminate\Container\Container;
6+
use Illuminate\Http\Client\Factory;
7+
use Illuminate\Support\Facades\Facade;
8+
use Illuminate\Support\Facades\Http;
9+
use PHPUnit\Framework\TestCase;
10+
11+
class SupportFacadesHttpTest extends TestCase
12+
{
13+
protected $app;
14+
15+
protected function setUp(): void
16+
{
17+
$this->app = new Container;
18+
Facade::setFacadeApplication($this->app);
19+
}
20+
21+
public function testFacadeRootIsNotSharedByDefault(): void
22+
{
23+
$this->assertNotSame(Http::getFacadeRoot(), $this->app->make(Factory::class));
24+
}
25+
26+
public function testFacadeRootIsSharedWhenFaked(): void
27+
{
28+
Http::fake([
29+
'https://laravel.com' => Http::response('OK!'),
30+
]);
31+
32+
$factory = $this->app->make(Factory::class);
33+
$this->assertSame('OK!', $factory->get('https://laravel.com')->body());
34+
}
35+
36+
public function testFacadeRootIsSharedWhenFakedWithSequence(): void
37+
{
38+
Http::fakeSequence('laravel.com/*')->push('OK!');
39+
40+
$factory = $this->app->make(Factory::class);
41+
$this->assertSame('OK!', $factory->get('https://laravel.com')->body());
42+
}
43+
44+
public function testFacadeRootIsSharedWhenStubbingUrls(): void
45+
{
46+
Http::stubUrl('laravel.com', Http::response('OK!'));
47+
48+
$factory = $this->app->make(Factory::class);
49+
$this->assertSame('OK!', $factory->get('https://laravel.com')->body());
50+
}
51+
}

0 commit comments

Comments
 (0)