-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathOpenAPIFaker.php
165 lines (134 loc) · 4.83 KB
/
OpenAPIFaker.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
declare(strict_types=1);
namespace Vural\OpenAPIFaker;
use cebe\openapi\exceptions\TypeErrorException;
use cebe\openapi\exceptions\UnresolvableReferenceException;
use cebe\openapi\spec\MediaType;
use cebe\openapi\spec\OpenApi;
use cebe\openapi\spec\Operation;
use cebe\openapi\spec\RequestBody;
use cebe\openapi\spec\Response;
use cebe\openapi\spec\Schema;
use Exception;
use League\OpenAPIValidation\PSR7 as LeagueOpenAPI;
use Vural\OpenAPIFaker\Exception\NoPath;
use Vural\OpenAPIFaker\Exception\NoRequest;
use Vural\OpenAPIFaker\Exception\NoResponse;
use Vural\OpenAPIFaker\Exception\NoSchema;
use Vural\OpenAPIFaker\SchemaFaker\SchemaFaker;
use function array_key_exists;
use function method_exists;
use function strtolower;
final class OpenAPIFaker
{
private OpenApi $openAPISchema;
private Options $options;
/** @codeCoverageIgnore */
private function __construct()
{
$this->options = new Options();
}
/**
* @throws TypeErrorException
* @throws UnresolvableReferenceException
*/
public static function createFromJson(string $json): self
{
$instance = new static();
$instance->openAPISchema = (new LeagueOpenAPI\SchemaFactory\JsonFactory($json))->createSchema();
return $instance;
}
/**
* @throws TypeErrorException
* @throws UnresolvableReferenceException
*/
public static function createFromYaml(string $yaml): self
{
$instance = new static();
$instance->openAPISchema = (new LeagueOpenAPI\SchemaFactory\YamlFactory($yaml))->createSchema();
return $instance;
}
/**
* @throws NoPath
* @throws NoRequest
*/
public function mockRequest(
string $path,
string $method,
string $contentType = 'application/json',
): mixed {
$operation = $this->findOperation($path, $method);
if ($operation->requestBody === null) {
throw NoRequest::forPathAndMethod($path, $method);
}
/** @var RequestBody $requestBody */
$requestBody = $operation->requestBody;
$contents = $requestBody->content;
if (! array_key_exists($contentType, $contents)) {
throw NoRequest::forPathAndMethodAndContentType($path, $method, $contentType);
}
return (new SchemaFaker($contents[$contentType]->schema, $this->options, true))->generate();
}
/**
* @throws NoPath
* @throws NoResponse
*/
public function mockResponse(
string $path,
string $method,
string $statusCode = '200',
string $contentType = 'application/json',
): mixed {
$operation = $this->findOperation($path, $method);
if ($operation->responses === null) {
throw NoResponse::forPathAndMethod($path, $method);
}
if (! $operation->responses->hasResponse($statusCode)) {
throw NoResponse::forPathAndMethodAndStatusCode($path, $method, $statusCode);
}
/** @var Response $response */
$response = $operation->responses->getResponse($statusCode);
$contents = $response->content;
if (! array_key_exists($contentType, $contents)) {
throw NoResponse::forPathAndMethodAndStatusCode($path, $method, $statusCode);
}
/** @var MediaType $content */
$content = $contents[$contentType];
return (new SchemaFaker($content->schema, $this->options))->generate();
}
/** @throws Exception */
public function mockComponentSchema(string $schemaName): mixed
{
if ($this->openAPISchema->components === null) {
throw NoSchema::forZeroComponents();
}
if (! array_key_exists($schemaName, $this->openAPISchema->components->schemas)) {
throw NoSchema::forComponentName($schemaName);
}
/** @var Schema $schema */
$schema = $this->openAPISchema->components->schemas[$schemaName];
return (new SchemaFaker($schema, $this->options))->generate();
}
/** @param array{minItems?:?int, maxItems?:?int, alwaysFakeOptionals?:bool} $options */
public function setOptions(array $options): self
{
foreach ($options as $key => $value) {
if (! method_exists($this->options, 'set' . $key)) {
continue;
}
$this->options->{'set' . $key}($value);
}
return $this;
}
/** @throws NoPath */
private function findOperation(string $path, string $method): Operation
{
try {
$operation = (new LeagueOpenAPI\SpecFinder($this->openAPISchema))
->findOperationSpec(new LeagueOpenAPI\OperationAddress($path, strtolower($method)));
} catch (LeagueOpenAPI\Exception\NoPath) {
throw NoPath::forPathAndMethod($path, $method);
}
return $operation;
}
}