-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathE2ETest.php
149 lines (122 loc) · 4.44 KB
/
E2ETest.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
<?php
declare(strict_types=1);
namespace Vural\OpenAPIFaker\Tests;
use cebe\openapi\spec\MediaType;
use cebe\openapi\spec\RequestBody;
use cebe\openapi\spec\Schema;
use League\OpenAPIValidation\PSR7\SchemaFactory\YamlFactory;
use League\OpenAPIValidation\Schema\SchemaValidator;
use PHPUnit\Framework\TestCase;
use Throwable;
use Vural\OpenAPIFaker\OpenAPIFaker;
use function Safe\file_get_contents;
use function Safe\sprintf;
/** @group Integration */
class E2ETest extends TestCase
{
/**
* @test
* @dataProvider specProvider
*/
function it_can_generate_valid_request(string $filename)
{
$file = file_get_contents(sprintf('%s/../specs/%s.yaml', __DIR__, $filename));
$faker = OpenAPIFaker::createFromYaml($file);
$schema = (new YamlFactory($file))->createSchema();
foreach ($schema->paths->getPaths() as $path => $pathItem) {
foreach ($pathItem->getOperations() as $method => $operation) {
/** @var RequestBody|null $requestBody */
$requestBody = $operation->requestBody;
if ($requestBody === null) {
continue;
}
/**
* @var string $contentType
* @var MediaType $mediaType
*/
foreach ($requestBody->content as $contentType => $mediaType) {
/** @var Schema|null $schema */
$schema = $mediaType->schema;
if ($schema === null) {
continue;
}
$response = $faker->mockRequest($path, $method, $contentType);
try {
(new SchemaValidator())->validate($response, $schema);
} catch (Throwable $e) {
self::fail($e->getMessage());
}
}
}
}
self::assertTrue(true);
}
/**
* @test
* @dataProvider specProvider
*/
function it_can_generate_valid_response(string $filename)
{
$file = file_get_contents(sprintf('%s/../specs/%s.yaml', __DIR__, $filename));
$faker = OpenAPIFaker::createFromYaml($file);
$schema = (new YamlFactory($file))->createSchema();
foreach ($schema->paths->getPaths() as $path => $pathItem) {
foreach ($pathItem->getOperations() as $method => $operation) {
if ($operation->responses === null) {
continue;
}
foreach ($operation->responses as $statusCode => $response) {
foreach ($response->content as $contentType => $mediaType) {
if ($mediaType->schema === null) {
continue;
}
if ($mediaType->schema->description !== 'Video search results') {
continue;
}
$response = $faker->mockResponse($path, $method, (string) $statusCode, $contentType);
try {
(new SchemaValidator())->validate($response, $mediaType->schema);
} catch (Throwable $e) {
self::fail($e->getMessage());
}
}
}
}
}
self::assertTrue(true);
}
/**
* @test
* @dataProvider specProvider
*/
function it_can_generate_valid_component(string $filename)
{
$file = file_get_contents(sprintf('%s/../specs/%s.yaml', __DIR__, $filename));
$faker = OpenAPIFaker::createFromYaml($file);
$schema = (new YamlFactory($file))->createSchema();
self::assertNotNull($schema->components);
/**
* @var string $schemaName
* @var Schema $schema
*/
foreach ($schema->components->schemas as $schemaName => $schema) {
$mockSchema = $faker->mockComponentSchema($schemaName);
try {
(new SchemaValidator())->validate($mockSchema, $schema);
} catch (Throwable $e) {
self::fail($e->getMessage());
}
}
self::assertTrue(true);
}
/** @return string[][] */
public function specProvider(): array
{
return [
['petstore'],
['twitter'],
['uber'],
['uspto'],
];
}
}