PHPackages                             ybelenko/openapi-data-mocker - 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. ybelenko/openapi-data-mocker

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

ybelenko/openapi-data-mocker
============================

Library that generates fake data from Swagger 2.0|Openapi 3.0 spec

1.1.0(4y ago)1230.5k↓45.9%1[4 issues](https://github.com/ybelenko/openapi-data-mocker/issues)1MITPHPPHP ^7.3 || ^8.0

Since Jun 8Pushed 4y ago1 watchersCompare

[ Source](https://github.com/ybelenko/openapi-data-mocker)[ Packagist](https://packagist.org/packages/ybelenko/openapi-data-mocker)[ RSS](/packages/ybelenko-openapi-data-mocker/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (3)Versions (7)Used By (1)

Openapi Data Mocker
===================

[](#openapi-data-mocker)

[![Latest Stable Version](https://camo.githubusercontent.com/0f0ae99aee45d22ff25a846300b04d6c9d742996f44ea09857e0c0e7497cbf64/68747470733a2f2f706f7365722e707567782e6f72672f7962656c656e6b6f2f6f70656e6170692d646174612d6d6f636b65722f762f737461626c65)](https://packagist.org/packages/ybelenko/openapi-data-mocker)[![Build Status](https://github.com/ybelenko/openapi-data-mocker/actions/workflows/ci.yml/badge.svg)](https://github.com/ybelenko/openapi-data-mocker/actions/workflows/ci.yml)[![Coverage Status](https://camo.githubusercontent.com/8979aea9d7601b5b819fc48deed4ba22d50d7ed9cd7d17120fbc97cc3fed6d3c/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f7962656c656e6b6f2f6f70656e6170692d646174612d6d6f636b65722f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/ybelenko/openapi-data-mocker?branch=master)[![License](https://camo.githubusercontent.com/36dca6075bde761e7b194535fcb089bd190b376900258d0e8c35da74b1ddcfad/68747470733a2f2f706f7365722e707567782e6f72672f7962656c656e6b6f2f6f70656e6170692d646174612d6d6f636b65722f6c6963656e7365)](https://packagist.org/packages/ybelenko/openapi-data-mocker)

Openapi Data Mocker helps to generate fake data from OpenAPI 3.0 documents. Most of the methods may work with 2.0 version(fka Swagger 2.0), but it's not tested. This package was an enhancement of PHP Slim4 server in [OpenAPI Generator](https://github.com/OpenAPITools/openapi-generator) project, but it easier to maintain it in separated repo.

Requirements
------------

[](#requirements)

- PHP ^7.3

**Important notice! While PHP 8.0 declared in composer.json this package hasn't been tested against it.**

Installation via Composer
-------------------------

[](#installation-via-composer)

Run in terminal:

```
composer require ybelenko/openapi-data-mocker
```

Usage example
-------------

[](#usage-example)

Imagine we have [OpenAPI Specification 3.0.3 - Schema Object](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.3.md#schema-object) like this:

```
description: Real world example schema
type: object
properties:
  id:
    type: integer
    format: int32
    minimum: 1
  purchased_items:
    type: array
    items:
      type: object
      properties:
        SKU:
          type: string
          format: uuid
          maxLength: 20
        quantity:
          type: integer
          format: int32
          minimum: 1
          maximum: 5
        price:
          type: object
          properties:
            currency:
              type: string
              minLength: 3
              maxLength: 3
              enum:
              - USD
              - EUR
              - RUB
            value:
              type: number
              format: float
              minimum: 0.01
              maximum: 99.99
        manufacturer:
          type: object
          properties:
            name:
              type: string
              maxLength: 30
            country:
              type: string
              enum:
              - CHN
              - USA
              - RUS
  buyer:
    type: object
    properties:
      first_name:
        type: string
        minLength: 3
        maxLength: 15
      last_name:
        type: string
        minLength: 3
        maxLength: 15
      credit_card:
        type: integer
        minimum: 1000000000000000
        maximum: 10000000000000000
      phone:
        type: integer
        minimum: 10000000000000
        maximum: 99999999999999
      email:
        type: string
        format: email
  status:
    type: string
    enum:
    - registered
    - paid
    - shipped
    - delivered
    default: registered
  created_at:
    type: string
    format: date-time
```

> Notice! While schema object presented in YAML format this library doesn't support YAML or JSON parsing right now. It means that `mockSchemaObject` method expects already decoded JSON value as argument.

When we mock mentioned schema with `mockSchemaObject` method:

```
require __DIR__ . '/vendor/autoload.php';

use OpenAPIServer\Mock\OpenApiDataMocker as Mocker;
$mocker = new Mocker();
// set model classes namespace for $ref handling
// current example doesn't use $refs in schemas, however
$mocker->setModelsNamespace('JohnDoesPackage\\Model\\');
// class InvoiceTest contains schema mentioned previously
// it returns that schema with getOpenApiSchema() method declared in OpenAPIServer\Mock\BaseModel parent class
$schema = \OpenAPIServer\Mock\Model\InvoiceTest::getOpenApiSchema();
$data = $mocker->mockSchemaObject($schema);
echo json_encode($data, \JSON_PRETTY_PRINT);
```

the output looks like:

```
{
    "id": 1912777939,
    "purchased_items": [
        {
            "SKU": "5ee78cfde9f05",
            "quantity": 4,
            "price": {
                "currency": "EUR",
                "value": 57.635
            },
            "manufacturer": {
                "name": "Lorem i",
                "country": "USA"
            }
        }
    ],
    "buyer": {
        "first_name": "Lorem ipsum do",
        "last_name": "Lorem ipsum ",
        "credit_card": 2455087473915908,
        "phone": 65526260517693,
        "email": "jfkennedy@example.com"
    },
    "status": "delivered",
    "created_at": "1978-08-08T04:03:09+00:00"
}
```

Of course that output will be slightly different on every call. That's what mocker package has been developed for.

You can check extended example at [examples/extended\_example.php](examples/extended_example.php).

Supported features
------------------

[](#supported-features)

All data types supported except specific string formats: `email`, `uuid`, `password` which are poorly implemented.

### Data Types Support

[](#data-types-support)

Data TypeData FormatSupported`integer``int32`✅`integer``int64`✅`number``float`✅`number``double``string``byte`✅`string``binary`✅`boolean`✅`string``date`✅`string``date-time`✅`string``password`✅`string``email`✅`string``uuid`✅### Data Options Support

[](#data-options-support)

Data TypeOptionSupported`string``minLength`✅`string``maxLength`✅`string``enum`✅`string``pattern``integer``minimum`✅`integer``maximum`✅`integer``exclusiveMinimum`✅`integer``exclusiveMaximum`✅`number``minimum`✅`number``maximum`✅`number``exclusiveMinimum`✅`number``exclusiveMaximum`✅`array``items`✅`array``additionalItems``array``minItems`✅`array``maxItems`✅`array``uniqueItems``object``properties`✅`object``maxProperties``object``minProperties``object``patternProperties``object``additionalProperties``object``required``*``$ref`✅`*``allOf``*``anyOf``*``oneOf``*``not`Known Limitations
-----------------

[](#known-limitations)

Avoid circular refs in your schema. Schema below can cause infinite loop and `Out of Memory` PHP error:

```
# ModelA has reference to ModelB while ModelB has reference to ModelA.
# Mock server will produce huge nested JSON example and ended with `Out of Memory` error.
definitions:
  ModelA:
    type: object
    properties:
      model_b:
        $ref: '#/definitions/ModelB'
  ModelB:
    type: array
    items:
      $ref: '#/definitions/ModelA'
```

Don't ref scalar types, because generator will not produce models which mock server can find. So schema below will cause error:

```
# generated build contains only `OuterComposite` model class which referenced to not existed `OuterNumber`, `OuterString`, `OuterBoolean` classes
# mock server cannot mock `OuterComposite` model and throws exception
definitions:
  OuterComposite:
    type: object
    properties:
      my_number:
        $ref: '#/definitions/OuterNumber'
      my_string:
        $ref: '#/definitions/OuterString'
      my_boolean:
        $ref: '#/definitions/OuterBoolean'
  OuterNumber:
    type: number
  OuterString:
    type: string
  OuterBoolean:
    type: boolean
```

Links to mentioned technologies
-------------------------------

[](#links-to-mentioned-technologies)

- [OpenAPI Specification 3.0.3](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.3.md)
- [OpenAPI Generator](https://openapi-generator.tech)
- [Composer](https://getcomposer.org/download/)

###  Health Score

35

—

LowBetter than 79% of packages

Maintenance13

Infrequent updates — may be unmaintained

Popularity36

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 100% 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 ~211 days

Total

3

Last Release

1738d ago

Major Versions

0.0.1 → 1.0.02020-06-19

PHP version history (3 changes)0.0.1PHP ^7.1

1.0.0PHP ^7.2

1.1.0PHP ^7.3 || ^8.0

### Community

Maintainers

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

---

Top Contributors

[![ybelenko](https://avatars.githubusercontent.com/u/5541023?v=4)](https://github.com/ybelenko "ybelenko (90 commits)")

---

Tags

datafakefakermockmockeroasoas3openapiswaggerfakerdataswaggeropenapimockfakemocker

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/ybelenko-openapi-data-mocker/health.svg)

```
[![Health](https://phpackages.com/badges/ybelenko-openapi-data-mocker/health.svg)](https://phpackages.com/packages/ybelenko-openapi-data-mocker)
```

###  Alternatives

[phpspec/prophecy

Highly opinionated mocking framework for PHP 5.3+

8.5k551.7M678](/packages/phpspec-prophecy)[nelmio/alice

Expressive fixtures generator

2.5k43.4M132](/packages/nelmio-alice)[fakerino/fakerino

Faker framework, for generate every kind of fake data for test, database seed, mock responses, other

12214.8k5](/packages/fakerino-fakerino)[icecave/isolator

Dependency injection for global functions.

371.3M29](/packages/icecave-isolator)[dnadesign/silverstripe-populate

Populate your database through YAML files

25102.3k2](/packages/dnadesign-silverstripe-populate)

PHPackages © 2026

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