Skip to content

Commit bd3cb6b

Browse files
committed
Added tests for OpenAPIv3Provider
1 parent ac673da commit bd3cb6b

File tree

9 files changed

+134
-10
lines changed

9 files changed

+134
-10
lines changed

docs/source/complexTypes/object.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Generated interface:
3737
public function setCar(?Car $name): self;
3838
public function getCar(): ?Car;
3939
40-
// class Person_Car
40+
// class Car
4141
public function setModel(?string $name): self;
4242
public function getModel(): ?string;
4343
public function setPs(?int $name): self;

src/SchemaProvider/OpenAPIv3Provider.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ public function __construct(string $sourceFile)
3434
throw new SchemaException("Invalid JSON-Schema file $sourceFile");
3535
}
3636

37-
if (!isset($this->openAPIv3Spec['components']['schemas'])) {
37+
if (!isset($this->openAPIv3Spec['components']['schemas']) ||
38+
empty($this->openAPIv3Spec['components']['schemas'])
39+
) {
3840
throw new SchemaException("Open API v3 spec file $sourceFile doesn't contain any schemas to process");
3941
}
4042
}

tests/AbstractPHPModelGeneratorTest.php

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace PHPModelGenerator\Tests;
44

5+
use Exception;
56
use FilesystemIterator;
7+
use PHPModelGenerator\SchemaProvider\OpenAPIv3Provider;
68
use PHPModelGenerator\SchemaProvider\RecursiveDirectoryProvider;
79
use PHPModelGenerator\Utils\ClassNameGenerator;
810
use PHPModelGenerator\Exception\ErrorRegistryException;
@@ -52,6 +54,7 @@ public function setUp(): void
5254

5355
/**
5456
* Check if the test has failed. In this case move all JSON files and generated classes in a directory for debugging
57+
*
5558
* Additionally clear the test folder so the next test starts in an empty environment
5659
*/
5760
public function tearDown(): void
@@ -105,6 +108,7 @@ private function copyExternalJSON(): void
105108
* @param string $file
106109
* @param GeneratorConfiguration|null $generatorConfiguration
107110
* @param bool $originalClassNames
111+
* @param string $schemaProviderClass
108112
*
109113
* @return string
110114
*
@@ -115,12 +119,14 @@ private function copyExternalJSON(): void
115119
protected function generateClassFromFile(
116120
string $file,
117121
GeneratorConfiguration $generatorConfiguration = null,
118-
bool $originalClassNames = false
122+
bool $originalClassNames = false,
123+
string $schemaProviderClass = RecursiveDirectoryProvider::class
119124
): string {
120125
return $this->generateClass(
121126
file_get_contents(__DIR__ . '/Schema/' . $this->getStaticClassName() . '/' . $file),
122127
$generatorConfiguration,
123-
$originalClassNames
128+
$originalClassNames,
129+
$schemaProviderClass
124130
);
125131
}
126132

