PHPackages                             angelov/phpunit-php-vcr - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. angelov/phpunit-php-vcr

ActiveLibrary[Testing &amp; Quality](/categories/testing)

angelov/phpunit-php-vcr
=======================

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

v1.3(4mo ago)627.1k↓43.9%3MITPHPPHP ^8.2CI passing

Since Mar 13Pushed 4mo ago1 watchersCompare

[ Source](https://github.com/angelov/phpunit-php-vcr)[ Packagist](https://packagist.org/packages/angelov/phpunit-php-vcr)[ RSS](/packages/angelov-phpunit-php-vcr/feed)WikiDiscussions main Synced 2d ago

READMEChangelog (6)Dependencies (4)Versions (11)Used By (0)

PHP-VCR integration for PHPUnit
===============================

[](#php-vcr-integration-for-phpunit)

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

Requirements
------------

[](#requirements)

- PHP 8.2+
- PHPUnit 10+

Installation
------------

[](#installation)

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

```

Then, add the extension to your PHPUnit configuration file.

(All parameters are optional.)

```

```

Usage
-----

[](#usage)

The library provides an `UseCassette` attribute that can be declared on test classes or specific test methods. The attribute accepts a cassette name and optional parameters for advanced functionality like separate cassettes per data provider case.

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.

    ```
    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.

    ```
    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`.

    ```
    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 { ... }
    }
    ```

DataProvider Support
--------------------

[](#dataprovider-support)

The library supports PHPUnit's `DataProvider` functionality with additional options for managing cassettes when using data providers.

### Basic DataProvider Usage

[](#basic-dataprovider-usage)

When using a data provider with the basic `UseCassette` attribute, all test cases from the data provider will share the same cassette file:

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

class ExampleTest extends TestCase
{
    #[Test]
    #[UseCassette("shared_cassette.yml")]
    #[DataProvider("urls")]
    public function testWithDataProvider(string $url): void
    {
        $content = file_get_contents($url);
        // All test cases will use the same cassette file
    }

    public static function urls(): iterable
    {
        yield ["https://example.com"];
        yield ["https://example.org"];
    }
}
```

### Separate Cassettes Per DataProvider Case

[](#separate-cassettes-per-dataprovider-case)

For more granular control, you can create separate cassette files for each data provider case using the `separateCassettePerCase` parameter:

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

class ExampleTest extends TestCase
{
    #[Test]
    #[UseCassette(name: "separate_cassettes.yml", separateCassettePerCase: true)]
    #[DataProvider("urls")]
    public function testWithSeparateCassettes(string $url): void
    {
        $content = file_get_contents($url);
        // Each test case will have its own cassette file:
        // - separate_cassettes-0.yml
        // - separate_cassettes-1.yml
    }

    public static function urls(): iterable
    {
        yield ["https://example.com"];
        yield ["https://example.org"];
    }
}
```

### Named DataProvider Cases

[](#named-dataprovider-cases)

When using named data provider cases, the cassette files will use the case names:

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

class ExampleTest extends TestCase
{
    #[Test]
    #[UseCassette(name: "named_cassettes.yml", separateCassettePerCase: true)]
    #[DataProvider("namedUrls")]
    public function testWithNamedCassettes(string $url): void
    {
        $content = file_get_contents($url);
        // Each test case will have its own cassette file:
        // - named_cassettes-example-com.yml
        // - named_cassettes-example-org.yml
    }

    public static function namedUrls(): iterable
    {
        yield 'example.com' => ["https://example.com"];
        yield 'example.org' => ["https://example.org"];
    }
}
```

### Grouping Cassettes in Directories

[](#grouping-cassettes-in-directories)

To organize separate cassette files in directories, use the `groupCaseFilesInDirectory` parameter:

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

class ExampleTest extends TestCase
{
    #[Test]
    #[UseCassette(
        name: "organized_cassettes.yml",
        separateCassettePerCase: true,
        groupCaseFilesInDirectory: true
    )]
    #[DataProvider("urls")]
    public function testWithOrganizedCassettes(string $url): void
    {
        $content = file_get_contents($url);
        // Cassette files will be organized in a directory:
        // - organized_cassettes/0.yml
        // - organized_cassettes/1.yml
    }

    public static function urls(): iterable
    {
        yield ["https://example.com"];
        yield ["https://example.org"];
    }
}
```

### Class-Level DataProvider Support

[](#class-level-dataprovider-support)

The dataProvider functionality also works when the `UseCassette` attribute is declared at the class level:

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

#[UseCassette(name: "class_level.yml", separateCassettePerCase: true)]
class ExampleTest extends TestCase
{
    #[Test]
    #[DataProvider("urls")]
    public function testMethod(string $url): void
    {
        $content = file_get_contents($url);
        // Each test case will have separate cassettes:
        // - class_level-0.yml
        // - class_level-1.yml
    }

    public static function urls(): iterable
    {
        yield ["https://example.com"];
        yield ["https://example.org"];
    }
}
```

###  Health Score

51

—

FairBetter than 95% of packages

Maintenance76

Regular maintenance activity

Popularity36

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 84.2% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~215 days

Recently: every ~185 days

Total

6

Last Release

132d ago

PHP version history (2 changes)v1.0PHP ^8.1

v1.1PHP ^8.2

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/138429?v=4)[Dejan Angelov](/maintainers/angelov)[@angelov](https://github.com/angelov)

---

Top Contributors

[![angelov](https://avatars.githubusercontent.com/u/138429?v=4)](https://github.com/angelov "angelov (16 commits)")[![cb-vova](https://avatars.githubusercontent.com/u/41348702?v=4)](https://github.com/cb-vova "cb-vova (1 commits)")[![daycry](https://avatars.githubusercontent.com/u/7590335?v=4)](https://github.com/daycry "daycry (1 commits)")[![nicodemuz](https://avatars.githubusercontent.com/u/1620454?v=4)](https://github.com/nicodemuz "nicodemuz (1 commits)")

---

Tags

php-vcrphpunittestingtestingphpunitunittestsvcrphp-vcr

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/angelov-phpunit-php-vcr/health.svg)

```
[![Health](https://phpackages.com/badges/angelov-phpunit-php-vcr/health.svg)](https://phpackages.com/packages/angelov-phpunit-php-vcr)
```

###  Alternatives

[dg/bypass-finals

Removes final keyword from source code on-the-fly and allows mocking of final methods and classes

57129.2M612](/packages/dg-bypass-finals)[nette/tester

Nette Tester: enjoyable unit testing in PHP with code coverage reporter. 🍏🍏🍎🍏

4957.6M1.7k](/packages/nette-tester)[janmarek/mockista

Mockista is library for mocking, which I've written, because I find mocking in PHPUnit awful.

29222.1k28](/packages/janmarek-mockista)[tcz/phpunit-mockfunction

PHPUnit extension that uses runkit to mock PHP functions (both user-defined and system)

4950.4k2](/packages/tcz-phpunit-mockfunction)[code-distortion/adapt

A Laravel package that builds databases for your tests, improving their speed.

2838.1k](/packages/code-distortion-adapt)[socialpoint/parallel-phpunit

Parallel Test Cases Runner for PHPUnit

1418.2k](/packages/socialpoint-parallel-phpunit)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
