Downloads
Stars
Version
Mocking and Testing for PHP Use a single JSON file to generate mock data and as an integration test assertion
Modern development is complicated. This project decouples front end and back end development by providing fixture data and a testing spec with a single file.
SpecterSeed
header to get repeatable results.This lets the teams rapidly create an endpoint specification together, the front end team uses the data from it, and the platform team tests with it.
This is available through composer as helpscout/specter-php
.
git clone
composer install
Work together among your development teams to spec a new endpoint and create a Specter JSON file that defines your new endpoint. This is a Specter JSON file:
{
"__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@" ]
}
Add a route to return it and use SpecterMiddleware
to process it:
$app->get('/api/v1/customer/{id}', function ($request, $response, $args) {
return $response->withJson(getFixture('customer'));
})->add(new \HelpScout\Specter\SpecterMiddleware);
Receive random data from your endpoint that fulfills the JSON and use it to build out your interface:
{
"__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":[
"[email protected]",
"[email protected]",
"[email protected]"
]
}
Write a unit test for the endpoint to confirm that it's meeting the spec, and then implement the endpoint for real:
use SpecterTestTrait;
public function testCustomerRouteMeetsSpec()
{
self::assertResponseContent(
$this->client->get('/api/v1/customer/37'),
'customer'
);
}
In addition to the Faker library, Specter provides a few other fomatters that offer some useful mocking.
randomRobotAvatar
randomGravatar
relatedElement