@@ -164,20 +170,23 @@ function ($item) use ($escape) {
164170
/**
165171
* Generate a class from a given JSON schema string and return the FQCN
166172
*
167-
* @param string $jsonSchema
168-
* @param GeneratorConfiguration $generatorConfiguration
169-
* @param bool $originalClassNames
173+
* @param string $jsonSchema
174+
* @param GeneratorConfiguration|null $generatorConfiguration
175+
* @param bool $originalClassNames
176+
* @param string $schemaProviderClass
170177
*
171178
* @return string
172179
*
180+
* @throws Exception
173181
* @throws FileSystemException
174182
* @throws RenderException
175183
* @throws SchemaException
176184
*/
177185
protected function generateClass(
178186
string $jsonSchema,
179187
GeneratorConfiguration $generatorConfiguration = null,
180-
bool $originalClassNames = false
188+
bool $originalClassNames = false,
189+
string $schemaProviderClass = RecursiveDirectoryProvider::class
181190
): string {
182191
$generatorConfiguration = ($generatorConfiguration ?? (new GeneratorConfiguration())->setCollectErrors(false))
183192
->setPrettyPrint(false)
@@ -213,11 +222,22 @@ public function getClassName(
213222
$jsonSchema = json_encode($jsonSchemaArray);
214223
}
215224

216-
file_put_contents($baseDir . DIRECTORY_SEPARATOR . $className . '.json', $jsonSchema);
225+
$mainFile = $baseDir . DIRECTORY_SEPARATOR . $className . '.json';
226+
file_put_contents($mainFile, $jsonSchema);
217227
$this->copyExternalJSON();
218228

229+
switch ($schemaProviderClass) {
230+
case RecursiveDirectoryProvider::class:
231+
$schemaProvider = new RecursiveDirectoryProvider($baseDir);
232+
break;
233+
case OpenAPIv3Provider::class:
234+
$schemaProvider = new OpenAPIv3Provider($mainFile);
235+
break;
236+
default: throw new Exception("Schema provider $schemaProviderClass not supported");
237+
}
238+
219239
$generatedFiles = (new ModelGenerator($generatorConfiguration))->generateModels(
220-
new RecursiveDirectoryProvider($baseDir),
240+
$schemaProvider,
221241
$baseDir . DIRECTORY_SEPARATOR . 'Models' . DIRECTORY_SEPARATOR
222242
);
223243

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"components": {
3+
"schemas": {
4+
}
5+
}
6+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"components": {
3+
"schemas": {
4+
}
5+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"components": {
3+
"schemas": {
4+
"OpenApiPerson": {
5+
"type": "object",
6+
"properties": {
7+
"name": {
8+
"type": "string"
9+
},
10+
"age": {
11+
"type": "integer"
12+
}
13+
}
14+
},
15+
"OpenApiCar": {
16+
"type": "object",
17+
"$id": "OpenApiCarWithCustomId",
18+
"properties": {
19+
"ps": {
20+
"type": "integer"
21+
}
22+
}
23+
}
24+
}
25+
}
26+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"id": "Demo"
3+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"components": {
3+
}
4+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace PHPModelGenerator\Tests\SchemaProvider;
4+
5+
use PHPModelGenerator\Exception\SchemaException;
6+
use PHPModelGenerator\SchemaProvider\OpenAPIv3Provider;
7+
use PHPModelGenerator\Tests\AbstractPHPModelGeneratorTest;
8+
9+
/**
10+
* Class OpenAPIv3ProviderTest
11+
*
12+
* @package PHPModelGenerator\Tests\SchemaProvider
13+
*/
14+
class OpenAPIv3ProviderTest extends AbstractPHPModelGeneratorTest
15+
{
16+
public function testInvalidJSONSchemaFileThrowsAnException(): void
17+
{
18+
$this->expectException(SchemaException::class);
19+
$this->expectExceptionMessageMatches('/^Invalid JSON-Schema file (.*)\.json$/');
20+
21+
$this->generateClassFromFile('InvalidJSONSchema.json', null, false, OpenAPIv3Provider::class);
22+
}
23+
24+
/**
25+
* @dataProvider missingSchemasDataProvider
26+
*/
27+
public function testOpenAPIv3JSONSchemaFileWithoutSchemasThrowsAnException(string $file): void
28+
{
29+
$this->expectException(SchemaException::class);
30+
$this->expectExceptionMessageMatches(
31+
"/^Open API v3 spec file (.*)\.json doesn't contain any schemas to process$/"
32+
);
33+
34+
$this->generateClassFromFile($file, null, false, OpenAPIv3Provider::class);
35+
}
36+
37+
public function missingSchemasDataProvider(): array
38+
{
39+
return [
40+
'No components section defined' => ['NoComponents.json'],
41+
'No schemas section defined' => ['NoSchemas.json'],
42+
'Empty schemas section' => ['EmptySchemas.json'],
43+
];
44+
}
45+
46+
public function testOpenAPIv3SchemaProvider(): void
47+
{
48+
$this->generateClassFromFile('MultipleSchemaDefinitions.json', null, false, OpenAPIv3Provider::class);
49+
50+
$person = new \OpenApiPerson(['name' => 'Hannes']);
51+
$this->assertSame('Hannes', $person->getName());
52+
$this->assertNull($person->getAge());
53+
54+
// test if the custom ID is preferred over the object key
55+
$car = new \OpenApiCarWithCustomId(['ps' => 150]);
56+
$this->assertSame(150, $car->getPs());
57+
}
58+
}

0 commit comments

Comments
 (0)