Skip to content

Commit

Permalink
Allow to specify file via Vcr attribute (#8)
Browse files Browse the repository at this point in the history
* Updated to accept vcr attribute

* Updated readme usage example
  • Loading branch information
cg-x authored Oct 1, 2024
1 parent 5633347 commit b34f655
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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).');
}
}
```

Expand Down
35 changes: 35 additions & 0 deletions src/Attributes/Vcr.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php declare(strict_types=1);

namespace VCR\PHPUnit\TestListener\Attributes;

use Attribute;

/**
* @immutable
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
*/
#[Attribute(Attribute::TARGET_METHOD)]
final class Vcr
{
/**
* @var non-empty-string
*/
private string $cassette;

/**
* @param non-empty-string $cassette
*/
public function __construct(string $cassette)
{
$this->cassette = $cassette;
}

/**
* @return non-empty-string
*/
public function cassette(): string
{
return $this->cassette;
}
}
9 changes: 9 additions & 0 deletions tests/VCRTestListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Tests\VCR\PHPUnit\TestListener;

use PHPUnit\Framework\TestCase;
use VCR\PHPUnit\TestListener\Attributes\Vcr;

final class VCRTestListenerTest extends TestCase
{
Expand Down Expand Up @@ -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[][]
*/
Expand Down

0 comments on commit b34f655

Please sign in to comment.