swisnl/php-http-fixture-client
Composer 安装命令:
composer require --dev swisnl/php-http-fixture-client
包简介
Fixture client for PHP-HTTP
README 文档
README
This is a fixture client for PHP-HTTP and is meant for testing purposes. It maps requests to static fixtures.
Install
$ composer require --dev swisnl/php-http-fixture-client
Usage
// Create client $responseBuilder = new \Swis\Http\Fixture\ResponseBuilder('/path/to/fixtures'); $client = new \Swis\Http\Fixture\Client($responseBuilder); // Send request $response = $client->sendRequest(new Request(...));
Fixture mapping
All requests send using this client are mapped to static fixtures located in the provided path. URLs are transformed to file paths by using the domain and path fragments and (optionally) the method and/or the query params (sorted alphabetically). A list of possible fixture paths is made and handled in order of specificity:
- {path}.{query}.{method}.mock
- {path}.{query}.mock
- {path}.{method}.mock
- {path}.mock
Please see the following table for some examples.
| URL | Method | Possible fixtures (in order of specificity) |
|---|---|---|
| http://example.com/api/articles/1 | GET | /path/to/fixtures/example.com/api/articles/1.get.mock |
| /path/to/fixtures/example.com/api/articles/1.mock | ||
| http://example.com/api/articles | POST | /path/to/fixtures/example.com/api/articles.post.mock |
| /path/to/fixtures/example.com/api/articles.mock | ||
| http://example.com/api/comments?query=json | GET | /path/to/fixtures/example.com/api/comments.query=json.get.mock |
| /path/to/fixtures/example.com/api/comments.query=json.mock | ||
| /path/to/fixtures/example.com/api/comments.get.mock | ||
| /path/to/fixtures/example.com/api/comments.mock | ||
| http://example.com/api/comments?query=json&order=id | GET | /path/to/fixtures/example.com/api/comments.order=id&query=json.get.mock |
| /path/to/fixtures/example.com/api/comments.order=id&query=json.mock | ||
| /path/to/fixtures/example.com/api/comments.get.mock | ||
| /path/to/fixtures/example.com/api/comments.mock |
Domain aliases
The ReponseBuilder can be instructed to use aliases for domains using setDomainAliases([...]).
When configured, the provided aliases will be normalized when transforming requests to file paths.
You should provide aliases in the form of ['alias' => 'abstract'].
Ignored query parameters
The ReponseBuilder can be instructed to ignore certain query parameters using setIgnoredQueryParameters([...]).
When configured, the provided parameters will be ignored when transforming requests to file paths.
You should only provide the parameter name, not the value.
This allows you to ignore 'dynamic' parameters that change in each test execution.
Parameters are matched strictly, after url decoding, so 'foo' will match 'foo=bar', but not 'foo[]=bar'.
Strict mode
The ReponseBuilder can be set to strict mode using setStrictMode(true).
When in strict mode, only the first possible fixture path will be used.
This means that both the method and query params must be present in the fixture file name and it does not fall back to other fixture files.
Helper
Please see https://swisnl.github.io/php-http-fixture-client/#helper for the URL helper.
Body
The body of a request is loaded directly from a fixture with the file extension .mock.
The contents of this file can be anything that is a valid HTTP response, e.g. HTML, JSON or even images.
If a fixture can not be found, a MockNotFoundException will be thrown.
This exception has a convenience method getPossiblePaths() which lists all file paths that were checked, in order of specificity.
Headers (optional)
The headers of a request are loaded from a fixture with the file extension .headers. This is a simple JSON object with headers, e.g.
{
"X-Made-With": "PHPUnit"
}
Status (optional)
The status code of a request is loaded from a fixture with the file extension .status. This is a plain file containing only the HTTP status code. If no .status file is found, 200 OK will be used.
Mocks (advanced)
This client extends php-http/mock-client, which allows you to add custom responses and exceptions that ignore fixture files. Please see the mock-client documentation for more information.
N.B. A default response can not be set because this client uses that under the hood.
Change log
Please see CHANGELOG for more information on what has changed recently.
Testing
$ composer test
Contributing
Please see CONTRIBUTING and CODE_OF_CONDUCT for details.
Security
If you discover any security related issues, please email security@swis.nl instead of using the issue tracker.
License
The MIT License (MIT). Please see License File for more information.
This package is Treeware. If you use it in production, then we ask that you buy the world a tree to thank us for our work. By contributing to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats.
SWIS ❤️ Open Source
SWIS is a web agency from Leiden, the Netherlands. We love working with open source software.
swisnl/php-http-fixture-client 适用场景与选型建议
swisnl/php-http-fixture-client 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 63.44k 次下载、GitHub Stars 达 9, 最近一次更新时间为 2018 年 02 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「testing」 「client」 「Fixture」 「psr-7」 「php-http」 「swisnl」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 swisnl/php-http-fixture-client 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 swisnl/php-http-fixture-client 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 swisnl/php-http-fixture-client 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Behat redis context for testing
Testing Suite For Lumen like Laravel does.
Fixtures replacement with a straightforward definition syntax
Fixtures documentation generator
Yii2 library to run fixtures the ActiveRecord Dao
Symfony bundle to manage fixtures with Alice and Faker.
统计信息
- 总下载量: 63.44k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 9
- 点击次数: 20
- 依赖项目数: 7
- 推荐数: 2
其他信息
- 授权协议: MIT
- 更新时间: 2018-02-09