macpaw / behat-http-mock-context
在测试模拟HTTP响应的其他服务时的Behat Context
v3.0.4
2024-03-15 14:23 UTC
Requires
- php: ^8.0
- behat/behat: ^3.0
- macpaw/extended_mock_http_client: ^3.0 || ^4.0
- symfony/cache: ^4.4 || ^5.0 || ^6.0 || ^7.0
- symfony/dependency-injection: ^4.4 || ^5.4 || ^6.0 || ^7.0
- symfony/http-client: ^4.4 || ^5.0 || ^6.0 || ^7.0
- symfony/http-kernel: ^4.4 || ^5.4 || ^6.0 || ^7.0
Requires (Dev)
- phpstan/phpstan: ^1.2
- phpunit/phpunit: ^9.5.10
- slevomat/coding-standard: ^7.0
- squizlabs/php_codesniffer: ^3.6.0
README
安装
步骤 1: 下载Bundle
打开命令行,进入您的项目目录并执行
使用Symfony Flex的应用程序[进行中]
$ composer require --dev macpaw/behat-http-mock-context
不使用Symfony Flex的应用程序
打开命令行,进入您的项目目录并执行以下命令以下载此bundle的最新稳定版本
$ composer require --dev macpaw/behat-http-mock-context
此命令要求您已全局安装Composer,如Composer文档中的安装章节中所述。
然后,通过将其添加到项目中app/AppKernel.php
文件中注册的bundle列表来启用此bundle
<?php // app/AppKernel.php // ... class AppKernel extends Kernel { public function registerBundles() { $bundles = array( // ... BehatHttpMockContext\BehatHttpMockContextBundle::class => ['test' => true], ); // ... } // ... }
步骤 2: 模拟HTTP客户端
示例:您在config/services.yaml
中已经有了HTTP客户端
oauth_http_client: class: Symfony\Contracts\HttpClient\HttpClientInterface factory: ['Symfony\Component\HttpClient\HttpClient', createForBaseUri] arguments: - '%env(OAUTH_URL)%'
现在您需要在config/services_test.yaml
中模拟此客户端
oauth_http_client: class: ExtendedMockHttpClient\ExtendedMockHttpClient arguments: - '%env(OAUTH_URL)%' tags: ['mock.http_client'] ...
现在我们准备添加构建模拟集合
BehatHttpMockContext\Collection\ExtendedMockHttpClientCollection: arguments: - !tagged_iterator mock.http_client ...
步骤 3: 配置Behat
转到behat.yml
... contexts: - BehatHttpMockContext\Context\MockContext ...
步骤 4: 如何使用
Given I mock "oauth_http_client" HTTP client next response status code should be 200 with body:
"""
{
"success": true,
"response": {
"user_id": 234
}
}
"""