55

GitHub - blastcloud/guzzler: Simplify your tests that use Guzzle

 5 years ago
source link: https://github.com/blastcloud/guzzler
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

README.md

Guzzler-logo.svg?sanitize=true

68747470733a2f2f7472617669732d63692e6f72672f626c617374636c6f75642f67757a7a6c65722e7376673f6272616e63683d6d6173746572 68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f30316336663636656161356462303265353431312f746573745f636f766572616765


Simplify your unit tests that use Guzzle with a mock-like syntax. Guzzler covers the process of setting up a mock handler, recording history of requests, and provides several convenience methods for creating expectations and assertions on that history.

Example Usage

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");
        });
    }
}

Full Documentation


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK