From 9232b4525eea270f7b25a7a3004f830c943f470f Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Wed, 25 Dec 2024 18:49:50 +0530 Subject: [PATCH 1/8] Create PR --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index f68df469..4cc1d72a 100644 --- a/README.md +++ b/README.md @@ -779,3 +779,4 @@ Professional support, consulting as well as software development services are av https://www.cebe.cc/en/contact Development of this library is sponsored by [cebe.:cloud: "Your Professional Deployment Platform"](https://cebe.cloud). + From 626818b9d31dc901b3a14e6d3abcf5c46abb37d1 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Sat, 28 Dec 2024 18:58:06 +0530 Subject: [PATCH 2/8] Add failing test - WIP --- README.md | 1 - src/lib/AttributeResolver.php | 2 +- .../index.php | 14 +++++++++ .../index.yaml | 31 +++++++++++++++++++ tests/unit/IssueFixTest.php | 16 ++++++++++ 5 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 tests/specs/issue_fix/74_invalid_schema_reference_error/index.php create mode 100644 tests/specs/issue_fix/74_invalid_schema_reference_error/index.yaml diff --git a/README.md b/README.md index 4cc1d72a..f68df469 100644 --- a/README.md +++ b/README.md @@ -779,4 +779,3 @@ Professional support, consulting as well as software development services are av https://www.cebe.cc/en/contact Development of this library is sponsored by [cebe.:cloud: "Your Professional Deployment Platform"](https://cebe.cloud). - diff --git a/src/lib/AttributeResolver.php b/src/lib/AttributeResolver.php index a43aa8ba..87e90541 100644 --- a/src/lib/AttributeResolver.php +++ b/src/lib/AttributeResolver.php @@ -498,7 +498,7 @@ protected function resolvePropertyRef(PropertySchema $property, Attribute $attri $attribute->setPhpType($fkProperty->guessPhpType()) ->setDbType($fkProperty->guessDbType(true)) ->setSize($fkProperty->getMaxLength()) - ->setDescription($fkProperty->getAttr('description')) + ->setDescription($fkProperty->getAttr('description', '')) ->setDefault($fkProperty->guessDefault()) ->setLimits($min, $max, $fkProperty->getMinLength()); $this->attributes[$property->getName()] = diff --git a/tests/specs/issue_fix/74_invalid_schema_reference_error/index.php b/tests/specs/issue_fix/74_invalid_schema_reference_error/index.php new file mode 100644 index 00000000..3b4df8b4 --- /dev/null +++ b/tests/specs/issue_fix/74_invalid_schema_reference_error/index.php @@ -0,0 +1,14 @@ + '@specs/issue_fix/74_invalid_schema_reference_error/index.yaml', + 'generateUrls' => true, + 'generateModels' => true, + 'excludeModels' => [ + 'Error', + ], + 'generateControllers' => true, + 'generateMigrations' => true, + 'generateModelFaker' => true, // `generateModels` must be `true` in order to use `generateModelFaker` as `true` + 'ignoreSpecErrors' => true, +]; diff --git a/tests/specs/issue_fix/74_invalid_schema_reference_error/index.yaml b/tests/specs/issue_fix/74_invalid_schema_reference_error/index.yaml new file mode 100644 index 00000000..d975fc79 --- /dev/null +++ b/tests/specs/issue_fix/74_invalid_schema_reference_error/index.yaml @@ -0,0 +1,31 @@ +openapi: "3.0.0" +info: + version: 1.0.0 + title: Invalid schema reference error \#74 +paths: + /: + get: + responses: + '200': + description: The information + +components: + schemas: + Invoice: + type: object + properties: + id: + type: integer + vat_rate: + $ref: '#/components/schemas/Product/properties/vat_rate' + Product: + type: object + properties: + id: + type: integer + vat_rate: + type: string + enum: + - normal + - none + default: normal diff --git a/tests/unit/IssueFixTest.php b/tests/unit/IssueFixTest.php index f1b2bfe8..f68dfc74 100644 --- a/tests/unit/IssueFixTest.php +++ b/tests/unit/IssueFixTest.php @@ -962,4 +962,20 @@ public function test63JustColumnNameRename() $this->checkFiles($actualFiles, $expectedFiles); Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%fruits}}')->execute(); } + + // https://github.com/php-openapi/yii2-openapi/issues/63 + public function test74InvalidSchemaReferenceError() + { + $testFile = Yii::getAlias("@specs/issue_fix/74_invalid_schema_reference_error/index.php"); + + $this->runGenerator($testFile); +// $this->runActualMigrations('mysql', 1); +// $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [ +// 'recursive' => true, +// ]); +// $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/74_invalid_schema_reference_error/mysql"), [ +// 'recursive' => true, +// ]); +// $this->checkFiles($actualFiles, $expectedFiles); + } } From 0c41b52cd9da0634a84405e46e23da8660e9c73f Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Tue, 31 Dec 2024 15:39:05 +0530 Subject: [PATCH 3/8] Fix another bug --- src/lib/migrations/BaseMigrationBuilder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/migrations/BaseMigrationBuilder.php b/src/lib/migrations/BaseMigrationBuilder.php index 416311d9..ded9a7af 100644 --- a/src/lib/migrations/BaseMigrationBuilder.php +++ b/src/lib/migrations/BaseMigrationBuilder.php @@ -491,7 +491,7 @@ public function tmpSaveNewCol(string $tableAlias, \cebe\yii2openapi\db\ColumnSch Yii::$app->db->createCommand()->createTable($tmpTableName, $column)->execute(); if (ApiGenerator::isPostgres() && $columnSchema->comment) { - Yii::$app->db->createCommand("COMMENT ON COLUMN $tmpTableName.$columnSchema->name IS {$this->db->quoteValue($columnSchema->comment)}")->execute(); + Yii::$app->db->createCommand("COMMENT ON COLUMN $tmpTableName.\"$columnSchema->name\" IS {$this->db->quoteValue($columnSchema->comment)}")->execute(); } $table = Yii::$app->db->getTableSchema($tmpTableName); From 8ab381024b02afd0dd57e83e732b875ff1c8eaee Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Tue, 31 Dec 2024 19:12:48 +0530 Subject: [PATCH 4/8] Add failing test --- .../Product.yaml | 26 +++++++++++++++++++ .../index.yaml | 3 ++- 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 tests/specs/issue_fix/74_invalid_schema_reference_error/Product.yaml diff --git a/tests/specs/issue_fix/74_invalid_schema_reference_error/Product.yaml b/tests/specs/issue_fix/74_invalid_schema_reference_error/Product.yaml new file mode 100644 index 00000000..bb23597a --- /dev/null +++ b/tests/specs/issue_fix/74_invalid_schema_reference_error/Product.yaml @@ -0,0 +1,26 @@ +# Product: +title: Product +x-table: products +type: object + +required: + - id + - vat_rate + + +properties: + + id: + type: integer + example: 12531 + readOnly: true + + + vat_rate: + type: string + enum: + - standard + - reduced + - custom + - none + default: standard diff --git a/tests/specs/issue_fix/74_invalid_schema_reference_error/index.yaml b/tests/specs/issue_fix/74_invalid_schema_reference_error/index.yaml index d975fc79..d2c8d1bb 100644 --- a/tests/specs/issue_fix/74_invalid_schema_reference_error/index.yaml +++ b/tests/specs/issue_fix/74_invalid_schema_reference_error/index.yaml @@ -17,7 +17,8 @@ components: id: type: integer vat_rate: - $ref: '#/components/schemas/Product/properties/vat_rate' + # $ref: '#/components/schemas/Product/properties/vat_rate' # issue is not observed + $ref: './Product.yaml#/properties/vat_rate' # issue is observed Product: type: object properties: From bccf2a8307f0db4148284bc84871bf4d8bf952e4 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Tue, 31 Dec 2024 21:20:48 +0530 Subject: [PATCH 5/8] In progress --- src/lib/openapi/PropertySchema.php | 12 +++++++++++- .../74_invalid_schema_reference_error/index.yaml | 2 +- tests/unit/IssueFixTest.php | 8 ++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/lib/openapi/PropertySchema.php b/src/lib/openapi/PropertySchema.php index 9ff64094..a8b85424 100644 --- a/src/lib/openapi/PropertySchema.php +++ b/src/lib/openapi/PropertySchema.php @@ -70,6 +70,7 @@ class PropertySchema /** @var string $refPointer */ private $refPointer; + private $uri; /** @var \cebe\yii2openapi\lib\openapi\ComponentSchema $refSchema */ private $refSchema; @@ -170,6 +171,7 @@ private function initReference():void { $this->isReference = true; $this->refPointer = $this->property->getJsonReference()->getJsonPointer()->getPointer(); + $this->uri = $this->property->getJsonReference()->getDocumentUri(); $refSchemaName = $this->getRefSchemaName(); if ($this->isRefPointerToSelf()) { $this->refSchema = $this->schema; @@ -194,6 +196,7 @@ private function initItemsReference():void return; } $this->refPointer = $items->getJsonReference()->getJsonPointer()->getPointer(); + $this->uri = $this->property->getJsonReference()->getDocumentUri(); if ($this->isRefPointerToSelf()) { $this->refSchema = $this->schema; } elseif ($this->isRefPointerToSchema()) { @@ -301,8 +304,15 @@ public function getRefSchemaName():string '~^'.self::REFERENCE_PATH.'(?.+)/properties/(?.+)$~' : '~^'.self::REFERENCE_PATH.'(?.+)$~'; if (!\preg_match($pattern, $this->refPointer, $matches)) { - throw new InvalidDefinitionException('Invalid schema reference'); + $pattern = '/((\.\/)*)(?.+)(\.)(yml|yaml)(.*)/'; +// $pattern = '~^(?.+)(\.+)(yaml+)(.*)$~'; + if (strpos($this->uri, '#') !== false && !\preg_match($pattern, $this->uri, $matches)) { +// throw new InvalidDefinitionException('Invalid schema reference'); + } else { + throw new InvalidDefinitionException('Invalid schema reference'); + } } + var_dump($matches); return $matches['schemaName']; } diff --git a/tests/specs/issue_fix/74_invalid_schema_reference_error/index.yaml b/tests/specs/issue_fix/74_invalid_schema_reference_error/index.yaml index d2c8d1bb..014fe5c8 100644 --- a/tests/specs/issue_fix/74_invalid_schema_reference_error/index.yaml +++ b/tests/specs/issue_fix/74_invalid_schema_reference_error/index.yaml @@ -17,7 +17,7 @@ components: id: type: integer vat_rate: - # $ref: '#/components/schemas/Product/properties/vat_rate' # issue is not observed + # $ref: '#/components/schemas/Product/properties/vat_rate' # issue is not observed $ref: './Product.yaml#/properties/vat_rate' # issue is observed Product: type: object diff --git a/tests/unit/IssueFixTest.php b/tests/unit/IssueFixTest.php index f68dfc74..83878436 100644 --- a/tests/unit/IssueFixTest.php +++ b/tests/unit/IssueFixTest.php @@ -966,6 +966,14 @@ public function test63JustColumnNameRename() // https://github.com/php-openapi/yii2-openapi/issues/63 public function test74InvalidSchemaReferenceError() { +// $pattern = '~^(?.+)(\.+)(yaml+)(.*)$~'; +// $pattern = '/((\.\/)*)(?.+)(\.)(yml|yaml)(.*)/'; +// +// $pointer = './Product.yaml#/properties/vat_rate'; +// preg_match($pattern, $pointer, $matches); +// $this->assertSame('supda saf', $matches); +// return; + $testFile = Yii::getAlias("@specs/issue_fix/74_invalid_schema_reference_error/index.php"); $this->runGenerator($testFile); From b130a4febf96b0ac9ac4302f2dda85c1d80f700d Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Fri, 3 Jan 2025 18:08:24 +0530 Subject: [PATCH 6/8] Fix this issue --- src/lib/openapi/PropertySchema.php | 16 ++++++++-------- .../Product.yaml | 8 -------- .../74_invalid_schema_reference_error/index.yaml | 11 +---------- 3 files changed, 9 insertions(+), 26 deletions(-) diff --git a/src/lib/openapi/PropertySchema.php b/src/lib/openapi/PropertySchema.php index a8b85424..eade252e 100644 --- a/src/lib/openapi/PropertySchema.php +++ b/src/lib/openapi/PropertySchema.php @@ -196,7 +196,7 @@ private function initItemsReference():void return; } $this->refPointer = $items->getJsonReference()->getJsonPointer()->getPointer(); - $this->uri = $this->property->getJsonReference()->getDocumentUri(); + $this->uri = $items->getJsonReference()->getDocumentUri(); if ($this->isRefPointerToSelf()) { $this->refSchema = $this->schema; } elseif ($this->isRefPointerToSchema()) { @@ -267,7 +267,9 @@ public function getSelfTargetProperty():?PropertySchema public function isRefPointerToSchema():bool { - return $this->refPointer && strpos($this->refPointer, self::REFERENCE_PATH) === 0; + return $this->refPointer && + ((strpos($this->refPointer, self::REFERENCE_PATH) === 0) || + (str_ends_with($this->uri, '.yml')) || (str_ends_with($this->uri, '.yaml'))); } public function isRefPointerToSelf():bool @@ -303,16 +305,14 @@ public function getRefSchemaName():string $pattern = strpos($this->refPointer, '/properties/') !== false ? '~^'.self::REFERENCE_PATH.'(?.+)/properties/(?.+)$~' : '~^'.self::REFERENCE_PATH.'(?.+)$~'; + $separateFilePattern = '/((\.\/)*)(?.+)(\.)(yml|yaml)(.*)/'; # https://github.com/php-openapi/yii2-openapi/issues/74 if (!\preg_match($pattern, $this->refPointer, $matches)) { - $pattern = '/((\.\/)*)(?.+)(\.)(yml|yaml)(.*)/'; -// $pattern = '~^(?.+)(\.+)(yaml+)(.*)$~'; - if (strpos($this->uri, '#') !== false && !\preg_match($pattern, $this->uri, $matches)) { -// throw new InvalidDefinitionException('Invalid schema reference'); - } else { + if (!\preg_match($separateFilePattern, $this->uri, $separateFilePatternMatches)) { throw new InvalidDefinitionException('Invalid schema reference'); + } else { + return $separateFilePatternMatches['schemaName']; } } - var_dump($matches); return $matches['schemaName']; } diff --git a/tests/specs/issue_fix/74_invalid_schema_reference_error/Product.yaml b/tests/specs/issue_fix/74_invalid_schema_reference_error/Product.yaml index bb23597a..ca81d464 100644 --- a/tests/specs/issue_fix/74_invalid_schema_reference_error/Product.yaml +++ b/tests/specs/issue_fix/74_invalid_schema_reference_error/Product.yaml @@ -7,20 +7,12 @@ required: - id - vat_rate - properties: - id: type: integer - example: 12531 - readOnly: true - - vat_rate: type: string enum: - standard - - reduced - - custom - none default: standard diff --git a/tests/specs/issue_fix/74_invalid_schema_reference_error/index.yaml b/tests/specs/issue_fix/74_invalid_schema_reference_error/index.yaml index 014fe5c8..cc18b7d2 100644 --- a/tests/specs/issue_fix/74_invalid_schema_reference_error/index.yaml +++ b/tests/specs/issue_fix/74_invalid_schema_reference_error/index.yaml @@ -20,13 +20,4 @@ components: # $ref: '#/components/schemas/Product/properties/vat_rate' # issue is not observed $ref: './Product.yaml#/properties/vat_rate' # issue is observed Product: - type: object - properties: - id: - type: integer - vat_rate: - type: string - enum: - - normal - - none - default: normal + $ref: ./Product.yaml From ddf4a6b40b217096898fd585bc3420a4a965b98a Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Tue, 7 Jan 2025 18:02:22 +0530 Subject: [PATCH 7/8] Fix issue --- src/lib/AttributeResolver.php | 4 ++++ .../issue_fix/74_invalid_schema_reference_error/index.php | 5 ++--- .../issue_fix/74_invalid_schema_reference_error/index.yaml | 2 ++ 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/lib/AttributeResolver.php b/src/lib/AttributeResolver.php index 87e90541..cc41a6fb 100644 --- a/src/lib/AttributeResolver.php +++ b/src/lib/AttributeResolver.php @@ -501,6 +501,10 @@ protected function resolvePropertyRef(PropertySchema $property, Attribute $attri ->setDescription($fkProperty->getAttr('description', '')) ->setDefault($fkProperty->guessDefault()) ->setLimits($min, $max, $fkProperty->getMinLength()); + + if ($fkProperty->hasEnum()) { + $attribute->setEnumValues($fkProperty->getAttr('enum')); + } $this->attributes[$property->getName()] = $attribute->setFakerStub($this->guessFakerStub($attribute, $fkProperty)); } diff --git a/tests/specs/issue_fix/74_invalid_schema_reference_error/index.php b/tests/specs/issue_fix/74_invalid_schema_reference_error/index.php index 3b4df8b4..cbe2f6dc 100644 --- a/tests/specs/issue_fix/74_invalid_schema_reference_error/index.php +++ b/tests/specs/issue_fix/74_invalid_schema_reference_error/index.php @@ -2,13 +2,12 @@ return [ 'openApiPath' => '@specs/issue_fix/74_invalid_schema_reference_error/index.yaml', - 'generateUrls' => true, + 'generateUrls' => false, 'generateModels' => true, 'excludeModels' => [ 'Error', ], - 'generateControllers' => true, + 'generateControllers' => false, 'generateMigrations' => true, 'generateModelFaker' => true, // `generateModels` must be `true` in order to use `generateModelFaker` as `true` - 'ignoreSpecErrors' => true, ]; diff --git a/tests/specs/issue_fix/74_invalid_schema_reference_error/index.yaml b/tests/specs/issue_fix/74_invalid_schema_reference_error/index.yaml index cc18b7d2..271c63d1 100644 --- a/tests/specs/issue_fix/74_invalid_schema_reference_error/index.yaml +++ b/tests/specs/issue_fix/74_invalid_schema_reference_error/index.yaml @@ -13,6 +13,8 @@ components: schemas: Invoice: type: object + required: + - vat_rate properties: id: type: integer From 064673a29c64f097d889f1133e809d508f8d0b74 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Tue, 7 Jan 2025 18:05:47 +0530 Subject: [PATCH 8/8] Complete test --- .../m200000_000000_create_table_invoices.php | 20 +++ .../m200000_000001_create_table_products.php | 20 +++ .../mysql/models/BaseModelFaker.php | 144 ++++++++++++++++++ .../mysql/models/Invoice.php | 10 ++ .../mysql/models/InvoiceFaker.php | 41 +++++ .../mysql/models/Product.php | 10 ++ .../mysql/models/ProductFaker.php | 41 +++++ .../mysql/models/base/Invoice.php | 35 +++++ .../mysql/models/base/Product.php | 35 +++++ tests/unit/IssueFixTest.php | 25 +-- 10 files changed, 364 insertions(+), 17 deletions(-) create mode 100644 tests/specs/issue_fix/74_invalid_schema_reference_error/mysql/migrations_mysql_db/m200000_000000_create_table_invoices.php create mode 100644 tests/specs/issue_fix/74_invalid_schema_reference_error/mysql/migrations_mysql_db/m200000_000001_create_table_products.php create mode 100644 tests/specs/issue_fix/74_invalid_schema_reference_error/mysql/models/BaseModelFaker.php create mode 100644 tests/specs/issue_fix/74_invalid_schema_reference_error/mysql/models/Invoice.php create mode 100644 tests/specs/issue_fix/74_invalid_schema_reference_error/mysql/models/InvoiceFaker.php create mode 100644 tests/specs/issue_fix/74_invalid_schema_reference_error/mysql/models/Product.php create mode 100644 tests/specs/issue_fix/74_invalid_schema_reference_error/mysql/models/ProductFaker.php create mode 100644 tests/specs/issue_fix/74_invalid_schema_reference_error/mysql/models/base/Invoice.php create mode 100644 tests/specs/issue_fix/74_invalid_schema_reference_error/mysql/models/base/Product.php diff --git a/tests/specs/issue_fix/74_invalid_schema_reference_error/mysql/migrations_mysql_db/m200000_000000_create_table_invoices.php b/tests/specs/issue_fix/74_invalid_schema_reference_error/mysql/migrations_mysql_db/m200000_000000_create_table_invoices.php new file mode 100644 index 00000000..1e21544d --- /dev/null +++ b/tests/specs/issue_fix/74_invalid_schema_reference_error/mysql/migrations_mysql_db/m200000_000000_create_table_invoices.php @@ -0,0 +1,20 @@ +createTable('{{%invoices}}', [ + 'id' => $this->primaryKey(), + 'vat_rate' => 'enum("standard", "none") NOT NULL DEFAULT \'standard\'', + ]); + } + + public function down() + { + $this->dropTable('{{%invoices}}'); + } +} diff --git a/tests/specs/issue_fix/74_invalid_schema_reference_error/mysql/migrations_mysql_db/m200000_000001_create_table_products.php b/tests/specs/issue_fix/74_invalid_schema_reference_error/mysql/migrations_mysql_db/m200000_000001_create_table_products.php new file mode 100644 index 00000000..fcde7ba8 --- /dev/null +++ b/tests/specs/issue_fix/74_invalid_schema_reference_error/mysql/migrations_mysql_db/m200000_000001_create_table_products.php @@ -0,0 +1,20 @@ +createTable('{{%products}}', [ + 'id' => $this->primaryKey(), + 'vat_rate' => 'enum("standard", "none") NOT NULL DEFAULT \'standard\'', + ]); + } + + public function down() + { + $this->dropTable('{{%products}}'); + } +} diff --git a/tests/specs/issue_fix/74_invalid_schema_reference_error/mysql/models/BaseModelFaker.php b/tests/specs/issue_fix/74_invalid_schema_reference_error/mysql/models/BaseModelFaker.php new file mode 100644 index 00000000..c367fbb4 --- /dev/null +++ b/tests/specs/issue_fix/74_invalid_schema_reference_error/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/74_invalid_schema_reference_error/mysql/models/Invoice.php b/tests/specs/issue_fix/74_invalid_schema_reference_error/mysql/models/Invoice.php new file mode 100644 index 00000000..43e37fd3 --- /dev/null +++ b/tests/specs/issue_fix/74_invalid_schema_reference_error/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->vat_rate = $faker->randomElement(['standard','none']); + if (!is_callable($attributes)) { + $model->setAttributes($attributes, false); + } else { + $model = $attributes($model, $faker, $uniqueFaker); + } + return $model; + } +} diff --git a/tests/specs/issue_fix/74_invalid_schema_reference_error/mysql/models/Product.php b/tests/specs/issue_fix/74_invalid_schema_reference_error/mysql/models/Product.php new file mode 100644 index 00000000..a8411efa --- /dev/null +++ b/tests/specs/issue_fix/74_invalid_schema_reference_error/mysql/models/Product.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 Product(); + //$model->id = $uniqueFaker->numberBetween(0, 1000000); + $model->vat_rate = $faker->randomElement(['standard','none']); + if (!is_callable($attributes)) { + $model->setAttributes($attributes, false); + } else { + $model = $attributes($model, $faker, $uniqueFaker); + } + return $model; + } +} diff --git a/tests/specs/issue_fix/74_invalid_schema_reference_error/mysql/models/base/Invoice.php b/tests/specs/issue_fix/74_invalid_schema_reference_error/mysql/models/base/Invoice.php new file mode 100644 index 00000000..d81c8ce3 --- /dev/null +++ b/tests/specs/issue_fix/74_invalid_schema_reference_error/mysql/models/base/Invoice.php @@ -0,0 +1,35 @@ + [['vat_rate'], 'required'], + 'vat_rate_string' => [['vat_rate'], 'string'], + 'vat_rate_in' => [['vat_rate'], 'in', 'range' => [ + 'standard', + 'none', + ]], + 'vat_rate_default' => [['vat_rate'], 'default', 'value' => 'standard'], + ]; + } +} diff --git a/tests/specs/issue_fix/74_invalid_schema_reference_error/mysql/models/base/Product.php b/tests/specs/issue_fix/74_invalid_schema_reference_error/mysql/models/base/Product.php new file mode 100644 index 00000000..184a6395 --- /dev/null +++ b/tests/specs/issue_fix/74_invalid_schema_reference_error/mysql/models/base/Product.php @@ -0,0 +1,35 @@ + [['vat_rate'], 'required'], + 'vat_rate_string' => [['vat_rate'], 'string'], + 'vat_rate_in' => [['vat_rate'], 'in', 'range' => [ + 'standard', + 'none', + ]], + 'vat_rate_default' => [['vat_rate'], 'default', 'value' => 'standard'], + ]; + } +} diff --git a/tests/unit/IssueFixTest.php b/tests/unit/IssueFixTest.php index 83878436..11134111 100644 --- a/tests/unit/IssueFixTest.php +++ b/tests/unit/IssueFixTest.php @@ -966,24 +966,15 @@ public function test63JustColumnNameRename() // https://github.com/php-openapi/yii2-openapi/issues/63 public function test74InvalidSchemaReferenceError() { -// $pattern = '~^(?.+)(\.+)(yaml+)(.*)$~'; -// $pattern = '/((\.\/)*)(?.+)(\.)(yml|yaml)(.*)/'; -// -// $pointer = './Product.yaml#/properties/vat_rate'; -// preg_match($pattern, $pointer, $matches); -// $this->assertSame('supda saf', $matches); -// return; - $testFile = Yii::getAlias("@specs/issue_fix/74_invalid_schema_reference_error/index.php"); - $this->runGenerator($testFile); -// $this->runActualMigrations('mysql', 1); -// $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [ -// 'recursive' => true, -// ]); -// $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/74_invalid_schema_reference_error/mysql"), [ -// 'recursive' => true, -// ]); -// $this->checkFiles($actualFiles, $expectedFiles); + $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [ + 'recursive' => true, + ]); + $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/74_invalid_schema_reference_error/mysql"), [ + 'recursive' => true, + ]); + $this->checkFiles($actualFiles, $expectedFiles); + $this->runActualMigrations(); } }