Skip to content

Commit 35b7450

Browse files
committed
Add real world example
1 parent d8d787a commit 35b7450

File tree

3 files changed

+290
-25
lines changed

3 files changed

+290
-25
lines changed

README.md

Lines changed: 127 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,40 +19,142 @@ composer require ybelenko/openapi-data-mocker
1919

2020
## Usage example
2121

22+
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:
23+
```yaml
24+
description: Real world example schema
25+
type: object
26+
properties:
27+
id:
28+
type: integer
29+
format: int32
30+
minimum: 1
31+
purchased_items:
32+
type: array
33+
items:
34+
type: object
35+
properties:
36+
SKU:
37+
type: string
38+
format: uuid
39+
maxLength: 20
40+
quantity:
41+
type: integer
42+
format: int32
43+
minimum: 1
44+
maximum: 5
45+
price:
46+
type: object
47+
properties:
48+
currency:
49+
type: string
50+
minLength: 3
51+
maxLength: 3
52+
enum:
53+
- USD
54+
- EUR
55+
- RUB
56+
value:
57+
type: number
58+
format: float
59+
minimum: 0.01
60+
maximum: 99.99
61+
manufacturer:
62+
type: object
63+
properties:
64+
name:
65+
type: string
66+
maxLength: 30
67+
country:
68+
type: string
69+
enum:
70+
- CHN
71+
- USA
72+
- RUS
73+
buyer:
74+
type: object
75+
properties:
76+
first_name:
77+
type: string
78+
minLength: 3
79+
maxLength: 15
80+
last_name:
81+
type: string
82+
minLength: 3
83+
maxLength: 15
84+
credit_card:
85+
type: integer
86+
minimum: 1000000000000000
87+
maximum: 10000000000000000
88+
phone:
89+
type: integer
90+
minimum: 10000000000000
91+
maximum: 99999999999999
92+
email:
93+
type: string
94+
format: email
95+
status:
96+
type: string
97+
enum:
98+
- registered
99+
- paid
100+
- shipped
101+
- delivered
102+
default: registered
103+
created_at:
104+
type: string
105+
format: date-time
106+
```
107+
> Notice! While schema object presented in YAML format this library doesn't support YAML or JSON parsing right now. It means that `mockFromSchema` method expects already decoded JSON value as argument.
108+
109+
When we mock mentioned schema with `mockFromSchema` method:
22110
```php
23111
require __DIR__ . '/vendor/autoload.php';
24112
25113
use OpenAPIServer\Mock\OpenApiDataMocker as Mocker;
26114
$mocker = new Mocker();
27115
// set model classes namespace for $ref handling
116+
// current example doesn't use $refs in schemas, however
28117
$mocker->setModelsNamespace('JohnDoesPackage\\Model\\');
29-
$data = [
30-
'Integer from 1 to 100' => $mocker->mockInteger(null, 1, 100),
31-
'Float from -3 to 3' => $mocker->mockNumber(null, -3, 3),
32-
'String 10 chars' => $mocker->mockString(null, 10, 10),
33-
'Boolean' => $mocker->mockBoolean(),
34-
'Array of strings' => $mocker->mockArray(
35-
[
36-
'type' => 'string',
37-
'maxLength' => 20,
38-
]
39-
),
40-
'Object' => $mocker->mockObject([
41-
'id' => [
42-
'type' => 'integer',
43-
'minimum' => 1,
44-
'maximum' => 10
45-
],
46-
'username' => [
47-
'type' => 'string',
48-
'maxLength' => 10,
49-
]
50-
])
51-
];
52-
53-
echo json_encode($data, JSON_PRETTY_PRINT);
118+
// class InvoiceTest contains schema mentioned previously
119+
// it returns that schema with getOpenApiSchema() method declared in OpenAPIServer\Mock\BaseModel parent class
120+
$schema = \OpenAPIServer\Mock\Model\InvoiceTest::getOpenApiSchema();
121+
$data = $mocker->mockFromSchema($schema);
122+
echo json_encode($data, \JSON_PRETTY_PRINT);
54123
```
55124

125+
the output looks like:
126+
```json
127+
{
128+
"id": 1912777939,
129+
"purchased_items": [
130+
{
131+
"SKU": "5ee78cfde9f05",
132+
"quantity": 4,
133+
"price": {
134+
"currency": "EUR",
135+
"value": 57.635
136+
},
137+
"manufacturer": {
138+
"name": "Lorem i",
139+
"country": "USA"
140+
}
141+
}
142+
],
143+
"buyer": {
144+
"first_name": "Lorem ipsum do",
145+
"last_name": "Lorem ipsum ",
146+
"credit_card": 2455087473915908,
147+
"phone": 65526260517693,
148+
"email": "jfkennedy@example.com"
149+
},
150+
"status": "delivered",
151+
"created_at": "1978-08-08T04:03:09+00:00"
152+
}
153+
```
154+
Of course that output will be slightly different on every call. That's what mocker package has been developed for.
155+
156+
You can check extended example at [examples/extended_example.php](examples/extended_example.php).
157+
56158
## Supported features
57159

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

