coduo / tutu
TuTu - PHP中的简单HTTP服务器模拟工具。
Requires
- php: >=5.5.0
- coduo/php-matcher: 2.1.*
- coduo/tutu-faker-extension: 1.0.*
- coduo/tutu-twig-extension: 1.0.*
- symfony/class-loader: ~2.3
- symfony/event-dispatcher: ~2.3
- symfony/http-kernel: ~2.3
- symfony/options-resolver: ~2.3
- symfony/yaml: ~2.3
- twig/twig: 1.*
Requires (Dev)
- behat/behat: 3.0.*
- behat/mink-extension: ~2.0
- behat/mink-goutte-driver: ~1.0
- bossa/phpspec2-expect: ~1.0
- phpspec/phpspec: 2.4.*
- symfony/filesystem: ~2.3
- symfony/process: ~2.3
Replaces
- coduo/tutu-core: 1.0.1
This package is auto-updated.
Last update: 2024-09-20 07:30:12 UTC
README
#TuTu
PHP中的灵活HTTP服务器模拟工具。TuTu可以与内置的PHP服务器一起工作,因此您需要的一切就是PHP。它可以用来模拟任何类型的Web应用行为。我们打算在API客户端测试中使用TuTu。
如何使用它
使用composer安装
$ composer create-project coduo/tutu --stability=dev
TuTu仍在开发中,因此您需要将稳定性设置为dev
为特定请求创建响应
TuTu可以为特定请求创建任何响应,但您需要教会它如何做。为此,您需要准备一个简单的responses.yml
yaml文件。
$ cd tutu
$ cp config/responses.yml.dist config/responses.yml
假设我们想创建某种“Hello world”响应
hello_world:
request:
path: /hello/world
methods: ['GET']
response:
content: |
Hello {{ request.query.get('name') }}!
运行TuTu
您可以使用PHP内置的web服务器来运行TuTu
$ cd web
$ php -S localhost:8000
测试TuTu
当您向"http://localhost:8000/hello/world?name=Norbert"发送GET请求时,TuTu应该返回以下内容的响应
Hello Norbert!
响应配置
# config/responses.yml
hello_world_get:
request:
path: /hello/world
methods: ['GET']
query: []
request: []
headers: []
body: ""
response:
content: "This is nothing more than twig template"
status: 200
headers:
"Content-type": "application/json"
如您所见,有几种方法可以自定义TuTu响应。
request.path
- 必要选项,它表示路由。您可以使用占位符来创建路由,例如/hello/{name}
request.methods
- 可选。当为空时,允许任何方法。必须是有效的数组request.request
- 可选。当不为空时,它将仅匹配包含body ($_POST)参数的请求。request.query
- 可选。当不为空时,它将仅匹配包含查询($_GET)参数的请求。request.headers
- 可选。当不为空时,它将仅匹配包含指定头部的请求。request.body
- 可选。当不为空时,它将仅匹配包含指定主体的请求。response.content
- 可选。内容是无非是twig模板在传递到响应之前渲染的内容。response.status
- 可选。响应代码response.headers
- 可选。添加到响应中的头部。必须是有效的数组
在内容模板中,您可以访问所有twig功能。您还可以使用request
变量来访问请求数据。
请求查询、头部、请求和主体配置可能包含 coduo/php-matcher模式。
从文件加载响应内容
为了尽可能保持配置文件的大小,您可以移动响应内容到单独的文件。
# config/responses.yml
hello_world_get:
request:
path: /hello/world
methods: ['GET']
response:
content: @resources/hello_world.twig.html
上述配置将从一个位于@resources
命名空间的hello_world.twig.html文件中加载内容。
{# resources/hello_world.twig.html #}
Hello {{ request.request.get('name') }}!
配置
TuTu还有一个配置文件,您可以设置一些东西。
# config/config.yml
parameters:
resources_path: '/var/www/tutu/custom_resources' # empty by default
responses_file_path: '/var/www/tutu/config/custom_responses.yml' # empty by default
twig: # optional, must be a valid array. Passed to twig env
debug: true
auto_reload: true
extensions:
Coduo\TuTu\Extension\Faker: ~
扩展
因为没有任何完美的工具,TuTu允许您创建扩展。要启用扩展,您只需要准备一个config.yml
文件。
# config/config.yml extensions: Coduo\TuTu\Extension\Faker: ~
上述示例显示如何加载Faker扩展(此存储库中可用)。您还可以在初始化扩展时传递参数。
# config/config.yml extensions: Coduo\TuTu\Extension\Faker: - "pl_PL"
在上面的示例中,扩展Coduo\TuTu\Extension\Faker
将使用一个具有值"pl_PL"的参数创建。
请注意,Faker扩展在TuTu中默认可用。您不需要手动启用它。
几个示例
# config/responses.yml
client_token_grant:
request:
path: "/oauth/v2/token"
query:
"client_id": "CLIENT_VALID_ID"
"client_secret": "CLIENT_VALID_SECRET"
"grant_type": "client_credentials"
response:
content: "@resources/oauth2/client_token_grant.json.twig"
headers:
Content-Type: application/json
missing_grant_type:
request:
path: "/oauth/v2/token"
response:
content: "@resources/oauth2/missing_grant_type.json.twig"
status: 400
headers:
Content-Type: "application/json"
register_customer:
request:
path: "/api/customer"
methods: ['POST']
headers:
Authorization: "Bearer VALID_CLIENT_ACCESS_TOKEN"
body: |
{
"email": @string@,
"plainPassword": @string@,
"firstName": @string@,
"lastName": @string@
}
response:
content: "@resources/api/customer/register_customer.json.twig"
headers:
Content-Type: application/json
register_with_missing_parameters:
request:
path: "/api/customer"
methods: ['POST']
headers:
Authorization: "Bearer VALID_CLIENT_ACCESS_TOKEN"
response:
content: "@resources/api/customer/register_missing_parameters.json.twig"
status: 422
headers:
Content-Type: application/json