blastcloud/hybrid

Charge up your app or SDK with a testing library specifically for Symfony/HttpClient

2.1.0 2024-02-20 03:58 UTC

This package is auto-updated.

Last update: 2024-04-20 04:43:40 UTC


README

Hybrid-logo.svg

badge.svg 68747470733a2f2f706f7365722e707567782e6f72672f626c617374636c6f75642f6879627269642f762f737461626c65 68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f31333531613162373564346265613135366636362f6d61696e7461696e6162696c697479 68747470733a2f2f706f7365722e707567782e6f72672f626c617374636c6f75642f6879627269642f6c6963656e7365

Full Documentation at hybrid.guzzler.dev

Charge up your app or SDK with a testing library specifically for Symfony/HttpClient.

Installation

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

Example Usage

<?php

use BlastCloud\Hybrid\{Expectation, UsesHybrid};
use Symfony\Component\HttpClient\Response\MockResponse;
use PHPUnit\Framework\TestCase;

class SomeTest extends TestCase
{
    use UsesHybrid;

    public $classToTest;

    public function setUp(): void
    {
        parent::setUp();
    
        $client = $this->hybrid->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->hybrid->expects($this->once())
            ->post("/some-url")
            ->withHeader("X-Authorization", "some-key")
            ->willRespond(new MockResponse("Some body"));
    
        $this->classToTest->someMethod();
    }

    public function testSomethingWithAssertions()
    {
        $this->hybrid->queueResponse(
            new MockResponse(null, ['http_code' => 204]),
            new \Exception("Some message"),
            // any needed responses to return from the client.
        );
    
        $this->classToTest->someMethod();
        // ... Some other number of calls
    
        $this->hybrid->assertAll(function (Expectation $expect) {
            return $expect->withHeader("Authorization", "some-key");
        });
    }
}

Documentation

Full Documentation

License

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