examples/extended_example.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
require_once __DIR__ . '/../vendor/autoload.php';
4+
5+
use OpenAPIServer\Mock\OpenapiDataMocker as Mocker;
6+
7+
$mocker = new Mocker();
8+
// set model classes namespace for $ref handling
9+
$mocker->setModelsNamespace('JohnDoesPackage\\Model\\');
10+
11+
// integer from 1 to 100
12+
echo 'Integer from 1 to 100' . PHP_EOL;
13+
echo $mocker->mockInteger(null, 1, 100);
14+
15+
// float from -3 to 3
16+
echo PHP_EOL . PHP_EOL . 'Float from -3 to 3' . PHP_EOL;
17+
echo $mocker->mockNumber(null, -3, 3);
18+
19+
// string 10 chars
20+
echo PHP_EOL . PHP_EOL . 'String 10 chars' . PHP_EOL;
21+
echo $mocker->mockString(null, 10, 10);
22+
23+
// boolean
24+
echo PHP_EOL . PHP_EOL . 'Boolean' . PHP_EOL;
25+
echo $mocker->mockBoolean() ? 'TRUE' : 'FALSE';
26+
27+
// array of strings
28+
echo PHP_EOL . PHP_EOL . 'Array of strings' . PHP_EOL;
29+
$items = ['type' => 'string', 'maxLength' => 20];
30+
echo json_encode($mocker->mockArray($items), \JSON_PRETTY_PRINT);
31+
32+
// object
33+
echo PHP_EOL . PHP_EOL . 'Object' . PHP_EOL;
34+
$props = ['id' => ['type' => 'integer', 'minimum' => 1, 'maximum' => 10], 'username' => ['type' => 'string', 'maxlength' => 10]];
35+
echo json_encode($mocker->mockObject($props), \JSON_PRETTY_PRINT);
36+
37+
echo PHP_EOL . PHP_EOL . 'Real world schema' . PHP_EOL ;
38+
$schema = \OpenAPIServer\Mock\Model\InvoiceTest::getOpenApiSchema();
39+
$data = $mocker->mockFromSchema($schema);
40+
echo json_encode($data, \JSON_PRETTY_PRINT);

test/Mock/Model/InvoiceTest.php

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
3+
namespace OpenAPIServer\Mock\Model;
4+
5+
use OpenAPIServer\Mock\BaseModel;
6+
7+
// real world complex schema
8+
class InvoiceTest extends BaseModel
9+
{
10+
protected const MODEL_SCHEMA = <<<'SCHEMA'
11+
{
12+
"description": "Real world example schema",
13+
"type" : "object",
14+
"properties": {
15+
"id": {
16+
"type": "integer",
17+
"format": "int32",
18+
"minimum": 1
19+
},
20+
"purchased_items": {
21+
"type": "array",
22+
"items": {
23+
"type": "object",
24+
"properties": {
25+
"SKU": {
26+
"type": "string",
27+
"format": "uuid",
28+
"maxLength": 20
29+
},
30+
"quantity": {
31+
"type": "integer",
32+
"format": "int32",
33+
"minimum": 1,
34+
"maximum": 5
35+
},
36+
"price": {
37+
"type": "object",
38+
"properties": {
39+
"currency": {
40+
"type": "string",
41+
"minLength": 3,
42+
"maxLength": 3,
43+
"enum": [
44+
"USD",
45+
"EUR",
46+
"RUB"
47+
]
48+
},
49+
"value": {
50+
"type": "number",
51+
"format": "float",
52+
"minimum": 0.01,
53+
"maximum": 99.99
54+
}
55+
}
56+
},
57+
"manufacturer": {
58+
"type": "object",
59+
"properties": {
60+
"name": {
61+
"type": "string",
62+
"maxLength": 30
63+
},
64+
"country": {
65+
"type": "string",
66+
"enum": [
67+
"CHN",
68+
"USA",
69+
"RUS"
70+
]
71+
}
72+
}
73+
}
74+
}
75+
}
76+
},
77+
"buyer": {
78+
"type": "object",
79+
"properties": {
80+
"first_name": {
81+
"type": "string",
82+
"minLength": 3,
83+
"maxLength": 15
84+
},
85+
"last_name": {
86+
"type": "string",
87+
"minLength": 3,
88+
"maxLength": 15
89+
},
90+
"credit_card": {
91+
"type": "integer",
92+
"minimum": 1000000000000000,
93+
"maximum": 9999999999999999
94+
},
95+
"phone": {
96+
"type": "integer",
97+
"minimum": 10000000000000,
98+
"maximum": 99999999999999
99+
},
100+
"email": {
101+
"type": "string",
102+
"format": "email"
103+
}
104+
}
105+
},
106+
"status": {
107+
"type": "string",
108+
"enum": [
109+
"registered",
110+
"paid",
111+
"shipped",
112+
"delivered"
113+
],
114+
"default": "registered"
115+
},
116+
"created_at": {
117+
"type": "string",
118+
"format": "date-time"
119+
}
120+
}
121+
}
122+
SCHEMA;
123+
}

0 commit comments

Comments
 (0)