From b34f6552e8a3a4bbb286f91372bd7f62dc984a76 Mon Sep 17 00:00:00 2001 From: cg-x <53591541+cg-x@users.noreply.github.com> Date: Tue, 1 Oct 2024 15:18:41 +1000 Subject: [PATCH] Allow to specify file via Vcr attribute (#8) * Updated to accept vcr attribute * Updated readme usage example --- README.md | 9 +++++++++ src/Attributes/Vcr.php | 35 +++++++++++++++++++++++++++++++++++ tests/VCRTestListenerTest.php | 9 +++++++++ 3 files changed, 53 insertions(+) create mode 100644 src/Attributes/Vcr.php diff --git a/README.md b/README.md index c149789..831d626 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ Use `@vcr cassette_name` on your tests to turn VCR automatically on and off. ``` php use PHPUnit\Framework\TestCase; +use VCR\PHPUnit\TestListener\Attributes\Vcr; class VCRTest extends TestCase { @@ -30,6 +31,14 @@ class VCRTest extends TestCase $result = file_get_contents('http://google.com'); $this->assertEquals('This is a annotation test dummy.', $result, 'Call was not intercepted (using annotations).'); } + + #[Vcr('unittest_annotation_test')] + public function testInterceptsWithAttributes(): void + { + // Content of tests/fixtures/unittest_annotation_test: "This is a annotation test dummy". + $result = file_get_contents('http://google.com'); + $this->assertEquals('This is a annotation test dummy.', $result, 'Call was not intercepted (using attributes).'); + } } ``` diff --git a/src/Attributes/Vcr.php b/src/Attributes/Vcr.php new file mode 100644 index 0000000..e763719 --- /dev/null +++ b/src/Attributes/Vcr.php @@ -0,0 +1,35 @@ +cassette = $cassette; + } + + /** + * @return non-empty-string + */ + public function cassette(): string + { + return $this->cassette; + } +} diff --git a/tests/VCRTestListenerTest.php b/tests/VCRTestListenerTest.php index 09c55bf..e297fa0 100644 --- a/tests/VCRTestListenerTest.php +++ b/tests/VCRTestListenerTest.php @@ -4,6 +4,7 @@ namespace Tests\VCR\PHPUnit\TestListener; use PHPUnit\Framework\TestCase; +use VCR\PHPUnit\TestListener\Attributes\Vcr; final class VCRTestListenerTest extends TestCase { @@ -48,6 +49,14 @@ public function testNoVcrAnnotationRunsSuccessfulAndDoesNotProduceWarnings() $this->assertTrue(true, 'just adding an assertion here'); } + #[Vcr('unittest_annotation_test')] + public function testInterceptsWithAttributes(): void + { + // Content of tests/fixtures/unittest_annotation_test: "This is a annotation test dummy". + $result = file_get_contents('http://google.com'); + $this->assertEquals('This is a annotation test dummy.', $result, 'Call was not intercepted (using attributes).'); + } + /** * @return \int[][] */