persata / symfony-api-extension
提供使用 Behat 测试 Symfony API 的功能。
0.2.9
2018-08-07 09:36 UTC
Requires
- php: ^7.0
- ext-json: *
- behat/behat: ^3.1
- friends-of-behat/symfony-extension: ^0.2.1|^1.0
- symfony/browser-kit: ^3.0|^4.0
- symfony/http-kernel: ^3.0|^4.0
- webmozart/assert: ^1.0
Requires (Dev)
- phpunit/phpunit: ^6
Suggests
README
提供使用 Behat 测试 Symfony API 的功能。
此包提供了一个可共享的 ApiClient
类,可以在所有需要修改请求或检查响应的相关上下文中使用。
需要 FriendsOfBehat 的 SymfonyExtension。
使用方法
-
通过 Composer 安装
$ composer require persata/symfony-api-extension --dev
-
在 Behat 配置中启用和配置
# behat.yml default: suites: default: contexts: - Persata\SymfonyApiExtension\Context\ApiContext: ~ extensions: Persata\SymfonyApiExtension: ~ FriendsOfBehat\SymfonyExtension: kernel: bootstrap: vendor/autoload.php
额外的自定义上下文
此包提供了一个 ContextInitializer
,它将为实现 ApiClientAwareContext
的任何 Behat 上下文设置共享的 ApiClient
。最好扩展 RawApiContext
,因为它已经包括了 ApiClient
实例的 getter 和 setter。
ContextServiceExtension 集成
如果使用 FriendsOfBehat 的 ContextServiceExtension,则声明 ApiContext
服务如下
services: # ... Persata\SymfonyApiExtension\Context\ApiContext: tags: - { name: fob.context_service }
如果您需要具有访问同一共享 ApiClient 实例的额外上下文服务,请让它们扩展 RawApiContext
,然后声明服务如下
services: # ... TheClass\Of\Your\CustomContext: tags: - { name: fob.context_service }
FOSRest 表单验证
如果您在 Symfony 应用程序中使用 FOSRestBundle,并使用 表单响应功能,则提供了额外的 FOSRestFormValidationContext
类来测试错误的存在或缺失
Then the JSON response should have the error "This value should not be null." at "user.firstName" And the JSON response should not have any errors on "user.lastName"
示例场景
Scenario: Testing Basic JSON Endpoint Given the "Authorization" request header is "Bearer MyToken" When the request is sent using GET to "/example-endpoint" Then the response status code should be 200 And the response content type should be JSON And the response content should be valid JSON And the JSON response should be """ { "hello": "world" } """ And the JSON response should have the structure """ [ "hello" ] """
扩展配置
# behat.yml default: extensions: Persata\SymfonyApiExtension: base_url: 'http://my-api.local' # Base URL used for requests sent to the Symfony kernel. Defaults to null. files_path: "%paths.base%/src/Behat/Resources/fixtures/" # Base directory of files to be used for upload tests. Defaults to null.