PHP code example of angelov / phpunit-php-vcr

1. Go to this page and download the library: Download angelov/phpunit-php-vcr library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

angelov / phpunit-php-vcr example snippets


    use Angelov\PHPUnitPHPVcr\UseCassette;
    use PHPUnit\Framework\Attributes\Test;
    use PHPUnit\Framework\TestCase;

    #[UseCassette("example_cassette.yml")]
    class ExampleTest extends TestCase
    {
        #[Test]
        public function example(): void { ... }

        #[Test]
        public function another(): void { ... }
    }
    

    use Angelov\PHPUnitPHPVcr\UseCassette;
    use PHPUnit\Framework\Attributes\Test;
    use PHPUnit\Framework\TestCase;

    class ExampleTest extends TestCase
    {
        #[Test]
        #[UseCassette("example.yml")]
        public function example(): void { ... }

        #[Test]
        public function another(): void { ... }

        #[Test]
        #[UseCassette("example_2.yml")]
        public function recorded(): void { ... }
    }
    

    use Angelov\PHPUnitPHPVcr\UseCassette;
    use PHPUnit\Framework\Attributes\Test;
    use PHPUnit\Framework\TestCase;

    #[UseCassette("example.yml")]
    class ExampleTest extends TestCase
    {
        #[Test]
        public function example(): void { ... }

        #[Test]
        #[UseCassette("example_2.yml")]
        public function recorded(): void { ... }
    }
    

use Angelov\PHPUnitPHPVcr\UseCassette;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

class ExampleTest extends TestCase
{
    #[Test]
    #[UseCassette("shared_cassette.yml")]
    #[DataProvider("urls")]
    public function testWithDataProvider(string $url): void
    {
        $content = file_get_contents($url);
        // All test cases will use the same cassette file
    }

    public static function urls(): iterable
    {
        yield ["https://example.com"];
        yield ["https://example.org"];
    }
}

use Angelov\PHPUnitPHPVcr\UseCassette;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

class ExampleTest extends TestCase
{
    #[Test]
    #[UseCassette(name: "separate_cassettes.yml", separateCassettePerCase: true)]
    #[DataProvider("urls")]
    public function testWithSeparateCassettes(string $url): void
    {
        $content = file_get_contents($url);
        // Each test case will have its own cassette file:
        // - separate_cassettes-0.yml
        // - separate_cassettes-1.yml
    }

    public static function urls(): iterable
    {
        yield ["https://example.com"];
        yield ["https://example.org"];
    }
}

use Angelov\PHPUnitPHPVcr\UseCassette;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

class ExampleTest extends TestCase
{
    #[Test]
    #[UseCassette(name: "named_cassettes.yml", separateCassettePerCase: true)]
    #[DataProvider("namedUrls")]
    public function testWithNamedCassettes(string $url): void
    {
        $content = file_get_contents($url);
        // Each test case will have its own cassette file:
        // - named_cassettes-example-com.yml
        // - named_cassettes-example-org.yml
    }

    public static function namedUrls(): iterable
    {
        yield 'example.com' => ["https://example.com"];
        yield 'example.org' => ["https://example.org"];
    }
}

use Angelov\PHPUnitPHPVcr\UseCassette;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

class ExampleTest extends TestCase
{
    #[Test]
    #[UseCassette(
        name: "organized_cassettes.yml",
        separateCassettePerCase: true,
        groupCaseFilesInDirectory: true
    )]
    #[DataProvider("urls")]
    public function testWithOrganizedCassettes(string $url): void
    {
        $content = file_get_contents($url);
        // Cassette files will be organized in a directory:
        // - organized_cassettes/0.yml
        // - organized_cassettes/1.yml
    }

    public static function urls(): iterable
    {
        yield ["https://example.com"];
        yield ["https://example.org"];
    }
}

use Angelov\PHPUnitPHPVcr\UseCassette;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

#[UseCassette(name: "class_level.yml", separateCassettePerCase: true)]
class ExampleTest extends TestCase
{
    #[Test]
    #[DataProvider("urls")]
    public function testMethod(string $url): void
    {
        $content = file_get_contents($url);
        // Each test case will have separate cassettes:
        // - class_level-0.yml
        // - class_level-1.yml
    }

    public static function urls(): iterable
    {
        yield ["https://example.com"];
        yield ["https://example.org"];
    }
}

composer 
xml
    <extensions>
        <bootstrap class="\Angelov\PHPUnitPHPVcr\Extension">
            <parameter name="cassettesPath" value="tests/fixtures" />
            <parameter name="storage" value="yaml" />                                   <!-- https://php-vcr.github.io/documentation/configuration/#storage -->
            <parameter name="libraryHooks" value="stream_wrapper, curl, soap" />        <!-- https://php-vcr.github.io/documentation/configuration/#library-hooks -->
            <parameter name="requestMatchers" value="method, url, query_string, ..." /> <!-- https://php-vcr.github.io/documentation/configuration/#request-matching -->
            <parameter name="whitelistedPaths" value="" />                              <!-- https://php-vcr.github.io/documentation/configuration/#white--and-blacklisting-paths -->
            <parameter name="blacklistedPaths" value="" />                              <!-- https://php-vcr.github.io/documentation/configuration/#white--and-blacklisting-paths -->
            <parameter name="mode" value="new_episodes" />                              <!-- https://php-vcr.github.io/documentation/configuration/#record-modes -->
        </bootstrap>
    </extensions>