Git Product home page Git Product logo

swagger-assert's Introduction

Swagger-assert

enable to assert swagger doc keys and API response.

Build Status Coverage Status

Installation

composer

    "require-dev": {
        "gong023/swagger-assert": "dev-master"
    }

requires PHP5.4 over

Usage

Sample API

Sample API is below. Swagger-assert enables you to assert that /plain response has swagger keys id,name.

**
 * @SWG\Resource(
 *     resourcePath="plain",
 *     @SWG\Api(
 *         path="/plain",
 *         description="plain api structure",
 *         @SWG\Operation(
 *             method="GET",type="SimpleMember",nickname="plain"
 *         )
 *     )
 * )
 *
 * @SWG\Model(
 *     id="SimpleMember",
 *     @SWG\Property(name="id", type="integer", required=true, description="user id"),
 *     @SWG\Property(name="name", type="string", required=true, description="user name")
 * )
 */
$app->get('/plain', function () use ($app) {
    $response = [
        'id'   => 0,
        'name' => 'kohsaka'
    ];

    return $app->json($response);
});

Ready

At first, call SwaggerAssert::analyze at the start of the test. Argument is directory path where annotation file exists.

// testing bootstrap.php
\SwaggerAssert\SwaggerAssert::analyze($targetDir);

Assert keys

Second, call SwaggerAssert::responseHasSwaggerKeys in test class.

  • First argument: array of API response
  • Second argument: string of http method
  • Thrid argument: url of API endpoint

When testing the sample API by PHPUnit, code is as follows.

class PlainApiTest extends \PHPUnit_Framework_TestCase
{
    public function testResponseHasSwaggerKeys()
    {
        $response = $this->request('get', '/plain');
        $result = \SwaggerAssert::responseHasSwaggerKeys(array $response, 'get', '/plain', $onlyRequired = true);

        $this->assertTrue($result);
    }
}

\SwaggerAssert::responseHasSwaggerKeys compares API response keys and keys in swagger doc and return true when they match. If they differs, output below error message.

SwaggerAssert\Exception\CompareException: Failed asserting that API response and swagger document are equal.
--- Response
+++ Swagger
@@ @@
 Array (
-    0 => 'id'
-    1 => 'name'
+    0 => 'name'
 )

The fourth argument is optional. If you give false, responseHasSwaggerKeys contains required=false keys to assert. Default value is true.

if you need more sample, please take a look at swagger-assert-sandbox.

Motivation

Swagger doc and API response sometimes differs. If they differs, the swagger doc causes confusion in development. So create library to assert API response and swagger doc.

Bugs & Contributions

Please report bugs by opening an issue.

Contributions are welcome.

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.