php-vcr / vcr-bundle
将 php-vcr 集成到 Symfony 及其 Web 性能分析器。
2.1.0
2022-12-15 11:40 UTC
Requires
- php: ^7.2|^8
- php-vcr/php-vcr: ^1.5
- symfony/browser-kit: ^4|^5
- symfony/config: ^4|^5
- symfony/dependency-injection: ^4|^5
- symfony/event-dispatcher: ^4|^5
- symfony/filesystem: ^4|^5
- symfony/framework-bundle: ^4.4|^5.4
- symfony/http-foundation: ^4|^5
- symfony/http-kernel: ^4.4|^5
- symfony/polyfill-php80: ^1.16
- symfony/yaml: ^4|^5
Requires (Dev)
- dms/phpunit-arraysubset-asserts: ^0.4
- neutron/temporary-filesystem: ^3
- phpunit/phpunit: ^8.5|^9.5
- symfony/phpunit-bridge: ^4.4|^5.4
README
将 php-vcr 集成到 Symfony 及其 Web 性能分析器。它还提供了一个用于测试的 VideoRecorderBrowser,并提供了处理 php-vcr 录音的额外辅助方法。
安装
通过在 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
鸣谢
- 凯文·戈麦斯
- 卢多维克·弗勒里 - 从他的 GuzzleBundle 中借用了 Web 性能分析器部分的设计。
- 西蒙·休布纳 - 使捆绑包与 SF 5.4 兼容
- 丹尼尔·赫特根 - 使捆绑包与 PHP 8 兼容并提供了额外的 TestCase 辅助
许可证
此捆绑包在 MIT 许可证下发布。