From 24a9f73ca8263bd11409cf03179f743be9196804 Mon Sep 17 00:00:00 2001 From: Astrid Blanco Date: Fri, 20 Sep 2024 13:59:17 +1000 Subject: [PATCH 1/3] Updated to accept vcr attribute --- src/Attributes/Vcr.php | 35 +++++++++++++++++++++++++++++++++++ tests/VCRTestListenerTest.php | 9 +++++++++ 2 files changed, 44 insertions(+) create mode 100644 src/Attributes/Vcr.php 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[][] */ From 3d489dfc951ffab2fd710bd65d0cc9b10f6fa749 Mon Sep 17 00:00:00 2001 From: Astrid Blanco Date: Fri, 20 Sep 2024 14:20:07 +1000 Subject: [PATCH 2/3] Updated readme usage example --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) 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).'); + } } ``` From 49caddb2c1eeb2c49742f12d5dc6cf0e248da74f Mon Sep 17 00:00:00 2001 From: Astrid Blanco Date: Wed, 2 Oct 2024 08:00:20 +1000 Subject: [PATCH 3/3] Added check on test prepared --- src/Subscribers/OnTestPrepared.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Subscribers/OnTestPrepared.php b/src/Subscribers/OnTestPrepared.php index 89a3667..2d748ba 100644 --- a/src/Subscribers/OnTestPrepared.php +++ b/src/Subscribers/OnTestPrepared.php @@ -50,8 +50,13 @@ public function getCassette(Test $test, string $tag = '@vcr'): ?string } $reflection = new \ReflectionMethod($class, $method); - $docblock = $reflection->getDocComment(); + $attributes = $reflection->getAttributes(\VCR\PHPUnit\TestListener\Attributes\Vcr::class); + if (!empty($attributes)) { + return $attributes[0]->getArguments()[0]; + } + + $docblock = $reflection->getDocComment(); if (!empty($docblock)) { $parsed = self::parseDocBlock($docblock, $tag); return array_pop($parsed);