Skip to content

Commit

Permalink
Share HTTP client when faking (#40771)
Browse files Browse the repository at this point in the history
  • Loading branch information
axlon committed Feb 5, 2022
1 parent 64366c9 commit b9203fc
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 2 deletions.
44 changes: 42 additions & 2 deletions src/Illuminate/Support/Facades/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

/**
* @method static \GuzzleHttp\Promise\PromiseInterface response($body = null, $status = 200, $headers = [])
* @method static \Illuminate\Http\Client\Factory fake($callback = null)
* @method static \Illuminate\Http\Client\PendingRequest accept(string $contentType)
* @method static \Illuminate\Http\Client\PendingRequest acceptJson()
* @method static \Illuminate\Http\Client\PendingRequest asForm()
Expand Down Expand Up @@ -43,7 +42,6 @@
* @method static \Illuminate\Http\Client\Response post(string $url, array $data = [])
* @method static \Illuminate\Http\Client\Response put(string $url, array $data = [])
* @method static \Illuminate\Http\Client\Response send(string $method, string $url, array $options = [])
* @method static \Illuminate\Http\Client\ResponseSequence fakeSequence(string $urlPattern = '*')
* @method static void assertSent(callable $callback)
* @method static void assertSentInOrder(array $callbacks)
* @method static void assertNotSent(callable $callback)
Expand All @@ -64,4 +62,46 @@ protected static function getFacadeAccessor()
{
return Factory::class;
}

/**
* Register a stub callable that will intercept requests and be able to return stub responses.
*
* @param \Closure|array $callback
* @return \Illuminate\Http\Client\Factory
*/
public static function fake($callback = null)
{
return tap(static::getFacadeRoot(), function ($fake) use ($callback) {
static::swap($fake->fake($callback));
});
}

/**
* Register a response sequence for the given URL pattern.
*
* @param string $urlPattern
* @return \Illuminate\Http\Client\ResponseSequence
*/
public static function fakeSequence(string $urlPattern = '*')
{
$fake = tap(static::getFacadeRoot(), function ($fake) {
static::swap($fake);
});

return $fake->fakeSequence($urlPattern);
}

/**
* Stub the given URL using the given callback.
*
* @param string $url
* @param \Illuminate\Http\Client\Response|\GuzzleHttp\Promise\PromiseInterface|callable $callback
* @return \Illuminate\Http\Client\Factory
*/
public static function stubUrl($url, $callback)
{
return tap(static::getFacadeRoot(), function ($fake) use ($url, $callback) {
static::swap($fake->stubUrl($url, $callback));
});
}
}
51 changes: 51 additions & 0 deletions tests/Support/SupportFacadesHttpTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Illuminate\Tests\Support;

use Illuminate\Container\Container;
use Illuminate\Http\Client\Factory;
use Illuminate\Support\Facades\Facade;
use Illuminate\Support\Facades\Http;
use PHPUnit\Framework\TestCase;

class SupportFacadesHttpTest extends TestCase
{
protected $app;

protected function setUp(): void
{
$this->app = new Container;
Facade::setFacadeApplication($this->app);
}

public function testFacadeRootIsNotSharedByDefault(): void
{
$this->assertNotSame(Http::getFacadeRoot(), $this->app->make(Factory::class));
}

public function testFacadeRootIsSharedWhenFaked(): void
{
Http::fake([
'https://laravel.com' => Http::response('OK!'),
]);

$factory = $this->app->make(Factory::class);
$this->assertSame('OK!', $factory->get('https://laravel.com')->body());
}

public function testFacadeRootIsSharedWhenFakedWithSequence(): void
{
Http::fakeSequence('laravel.com/*')->push('OK!');

$factory = $this->app->make(Factory::class);
$this->assertSame('OK!', $factory->get('https://laravel.com')->body());
}

public function testFacadeRootIsSharedWhenStubbingUrls(): void
{
Http::stubUrl('laravel.com', Http::response('OK!'));

$factory = $this->app->make(Factory::class);
$this->assertSame('OK!', $factory->get('https://laravel.com')->body());
}
}

0 comments on commit b9203fc

Please sign in to comment.