guzzler

Supercharge your app or SDK with a testing library specifically for Guzzle

Github stars Tracking Chart


Full Documentation at guzzler.dev

Supercharge your app or SDK with a testing library specifically for Guzzle. 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.

Installation

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

Example Usage

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

Documentation

Full Documentation

License

Guzzler is open-source software licensed under the MIT License.

Main metrics

Overview
Name With Ownerblastcloud/guzzler
Primary LanguagePHP
Program languagePHP (Language Count: 1)
Platform
License:MIT License
所有者活动
Created At2019-02-08 03:58:30
Pushed At2024-02-20 03:56:33
Last Commit At2024-02-19 20:52:25
Release Count23
Last Release Name2.2.0 (Posted on )
First Release Name1.0.0 (Posted on )
用户参与
Stargazers Count272
Watchers Count7
Fork Count4
Commits Count158
Has Issues Enabled
Issues Count14
Issue Open Count1
Pull Requests Count1
Pull Requests Open Count1
Pull Requests Close Count0
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private