Code Monkey home page Code Monkey logo

specter-php's Introduction

Specter Build Status Code Climate Test Coverage

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.

  1. Client and Server teams build a JSON spec file together
  2. Mock the endpoint, and have it return that spec file and add the Specter Middleware to convert that spec file into a response filled with random, but sane, data
  3. The client team can begin development with this endpoint, and iterate over any changes with the JSON spec. The endpoint delivers real data, and they can set a SpecterSeed header to get repeatable results.
  4. The server team can then implement the actual endpoint to meet that spec at their own pace, perhaps in the next sprint. They can use the same spec file to drive an PHPUnit integration test by handing the spec file to the SpecterTestTrait

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.

Installation

This is available through composer as helpscout/specter-php.

Contributing

  1. git clone
  2. composer install
  3. It will prompt you to please install our commit hooks driven by pre-commit.

Demonstration

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'
    );
}

Custom Formatters

In addition to the Faker library, Specter provides a few other fomatters that offer some useful mocking.

  • randomRobotAvatar
  • randomGravatar
  • relatedElement

specter-php's People

Contributors

bkuhl avatar craig-davis avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

specter-php's Issues

Updating composer dependencies

  • Updated all dependencies
  • Replaced helpscout/specter with helpscout/specter-php
  - Removing helpscout/specter (0.5)
  - Updating symfony/polyfill-mbstring (v1.7.0 => v1.8.0): Loading from cache
  - Updating symfony/translation (v4.0.8 => v4.0.9): Loading from cache
  - Updating illuminate/contracts (v5.6.17 => v5.6.20): Loading from cache
  - Updating illuminate/support (v5.6.17 => v5.6.20): Loading from cache
  - Updating illuminate/redis (v5.6.17 => v5.6.20): Loading from cache
  - Updating aws/aws-sdk-php (3.55.1 => 3.55.4): Loading from cache
  - Updating pusher/pusher-php-server (v3.0.2 => v3.0.3): Loading from cache
  - Updating phpunit/php-code-coverage (6.0.3 => 6.0.4): Loading from cache
  - Updating phpunit/phpunit (7.1.4 => 7.1.5): Loading from cache
  - Updating symfony/process (v4.0.8 => v4.0.9): Loading from cache
  - Updating symfony/debug (v4.0.8 => v4.0.9): Loading from cache
  - Updating symfony/finder (v4.0.8 => v4.0.9): Loading from cache
  - Updating illuminate/filesystem (v5.6.17 => v5.6.20): Loading from cache
  - Updating illuminate/container (v5.6.17 => v5.6.20): Loading from cache
  - Updating illuminate/database (v5.6.17 => v5.6.20): Loading from cache
  - Updating symfony/console (v4.0.8 => v4.0.9): Loading from cache
  - Updating illuminate/console (v5.6.17 => v5.6.20): Loading from cache
  - Updating illuminate/queue (v5.6.17 => v5.6.20): Loading from cache
  - Updating symfony/http-foundation (v4.0.8 => v4.0.9): Loading from cache
  - Updating symfony/polyfill-php70 (v1.7.0 => v1.8.0): Loading from cache
  - Updating symfony/event-dispatcher (v4.0.8 => v4.0.9): Loading from cache
  - Updating symfony/http-kernel (v4.0.8 => v4.0.9): Loading from cache
  - Updating illuminate/session (v5.6.17 => v5.6.20): Loading from cache
  - Updating illuminate/http (v5.6.17 => v5.6.20): Loading from cache
  - Updating illuminate/auth (v5.6.17 => v5.6.20): Loading from cache
  - Updating illuminate/pipeline (v5.6.17 => v5.6.20): Loading from cache
  - Updating illuminate/bus (v5.6.17 => v5.6.20): Loading from cache
  - Updating illuminate/broadcasting (v5.6.17 => v5.6.20): Loading from cache
  - Updating illuminate/cache (v5.6.17 => v5.6.20): Loading from cache
  - Updating illuminate/config (v5.6.17 => v5.6.20): Loading from cache
  - Updating illuminate/encryption (v5.6.17 => v5.6.20): Loading from cache
  - Updating illuminate/hashing (v5.6.17 => v5.6.20): Loading from cache
  - Updating illuminate/log (v5.6.17 => v5.6.20): Loading from cache
  - Updating illuminate/pagination (v5.6.17 => v5.6.20): Loading from cache
  - Updating illuminate/translation (v5.6.17 => v5.6.20): Loading from cache
  - Updating illuminate/validation (v5.6.17 => v5.6.20): Loading from cache
  - Updating illuminate/events (v5.6.17 => v5.6.20): Loading from cache
  - Updating illuminate/view (v5.6.17 => v5.6.20): Loading from cache
  - Updating symfony/cache (v4.0.8 => v4.0.9): Loading from cache
  - Updating symfony/expression-language (v3.4.8 => v3.4.9): Loading from cache
  - Updating symfony/inflector (v4.0.8 => v4.0.9): Loading from cache
  - Updating symfony/property-access (v3.4.8 => v3.4.9): Loading from cache
  - Updating nette/php-generator (v3.0.3 => v3.0.4): Loading from cache
  - Installing helpscout/specter-php (0.5): Loading from cache

Add nullable properties to json spec

Feature

Allow nullable properties in a Specter file, with syntax influenced by the nullable type RFC. In the example below, the jobTitle is considered nullable.

{
  "__specter": "Sample customer record",
  "id": "@randomDigitNotNull@",
  "fname": "@firstName@",
  "lname": "@lastName@",
  "company": "@company@",
  "jobTitle": "?@jobTitle@"
}

Implementation

For property values prepended with a question mark ?, the generator will return either the result of the faker generator or a null value for some percentage of the results. The result must respect the seed value so that the results are repeatable. For string values, the return should be a null rather than an empty string.

The percentage of null returns is up for debate. I think 25% is a reasonable starting point.

Scope

This does not implement optional properties - where a property 'jobTitle' might be missing from the result altogether. This only addresses optional values.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.