PHPackages                             helpscout/specter-php - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. helpscout/specter-php

ActiveLibrary[Testing &amp; Quality](/categories/testing)

helpscout/specter-php
=====================

JSON API Mocking and Testing for PHP

0.5(8y ago)18570[1 issues](https://github.com/helpscout/specter-php/issues)MITPHPPHP ^7.0

Since May 9Pushed 8y ago36 watchersCompare

[ Source](https://github.com/helpscout/specter-php)[ Packagist](https://packagist.org/packages/helpscout/specter-php)[ RSS](/packages/helpscout-specter-php/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (6)Dependencies (12)Versions (11)Used By (0)

Specter [![Build Status](https://camo.githubusercontent.com/31e21820e469112695550b5d8461eebf85373ec69c287dec2cbbc32a1aeb2c97/68747470733a2f2f7472617669732d63692e6f72672f68656c7073636f75742f737065637465722d7068702e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/helpscout/specter-php) [![Code Climate](https://camo.githubusercontent.com/701538c9bc0d8112ea7d2cf3705a41d7dc1aec161aea23e9f8eff49537c60aa1/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f68656c7073636f75742f737065637465722d7068702f6261646765732f6770612e737667)](https://codeclimate.com/github/helpscout/specter-php) [![Test Coverage](https://camo.githubusercontent.com/b427d307e64a3285dc65309ad0d59cd13a48b9892b60311cc4806c7b3f1f50d6/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f68656c7073636f75742f737065637465722d7068702f6261646765732f636f7665726167652e737667)](https://codeclimate.com/github/helpscout/specter-php/coverage)
==================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================

[](#specter---)

> **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](https://raw.githubusercontent.com/helpscout/specter/master/tests/fixture/customer.json) together
2. Mock the endpoint, and have it return that spec file and add the [Specter Middleware](https://github.com/helpscout/specter/blob/master/src/SpecterMiddleware.php) 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](https://github.com/helpscout/specter/blob/master/src/SpecterTestTrait.php)

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
------------

[](#installation)

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

Contributing
------------

[](#contributing)

1. `git clone`
2. `composer install`
3. It will prompt you to please install our commit hooks driven by [pre-commit](http://pre-commit.com/).

Demonstration
-------------

[](#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":[
      "dward@friesen.org",
      "nwisozk@gmail.com",
      "juliet.dooley@yahoo.com"
   ]
}
```

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
-----------------

[](#custom-formatters)

In addition to the Faker library, Specter provides a few [other fomatters](https://github.com/helpscout/specter/tree/master/src/Provider)that offer some useful mocking.

- `randomRobotAvatar`
- `randomGravatar`
- `relatedElement`

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity21

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 98.4% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~73 days

Recently: every ~34 days

Total

8

Last Release

3147d ago

PHP version history (2 changes)v0.2PHP &gt;=5.5

0.4PHP ^7.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/2a560935372705d52a621836e7bee49a6d6576c646bc87feb4c7832baf9e8fc0?d=identicon)[helpscout](/maintainers/helpscout)

![](https://www.gravatar.com/avatar/ef78f0ca3100de38d3af37b825048ff460685d9aa49ff866e3d6fe6b248eaef9?d=identicon)[there4](/maintainers/there4)

---

Top Contributors

[![craig-davis](https://avatars.githubusercontent.com/u/9907304?v=4)](https://github.com/craig-davis "craig-davis (62 commits)")[![bkuhl](https://avatars.githubusercontent.com/u/524933?v=4)](https://github.com/bkuhl "bkuhl (1 commits)")

---

Tags

json-speclaravel-middlewaremockingphpphpunitpsr-7middlewarelaravelFixturepsr7illuminateHelp Scout

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/helpscout-specter-php/health.svg)

```
[![Health](https://phpackages.com/badges/helpscout-specter-php/health.svg)](https://phpackages.com/packages/helpscout-specter-php)
```

###  Alternatives

[laracasts/testdummy

Easy test stubs

4671.4M36](/packages/laracasts-testdummy)[stevenmaguire/laravel-middleware-csp

Provides support for enforcing Content Security Policy with headers in Laravel responses.

39107.6k](/packages/stevenmaguire-laravel-middleware-csp)[zizaco/testcases-laravel

Some classes to help write tests for laravel 4 applications

4650.5k2](/packages/zizaco-testcases-laravel)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
