2017 © Pedro Peláez
 

library rest-spec

A small library for describing and testing REST APIs in PHP

image

talkrz/rest-spec

A small library for describing and testing REST APIs in PHP

  • Friday, September 29, 2017
  • by talkrz
  • Repository
  • 1 Watchers
  • 0 Stars
  • 21 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Build Status, (*1)

rest-spec

A simple library for creating self-verifying API specifications, and end-to-end testing written in PHP., (*2)

I do not recommend to use in on production yet, however will be happy if you will., (*3)

Installation

You can install rest-spec by Composer., (*4)

Add package to your project's composer.json file:, (*5)

{
    "require-dev": {
        "talkrz/rest-spec": "dev-master"
    }
}

Install it with Composer:, (*6)

composer install

Usage

Basic example: describing GitHub API

Create rest-spec directory in your project's root., (*7)

Create github.php file (you can choose any file name) inside it with an API specification:, (*8)

<?php
# rest-spec/github.php

api('https://api.github.com', function() {
    url('/repos/{userName}/{repoName}', 'Fetch user repository data', function() {
        useCase('Fetch octocat\'s hello-world repository', function() {
            givenRequest()
                ->method('GET')
                ->headers([
                    'User-agent' => 'rest-spec'
                ]);

            expectResponse()
                ->toHaveStatusCode(200)
                ->toHaveHeaders([
                    'Content-Type' => 'application/json; charset=utf-8',
                    'Cache-Control' => 'public, max-age=60, s-maxage=60',
                    'Access-Control-Allow-Origin' => '*',
                ])
                ->toBeJson()
                ;
        })->withExampleParameters([
            'userName' => 'octocat',
            'repoName' => 'hello-world',
        ]);
    });
});

Run rest-spec, (*9)

php vendor/bin/rest-spec

That's it!, (*10)

As a result of this command you should see result of verifying previously defined specification against real API:, (*11)

alt text, (*12)

Always up-to-date specification

This library has two main puproses. First one is to provide API specification that is always up-to-date., (*13)

Let's change response specification in previous example to one that is not valid:, (*14)

<?php
# rest-spec/github.php

api('https://api.github.com', function() {
    url('/repos/{userName}/{repoName}', 'Fetch user repository data', function() {
        useCase('Fetch octocat\'s hello-world repository', function() {

        // ...
                ->toHaveHeaders([
                    'Content-Type' => 'application/json', // wrong header!
                    'Cache-Control' => 'public, max-age=60, s-maxage=60',
                    'Access-Control-Allow-Origin' => '*',
                ]);
        // ...
    });
});

After running rest-spec we see, that actual response contains different header than in specification, so we know that we need to update it:, (*15)

alt text, (*16)

The other purpose is testing of your application. As rest-spec perform calls to actual application, it can detect problems. Of course you should remember that such end-to-end testing is very limited. It tests only very narrow set of features and paths of code execution inside application. Well written set of unit tests is far more important for detecting bugs., (*17)

The Versions

29/09 2017

dev-master

9999999-dev https://github.com/talkrz/rest-spec

A small library for describing and testing REST APIs in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

by Krzysztof Tałajko

api test rest