blastcloud / guzzler

使用专为Guzzle设计的测试库,为您的应用程序或SDK加速。

2.2.0 2024-02-20 03:52 UTC

This package is auto-updated.

Last update: 2024-09-20 05:49:24 UTC


README

完整文档位于guzzler.dev

使用专为Guzzle设计的测试库,为您的应用程序或SDK加速。Guzzler涵盖了设置模拟处理程序、记录请求历史以及在该历史记录上创建期望和断言的便捷方法。

安装

composer require --dev --prefer-dist blastcloud/guzzler

示例用法

<?php

use BlastCloud\Guzzler\UsesGuzzler;
use GuzzleHttp\Client;

class SomeTest extends TestCase
{
    use UsesGuzzler;

    public $classToTest;

    public function setUp(): void
    {
        parent::setUp();
    
        $client = $this->guzzler->getClient([
            /* Any configs for a client */
            "base_uri" => "https://example.com/api"
        ]);
        
        // You can then inject this client object into your code or IOC container.
        $this->classToTest = new ClassToTest($client);
    }

    public function testSomethingWithExpectations()
    {
        $this->guzzler->expects($this->once())
            ->post("/some-url")
            ->withHeader("X-Authorization", "some-key")
            ->willRespond(new Response(201));
    
        $this->classToTest->someMethod();
    }

    public function testSomethingWithAssertions()
    {
        $this->guzzler->queueResponse(
            new Response(204),
            new \Exception("Some message"),
            // any needed responses to return from the client.
        );
    
        $this->classToTest->someMethod();
        // ... Some other number of calls
    
        $this->guzzler->assertAll(function ($expect) {
            return $expect->withHeader("Authorization", "some-key");
        });
    }
}

文档

完整文档

许可

Guzzler是开源软件,遵循MIT许可证