Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/angelov/phpunit-php-vcr

A library that allows you to easily use the PHP-VCR library in your PHPUnit tests.
https://github.com/angelov/phpunit-php-vcr

php-vcr phpunit testing

Last synced: 2 months ago
JSON representation

A library that allows you to easily use the PHP-VCR library in your PHPUnit tests.

Awesome Lists containing this project

README

        

# PHP-VCR integration for PHPUnit

A library that allows you to easily use the PHP-VCR library in your PHPUnit tests.

## Requirements

* PHP 8.2+
* PHPUnit 10+

## Installation

```
composer require --dev angelov/phpunit-php-vcr
```

Then, add the extension to your PHPUnit configuration file.

(All parameters are optional.)

```xml











```

## Usage

The library provides an `UseCassette` attribute that can be declared on test classes or specific test methods. The
attribute expects one string argument - the name of the cassette.

When running the tests, the library will automatically turn the recorder on and off, and insert the cassettes when
needed.

**Examples:**

* When declared on a class, PHP-VCR will intercept the requests in all test methods in that class, and will store the
responses in the given cassette.

```php
use Angelov\PHPUnitPHPVcr\UseCassette;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

#[UseCassette("example_cassette.yml")]
class ExampleTest extends TestCase
{
#[Test]
public function example(): void { ... }

#[Test]
public function another(): void { ... }
}
```

* When declared on a test method, only requests in that methods will be intercepted and stored in the given cassette.
Note that it can be declared on multiple test methods with different cassettes.

```php
use Angelov\PHPUnitPHPVcr\UseCassette;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

class ExampleTest extends TestCase
{
#[Test]
#[UseCassette("example.yml")]
public function example(): void { ... }

#[Test]
public function another(): void { ... }

#[Test]
#[UseCassette("example_2.yml")]
public function recorded(): void { ... }
}
```

* When declared both on the class and on a specific method, the name from the attribute declared on the method will be
used for that method. In this example, the responses from the requests made in the `example()` method will be stored in
`example.yml` and the ones from `recorded()` in `example_2.yml`.

```php
use Angelov\PHPUnitPHPVcr\UseCassette;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

#[UseCassette("example.yml")]
class ExampleTest extends TestCase
{
#[Test]
public function example(): void { ... }

#[Test]
#[UseCassette("example_2.yml")]
public function recorded(): void { ... }
}
```