php-vcr/vcr-bundle

将 php-vcr 集成到 Symfony 及其 Web 性能分析器。

维护者

详细信息

github.com/php-vcr/VCRBundle

来源

问题

安装次数: 25,439

依赖: 0

建议者: 0

安全: 0

星标: 12

关注者: 7

分支: 10

公开问题: 4

类型:symfony-bundle

2.1.0 2022-12-15 11:40 UTC

This package is not auto-updated.

Last update: 2024-09-09 21:19:45 UTC


README

php-vcr 集成到 Symfony 及其 Web 性能分析器。它还提供了一个用于测试的 VideoRecorderBrowser,并提供了处理 php-vcr 录音的额外辅助方法。

PHP-VCR Symfony web profiler panel

PHP-VCR Symfony web profiler panel - request details

PHP-VCR Symfony web profiler panel - response details

安装

通过在 composer.json 中添加 php-vcr/vcr-bundle 或从 CLI 安装此行为

composer require php-vcr/vcr-bundle

并在您的 config/bundles.php 文件中声明该捆绑包

<?php
declare(strict_types = 1);

return [
    // ...
    VCR\VCRBundle\VCRBundle::class => ['test' => true],
];

用法

启用您所需的库钩子并编写测试用例。

VideoRecorderBrowser(不使用 Traits)

<?php
declare(strict_types = 1);

class ExampleTest extends \VCR\VCRBundle\Tests\Functional\WebTestCase
{
    public function test(): void
    {
        $kernel = static::bootKernel();
        /** @var \VCR\VCRBundle\VideoRecorderBrowser $client */
        $client = $kernel->getContainer()->get('test.client.vcr');
        
        $client->insertVideoRecorderCassette('my-test-cassette-name');
        
        // this is an example, normally services inside you project do stuff like this and you trigger them by
        // execute requests via the KernelBrowser client
        file_get_contents('https://www.google.de');
        
        // cassette.path is configured to '%kernel.project_dir%/tests/Fixtures'
        // recordings are written to %kernel.project_dir%/tests/Fixtures/my-test-cassette-name
        // cassette.path + cassetteName (done by inserting the cassette)
    }
}

VideoRecorderBrowser(使用 Traits)

<?php
declare(strict_types = 1);

namespace MyCompany\MyProject\Tests\Functional;

class ExampleTest extends \VCR\VCRBundle\Tests\Functional\WebTestCase
{
    use \VCR\VCRBundle\Test\VCRTestCaseTrait;

    /**
     * Specify a namespace prefix which should be ignored while generating the base path for this test case. 
     */
    protected $ignoredTestSuiteNamespacePrefix = 'MyCompany\\MyProject\\Tests\\';

    public function test(): void
    {
        /** @var \VCR\VCRBundle\VideoRecorderBrowser $client */
        $client = static::createVideoRecorderClient();
        
        // this is an example, normally services inside you project do stuff like this and you trigger them by
        // execute requests via the KernelBrowser client
        file_get_contents('https://www.google.de');
        
        // cassette.path is configured to '%kernel.project_dir%/tests/Fixtures'
        // recordings are written to %kernel.project_dir%/tests/Fixtures/Functional/ExampleTest/test
        // cassette.path + TestCasePath (- ignoredTestSuiteNamespacePrefix) + TestName
    }
}

配置参考

vcr:
  enabled: true
  library_hooks:
    stream_wrapper: false
    curl: false
    soap: false
  request_matchers:
    method: true
    url: true
    query_string: true
    host: true
    headers: true
    body: true
    post_fields: true
  cassette:
    type: json
    path: '%kernel.cache_dir%/vcr'
    name: vcr

鸣谢

许可证

此捆绑包在 MIT 许可证下发布。