sjorso/gobble

此包已被放弃,不再维护。作者建议使用 "Laravel 7 Http::fake()" 包代替。

一个针对 Laravel 的 Guzzle 包装器,使模拟响应变得容易

1.4.2 2020-12-21 09:42 UTC

This package is auto-updated.

Last update: 2023-12-07 19:45:49 UTC


README

从版本 7 开始,Laravel 现在包含一个 HTTP,这使得模拟 Guzzle 响应变得容易。

从 Gobble 迁移到新的 HTTP 门面并不困难,例如

// old
Gobble::fake()->pushEmptyResponse()

// new
HTTP::fakeSequence()->pushStatus(200);

Gobble

一个针对 Laravel 的 Guzzle 包装器,使模拟响应变得容易。

安装

composer require sjorso/gobble

使用

您可以在代码中使用 Gobble 门面通过 Guzzle 发送请求。Gobble 门面将所有方法调用代理到 GuzzleHttp\Client

use SjorsO\Gobble\Facades\Gobble;

$response = Gobble::get('https://laravel.net.cn');

模拟响应

当编写测试时,您可以使用伪造的 Gobble 使其使用 Guzzle 的内置模拟处理程序。当 Gobble 被伪造时,您可以使用它将响应推送到模拟处理程序堆栈

use SjorsO\Gobble\Facades\Gobble as Guzzle;

class CatFactController
{
    public function create()
    {
        $response = Guzzle::get('https://catfact.ninja/fact');

        $json = json_decode($response->getBody()->getContents(), true);

        CatFact::create([
            'fact' => $json['fact'],
        ]);
    }
}
/** @test */
function it_can_create_a_cat_fact()
{
    Gobble::fake()->pushJson(['fact' => 'Cats are great!']);

    $this->post('/cat-fact/create')->assertStatus(200);

    $this->assertSame('Cats are great!', CatFact::firstOrFail()->fact);
}

当 Gobble 被伪造时,您可以使用以下方法将模拟响应推送到模拟处理程序堆栈

Gobble::pushResponse($response);

Gobble::pushEmptyResponse($status = 200, $headers = []);

Gobble::pushString($string, $status = 200, $headers = []);

Gobble::pushJson(array $data, $status = 200, $headers = []);

Gobble::pushFile($filePath, $status = 200, $headers = []);

您还可以在请求时自动将空响应推送到响应堆栈。这在某些情况下很有用,可以防止您的日志被 "Mock queue is empty" 错误填满

Gobble::fake()->autofillResponseStack();

Gobble 提供了两种方法来断言模拟处理程序队列中的响应数量

Gobble::assertMockQueueCount(3);

Gobble::assertMockQueueEmpty();

请求历史记录

当 Gobble 被伪造时,它使用 Guzzle 的内置历史中间件 来跟踪所有请求。请求历史记录条目被包装在 RequestHistory 类中,以添加一些有用的断言,并改进 IDE 自动完成。

/** @test */
function it_makes_a_call_to_the_cat_fact_api()
{
    Gobble::fake()->pushEmptyResponse();

    CreateCatFactJob::dispatchNow();

    $history = Gobble::requestHistory();

    $this->assertCount(1, $history);

    $history[0]->assertRequestUri('https://catfact.ninja/fact');
}

您可以使用 lastRequest() 方法获取使用 Gobble 发出的最后一个请求

/** @test */
function it_is_an_example_in_the_readme()
{
    Gobble::fake()->pushEmptyResponse();

    $response = Gobble::get('https://example.com');

    $lastRequest = Gobble::lastRequest();

    // $response === $lastRequest->response
}

RequestHistory 类提供以下断言

public function assertRequestBodyExact($expected);

public function assertRequestBodyJson(array $data, $strict = false);

public function assertRequestBodyExactJson(array $data);

public function assertRequestBodyContains($string);

public function assertRequestBodyDoesntContain($string);

public function assertRequestUri($expected);

public function assertRequestHeaderPresent($key);

public function assertRequestHeaderMissing($key);

public function assertRequestHeader($key, $expected);

Guzzle 配置

对于每个调用,GuzzleWrapper 从容器中解析一个 GuzzleHttp\Client。如果容器中没有绑定 GuzzleHttp\Client,则会创建一个新的 Guzzle 客户端。您可以在容器中绑定 Guzzle 客户端来配置它

class AppServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->bind(\GuzzleHttp\Client::class, function () {
            return new \GuzzleHttp\Client(['timeout' => 5, 'connect_timeout' => 5]);
        });
    }

    public function boot()
    {
        //
    }
}

许可证

此项目是开源软件,受 MIT 许可证 许可。