PHPackages                             gong023/swagger-assert - 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. gong023/swagger-assert

ActiveLibrary

gong023/swagger-assert
======================

enable to assert the swagger documented keys and API response

131.8k2[1 issues](https://github.com/gong023/swagger-assert/issues)PHP

Since Apr 9Pushed 11y ago1 watchersCompare

[ Source](https://github.com/gong023/swagger-assert)[ Packagist](https://packagist.org/packages/gong023/swagger-assert)[ RSS](/packages/gong023-swagger-assert/feed)WikiDiscussions master Synced today

READMEChangelogDependenciesVersions (1)Used By (0)

Swagger-assert
==============

[](#swagger-assert)

enable to assert swagger 1.2 doc keys and API response. For Swagger 2 take a look to [SwaggerAssertions](https://github.com/Maks3w/SwaggerAssertions)

[![Build Status](https://camo.githubusercontent.com/d0f6f8fa659653c5f3c34f9be6890ace5b9fbf3e1f11ca2ce1c3cd67d40f9b40/68747470733a2f2f7472617669732d63692e6f72672f676f6e673032332f737761676765722d6173736572742e737667)](https://travis-ci.org/gong023/swagger-assert)[![Coverage Status](https://camo.githubusercontent.com/0232ca110f68b15efc33d15044e077640a625da458c336053537a3251da7c9ed/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f676f6e673032332f737761676765722d6173736572742f62616467652e706e673f6272616e63683d6d6173746572)](https://coveralls.io/r/gong023/swagger-assert?branch=master)

Installation
------------

[](#installation)

composer

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

requires PHP5.4+

Usage
-----

[](#usage)

### Sample API

[](#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

[](#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

[](#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

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

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

```
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](https://github.com/gong023/swagger-assert-sandbox).

Motivation
----------

[](#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.

Bugs &amp; Contributions
------------------------

[](#bugs--contributions)

Please report bugs by opening an issue.

Contributions are welcome.

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance18

Infrequent updates — may be unmaintained

Popularity24

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 80.8% 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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/034b987bca6692b17b6a5923eca68302fec17cd13f09f8d2f0a45c6d38125c03?d=identicon)[gong023](/maintainers/gong023)

---

Top Contributors

[![gong023](https://avatars.githubusercontent.com/u/1222856?v=4)](https://github.com/gong023 "gong023 (59 commits)")[![Maks3w](https://avatars.githubusercontent.com/u/1301698?v=4)](https://github.com/Maks3w "Maks3w (13 commits)")[![svemir](https://avatars.githubusercontent.com/u/154453?v=4)](https://github.com/svemir "svemir (1 commits)")

### Embed Badge

![Health badge](/badges/gong023-swagger-assert/health.svg)

```
[![Health](https://phpackages.com/badges/gong023-swagger-assert/health.svg)](https://phpackages.com/packages/gong023-swagger-assert)
```

PHPackages © 2026

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