|
| 1 | +# PHP-VCR integration for PHPUnit |
| 2 | + |
| 3 | +A library that allows you to easily use the PHP-VCR library in your PHPUnit tests. |
| 4 | + |
| 5 | +## Requirements |
| 6 | + |
| 7 | +* PHP 8.1+ |
| 8 | +* PHPUnit 10+ |
| 9 | + |
| 10 | +## Installation |
| 11 | + |
| 12 | +``` |
| 13 | +composer require --dev angelov/phpunit-php-vcr |
| 14 | +``` |
| 15 | + |
| 16 | +Then, add the extension to your PHPUnit configuration file. |
| 17 | + |
| 18 | +(All parameters are optional.) |
| 19 | + |
| 20 | +```xml |
| 21 | + <extensions> |
| 22 | + <bootstrap class="\Angelov\PHPUnitPHPVcr\Extension"> |
| 23 | + <parameter name="cassettesPath" value="tests/fixtures" /> |
| 24 | + <parameter name="storage" value="yaml" /> <!-- https://php-vcr.github.io/documentation/configuration/#storage --> |
| 25 | + <parameter name="libraryHooks" value="stream_wrapper, curl, soap" /> <!-- https://php-vcr.github.io/documentation/configuration/#library-hooks --> |
| 26 | + <parameter name="requestMatchers" value="method, url, query_string, ..." /> <!-- https://php-vcr.github.io/documentation/configuration/#request-matching --> |
| 27 | + <parameter name="whitelistedPaths" value="" /> <!-- https://php-vcr.github.io/documentation/configuration/#white--and-blacklisting-paths --> |
| 28 | + <parameter name="blacklistedPaths" value="" /> <!-- https://php-vcr.github.io/documentation/configuration/#white--and-blacklisting-paths --> |
| 29 | + <parameter name="mode" value="new_episodes" /> <!-- https://php-vcr.github.io/documentation/configuration/#record-modes --> |
| 30 | + </bootstrap> |
| 31 | + </extensions> |
| 32 | +``` |
| 33 | + |
| 34 | +## Usage |
| 35 | + |
| 36 | +The 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. |
| 38 | + |
| 39 | +When running the tests, the library will automatically turn the recorder on and off, and insert the cassettes when |
| 40 | +needed. |
| 41 | + |
| 42 | +**Examples:** |
| 43 | + |
| 44 | +* When declared on a class, PHP-VCR will intercept the requests in all test methods in that class, and will store the |
| 45 | +responses in the given cassette. |
| 46 | + |
| 47 | + ```php |
| 48 | + use Angelov\PHPUnitPHPVcr\UseCassette; |
| 49 | + use PHPUnit\Framework\Attributes\Test; |
| 50 | + use PHPUnit\Framework\TestCase; |
| 51 | + |
| 52 | + #[UseCassette("example_cassette.yml")] |
| 53 | + class ExampleTest extends TestCase |
| 54 | + { |
| 55 | + #[Test] |
| 56 | + public function example(): void { ... } |
| 57 | + |
| 58 | + #[Test] |
| 59 | + public function another(): void { ... } |
| 60 | + } |
| 61 | + ``` |
| 62 | + |
| 63 | +* When declared on a test method, only requests in that methods will be intercepted and stored in the given cassette. |
| 64 | +Note that it can applied on multiple test methods with different cassettes. |
| 65 | + |
| 66 | + ```php |
| 67 | + use Angelov\PHPUnitPHPVcr\UseCassette; |
| 68 | + use PHPUnit\Framework\Attributes\Test; |
| 69 | + use PHPUnit\Framework\TestCase; |
| 70 | + |
| 71 | + class ExampleTest extends TestCase |
| 72 | + { |
| 73 | + #[Test] |
| 74 | + #[UseCassette("example.yml")] |
| 75 | + public function example(): void { ... } |
| 76 | + |
| 77 | + #[Test] |
| 78 | + public function another(): void { ... } |
| 79 | + |
| 80 | + #[Test] |
| 81 | + #[UseCassette("example_2.yml")] |
| 82 | + public function recorded(): void { ... } |
| 83 | + } |
| 84 | + ``` |
0 commit comments