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