korchasa/php-vhs

This package is abandoned and no longer maintained. No replacement package was suggested.

HTTP request/response recording and mock library for PHP

0.5.0 2019-09-22 23:56 UTC

This package is auto-updated.

Last update: 2022-09-23 18:03:51 UTC


README

Usage

Latest Version Build Status Minimum PHP Version

Install:

composer require --dev korchasa/php-vhs

Client testing:

1. Write test with VhsTestCase trait. Surround client calls with assertVhs().

<?php declare(strict_types=1);

namespace korchasa\Vhs\Tests;

use korchasa\Vhs\VhsTestCase;
use PHPUnit\Framework\TestCase;

class AwesomeClientOfflineTest extends TestCase
{
    use VhsTestCase;

    /** @var AwesomeClient */
    private $packagistClient;

    public function setUp()
    {
        $client = new AwesomeClient("bla-bla-bla-bla-bla-bla.commmm");
        $client->setGuzzle($this->connectVhs($client->getGuzzle(), $offline = true));
        $this->packagistClient = $client;
    }

    public function testSuccessStory(): void
    {
        $this->assertVhs(function () {
            $packageName = $this->packagistClient->getFirstTagName();
            $this->assertEquals('korchasa/php-vhs', $packageName);
        });
    }

    public function testWithFail(): void
    {
        $this->assertVhs(function () {
            $resp = $this->packagistClient->auth();
            $this->assertEquals(403, $resp);
        });
    }
}

2. Run test to record cassette (test will be incomplete)

Cassette tests/vhs_cassettes/AwesomeClientOfflineTest_testSuccessStory.json content:

[
    {
        "request": {
            "uri": "https:\/\/httpbin.org\/anything?name=korchasa\/php-vhs",
            "method": "GET",
            "headers": {
                "X-Foo": [
                    "Bar"
                ],
                "Host": [
                    "httpbin.org"
                ]
            },
            "body": "",
            "body_format": "raw"
        },
        "response": {
            "status": 200,
            "headers": {
                "Content-Type": [
                    "application\/json"
                ]
            },
            "body": {
                "args": {
                    "name": "korchasa\/php-vhs"
                },
                "data": "",
                "files": {},
                "form": {},
                "headers": {
                    "Host": "httpbin.org",
                    "User-Agent": "***",
                    "X-Foo": "Bar"
                },
                "json": null,
                "method": "GET",
                "origin": "***",
                "url": "https:\/\/httpbin.org\/anything?name=korchasa%2Fphp-vhs"
            },
            "body_format": "json"
        }
    }
]

3. Run test again

If the cassette is already exists, then we will check the request and replace the response to the one recorded in the cassette.