helpscout / specter-php
PHP 的 JSON API 模拟和测试
0.5
2017-10-02 16:02 UTC
Requires
- php: ^7.0
- coduo/php-matcher: ^2.0
- fzaninotto/faker: ^1.6
- guzzlehttp/psr7: ^1.3
- phpspec/php-diff: ^1.1
- psr/http-message: ^1.0
Requires (Dev)
- codeclimate/php-test-reporter: dev-master
- helpscout/php-standards: ^1.0
- illuminate/http: 5.*
- jakub-onderka/php-console-highlighter: ^0.3.2
- jakub-onderka/php-parallel-lint: ^0.9.2
- phpunit/phpunit: ^6.1
- squizlabs/php_codesniffer: ^3.0
This package is not auto-updated.
Last update: 2024-09-15 23:21:38 UTC
README
PHP 的模拟和测试 使用单个 JSON 文件生成模拟数据,并将其用作集成测试断言
现代开发很复杂。该项目通过提供固定数据和单个文件的测试规范来解耦前端和后端开发。
- 客户端和服务器团队一起创建 一个 JSON 规范文件
- 模拟端点,并使其返回该规范文件,并将 Specter 中间件 添加到将规范文件转换为充满随机但合理的响应的响应中
- 客户端团队可以开始使用此端点进行开发,并使用 JSON 规范进行任何更改的迭代。端点提供真实数据,并且他们可以设置
SpecterSeed头以获取可重复的结果。 - 然后服务器团队可以按自己的节奏实现实际端点以满足该规范,也许在下个冲刺中。他们可以使用 相同的 规范文件,通过将规范文件交给 SpecterTestTrait 来驱动 PHPUnit 集成测试
这允许团队快速创建端点规范,前端团队使用它从中获取数据,平台团队使用它进行测试。
安装
此软件包可通过 composer 以 helpscout/specter-php 的形式获得。
贡献
git clonecomposer install- 它将提示您安装由 pre-commit 驱动的提交钩子。
演示
在您的开发团队中共同协作,为新端点制定规范并创建一个定义您新端点的 Specter JSON 文件。这是一个 Specter JSON 文件
{
"__specter": "Sample customer record",
"id": "@randomDigitNotNull@",
"fname": "@firstName@",
"lname": "@lastName@",
"company": "@company@",
"jobTitle": "@jobTitle@",
"background": "@catchPhrase@",
"address": {
"city": "@city@",
"state": "@stateAbbr@",
"zip": "@postcode@",
"country": "@country@"
},
"emails": ["@companyEmail@", "@freeEmail@", "@email@" ]
}
添加一个路由来返回它,并使用 SpecterMiddleware 来处理它
$app->get('/api/v1/customer/{id}', function ($request, $response, $args) { return $response->withJson(getFixture('customer')); })->add(new \HelpScout\Specter\SpecterMiddleware);
从端点接收满足 JSON 的随机数据,并使用它来构建您的界面
{
"__specter":"Sample customer record",
"id":6,
"fname":"Glenda",
"lname":"Trantow",
"company":"Kerluke, Rodriguez and Wisoky",
"jobTitle":"Power Generating Plant Operator",
"background":"Configurable multi-state standardization",
"address":{
"city":"Georgiannachester",
"state":"TX",
"zip":"89501",
"country":"Afghanistan"
},
"emails":[
"dward@friesen.org",
"nwisozk@gmail.com",
"juliet.dooley@yahoo.com"
]
}
为端点编写单元测试以确认它是否满足规范,然后实现实际的端点
use SpecterTestTrait; public function testCustomerRouteMeetsSpec() { self::assertResponseContent( $this->client->get('/api/v1/customer/37'), 'customer' ); }
自定义格式化程序
除了 Faker 库之外,Specter 还提供了一些 其他格式化程序,它们提供了一些有用的模拟。
randomRobotAvatarrandomGravatarrelatedElement