2017 © Pedro Peláez
 

library swagger-assert

enable to assert the swagger documented keys and API response

image

gong023/swagger-assert

enable to assert the swagger documented keys and API response

  • Thursday, April 9, 2015
  • by gong023
  • Repository
  • 3 Watchers
  • 13 Stars
  • 1,829 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 1 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Swagger-assert

enable to assert swagger 1.2 doc keys and API response. For Swagger 2 take a look to SwaggerAssertions, (*1)

Build Status Coverage Status, (*2)

Installation

composer, (*3)

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

requires PHP5.4+, (*4)

Usage

Sample API

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

/**
 * @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., (*6)

// 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 - Third argument: url of API endpoint, (*7)

When testing the sample API by PHPUnit, code is as follows., (*8)


use SwaggerAssert\SwaggerAssert; 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., (*9)

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., (*10)

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

Motivation

Swagger doc and API response sometimes differ. If they differ, the swagger doc causes confusion in development. So create library to assert API response and swagger doc., (*12)

Bugs & Contributions

Please report bugs by opening an issue., (*13)

Contributions are welcome., (*14)

The Versions

09/04 2015

dev-master

9999999-dev

enable to assert the swagger documented keys and API response

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar gong023

api test rest swagger assert