taluu / behapi
通过Behat在本地测试远程API
Requires
- php: ^7.3 || ^8.0
- beberlei/assert: ^3.2
- php-http/client-common: ^2.2
- php-http/discovery: ^1.11
- psr/http-client: ^1.0
- psr/http-client-implementation: ^1.0
- psr/http-factory: ^1.0
- psr/http-factory-implementation: ^1.0
- psr/http-message: ^1.0
- psr/http-message-implementation: ^1.0
- symfony/property-access: ^4.4 || ^5.4 || ^6.0
Requires (Dev)
- behat/behat: ^3.5
- nyholm/psr7: ^1.0
- php-http/message: ^1.0
- symfony/http-client: ^4.4 || ^5.4 || ^6.0
- symfony/var-dumper: ^4.4 || ^5.4 || ^6.0
Suggests
- symfony/var-dumper: In order to use the VarDumper debug dumper
Conflicts
- behat/behat: < 3.7
- php-http/discovery: < 1.11
README
Behat 扩展,用于编写与HTTP API相关的特征描述。
要使此扩展正常工作,需要PHP 7.3、Behat 3.7和一个可发现的php-http客户端。
安装此扩展需要您要求taluu/behapi
和实现一个http客户端(提供psr/http-client-implementation ^1.0
、psr/http-factory-implementation ^1.0
和psr/http-message-implementation ^1.0
)。
您可以在examples/
目录下找到一些示例。
如何使用
在您的behat.yml中添加以下内容(这是针对default
配置的,但实际上您可以为任何配置使用它)
default: suites: main: paths: ['%paths.base%/features'] services: '@Behapi\Container' autowire: true contexts: #- List #- your #- contexts #- here # examples : - Behapi\Http\RequestContext: ~ - Behapi\Http\ResponseContext: ~ extensions: Behapi\Behapi: base_url: 'http://localhost'
base_url
是此扩展正常工作所需的唯一配置要求。
还有其他配置键,例如在调试环境中使用哪个格式化程序,以及您希望在调试时输出哪些请求或响应头;在调用behat时使用--config-reference
标志可以获得有关可用配置的更多信息。
安装扩展后,您可以然后使用提供的上下文,如Behapi\Http\RequestContext
进行http API操作。为了使用它们,您需要使用behat的容器(@Behapi\Container
),或者一个能够使用behat容器的容器。
提供了一些服务以注入上下文,如下所示
@Http\Client\Common\PluginClientBuilder
,当需要时将构建一个Http\Client\Common\PluginClient
@Behapi\HttpHistory\History
,它是一种包含已执行的最后请求和接收的最后响应的容器@Http\Message\MessageFactory
@Http\Message\StreamFactory
注意:您实际上不需要担心服务名称,因为它们与behat的自动绑定功能兼容。
为了启用Json断言,您需要使用Behapi\Context\Json
上下文。注意,如果您使用json上下文,则应使用在Behapi\Http\RequestContext
上下文中使用的客户端构建器提供的客户端。
如果您需要处理正在构建的请求或发送请求时创建的响应,则需要注入@Behapi\HttpHistory\History
。它会在场景(和场景概述)之间自动重置。
将制作(希望很快)更详细的文档。
贡献
贡献(问题、拉取请求)当然是受欢迎的!请确保遵守标准(如psr-2、...等),遵循正确的git礼仪(原子提交、...),以及适当的行为,这样一切都会顺利进行!
感谢
我在@Wisembly工作时制作了此扩展,并大量用于编写我们的功能和集成测试。特别感谢@lunika、@rgazelot和@krichprollsch,他们帮助构思了这个扩展,并鼓励我将其开源。