PHP code example of blob-solutions / laravel-vcr-am
1. Go to this page and download the library: Download blob-solutions/laravel-vcr-am library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use BlobSolutions\LaravelVcrAm\Facades\VcrAm;
use BlobSolutions\VcrAm\Input\RegisterSaleInput;
$response = VcrAm::registerSale(new RegisterSaleInput(/* ... */));
use BlobSolutions\VcrAm\VcrClient;
class CheckoutController
{
public function __construct(private readonly VcrClient $vcr) {}
public function __invoke(): RedirectResponse
{
$response = $this->vcr->registerSale(/* ... */);
// ...
}
}
// Plain array — turned into a 200 JSON response.
VcrAm::fake([
'POST /sales' => ['urlId' => 'X', /* ... */],
]);
// Closure — receives the captured PSR-7 request, returns either a
// ResponseInterface or a plain array.
VcrAm::fake([
'POST /sales' => function (Psr\Http\Message\RequestInterface $request) {
return ['urlId' => 'dyn', /* ... */];
},
]);
// Throw a server-side error to test how your code handles failures.
use Nyholm\Psr7\Response;
VcrAm::fake([
'POST /sales' => fn () => new Response(
400,
['Content-Type' => 'application/json'],
'{"code":"VALIDATION","message":"price must be positive"}',
),
]);
VcrAm::assertSent('POST /sales'); // any matching request
VcrAm::assertSent('POST /sales', fn (?array $body) => /* ... */); // narrow on body
VcrAm::assertNotSent('POST /prepayments'); // negative assertion
VcrAm::assertNothingSent(); // no SDK calls at all
VcrAm::assertSentCount(2); // exact request count
VcrAm::registerSale($input); // hits the production VCR
VcrAm::sandbox()->registerSale($input); // hits the sandbox VCR
use BlobSolutions\LaravelVcrAm\VcrAmServiceProvider;
use BlobSolutions\VcrAm\VcrClient;
$sandbox = app(VcrAmServiceProvider::SANDBOX_BINDING); // returns VcrClient