korchasa / php-vhs
HTTP request/response recording and mock library for PHP
Installs: 2 803
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: >=7.1
- ext-json: *
- korchasa/matcho: ^1.1
Requires (Dev)
- guzzlehttp/guzzle: ^6.3
- infection/infection: ^0.12
- phpstan/phpstan: ^0.10
- phpunit/phpunit: ^6.0 || ^7.0
- rappasoft/laravel-helpers: dev-master
- roave/security-advisories: dev-master
- squizlabs/php_codesniffer: ^2.9.2 || ^3.3.1
This package is auto-updated.
Last update: 2022-09-23 18:03:51 UTC
README
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.