{"id":21035233,"url":"https://github.com/angelov/phpunit-php-vcr","last_synced_at":"2026-02-21T13:04:47.539Z","repository":{"id":142494159,"uuid":"612976038","full_name":"angelov/phpunit-php-vcr","owner":"angelov","description":"A library that allows you to easily use the PHP-VCR library in your PHPUnit tests.","archived":false,"fork":false,"pushed_at":"2025-04-13T18:09:21.000Z","size":37,"stargazers_count":6,"open_issues_count":2,"forks_count":2,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-06-15T16:46:13.262Z","etag":null,"topics":["php-vcr","phpunit","testing"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/angelov.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-03-12T14:44:10.000Z","updated_at":"2025-03-22T09:31:15.000Z","dependencies_parsed_at":null,"dependency_job_id":"58b4248c-f148-4500-9df8-71281b6a2d55","html_url":"https://github.com/angelov/phpunit-php-vcr","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/angelov/phpunit-php-vcr","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/angelov%2Fphpunit-php-vcr","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/angelov%2Fphpunit-php-vcr/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/angelov%2Fphpunit-php-vcr/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/angelov%2Fphpunit-php-vcr/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/angelov","download_url":"https://codeload.github.com/angelov/phpunit-php-vcr/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/angelov%2Fphpunit-php-vcr/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265325004,"owners_count":23747198,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["php-vcr","phpunit","testing"],"created_at":"2024-11-19T13:14:07.377Z","updated_at":"2026-02-21T13:04:47.526Z","avatar_url":"https://github.com/angelov.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PHP-VCR integration for PHPUnit\n\nA library that allows you to easily use the PHP-VCR library in your PHPUnit tests.\n\n## Requirements\n\n* PHP 8.2+\n* PHPUnit 10+\n\n## Installation\n\n```\ncomposer require --dev angelov/phpunit-php-vcr\n```\n\nThen, add the extension to your PHPUnit configuration file.\n\n(All parameters are optional.)\n\n```xml\n    \u003cextensions\u003e\n        \u003cbootstrap class=\"\\Angelov\\PHPUnitPHPVcr\\Extension\"\u003e\n            \u003cparameter name=\"cassettesPath\" value=\"tests/fixtures\" /\u003e\n            \u003cparameter name=\"storage\" value=\"yaml\" /\u003e                                   \u003c!-- https://php-vcr.github.io/documentation/configuration/#storage --\u003e\n            \u003cparameter name=\"libraryHooks\" value=\"stream_wrapper, curl, soap\" /\u003e        \u003c!-- https://php-vcr.github.io/documentation/configuration/#library-hooks --\u003e\n            \u003cparameter name=\"requestMatchers\" value=\"method, url, query_string, ...\" /\u003e \u003c!-- https://php-vcr.github.io/documentation/configuration/#request-matching --\u003e\n            \u003cparameter name=\"whitelistedPaths\" value=\"\" /\u003e                              \u003c!-- https://php-vcr.github.io/documentation/configuration/#white--and-blacklisting-paths --\u003e\n            \u003cparameter name=\"blacklistedPaths\" value=\"\" /\u003e                              \u003c!-- https://php-vcr.github.io/documentation/configuration/#white--and-blacklisting-paths --\u003e\n            \u003cparameter name=\"mode\" value=\"new_episodes\" /\u003e                              \u003c!-- https://php-vcr.github.io/documentation/configuration/#record-modes --\u003e\n        \u003c/bootstrap\u003e\n    \u003c/extensions\u003e\n```\n\n## Usage\n\nThe library provides an `UseCassette` attribute that can be declared on test classes or specific test methods. The \nattribute accepts a cassette name and optional parameters for advanced functionality like separate cassettes per \ndata provider case.\n\nWhen running the tests, the library will automatically turn the recorder on and off, and insert the cassettes when \nneeded.\n\n**Examples:**\n\n* When declared on a class, PHP-VCR will intercept the requests in all test methods in that class, and will store the \nresponses in the given cassette.\n\n    ```php\n    use Angelov\\PHPUnitPHPVcr\\UseCassette;\n    use PHPUnit\\Framework\\Attributes\\Test;\n    use PHPUnit\\Framework\\TestCase;\n\n    #[UseCassette(\"example_cassette.yml\")]\n    class ExampleTest extends TestCase\n    {\n        #[Test]\n        public function example(): void { ... }\n\n        #[Test]\n        public function another(): void { ... }\n    }\n    ```\n\n* When declared on a test method, only requests in that methods will be intercepted and stored in the given cassette. \nNote that it can be declared on multiple test methods with different cassettes.\n\n    ```php\n    use Angelov\\PHPUnitPHPVcr\\UseCassette;\n    use PHPUnit\\Framework\\Attributes\\Test;\n    use PHPUnit\\Framework\\TestCase;\n\n    class ExampleTest extends TestCase\n    {\n        #[Test]\n        #[UseCassette(\"example.yml\")]\n        public function example(): void { ... }\n\n        #[Test]\n        public function another(): void { ... }\n\n        #[Test]\n        #[UseCassette(\"example_2.yml\")]\n        public function recorded(): void { ... }\n    }\n    ```\n\n* When declared both on the class and on a specific method, the name from the attribute declared on the method will be \nused for that method. In this example, the responses from the requests made in the `example()` method will be stored in \n`example.yml` and the ones from `recorded()` in `example_2.yml`.\n\n    ```php\n    use Angelov\\PHPUnitPHPVcr\\UseCassette;\n    use PHPUnit\\Framework\\Attributes\\Test;\n    use PHPUnit\\Framework\\TestCase;\n\n    #[UseCassette(\"example.yml\")]\n    class ExampleTest extends TestCase\n    {\n        #[Test]\n        public function example(): void { ... }\n\n        #[Test]\n        #[UseCassette(\"example_2.yml\")]\n        public function recorded(): void { ... }\n    }\n    ```\n\n## DataProvider Support\n\nThe library supports PHPUnit's `DataProvider` functionality with additional options for managing cassettes when using data providers.\n\n### Basic DataProvider Usage\n\nWhen using a data provider with the basic `UseCassette` attribute, all test cases from the data provider will share the same cassette file:\n\n```php\nuse Angelov\\PHPUnitPHPVcr\\UseCassette;\nuse PHPUnit\\Framework\\Attributes\\DataProvider;\nuse PHPUnit\\Framework\\Attributes\\Test;\nuse PHPUnit\\Framework\\TestCase;\n\nclass ExampleTest extends TestCase\n{\n    #[Test]\n    #[UseCassette(\"shared_cassette.yml\")]\n    #[DataProvider(\"urls\")]\n    public function testWithDataProvider(string $url): void\n    {\n        $content = file_get_contents($url);\n        // All test cases will use the same cassette file\n    }\n\n    public static function urls(): iterable\n    {\n        yield [\"https://example.com\"];\n        yield [\"https://example.org\"];\n    }\n}\n```\n\n### Separate Cassettes Per DataProvider Case\n\nFor more granular control, you can create separate cassette files for each data provider case using the `separateCassettePerCase` parameter:\n\n```php\nuse Angelov\\PHPUnitPHPVcr\\UseCassette;\nuse PHPUnit\\Framework\\Attributes\\DataProvider;\nuse PHPUnit\\Framework\\Attributes\\Test;\nuse PHPUnit\\Framework\\TestCase;\n\nclass ExampleTest extends TestCase\n{\n    #[Test]\n    #[UseCassette(name: \"separate_cassettes.yml\", separateCassettePerCase: true)]\n    #[DataProvider(\"urls\")]\n    public function testWithSeparateCassettes(string $url): void\n    {\n        $content = file_get_contents($url);\n        // Each test case will have its own cassette file:\n        // - separate_cassettes-0.yml\n        // - separate_cassettes-1.yml\n    }\n\n    public static function urls(): iterable\n    {\n        yield [\"https://example.com\"];\n        yield [\"https://example.org\"];\n    }\n}\n```\n\n### Named DataProvider Cases\n\nWhen using named data provider cases, the cassette files will use the case names:\n\n```php\nuse Angelov\\PHPUnitPHPVcr\\UseCassette;\nuse PHPUnit\\Framework\\Attributes\\DataProvider;\nuse PHPUnit\\Framework\\Attributes\\Test;\nuse PHPUnit\\Framework\\TestCase;\n\nclass ExampleTest extends TestCase\n{\n    #[Test]\n    #[UseCassette(name: \"named_cassettes.yml\", separateCassettePerCase: true)]\n    #[DataProvider(\"namedUrls\")]\n    public function testWithNamedCassettes(string $url): void\n    {\n        $content = file_get_contents($url);\n        // Each test case will have its own cassette file:\n        // - named_cassettes-example-com.yml\n        // - named_cassettes-example-org.yml\n    }\n\n    public static function namedUrls(): iterable\n    {\n        yield 'example.com' =\u003e [\"https://example.com\"];\n        yield 'example.org' =\u003e [\"https://example.org\"];\n    }\n}\n```\n\n### Grouping Cassettes in Directories\n\nTo organize separate cassette files in directories, use the `groupCaseFilesInDirectory` parameter:\n\n```php\nuse Angelov\\PHPUnitPHPVcr\\UseCassette;\nuse PHPUnit\\Framework\\Attributes\\DataProvider;\nuse PHPUnit\\Framework\\Attributes\\Test;\nuse PHPUnit\\Framework\\TestCase;\n\nclass ExampleTest extends TestCase\n{\n    #[Test]\n    #[UseCassette(\n        name: \"organized_cassettes.yml\",\n        separateCassettePerCase: true,\n        groupCaseFilesInDirectory: true\n    )]\n    #[DataProvider(\"urls\")]\n    public function testWithOrganizedCassettes(string $url): void\n    {\n        $content = file_get_contents($url);\n        // Cassette files will be organized in a directory:\n        // - organized_cassettes/0.yml\n        // - organized_cassettes/1.yml\n    }\n\n    public static function urls(): iterable\n    {\n        yield [\"https://example.com\"];\n        yield [\"https://example.org\"];\n    }\n}\n```\n\n### Class-Level DataProvider Support\n\nThe dataProvider functionality also works when the `UseCassette` attribute is declared at the class level:\n\n```php\nuse Angelov\\PHPUnitPHPVcr\\UseCassette;\nuse PHPUnit\\Framework\\Attributes\\DataProvider;\nuse PHPUnit\\Framework\\Attributes\\Test;\nuse PHPUnit\\Framework\\TestCase;\n\n#[UseCassette(name: \"class_level.yml\", separateCassettePerCase: true)]\nclass ExampleTest extends TestCase\n{\n    #[Test]\n    #[DataProvider(\"urls\")]\n    public function testMethod(string $url): void\n    {\n        $content = file_get_contents($url);\n        // Each test case will have separate cassettes:\n        // - class_level-0.yml\n        // - class_level-1.yml\n    }\n\n    public static function urls(): iterable\n    {\n        yield [\"https://example.com\"];\n        yield [\"https://example.org\"];\n    }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fangelov%2Fphpunit-php-vcr","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fangelov%2Fphpunit-php-vcr","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fangelov%2Fphpunit-php-vcr/lists"}