Skip to content

Commit 856e1d6

Browse files
committed
Merge branches 'master' and '96-component-schema-should-be-optional' of github.com:php-openapi/yii2-openapi into fix-conflicting-pull-requests
2 parents 75a8cf0 + 24d559a commit 856e1d6

File tree

5 files changed

+186
-2
lines changed

5 files changed

+186
-2
lines changed

src/lib/SchemaToDatabase.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public function prepareModels(): array
9494

9595
$openApi = $this->config->getOpenApi();
9696
$junctions = $this->findJunctionSchemas();
97-
foreach ($openApi->components->schemas as $schemaName => $openApiSchema) {
97+
foreach ($openApi->components->schemas ?? [] as $schemaName => $openApiSchema) {
9898
$schema = Yii::createObject(ComponentSchema::class, [$openApiSchema, $schemaName]);
9999

100100
if (!$this->canGenerateModel($schemaName, $schema)) {
@@ -153,7 +153,7 @@ public function findJunctionSchemas(): JunctionSchemas
153153
{
154154
$junctions = [];
155155
$openApi = $this->config->getOpenApi();
156-
foreach ($openApi->components->schemas as $schemaName => $openApiSchema) {
156+
foreach ($openApi->components->schemas ?? [] as $schemaName => $openApiSchema) {
157157
/**@var ComponentSchema $schema */
158158
$schema = Yii::createObject(ComponentSchema::class, [$openApiSchema, $schemaName]);
159159
if ($schema->isNonDb()) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
return [
4+
'openApiPath' => '@specs/issue_fix/96_component_schema_should_be_optional/index.yml',
5+
'generateUrls' => false,
6+
'generateModels' => true,
7+
'excludeModels' => [
8+
'Error',
9+
],
10+
'generateControllers' => false,
11+
'generateMigrations' => false,
12+
'generateModelFaker' => true,
13+
];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
openapi: "3.0.0"
2+
info:
3+
version: 1.0.0
4+
title: 96_component_schema_should_be_optional
5+
6+
paths:
7+
/:
8+
get:
9+
summary: List
10+
operationId: list
11+
responses:
12+
'200':
13+
description: The information
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
<?php
2+
3+
namespace app\models;
4+
5+
use Faker\Factory as FakerFactory;
6+
use Faker\Generator;
7+
use Faker\UniqueGenerator;
8+
9+
/**
10+
* Base fake data generator
11+
*/
12+
abstract class BaseModelFaker
13+
{
14+
/**
15+
* @var Generator
16+
*/
17+
protected $faker;
18+
/**
19+
* @var UniqueGenerator
20+
*/
21+
protected $uniqueFaker;
22+
23+
public function __construct()
24+
{
25+
$this->faker = FakerFactory::create(str_replace('-', '_', \Yii::$app->language));
26+
$this->uniqueFaker = new UniqueGenerator($this->faker);
27+
}
28+
29+
abstract public function generateModel($attributes = []);
30+
31+
public function getFaker():Generator
32+
{
33+
return $this->faker;
34+
}
35+
36+
public function getUniqueFaker():UniqueGenerator
37+
{
38+
return $this->uniqueFaker;
39+
}
40+
41+
public function setFaker(Generator $faker):void
42+
{
43+
$this->faker = $faker;
44+
}
45+
46+
public function setUniqueFaker(UniqueGenerator $faker):void
47+
{
48+
$this->uniqueFaker = $faker;
49+
}
50+
51+
/**
52+
* Generate and return model
53+
* @param array|callable $attributes
54+
* @param UniqueGenerator|null $uniqueFaker
55+
* @return \yii\db\ActiveRecord
56+
* @example MyFaker::makeOne(['user_id' => 1, 'title' => 'foo']);
57+
* @example MyFaker::makeOne( function($model, $faker) {
58+
* $model->scenario = 'create';
59+
* $model->setAttributes(['user_id' => 1, 'title' => $faker->sentence]);
60+
* return $model;
61+
* });
62+
*/
63+
public static function makeOne($attributes = [], ?UniqueGenerator $uniqueFaker = null)
64+
{
65+
$fakeBuilder = new static();
66+
if ($uniqueFaker !== null) {
67+
$fakeBuilder->setUniqueFaker($uniqueFaker);
68+
}
69+
$model = $fakeBuilder->generateModel($attributes);
70+
return $model;
71+
}
72+
73+
/**
74+
* Generate, save and return model
75+
* @param array|callable $attributes
76+
* @param UniqueGenerator|null $uniqueFaker
77+
* @return \yii\db\ActiveRecord
78+
* @example MyFaker::saveOne(['user_id' => 1, 'title' => 'foo']);
79+
* @example MyFaker::saveOne( function($model, $faker) {
80+
* $model->scenario = 'create';
81+
* $model->setAttributes(['user_id' => 1, 'title' => $faker->sentence]);
82+
* return $model;
83+
* });
84+
*/
85+
public static function saveOne($attributes = [], ?UniqueGenerator $uniqueFaker = null)
86+
{
87+
$model = static::makeOne($attributes, $uniqueFaker);
88+
$model->save();
89+
return $model;
90+
}
91+
92+
/**
93+
* Generate and return multiple models
94+
* @param int $number
95+
* @param array|callable $commonAttributes
96+
* @return \yii\db\ActiveRecord[]|array
97+
* @example TaskFaker::make(5, ['project_id'=>1, 'user_id' => 2]);
98+
* @example TaskFaker::make(5, function($model, $faker, $uniqueFaker) {
99+
* $model->setAttributes(['name' => $uniqueFaker->username, 'state'=>$faker->boolean(20)]);
100+
* return $model;
101+
* });
102+
*/
103+
public static function make(int $number, $commonAttributes = [], ?UniqueGenerator $uniqueFaker = null):array
104+
{
105+
if ($number < 1) {
106+
return [];
107+
}
108+
$fakeBuilder = new static();
109+
if ($uniqueFaker !== null) {
110+
$fakeBuilder->setUniqueFaker($uniqueFaker);
111+
}
112+
return array_map(function () use ($commonAttributes, $fakeBuilder) {
113+
$model = $fakeBuilder->generateModel($commonAttributes);
114+
return $model;
115+
}, range(0, $number -1));
116+
}
117+
118+
/**
119+
* Generate, save and return multiple models
120+
* @param int $number
121+
* @param array|callable $commonAttributes
122+
* @return \yii\db\ActiveRecord[]|array
123+
* @example TaskFaker::save(5, ['project_id'=>1, 'user_id' => 2]);
124+
* @example TaskFaker::save(5, function($model, $faker, $uniqueFaker) {
125+
* $model->setAttributes(['name' => $uniqueFaker->username, 'state'=>$faker->boolean(20)]);
126+
* return $model;
127+
* });
128+
*/
129+
public static function save(int $number, $commonAttributes = [], ?UniqueGenerator $uniqueFaker = null):array
130+
{
131+
if ($number < 1) {
132+
return [];
133+
}
134+
$fakeBuilder = new static();
135+
if ($uniqueFaker !== null) {
136+
$fakeBuilder->setUniqueFaker($uniqueFaker);
137+
}
138+
return array_map(function () use ($commonAttributes, $fakeBuilder) {
139+
$model = $fakeBuilder->generateModel($commonAttributes);
140+
$model->save();
141+
return $model;
142+
}, range(0, $number -1));
143+
}
144+
}

tests/unit/IssueFixTest.php

+14
Original file line numberDiff line numberDiff line change
@@ -1030,6 +1030,20 @@ public function test23ConsiderOpenapiExtensionXNoRelationAlsoInOtherPertinentPla
10301030
$this->runActualMigrations();
10311031
}
10321032

1033+
// https://github.com/php-openapi/yii2-openapi/issues/96
1034+
public function test96ComponentSchemaShouldBeOptional()
1035+
{
1036+
$testFile = Yii::getAlias("@specs/issue_fix/96_component_schema_should_be_optional/index.php");
1037+
$this->runGenerator($testFile);
1038+
$actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [
1039+
'recursive' => true,
1040+
]);
1041+
$expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/96_component_schema_should_be_optional/mysql"), [
1042+
'recursive' => true,
1043+
]);
1044+
$this->checkFiles($actualFiles, $expectedFiles);
1045+
}
1046+
10331047
// https://github.com/php-openapi/yii2-openapi/issues/87
10341048
public function test87ImplementForJsonInIsrefpointertoschema()
10351049
{

0 commit comments

Comments
 (0)