helpscout / specter
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: 2022-02-01 12:58:10 UTC
README
PHP 模拟和测试 使用单个 JSON 文件生成模拟数据和集成测试断言
现代开发复杂。该项目通过提供固定数据和单个文件的测试规范来解耦前端和后端开发。
- 客户端和服务器团队共同创建一个 JSON 规范文件
- 模拟端点,并返回该规范文件,并将Specter 中间件添加到将规范文件转换为填充随机但合理的数据的响应
- 客户端团队可以使用此端点开始开发,并通过 JSON 规范进行迭代。端点提供真实数据,并且他们可以设置
SpecterSeed
头来获取可重复的结果。 - 然后,服务器团队可以按自己的节奏实现实际端点以符合该规范,也许在下个冲刺中。他们可以使用 相同的 规范文件通过将规范文件交给SpecterTestTrait来驱动 PHPUnit 集成测试
这使得团队可以快速共同创建端点规范,前端团队使用其中的数据,平台团队使用它进行测试。
安装
这可以通过 composer 以 helpscout/specter-php
的形式获得。
贡献
git clone
composer 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 还提供了一些其他格式化程序,提供了一些有用的模拟。
randomRobotAvatar
randomGravatar
relatedElement