PHPackages                             mlambley/swagception - 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. mlambley/swagception

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

mlambley/swagception
====================

Validate your API against Swagger 2.0 using Codeception

1.1(5y ago)4629.4k4[10 issues](https://github.com/mlambley/swagception/issues)[4 PRs](https://github.com/mlambley/swagception/pulls)1MITPHP

Since Jun 4Pushed 3y ago2 watchersCompare

[ Source](https://github.com/mlambley/swagception)[ Packagist](https://packagist.org/packages/mlambley/swagception)[ RSS](/packages/mlambley-swagception/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependencies (8)Versions (14)Used By (1)

Swagception
===========

[](#swagception)

Validate your API against Swagger 2.0 using Codeception

How to Install
--------------

[](#how-to-install)

```
composer require --dev mlambley/swagception

```

What is Swagger?
----------------

[](#what-is-swagger)

Swagger 2.0 (aka Open API 2.0) defines the structure of your API, including end points and the structure of input and output data. See [their website](https://swagger.io/) for more information.

What is Swagception?
--------------------

[](#what-is-swagception)

If you have an existing Swagger 2.0 specification, you can use it to validate your API using this tool. This tool is designed to work with [Codeception](https://codeception.com/). Specifically, it automates the generation of acceptance tests for your API. It will not validate your specification itself. It will only validate that your API matches your existing specification.

Why Swagception?
----------------

[](#why-swagception)

I could not find any API validator which uses Swagger and is designed specifically for Codeception. Also, this library aims to fully take into account the features of the [Swagger 2.0 specification](https://swagger.io/docs/specification/2-0/basic-structure/).

Acceptance test generation
--------------------------

[](#acceptance-test-generation)

The paths in your Swagger specification will look something like `/api/entity/{entityID}/other/{otherID}`How then do we generate an actual url using real entity ids which will produce a real response? By default, this library will do this using the `enum` and `x-example` fields. However, it is more useful to be able to specify your own ids. You can create a handler for each entity. Use annotations to link them to one or more paths. Then, all you need to do is tell the system where to find them.

```
namespace My\API\PathHandlers

/**
 * @path /api/entity/{entityID}/other/
 * @path /api/entity/{entityID}/other/{otherID}
 */
class MyPathHandler implements \Swagception\PathHandler\HandlesPath
{
    public function convertPath($path)
    {
        //Replace {entityID} with a real id
        if (strpos($path, '{entityID}') !== false) {
            $path = str_replace('{entityID}', $this->getEntityID(), $path);
        }

        //Replace {otherID} with a real id
        if (strpos($path, '{otherID}') !== false) {
            $path = str_replace('{otherID}', $this->getOtherID(), $path);
        }

        return $path;
    }

    protected function getEntityID()
    {
        //$ids = 100 valid entity ids. Pulled from either the database or the api.
        return $ids[mt_rand(0, 99)];
    }

    protected function getOtherID()
    {
        //$ids = 100 valid other ids. Pulled from either the database or the api.
        return $ids[mt_rand(0, 99)];
    }
}
```

Swagception extension
---------------------

[](#swagception-extension)

Now that you have some real data at your disposal, it's time to configure your codeception environment for Swagception. You only need to set up one extension. More information about codeception extensions is available [here](https://codeception.com/docs/08-Customization#Extension). Put the following in your codeception.yml or suite.yml file:

```
extensions:
  enabled:
    - \Swagception\Extension\Swagception
```

Cest structure
--------------

[](#cest-structure)

With the full functionality of Swagception enabled, it's time to link everything together in your Codeception cest. The recommended way is to feed the paths (end points) into a cest data provider. Your terminal will then output a line for each path.

```
class MyCest
{
    use \Swagception\ContainerTrait;

    public function __construct()
    {
        //Configure the swagger schema object.
        $this->swaggerContainer = new \Swagception\Container\Container();

        $this->swaggerContainer->getSchema()
            //Path to your existing Swagger specification
            ->withSchemaURI('/path/to/swagger.json')
        ;

        //Configure the path handler loader.
        $this->swaggerContainer->getPathHandlerLoader()
            //Set this if you are using your own path handlers, and not relying upon enum and x-example.
            ->withNamespace('My\\API\\PathHandlers')

            //Set this if your path handler classes have not been loaded into the system yet.
            ->withFilePath('/path/to/pathhandlers/')
        ;
    }

    /**
     * @dataProvider _pathProvider
     */
    public function path(MyTester $I, \Codeception\Scenario $S, \Codeception\Example $data)
    {
        $path = $data[0];
        $this->swaggerContainer->getSchema()->testPath($path);
    }

    protected function _pathProvider()
    {
        //Will return an array of arrays.
        return array_map(function($val) {
            return [$val];
        }, $this->swaggerContainer->getSchema()->getPaths());
    }
}
```

Alternatively, you can loop through them in a single function.

```
public function paths(MyTester $I, \Codeception\Scenario $S)
{
    $schema = $this->swaggerContainer->getSchema();
    foreach ($schema->getPaths() as $path) {
        $schema->testPath($path);
    }
}
```

Or, if you already have the json and the schema objects, you can call the validation method directly.

```
(new \Swagception\Validator\Validator())
    ->validate($schema, $json);
```

More settings
-------------

[](#more-settings)

See more [configuration options](docs/01-MoreConfiguration.md).

Issues?
-------

[](#issues)

Log a [github issue](https://github.com/mlambley/swagception/issues). Your assistance is appreciated.

###  Health Score

37

—

LowBetter than 83% of packages

Maintenance16

Infrequent updates — may be unmaintained

Popularity33

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity70

Established project with proven stability

 Bus Factor1

Top contributor holds 56.3% 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 ~85 days

Recently: every ~174 days

Total

10

Last Release

2127d ago

Major Versions

0.0.1-alpha.8 → 1.02018-10-30

### Community

Maintainers

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

---

Top Contributors

[![mlambley](https://avatars.githubusercontent.com/u/17264636?v=4)](https://github.com/mlambley "mlambley (9 commits)")[![mark-lambley-simpro](https://avatars.githubusercontent.com/u/24515999?v=4)](https://github.com/mark-lambley-simpro "mark-lambley-simpro (7 commits)")

###  Code Quality

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/mlambley-swagception/health.svg)

```
[![Health](https://phpackages.com/badges/mlambley-swagception/health.svg)](https://phpackages.com/packages/mlambley-swagception)
```

###  Alternatives

[magento/magento2-functional-testing-framework

Magento2 Functional Testing Framework

15511.5M30](/packages/magento-magento2-functional-testing-framework)[lucatume/wp-browser

A set of Codeception modules to test WordPress projects.

6343.8M153](/packages/lucatume-wp-browser)[oat-sa/tao-core

TAO core extension

66136.7k74](/packages/oat-sa-tao-core)[docler-labs/codeception-slim-module

Codeception Module for Slim framework.

13178.0k1](/packages/docler-labs-codeception-slim-module)[whatdafox/codeception-mailtrap

Mailtrap module for Codeception

1545.0k](/packages/whatdafox-codeception-mailtrap)

PHPackages © 2026

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