Coder Social home page Coder Social logo

php-vhs's Introduction

HTTP request/response recording and mock library for PHP

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.

php-vhs's People

Stargazers

 avatar  avatar

Watchers

 avatar

php-vhs's Issues

Installing problem: Requirements could not be resolved!

Getting this error while trying to install the package:

composer require --dev korchasa/php-vhs
    1/5:        http://repo.packagist.org/p/provider-latest$974c6fb7d32f47cf8d6f802870f70e1321d9e3540e2b5557427c45740b64cf29.json
    2/5:        http://repo.packagist.org/p/provider-2019-04$076d8c50c3d0a996d2509d3400b9bfa5c6c9ae959a79ea74f657c794bb11ca41.json
    3/5:        http://repo.packagist.org/p/provider-2019-07$db4aee3ab87f3a3bc9942195981c3db38fce6a55dedf07f3e7ea71fe7dd53dd2.json
    4/5:        http://repo.packagist.org/p/provider-2017$eb526f3f12eb53ec1cea43e21d1bb7f00cbdbb41d73ea9be1a6acae337477e53.json
    5/5:        http://repo.packagist.org/p/provider-2018$8805cbaa0f4f9b28eadec6c7766d8776059f74287d58a57080d87b907df613de.json
    Finished: success: 5, skipped: 0, failure: 0, total: 5
Using version ^0.3.1@beta for korchasa/php-vhs
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - korchasa/php-vhs 0.3.1-beta requires korchasa/matcho ^0.5 -> satisfiable by korchasa/matcho[0.5].
    - Installation request for korchasa/php-vhs ^0.3.1@beta -> satisfiable by korchasa/php-vhs[0.3.1-beta].
    - Conclusion: remove sebastian/diff 3.0.2
    - Conclusion: don't install sebastian/diff 3.0.2
    - korchasa/matcho 0.5 requires sebastian/diff ^2.0 -> satisfiable by sebastian/diff[2.0.1, 2.0.x-dev].
    - Can only install one of: sebastian/diff[2.0.1, 3.0.2].
    - Can only install one of: sebastian/diff[2.0.x-dev, 3.0.2].
    - Installation request for sebastian/diff (locked at 3.0.2) -> satisfiable by sebastian/diff[3.0.2].


Installation failed, reverting ./composer.json to its original content.

New server testing implementation

class MyAwesomePackagistServerTest extends TestCase
{
    use VhsServerTestCase;

    public function testSuccessSignUp()
    {
        $this->assertValidServerResponse('cassetes/*');
    }
}```

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.