Dshafik\GuzzleHttp\VcrHandler::turnOn PHP Method

turnOn() public static method

public static turnOn ( string $cassette ) : GuzzleHttp\HandlerStack
$cassette string fixture path
return GuzzleHttp\HandlerStack
    public static function turnOn($cassette)
    {
        if (!file_exists($cassette)) {
            $handler = \GuzzleHttp\HandlerStack::create();
            $handler->after('allow_redirects', new static($cassette));
            return $handler;
        } else {
            $responses = json_decode(file_get_contents($cassette), true);
            $queue = [];
            $class = new \ReflectionClass(\GuzzleHttp\Psr7\Response::class);
            foreach ($responses as $response) {
                $queue[] = $class->newInstanceArgs($response);
            }
            return \GuzzleHttp\HandlerStack::create(new \GuzzleHttp\Handler\MockHandler($queue));
        }
    }

Usage Example

 public function testExisting()
 {
     $vcr = \Dshafik\GuzzleHttp\VcrHandler::turnOn(__DIR__ . '/fixtures/test-existing.json');
     $client = new \GuzzleHttp\Client(['handler' => $vcr]);
     $response = $client->get('/test');
     $this->assertEquals(200, $response->getStatusCode());
     $this->assertArrayHasKey('X-VCR-Recording', $response->getHeaders());
     $this->assertEquals("1440121471", $response->getHeader('X-VCR-Recording')[0]);
     $this->assertEquals('Hello World', (string) $response->getBody());
 }
All Usage Examples Of Dshafik\GuzzleHttp\VcrHandler::turnOn