11# PHP-VCR integration for PHPUnit
22
3+ ![ Coverage] ( https://raw.githubusercontent.com/angelov/phpunit-php-vcr/image-data/coverage.svg )
4+
35A library that allows you to easily use the PHP-VCR library in your PHPUnit tests.
46
57## Requirements
@@ -34,7 +36,8 @@ Then, add the extension to your PHPUnit configuration file.
3436## Usage
3537
3638The library provides an ` UseCassette ` attribute that can be declared on test classes or specific test methods. The
37- attribute expects one string argument - the name of the cassette.
39+ attribute accepts a cassette name and optional parameters for advanced functionality like separate cassettes per
40+ data provider case.
3841
3942When running the tests, the library will automatically turn the recorder on and off, and insert the cassettes when
4043needed.
@@ -54,7 +57,7 @@ responses in the given cassette.
5457 {
5558 #[Test]
5659 public function example(): void { ... }
57-
60+
5861 #[Test]
5962 public function another(): void { ... }
6063 }
@@ -102,4 +105,165 @@ used for that method. In this example, the responses from the requests made in t
102105 #[UseCassette("example_2.yml")]
103106 public function recorded(): void { ... }
104107 }
105- ```
108+ ```
109+
110+ ## DataProvider Support
111+
112+ The library supports PHPUnit's ` DataProvider ` functionality with additional options for managing cassettes when using data providers.
113+
114+ ### Basic DataProvider Usage
115+
116+ When using a data provider with the basic ` UseCassette ` attribute, all test cases from the data provider will share the same cassette file:
117+
118+ ``` php
119+ use Angelov\PHPUnitPHPVcr\UseCassette;
120+ use PHPUnit\Framework\Attributes\DataProvider;
121+ use PHPUnit\Framework\Attributes\Test;
122+ use PHPUnit\Framework\TestCase;
123+
124+ class ExampleTest extends TestCase
125+ {
126+ #[Test]
127+ #[UseCassette("shared_cassette.yml")]
128+ #[DataProvider("urls")]
129+ public function testWithDataProvider(string $url): void
130+ {
131+ $content = file_get_contents($url);
132+ // All test cases will use the same cassette file
133+ }
134+
135+ public static function urls(): iterable
136+ {
137+ yield ["https://example.com"];
138+ yield ["https://example.org"];
139+ }
140+ }
141+ ```
142+
143+ ### Separate Cassettes Per DataProvider Case
144+
145+ For more granular control, you can create separate cassette files for each data provider case using the ` separateCassettePerCase ` parameter:
146+
147+ ``` php
148+ use Angelov\PHPUnitPHPVcr\UseCassette;
149+ use PHPUnit\Framework\Attributes\DataProvider;
150+ use PHPUnit\Framework\Attributes\Test;
151+ use PHPUnit\Framework\TestCase;
152+
153+ class ExampleTest extends TestCase
154+ {
155+ #[Test]
156+ #[UseCassette(name: "separate_cassettes.yml", separateCassettePerCase: true)]
157+ #[DataProvider("urls")]
158+ public function testWithSeparateCassettes(string $url): void
159+ {
160+ $content = file_get_contents($url);
161+ // Each test case will have its own cassette file:
162+ // - separate_cassettes-0.yml
163+ // - separate_cassettes-1.yml
164+ }
165+
166+ public static function urls(): iterable
167+ {
168+ yield ["https://example.com"];
169+ yield ["https://example.org"];
170+ }
171+ }
172+ ```
173+
174+ ### Named DataProvider Cases
175+
176+ When using named data provider cases, the cassette files will use the case names:
177+
178+ ``` php
179+ use Angelov\PHPUnitPHPVcr\UseCassette;
180+ use PHPUnit\Framework\Attributes\DataProvider;
181+ use PHPUnit\Framework\Attributes\Test;
182+ use PHPUnit\Framework\TestCase;
183+
184+ class ExampleTest extends TestCase
185+ {
186+ #[Test]
187+ #[UseCassette(name: "named_cassettes.yml", separateCassettePerCase: true)]
188+ #[DataProvider("namedUrls")]
189+ public function testWithNamedCassettes(string $url): void
190+ {
191+ $content = file_get_contents($url);
192+ // Each test case will have its own cassette file:
193+ // - named_cassettes-example-com.yml
194+ // - named_cassettes-example-org.yml
195+ }
196+
197+ public static function namedUrls(): iterable
198+ {
199+ yield 'example.com' => ["https://example.com"];
200+ yield 'example.org' => ["https://example.org"];
201+ }
202+ }
203+ ```
204+
205+ ### Grouping Cassettes in Directories
206+
207+ To organize separate cassette files in directories, use the ` groupCaseFilesInDirectory ` parameter:
208+
209+ ``` php
210+ use Angelov\PHPUnitPHPVcr\UseCassette;
211+ use PHPUnit\Framework\Attributes\DataProvider;
212+ use PHPUnit\Framework\Attributes\Test;
213+ use PHPUnit\Framework\TestCase;
214+
215+ class ExampleTest extends TestCase
216+ {
217+ #[Test]
218+ #[UseCassette(
219+ name: "organized_cassettes.yml",
220+ separateCassettePerCase: true,
221+ groupCaseFilesInDirectory: true
222+ )]
223+ #[DataProvider("urls")]
224+ public function testWithOrganizedCassettes(string $url): void
225+ {
226+ $content = file_get_contents($url);
227+ // Cassette files will be organized in a directory:
228+ // - organized_cassettes/0.yml
229+ // - organized_cassettes/1.yml
230+ }
231+
232+ public static function urls(): iterable
233+ {
234+ yield ["https://example.com"];
235+ yield ["https://example.org"];
236+ }
237+ }
238+ ```
239+
240+ ### Class-Level DataProvider Support
241+
242+ The dataProvider functionality also works when the ` UseCassette ` attribute is declared at the class level:
243+
244+ ``` php
245+ use Angelov\PHPUnitPHPVcr\UseCassette;
246+ use PHPUnit\Framework\Attributes\DataProvider;
247+ use PHPUnit\Framework\Attributes\Test;
248+ use PHPUnit\Framework\TestCase;
249+
250+ #[UseCassette(name: "class_level.yml", separateCassettePerCase: true)]
251+ class ExampleTest extends TestCase
252+ {
253+ #[Test]
254+ #[DataProvider("urls")]
255+ public function testMethod(string $url): void
256+ {
257+ $content = file_get_contents($url);
258+ // Each test case will have separate cassettes:
259+ // - class_level-0.yml
260+ // - class_level-1.yml
261+ }
262+
263+ public static function urls(): iterable
264+ {
265+ yield ["https://example.com"];
266+ yield ["https://example.org"];
267+ }
268+ }
269+ ```
0 commit comments