diff --git a/src/generator/default/dbmodel.php b/src/generator/default/dbmodel.php index 17fa4a84..a3d269b6 100644 --- a/src/generator/default/dbmodel.php +++ b/src/generator/default/dbmodel.php @@ -49,8 +49,8 @@ */ abstract class getClassName() ?> extends \yii\db\ActiveRecord { -getScenarios()): -foreach($scenarios as $scenario): ?> +getScenarios()): +foreach ($scenarios as $scenario): ?> /** * @@ -76,7 +76,7 @@ public static function tableName() { return getTableAlias()) ?>; } - + /** * Automatically generated scenarios from the model 'x-scenarios'. @@ -92,7 +92,7 @@ public function scenarios() $default = parent::scenarios()[self::SCENARIO_DEFAULT]; return [ - + self:: => $default, /** diff --git a/src/lib/generators/ModelsGenerator.php b/src/lib/generators/ModelsGenerator.php index 27dc6aea..d2672ff9 100644 --- a/src/lib/generators/ModelsGenerator.php +++ b/src/lib/generators/ModelsGenerator.php @@ -72,12 +72,6 @@ public function generate():CodeFiles ) )); if ($this->config->generateModelFaker) { - $deps = []; # list of all models that this model is dependent on - foreach ($model->hasOneRelations as $key => $hasOneRelation) { - $deps[] = $model->hasOneRelations[$key]->getClassName(); - } - $deps = array_unique($deps); - $this->files->add(new CodeFile( Yii::getAlias("$fakerPath/{$className}Faker.php"), $this->config->render( @@ -86,7 +80,7 @@ public function generate():CodeFiles 'model' => $model, 'modelNamespace' => $this->config->modelNamespace, 'namespace' => $this->config->fakerNamespace, - 'deps' => $deps, + 'deps' => $model->fakerDependentModels(), ] ) )); diff --git a/src/lib/items/DbModel.php b/src/lib/items/DbModel.php index 5c33c09b..6fab034b 100644 --- a/src/lib/items/DbModel.php +++ b/src/lib/items/DbModel.php @@ -13,7 +13,6 @@ use yii\base\BaseObject; use yii\db\ColumnSchema; use yii\helpers\Inflector; -use yii\helpers\StringHelper; use yii\helpers\VarDumper; use function array_filter; use function array_map; @@ -317,4 +316,21 @@ public function getModelClassDescription(): string } return FormatHelper::getFormattedDescription($this->description); } + + /** + * Return array of models that this models depends on exclusively used in faker. + * Models with `x-faker: false` or with self-reference are excluded + */ + public function fakerDependentModels(): array + { + $result = []; + foreach ($this->attributes as $attribute) { + if ($attribute->reference && $attribute->fakerStub) { + if ($this->name !== $attribute->reference) { # exclude self-referenced models + $result[] = $attribute->reference; + } + } + } + return array_unique($result); + } } diff --git a/src/lib/openapi/PropertySchema.php b/src/lib/openapi/PropertySchema.php index 6b27def1..e4a282dd 100644 --- a/src/lib/openapi/PropertySchema.php +++ b/src/lib/openapi/PropertySchema.php @@ -7,29 +7,27 @@ namespace cebe\yii2openapi\lib\openapi; -use yii\db\ColumnSchema; -use cebe\yii2openapi\generator\ApiGenerator; -use yii\db\mysql\Schema as MySqlSchema; -use SamIT\Yii2\MariaDb\Schema as MariaDbSchema; -use yii\db\pgsql\Schema as PgSqlSchema; -use cebe\yii2openapi\lib\items\Attribute; -use yii\base\NotSupportedException; use BadMethodCallException; use cebe\openapi\ReferenceContext; use cebe\openapi\spec\Reference; use cebe\openapi\SpecObjectInterface; +use cebe\yii2openapi\generator\ApiGenerator; use cebe\yii2openapi\lib\CustomSpecAttr; use cebe\yii2openapi\lib\exceptions\InvalidDefinitionException; +use cebe\yii2openapi\lib\traits\ForeignKeyConstraints; +use SamIT\Yii2\MariaDb\Schema as MariaDbSchema; use Throwable; use Yii; +use yii\base\NotSupportedException; +use yii\db\ColumnSchema; +use yii\db\mysql\Schema as MySqlSchema; +use yii\db\pgsql\Schema as PgSqlSchema; use yii\db\Schema as YiiDbSchema; use yii\helpers\Inflector; use yii\helpers\Json; use yii\helpers\StringHelper; -use yii\helpers\VarDumper; use function is_int; use function strpos; -use cebe\yii2openapi\lib\traits\ForeignKeyConstraints; class PropertySchema { @@ -49,7 +47,7 @@ class PropertySchema /** * @var null|bool|string * If `false`, no faker will be generated in faker model - * See more about usage in README.md file present in root directory of the project + * See more about usage in README.md file present in root directory of this library */ public $xFaker; diff --git a/tests/specs/issue_fix/162_bug_dollarref_with_x_faker/app/models/OrderFaker.php b/tests/specs/issue_fix/162_bug_dollarref_with_x_faker/app/models/OrderFaker.php index c8bbe589..0fca8d6c 100644 --- a/tests/specs/issue_fix/162_bug_dollarref_with_x_faker/app/models/OrderFaker.php +++ b/tests/specs/issue_fix/162_bug_dollarref_with_x_faker/app/models/OrderFaker.php @@ -43,7 +43,6 @@ public static function dependentOn() { return [ // just model class names - 'Invoice', ]; } diff --git a/tests/specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/index.php b/tests/specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/index.php new file mode 100644 index 00000000..9a0fe5d5 --- /dev/null +++ b/tests/specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/index.php @@ -0,0 +1,14 @@ + '@specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/index.yml', + 'generateUrls' => false, + 'generateModels' => true, + 'excludeModels' => [ + 'Error', + ], + 'generateControllers' => false, + 'generateMigrations' => false, + 'generateModelFaker' => true, // `generateModels` must be `true` in order to use `generateModelFaker` as `true` +]; + diff --git a/tests/specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/index.yml b/tests/specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/index.yml new file mode 100644 index 00000000..685dcac6 --- /dev/null +++ b/tests/specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/index.yml @@ -0,0 +1,64 @@ +openapi: 3.0.3 + +info: + title: 'Bug: dependentOn: allOf with "x-faker: false" #52' + version: 1.0.0 + +components: + schemas: + User: + type: object + properties: + id: + type: integer + name: + type: string + Fruit: + type: object + properties: + id: + type: integer + name: + type: string + Animal: + type: object + properties: + id: + type: integer + name: + type: string + Invoice: + title: Invoice + x-table: invoices + type: object + properties: + id: + type: integer + reference_invoice: + allOf: + - $ref: '#/components/schemas/Invoice' + - x-faker: false + - description: This field is only set on invoices of type "cancellation_invoice" + reference_invoice_2: + allOf: + - $ref: '#/components/schemas/Invoice' + - x-faker: true + user: + $ref: '#/components/schemas/User' + user_2: + allOf: + - $ref: '#/components/schemas/User' + - x-faker: false + fruit: + $ref: '#/components/schemas/Fruit' + animal: + allOf: + - $ref: '#/components/schemas/Animal' + - x-faker: false + +paths: + '/': + get: + responses: + '200': + description: OK diff --git a/tests/specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/mysql/models/Animal.php b/tests/specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/mysql/models/Animal.php new file mode 100644 index 00000000..92caf2f3 --- /dev/null +++ b/tests/specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/mysql/models/Animal.php @@ -0,0 +1,10 @@ +generateModels(['author_id' => 1]); + * $model = (new PostFaker())->generateModels(function($model, $faker, $uniqueFaker) { + * $model->scenario = 'create'; + * $model->author_id = 1; + * return $model; + * }); + **/ + public function generateModel($attributes = []) + { + $faker = $this->faker; + $uniqueFaker = $this->uniqueFaker; + $model = new Animal(); + //$model->id = $uniqueFaker->numberBetween(0, 1000000); + $model->name = $faker->sentence; + if (!is_callable($attributes)) { + $model->setAttributes($attributes, false); + } else { + $model = $attributes($model, $faker, $uniqueFaker); + } + return $model; + } +} diff --git a/tests/specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/mysql/models/BaseModelFaker.php b/tests/specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/mysql/models/BaseModelFaker.php new file mode 100644 index 00000000..c367fbb4 --- /dev/null +++ b/tests/specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/mysql/models/BaseModelFaker.php @@ -0,0 +1,144 @@ +faker = FakerFactory::create(str_replace('-', '_', \Yii::$app->language)); + $this->uniqueFaker = new UniqueGenerator($this->faker); + } + + abstract public function generateModel($attributes = []); + + public function getFaker():Generator + { + return $this->faker; + } + + public function getUniqueFaker():UniqueGenerator + { + return $this->uniqueFaker; + } + + public function setFaker(Generator $faker):void + { + $this->faker = $faker; + } + + public function setUniqueFaker(UniqueGenerator $faker):void + { + $this->uniqueFaker = $faker; + } + + /** + * Generate and return model + * @param array|callable $attributes + * @param UniqueGenerator|null $uniqueFaker + * @return \yii\db\ActiveRecord + * @example MyFaker::makeOne(['user_id' => 1, 'title' => 'foo']); + * @example MyFaker::makeOne( function($model, $faker) { + * $model->scenario = 'create'; + * $model->setAttributes(['user_id' => 1, 'title' => $faker->sentence]); + * return $model; + * }); + */ + public static function makeOne($attributes = [], ?UniqueGenerator $uniqueFaker = null) + { + $fakeBuilder = new static(); + if ($uniqueFaker !== null) { + $fakeBuilder->setUniqueFaker($uniqueFaker); + } + $model = $fakeBuilder->generateModel($attributes); + return $model; + } + + /** + * Generate, save and return model + * @param array|callable $attributes + * @param UniqueGenerator|null $uniqueFaker + * @return \yii\db\ActiveRecord + * @example MyFaker::saveOne(['user_id' => 1, 'title' => 'foo']); + * @example MyFaker::saveOne( function($model, $faker) { + * $model->scenario = 'create'; + * $model->setAttributes(['user_id' => 1, 'title' => $faker->sentence]); + * return $model; + * }); + */ + public static function saveOne($attributes = [], ?UniqueGenerator $uniqueFaker = null) + { + $model = static::makeOne($attributes, $uniqueFaker); + $model->save(); + return $model; + } + + /** + * Generate and return multiple models + * @param int $number + * @param array|callable $commonAttributes + * @return \yii\db\ActiveRecord[]|array + * @example TaskFaker::make(5, ['project_id'=>1, 'user_id' => 2]); + * @example TaskFaker::make(5, function($model, $faker, $uniqueFaker) { + * $model->setAttributes(['name' => $uniqueFaker->username, 'state'=>$faker->boolean(20)]); + * return $model; + * }); + */ + public static function make(int $number, $commonAttributes = [], ?UniqueGenerator $uniqueFaker = null):array + { + if ($number < 1) { + return []; + } + $fakeBuilder = new static(); + if ($uniqueFaker !== null) { + $fakeBuilder->setUniqueFaker($uniqueFaker); + } + return array_map(function () use ($commonAttributes, $fakeBuilder) { + $model = $fakeBuilder->generateModel($commonAttributes); + return $model; + }, range(0, $number -1)); + } + + /** + * Generate, save and return multiple models + * @param int $number + * @param array|callable $commonAttributes + * @return \yii\db\ActiveRecord[]|array + * @example TaskFaker::save(5, ['project_id'=>1, 'user_id' => 2]); + * @example TaskFaker::save(5, function($model, $faker, $uniqueFaker) { + * $model->setAttributes(['name' => $uniqueFaker->username, 'state'=>$faker->boolean(20)]); + * return $model; + * }); + */ + public static function save(int $number, $commonAttributes = [], ?UniqueGenerator $uniqueFaker = null):array + { + if ($number < 1) { + return []; + } + $fakeBuilder = new static(); + if ($uniqueFaker !== null) { + $fakeBuilder->setUniqueFaker($uniqueFaker); + } + return array_map(function () use ($commonAttributes, $fakeBuilder) { + $model = $fakeBuilder->generateModel($commonAttributes); + $model->save(); + return $model; + }, range(0, $number -1)); + } +} diff --git a/tests/specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/mysql/models/Fruit.php b/tests/specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/mysql/models/Fruit.php new file mode 100644 index 00000000..c74c53d9 --- /dev/null +++ b/tests/specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/mysql/models/Fruit.php @@ -0,0 +1,10 @@ +generateModels(['author_id' => 1]); + * $model = (new PostFaker())->generateModels(function($model, $faker, $uniqueFaker) { + * $model->scenario = 'create'; + * $model->author_id = 1; + * return $model; + * }); + **/ + public function generateModel($attributes = []) + { + $faker = $this->faker; + $uniqueFaker = $this->uniqueFaker; + $model = new Fruit(); + //$model->id = $uniqueFaker->numberBetween(0, 1000000); + $model->name = $faker->sentence; + if (!is_callable($attributes)) { + $model->setAttributes($attributes, false); + } else { + $model = $attributes($model, $faker, $uniqueFaker); + } + return $model; + } +} diff --git a/tests/specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/mysql/models/Invoice.php b/tests/specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/mysql/models/Invoice.php new file mode 100644 index 00000000..43e37fd3 --- /dev/null +++ b/tests/specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/mysql/models/Invoice.php @@ -0,0 +1,10 @@ +generateModels(['author_id' => 1]); + * $model = (new PostFaker())->generateModels(function($model, $faker, $uniqueFaker) { + * $model->scenario = 'create'; + * $model->author_id = 1; + * return $model; + * }); + **/ + public function generateModel($attributes = []) + { + $faker = $this->faker; + $uniqueFaker = $this->uniqueFaker; + $model = new Invoice(); + //$model->id = $uniqueFaker->numberBetween(0, 1000000); + $model->reference_invoice_2_id = $faker->randomElement(\app\models\Invoice::find()->select("id")->column()); + $model->user_id = $faker->randomElement(\app\models\User::find()->select("id")->column()); + $model->fruit_id = $faker->randomElement(\app\models\Fruit::find()->select("id")->column()); + if (!is_callable($attributes)) { + $model->setAttributes($attributes, false); + } else { + $model = $attributes($model, $faker, $uniqueFaker); + } + return $model; + } + + public static function dependentOn() + { + return [ + // just model class names + 'User', + 'Fruit', + + ]; + } +} diff --git a/tests/specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/mysql/models/User.php b/tests/specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/mysql/models/User.php new file mode 100644 index 00000000..9b837d6e --- /dev/null +++ b/tests/specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/mysql/models/User.php @@ -0,0 +1,10 @@ +generateModels(['author_id' => 1]); + * $model = (new PostFaker())->generateModels(function($model, $faker, $uniqueFaker) { + * $model->scenario = 'create'; + * $model->author_id = 1; + * return $model; + * }); + **/ + public function generateModel($attributes = []) + { + $faker = $this->faker; + $uniqueFaker = $this->uniqueFaker; + $model = new User(); + //$model->id = $uniqueFaker->numberBetween(0, 1000000); + $model->name = $faker->sentence; + if (!is_callable($attributes)) { + $model->setAttributes($attributes, false); + } else { + $model = $attributes($model, $faker, $uniqueFaker); + } + return $model; + } +} diff --git a/tests/specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/mysql/models/base/Animal.php b/tests/specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/mysql/models/base/Animal.php new file mode 100644 index 00000000..a99b34da --- /dev/null +++ b/tests/specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/mysql/models/base/Animal.php @@ -0,0 +1,30 @@ + [['name'], 'trim'], + 'name_string' => [['name'], 'string'], + ]; + } +} diff --git a/tests/specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/mysql/models/base/Fruit.php b/tests/specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/mysql/models/base/Fruit.php new file mode 100644 index 00000000..ccdbe894 --- /dev/null +++ b/tests/specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/mysql/models/base/Fruit.php @@ -0,0 +1,30 @@ + [['name'], 'trim'], + 'name_string' => [['name'], 'string'], + ]; + } +} diff --git a/tests/specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/mysql/models/base/Invoice.php b/tests/specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/mysql/models/base/Invoice.php new file mode 100644 index 00000000..1495d16b --- /dev/null +++ b/tests/specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/mysql/models/base/Invoice.php @@ -0,0 +1,81 @@ + [['reference_invoice_id'], 'integer'], + 'reference_invoice_id_exist' => [['reference_invoice_id'], 'exist', 'targetRelation' => 'ReferenceInvoice'], + 'reference_invoice_2_id_integer' => [['reference_invoice_2_id'], 'integer'], + 'reference_invoice_2_id_exist' => [['reference_invoice_2_id'], 'exist', 'targetRelation' => 'ReferenceInvoice2'], + 'user_id_integer' => [['user_id'], 'integer'], + 'user_id_exist' => [['user_id'], 'exist', 'targetRelation' => 'User'], + 'user_2_id_integer' => [['user_2_id'], 'integer'], + 'user_2_id_exist' => [['user_2_id'], 'exist', 'targetRelation' => 'User2'], + 'fruit_id_integer' => [['fruit_id'], 'integer'], + 'fruit_id_exist' => [['fruit_id'], 'exist', 'targetRelation' => 'Fruit'], + 'animal_id_integer' => [['animal_id'], 'integer'], + 'animal_id_exist' => [['animal_id'], 'exist', 'targetRelation' => 'Animal'], + ]; + } + + public function getReferenceInvoice() + { + return $this->hasOne(\app\models\Invoice::class, ['id' => 'reference_invoice_id']); + } + + public function getReferenceInvoice2() + { + return $this->hasOne(\app\models\Invoice::class, ['id' => 'reference_invoice_2_id']); + } + + public function getUser() + { + return $this->hasOne(\app\models\User::class, ['id' => 'user_id']); + } + + public function getUser2() + { + return $this->hasOne(\app\models\User::class, ['id' => 'user_2_id']); + } + + public function getFruit() + { + return $this->hasOne(\app\models\Fruit::class, ['id' => 'fruit_id']); + } + + public function getAnimal() + { + return $this->hasOne(\app\models\Animal::class, ['id' => 'animal_id']); + } +} diff --git a/tests/specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/mysql/models/base/User.php b/tests/specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/mysql/models/base/User.php new file mode 100644 index 00000000..bce4e105 --- /dev/null +++ b/tests/specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/mysql/models/base/User.php @@ -0,0 +1,30 @@ + [['name'], 'trim'], + 'name_string' => [['name'], 'string'], + ]; + } +} diff --git a/tests/specs/menu/models/MenuFaker.php b/tests/specs/menu/models/MenuFaker.php index 64e82b3b..8af0e400 100644 --- a/tests/specs/menu/models/MenuFaker.php +++ b/tests/specs/menu/models/MenuFaker.php @@ -46,7 +46,6 @@ public static function dependentOn() { return [ // just model class names - 'Menu', ]; } diff --git a/tests/unit/IssueFixTest.php b/tests/unit/IssueFixTest.php index b6c7abdb..fd102951 100644 --- a/tests/unit/IssueFixTest.php +++ b/tests/unit/IssueFixTest.php @@ -360,4 +360,18 @@ public function test158BugGiiapiGeneratedRulesEnumWithTrim() ]); $this->checkFiles($actualFiles, $expectedFiles); } + + // https://github.com/php-openapi/yii2-openapi/issues/52 + public function test52BugDependentonAllofWithXFakerFalse() + { + $testFile = Yii::getAlias("@specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/index.php"); + $this->runGenerator($testFile); + $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [ + 'recursive' => true, + ]); + $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/mysql"), [ + 'recursive' => true, + ]); + $this->checkFiles($actualFiles, $expectedFiles); + } }