diff --git a/src/generator/default/dbmodel.php b/src/generator/default/dbmodel.php index f8c3ba3c..15c9f722 100644 --- a/src/generator/default/dbmodel.php +++ b/src/generator/default/dbmodel.php @@ -146,12 +146,14 @@ public function getgetCamelName() ?>() } -inverseRelations as $relationName => $relation): ?> +belongsToRelations as $relationName => $relation): ?>getCamelName(), $usedRelationNames) ? $i : '' ?> - public function getgetCamelName().($i===1 ? '' : $i) ?>() + # belongs to relation + public function getgetCamelName() . ($number) ?>() { return $this->getMethod() ?>(\\getClassName() ?>::class, linkToString() ?>)->inverseOf('getInverse() ?>'); + echo $relation->linkToString() ?>); } - +getCamelName(); endforeach; ?> } diff --git a/src/lib/AttributeResolver.php b/src/lib/AttributeResolver.php index cc41a6fb..0c0c41c9 100644 --- a/src/lib/AttributeResolver.php +++ b/src/lib/AttributeResolver.php @@ -37,6 +37,11 @@ class AttributeResolver */ public array $relations = []; + /** + * @var array keys contains class names and value contains array of belongs to relations + */ + public array $belongsToRelations = []; + /** * @var NonDbRelation[]|array */ @@ -60,11 +65,6 @@ class AttributeResolver private ?Config $config; - /** - * @var AttributeRelation[]|array - */ - public array $inverseRelations = []; - public function __construct(string $schemaName, ComponentSchema $schema, JunctionSchemas $junctions, ?Config $config = null) { $this->schemaName = $schemaName; @@ -232,6 +232,7 @@ protected function resolveProperty( ->setForeignKeyColumnName($property->fkColName) ->setFakerStub($this->guessFakerStub($attribute, $property)) ->setTableName($this->componentSchema->resolveTableName($this->schemaName)); + if ($property->isReference()) { if ($property->isVirtual()) { throw new InvalidDefinitionException('References not supported for virtual attributes'); @@ -277,12 +278,17 @@ protected function resolveProperty( $relation->onDeleteFkConstraint = $property->onDeleteFkConstraint; if ($property->isRefPointerToSelf()) { $relation->asSelfReference(); + } else { # belongs to relations https://github.com/php-openapi/yii2-openapi/issues/90 + $belongsToRelation = Yii::createObject( + AttributeRelation::class, + [$this->schemaName, $this->tableName, $this->schemaName] + ) + ->asHasOne([$attribute->columnName => $fkProperty->getName()]); + $this->belongsToRelations[$property->getRefClassName()][] = $belongsToRelation; } $this->relations[$property->getName()] = $relation; - if (!$property->isRefPointerToSelf()) { - $this->addInverseRelation($relatedClassName, $attribute, $property, $fkProperty); - } } + if (!$property->isReference() && !$property->hasRefItems()) { [$min, $max] = $property->guessMinMax(); $attribute->setIsVirtual($property->isVirtual()) @@ -338,6 +344,7 @@ protected function resolveProperty( ->asHasMany([$foreignPk => $this->componentSchema->getPkName()]); return; } + $relatedClassName = $property->getRefClassName(); $relatedTableName = $property->getRefSchema()->resolveTableName($relatedClassName); if ($this->catchManyToMany( @@ -518,22 +525,4 @@ public static function relationName(string $propertyName, ?string $fkColumnName) } return $relationName; } - - /** - * @throws InvalidConfigException - */ - public function addInverseRelation( - string $relatedClassName, - Attribute $attribute, - PropertySchema $property, - PropertySchema $fkProperty - ): void { - $inverseRelation = Yii::createObject( - AttributeRelation::class, - [$this->schemaName, $this->tableName, $this->schemaName] - ) - ->asHasOne([$attribute->columnName => $fkProperty->getName()]); - $inverseRelation->setInverse($property->getName()); - $this->inverseRelations[$relatedClassName][] = $inverseRelation; - } } diff --git a/src/lib/SchemaToDatabase.php b/src/lib/SchemaToDatabase.php index ccb230a3..14a8ac24 100644 --- a/src/lib/SchemaToDatabase.php +++ b/src/lib/SchemaToDatabase.php @@ -94,7 +94,7 @@ public function prepareModels(): array $openApi = $this->config->getOpenApi(); $junctions = $this->findJunctionSchemas(); - foreach ($openApi->components->schemas as $schemaName => $openApiSchema) { + foreach ($openApi->components->schemas ?? [] as $schemaName => $openApiSchema) { $schema = Yii::createObject(ComponentSchema::class, [$openApiSchema, $schemaName]); if (!$this->canGenerateModel($schemaName, $schema)) { @@ -106,18 +106,15 @@ public function prepareModels(): array /** @var AttributeResolver $resolver */ $resolver = Yii::createObject(AttributeResolver::class, [$schemaName, $schema, $junctions, $this->config]); - // $models[$schemaName] = $resolver->resolve(); $resolvers[$schemaName] = $resolver; $models[$schemaName] = $resolvers[$schemaName]->resolve(); } - // handle inverse relation + // handle belongs to relation foreach ($resolvers as $aResolver) { - foreach ($aResolver->inverseRelations as $name => $relations) { - foreach ($relations as $relation) { - /** @var AttributeRelation $relation */ - $models[$name]->inverseRelations[] = $relation; - } + foreach ($aResolver->belongsToRelations as $name => $relations) { + /** @var AttributeRelation[] $relations */ + $models[$name]->belongsToRelations = [...$models[$name]->belongsToRelations, ...$relations]; } } @@ -153,7 +150,7 @@ public function findJunctionSchemas(): JunctionSchemas { $junctions = []; $openApi = $this->config->getOpenApi(); - foreach ($openApi->components->schemas as $schemaName => $openApiSchema) { + foreach ($openApi->components->schemas ?? [] as $schemaName => $openApiSchema) { /**@var ComponentSchema $schema */ $schema = Yii::createObject(ComponentSchema::class, [$openApiSchema, $schemaName]); if ($schema->isNonDb()) { diff --git a/src/lib/generators/ControllersGenerator.php b/src/lib/generators/ControllersGenerator.php index bd749c9f..ea0c73c4 100644 --- a/src/lib/generators/ControllersGenerator.php +++ b/src/lib/generators/ControllersGenerator.php @@ -59,7 +59,7 @@ public function generate():CodeFiles $controllerPath = $path; /** * @var RestAction|FractalAction $action - **/ + **/ $action = $actions[0]; if ($action->prefix && !empty($action->prefixSettings)) { $controllerNamespace = trim($action->prefixSettings['namespace'], '\\'); @@ -126,15 +126,31 @@ protected function makeCustomController( ]; $reflection->addMethod('checkAccess', $params, AbstractMemberGenerator::FLAG_PUBLIC, '//TODO implement checkAccess'); foreach ($abstractActions as $action) { + $responseHttpStatusCodes = ''; + foreach ($this->config->getOpenApi()->paths->getPaths()[$action->urlPath]->getOperations() as $verb => $operation) { + $codes = array_keys($operation->responses->getResponses()); + + $only200OrDefault = false; + if ($codes === [200] || $codes === ['default']) { + $only200OrDefault = true; + } + if (in_array('default', $codes) && in_array(200, $codes) && count($codes) === 2) { + $only200OrDefault = true; + } + + if ($verb === strtolower($action->requestMethod) && !$only200OrDefault) { + $responseHttpStatusCodes = implode(', ', $codes); + } + } + $params = array_map(static function ($param) { return ['name' => $param]; }, $action->getParamNames()); - $reflection->addMethod( $action->actionMethodName, $params, AbstractMemberGenerator::FLAG_PUBLIC, - '//TODO implement ' . $action->actionMethodName + '//TODO implement ' . $action->actionMethodName . ($responseHttpStatusCodes ? PHP_EOL . '// In order to conform with OpenAPI spec, response of this action must have one of the following HTTP status code: ' . $responseHttpStatusCodes : '') ); } $classFileGenerator->setClasses([$reflection]); diff --git a/src/lib/items/DbModel.php b/src/lib/items/DbModel.php index fcfa6d40..226acb15 100644 --- a/src/lib/items/DbModel.php +++ b/src/lib/items/DbModel.php @@ -67,11 +67,6 @@ class DbModel extends BaseObject */ public array $many2many = []; - /** - * @var array|AttributeRelation[] inverse relations - */ - public array $inverseRelations = []; - public array $junctionCols = []; /** @@ -91,9 +86,9 @@ class DbModel extends BaseObject public bool $descriptionIsComment = false; /** - * @var array Automatically generated scenarios from the model 'x-scenarios'. + * @var AttributeRelation[] belongs to relations */ - private $_scenarios; + public array $belongsToRelations = []; /** * @var bool @@ -103,6 +98,11 @@ class DbModel extends BaseObject */ public $drop = false; + /** + * @var array Automatically generated scenarios from the model 'x-scenarios'. + */ + private $_scenarios; + public function getTableAlias(): string { return '{{%' . $this->tableName . '}}'; diff --git a/tests/specs/blog/models/base/Category.php b/tests/specs/blog/models/base/Category.php index 188ec03c..287bdc9f 100644 --- a/tests/specs/blog/models/base/Category.php +++ b/tests/specs/blog/models/base/Category.php @@ -39,8 +39,9 @@ public function getPosts() return $this->hasMany(\app\models\Post::class, ['category_id' => 'id'])->inverseOf('category'); } + # belongs to relation public function getPost() { - return $this->hasOne(\app\models\Post::class, ['category_id' => 'id'])->inverseOf('category'); + return $this->hasOne(\app\models\Post::class, ['category_id' => 'id']); } } diff --git a/tests/specs/blog/models/base/Post.php b/tests/specs/blog/models/base/Post.php index 633de3f4..2a97d0fa 100644 --- a/tests/specs/blog/models/base/Post.php +++ b/tests/specs/blog/models/base/Post.php @@ -62,8 +62,9 @@ public function getComments() return $this->hasMany(\app\models\Comment::class, ['post_id' => 'uid'])->inverseOf('post'); } + # belongs to relation public function getComment() { - return $this->hasOne(\app\models\Comment::class, ['post_id' => 'uid'])->inverseOf('post'); + return $this->hasOne(\app\models\Comment::class, ['post_id' => 'uid']); } } diff --git a/tests/specs/blog/models/base/User.php b/tests/specs/blog/models/base/User.php index dbd3a708..557328f9 100644 --- a/tests/specs/blog/models/base/User.php +++ b/tests/specs/blog/models/base/User.php @@ -45,13 +45,15 @@ public function rules() ]; } + # belongs to relation public function getPost() { - return $this->hasOne(\app\models\Post::class, ['created_by_id' => 'id'])->inverseOf('created_by'); + return $this->hasOne(\app\models\Post::class, ['created_by_id' => 'id']); } - public function getComment2() + # belongs to relation + public function getComment() { - return $this->hasOne(\app\models\Comment::class, ['author_id' => 'id'])->inverseOf('author'); + return $this->hasOne(\app\models\Comment::class, ['author_id' => 'id']); } } diff --git a/tests/specs/blog_v2/controllers/CommentController.php b/tests/specs/blog_v2/controllers/CommentController.php index 0448ec72..c46307ed 100644 --- a/tests/specs/blog_v2/controllers/CommentController.php +++ b/tests/specs/blog_v2/controllers/CommentController.php @@ -18,6 +18,7 @@ public function actionListForPost($postId) public function actionCreateForPost($postId) { //TODO implement actionCreateForPost + // In order to conform with OpenAPI spec, response of this action must have one of the following HTTP status code: 201, default } public function actionViewForPost($slug, $id) @@ -28,6 +29,7 @@ public function actionViewForPost($slug, $id) public function actionDeleteForPost($slug, $id) { //TODO implement actionDeleteForPost + // In order to conform with OpenAPI spec, response of this action must have one of the following HTTP status code: 204 } public function actionUpdateForPost($slug, $id) diff --git a/tests/specs/blog_v2/models/base/Category.php b/tests/specs/blog_v2/models/base/Category.php index 4207f516..1b97af75 100644 --- a/tests/specs/blog_v2/models/base/Category.php +++ b/tests/specs/blog_v2/models/base/Category.php @@ -39,8 +39,9 @@ public function getPosts() return $this->hasMany(\app\models\Post::class, ['category_id' => 'id'])->inverseOf('category'); } + # belongs to relation public function getPost() { - return $this->hasOne(\app\models\Post::class, ['category_id' => 'id'])->inverseOf('category'); + return $this->hasOne(\app\models\Post::class, ['category_id' => 'id']); } } diff --git a/tests/specs/blog_v2/models/base/Post.php b/tests/specs/blog_v2/models/base/Post.php index debc5008..0b82cefc 100644 --- a/tests/specs/blog_v2/models/base/Post.php +++ b/tests/specs/blog_v2/models/base/Post.php @@ -74,8 +74,9 @@ public function getTags() ->viaTable('posts2tags', ['post_id' => 'id']); } + # belongs to relation public function getComment() { - return $this->hasOne(\app\models\Comment::class, ['post_id' => 'id'])->inverseOf('post'); + return $this->hasOne(\app\models\Comment::class, ['post_id' => 'id']); } } diff --git a/tests/specs/blog_v2/models/base/User.php b/tests/specs/blog_v2/models/base/User.php index 45c5b4e0..820b8259 100644 --- a/tests/specs/blog_v2/models/base/User.php +++ b/tests/specs/blog_v2/models/base/User.php @@ -48,13 +48,15 @@ public function rules() ]; } + # belongs to relation public function getPost() { - return $this->hasOne(\app\models\Post::class, ['created_by_id' => 'id'])->inverseOf('created_by'); + return $this->hasOne(\app\models\Post::class, ['created_by_id' => 'id']); } - public function getComment2() + # belongs to relation + public function getComment() { - return $this->hasOne(\app\models\Comment::class, ['user_id' => 'id'])->inverseOf('user'); + return $this->hasOne(\app\models\Comment::class, ['user_id' => 'id']); } } diff --git a/tests/specs/fk_col_name/app/models/base/Delivery.php b/tests/specs/fk_col_name/app/models/base/Delivery.php index 75a64400..3bb543a1 100644 --- a/tests/specs/fk_col_name/app/models/base/Delivery.php +++ b/tests/specs/fk_col_name/app/models/base/Delivery.php @@ -28,8 +28,9 @@ public function rules() ]; } + # belongs to relation public function getWebhook() { - return $this->hasOne(\app\models\Webhook::class, ['redelivery_of' => 'id'])->inverseOf('redelivery_of'); + return $this->hasOne(\app\models\Webhook::class, ['redelivery_of' => 'id']); } } diff --git a/tests/specs/fk_col_name/app/models/base/User.php b/tests/specs/fk_col_name/app/models/base/User.php index ede2e7c0..7847f799 100644 --- a/tests/specs/fk_col_name/app/models/base/User.php +++ b/tests/specs/fk_col_name/app/models/base/User.php @@ -29,8 +29,9 @@ public function rules() ]; } + # belongs to relation public function getWebhook() { - return $this->hasOne(\app\models\Webhook::class, ['user_id' => 'id'])->inverseOf('user'); + return $this->hasOne(\app\models\Webhook::class, ['user_id' => 'id']); } } diff --git a/tests/specs/fk_col_name_index/app/models/base/Delivery.php b/tests/specs/fk_col_name_index/app/models/base/Delivery.php index a17531d1..98b9c801 100644 --- a/tests/specs/fk_col_name_index/app/models/base/Delivery.php +++ b/tests/specs/fk_col_name_index/app/models/base/Delivery.php @@ -28,13 +28,15 @@ public function rules() ]; } + # belongs to relation public function getWebhook() { - return $this->hasOne(\app\models\Webhook::class, ['redelivery_of' => 'id'])->inverseOf('redelivery_of'); + return $this->hasOne(\app\models\Webhook::class, ['redelivery_of' => 'id']); } + # belongs to relation public function getWebhook2() { - return $this->hasOne(\app\models\Webhook::class, ['rd_abc_2' => 'id'])->inverseOf('rd2'); + return $this->hasOne(\app\models\Webhook::class, ['rd_abc_2' => 'id']); } } diff --git a/tests/specs/fk_col_name_index/app/models/base/User.php b/tests/specs/fk_col_name_index/app/models/base/User.php index ede2e7c0..7847f799 100644 --- a/tests/specs/fk_col_name_index/app/models/base/User.php +++ b/tests/specs/fk_col_name_index/app/models/base/User.php @@ -29,8 +29,9 @@ public function rules() ]; } + # belongs to relation public function getWebhook() { - return $this->hasOne(\app\models\Webhook::class, ['user_id' => 'id'])->inverseOf('user'); + return $this->hasOne(\app\models\Webhook::class, ['user_id' => 'id']); } } diff --git a/tests/specs/issue_fix/159_bug_giiapi_generated_rules_emailid/maria/models/base/Mailing.php b/tests/specs/issue_fix/159_bug_giiapi_generated_rules_emailid/maria/models/base/Mailing.php index 57bb103f..2639a486 100644 --- a/tests/specs/issue_fix/159_bug_giiapi_generated_rules_emailid/maria/models/base/Mailing.php +++ b/tests/specs/issue_fix/159_bug_giiapi_generated_rules_emailid/maria/models/base/Mailing.php @@ -31,8 +31,9 @@ public function rules() ]; } + # belongs to relation public function getContact() { - return $this->hasOne(\app\models\Contact::class, ['mailing_id' => 'id'])->inverseOf('mailing'); + return $this->hasOne(\app\models\Contact::class, ['mailing_id' => 'id']); } } diff --git a/tests/specs/issue_fix/162_bug_dollarref_with_x_faker/app/models/base/Invoice.php b/tests/specs/issue_fix/162_bug_dollarref_with_x_faker/app/models/base/Invoice.php index cf4c0f73..1f685781 100644 --- a/tests/specs/issue_fix/162_bug_dollarref_with_x_faker/app/models/base/Invoice.php +++ b/tests/specs/issue_fix/162_bug_dollarref_with_x_faker/app/models/base/Invoice.php @@ -24,8 +24,9 @@ public function rules() return []; } + # belongs to relation public function getOrder() { - return $this->hasOne(\app\models\Order::class, ['invoice_id' => 'id'])->inverseOf('invoice'); + return $this->hasOne(\app\models\Order::class, ['invoice_id' => 'id']); } } diff --git a/tests/specs/issue_fix/163_generator_crash_when_using_reference_inside_an_object/pgsql/controllers/ContactController.php b/tests/specs/issue_fix/163_generator_crash_when_using_reference_inside_an_object/pgsql/controllers/ContactController.php index 10422b18..5f21cabb 100644 --- a/tests/specs/issue_fix/163_generator_crash_when_using_reference_inside_an_object/pgsql/controllers/ContactController.php +++ b/tests/specs/issue_fix/163_generator_crash_when_using_reference_inside_an_object/pgsql/controllers/ContactController.php @@ -13,11 +13,13 @@ public function checkAccess($action, $model = null, $params = []) public function actionListForAccount($accountId) { //TODO implement actionListForAccount + // In order to conform with OpenAPI spec, response of this action must have one of the following HTTP status code: 200, 403 } public function actionViewForAccount($accountId, $contactId) { //TODO implement actionViewForAccount + // In order to conform with OpenAPI spec, response of this action must have one of the following HTTP status code: 200, 403 } diff --git a/tests/specs/issue_fix/175_bug_allof_with_multiple_dollarrefs/pgsql/controllers/AccountController.php b/tests/specs/issue_fix/175_bug_allof_with_multiple_dollarrefs/pgsql/controllers/AccountController.php index b4044b8f..cfa4a6d3 100644 --- a/tests/specs/issue_fix/175_bug_allof_with_multiple_dollarrefs/pgsql/controllers/AccountController.php +++ b/tests/specs/issue_fix/175_bug_allof_with_multiple_dollarrefs/pgsql/controllers/AccountController.php @@ -13,6 +13,7 @@ public function checkAccess($action, $model = null, $params = []) public function actionView($id) { //TODO implement actionView + // In order to conform with OpenAPI spec, response of this action must have one of the following HTTP status code: 200, 404 } diff --git a/tests/specs/issue_fix/175_bug_allof_with_multiple_dollarrefs/pgsql/models/base/Account.php b/tests/specs/issue_fix/175_bug_allof_with_multiple_dollarrefs/pgsql/models/base/Account.php index cd597631..079b93b9 100644 --- a/tests/specs/issue_fix/175_bug_allof_with_multiple_dollarrefs/pgsql/models/base/Account.php +++ b/tests/specs/issue_fix/175_bug_allof_with_multiple_dollarrefs/pgsql/models/base/Account.php @@ -31,8 +31,9 @@ public function rules() ]; } + # belongs to relation public function getContact() { - return $this->hasOne(\app\models\Contact::class, ['account_id' => 'id'])->inverseOf('account'); + return $this->hasOne(\app\models\Contact::class, ['account_id' => 'id']); } } diff --git a/tests/specs/issue_fix/25_generate_inverse_relations/mysql/models/base/User.php b/tests/specs/issue_fix/25_generate_inverse_relations/mysql/models/base/User.php index 7e26bb25..c1b63234 100644 --- a/tests/specs/issue_fix/25_generate_inverse_relations/mysql/models/base/User.php +++ b/tests/specs/issue_fix/25_generate_inverse_relations/mysql/models/base/User.php @@ -35,18 +35,21 @@ public function getAccounts() return $this->hasMany(\app\models\Account::class, ['user_id' => 'id'])->inverseOf('user'); } + # belongs to relation public function getAccount() { - return $this->hasOne(\app\models\Account::class, ['user_id' => 'id'])->inverseOf('user'); + return $this->hasOne(\app\models\Account::class, ['user_id' => 'id']); } + # belongs to relation public function getAccount2() { - return $this->hasOne(\app\models\Account::class, ['user2_id' => 'id'])->inverseOf('user2'); + return $this->hasOne(\app\models\Account::class, ['user2_id' => 'id']); } + # belongs to relation public function getAccount3() { - return $this->hasOne(\app\models\Account::class, ['user3' => 'id'])->inverseOf('user3'); + return $this->hasOne(\app\models\Account::class, ['user3' => 'id']); } } diff --git a/tests/specs/issue_fix/29_extension_fk_column_name_cause_error_in_case_of_column_name_without_underscore/mysql/models/base/User.php b/tests/specs/issue_fix/29_extension_fk_column_name_cause_error_in_case_of_column_name_without_underscore/mysql/models/base/User.php index 390d8bdb..547aa903 100644 --- a/tests/specs/issue_fix/29_extension_fk_column_name_cause_error_in_case_of_column_name_without_underscore/mysql/models/base/User.php +++ b/tests/specs/issue_fix/29_extension_fk_column_name_cause_error_in_case_of_column_name_without_underscore/mysql/models/base/User.php @@ -28,8 +28,9 @@ public function rules() ]; } + # belongs to relation public function getPost() { - return $this->hasOne(\app\models\Post::class, ['user' => 'id'])->inverseOf('user'); + return $this->hasOne(\app\models\Post::class, ['user' => 'id']); } } 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 index 81e157ac..bfd317f7 100644 --- 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 @@ -28,8 +28,9 @@ public function rules() ]; } + # belongs to relation public function getInvoice() { - return $this->hasOne(\app\models\Invoice::class, ['animal_id' => 'id'])->inverseOf('animal'); + return $this->hasOne(\app\models\Invoice::class, ['animal_id' => 'id']); } } 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 index d56f106c..ab5c4054 100644 --- 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 @@ -28,8 +28,9 @@ public function rules() ]; } + # belongs to relation public function getInvoice() { - return $this->hasOne(\app\models\Invoice::class, ['fruit_id' => 'id'])->inverseOf('fruit'); + return $this->hasOne(\app\models\Invoice::class, ['fruit_id' => '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 index a6ce6e79..c132c14b 100644 --- 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 @@ -28,13 +28,15 @@ public function rules() ]; } + # belongs to relation public function getInvoice() { - return $this->hasOne(\app\models\Invoice::class, ['user_id' => 'id'])->inverseOf('user'); + return $this->hasOne(\app\models\Invoice::class, ['user_id' => 'id']); } + # belongs to relation public function getInvoice2() { - return $this->hasOne(\app\models\Invoice::class, ['user_2_id' => 'id'])->inverseOf('user_2'); + return $this->hasOne(\app\models\Invoice::class, ['user_2_id' => 'id']); } } diff --git a/tests/specs/issue_fix/78_properties_that_are_marked_as_readonly_are_not_read_only/index.php b/tests/specs/issue_fix/78_properties_that_are_marked_as_readonly_are_not_read_only/index.php new file mode 100644 index 00000000..9a6fa0f2 --- /dev/null +++ b/tests/specs/issue_fix/78_properties_that_are_marked_as_readonly_are_not_read_only/index.php @@ -0,0 +1,13 @@ + '@specs/issue_fix/78_properties_that_are_marked_as_readonly_are_not_read_only/index.yaml', + '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/78_properties_that_are_marked_as_readonly_are_not_read_only/index.yaml b/tests/specs/issue_fix/78_properties_that_are_marked_as_readonly_are_not_read_only/index.yaml new file mode 100644 index 00000000..8f748b8e --- /dev/null +++ b/tests/specs/issue_fix/78_properties_that_are_marked_as_readonly_are_not_read_only/index.yaml @@ -0,0 +1,24 @@ +openapi: 3.0.3 + +info: + title: '#78' + version: 1.0.0 + +paths: + /: + get: + responses: + '200': + description: The Response + +components: + schemas: + Payment: + properties: + id: + type: integer + amount: + type: integer + readOnly: true + currency: + type: string diff --git a/tests/specs/issue_fix/78_properties_that_are_marked_as_readonly_are_not_read_only/mysql/models/BaseModelFaker.php b/tests/specs/issue_fix/78_properties_that_are_marked_as_readonly_are_not_read_only/mysql/models/BaseModelFaker.php new file mode 100644 index 00000000..c367fbb4 --- /dev/null +++ b/tests/specs/issue_fix/78_properties_that_are_marked_as_readonly_are_not_read_only/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/78_properties_that_are_marked_as_readonly_are_not_read_only/mysql/models/Payment.php b/tests/specs/issue_fix/78_properties_that_are_marked_as_readonly_are_not_read_only/mysql/models/Payment.php new file mode 100644 index 00000000..660670f4 --- /dev/null +++ b/tests/specs/issue_fix/78_properties_that_are_marked_as_readonly_are_not_read_only/mysql/models/Payment.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 Payment(); + //$model->id = $uniqueFaker->numberBetween(0, 1000000); + $model->amount = $faker->numberBetween(0, 1000000); + $model->currency = $faker->currencyCode; + if (!is_callable($attributes)) { + $model->setAttributes($attributes, false); + } else { + $model = $attributes($model, $faker, $uniqueFaker); + } + return $model; + } +} diff --git a/tests/specs/issue_fix/78_properties_that_are_marked_as_readonly_are_not_read_only/mysql/models/base/Payment.php b/tests/specs/issue_fix/78_properties_that_are_marked_as_readonly_are_not_read_only/mysql/models/base/Payment.php new file mode 100644 index 00000000..129cce90 --- /dev/null +++ b/tests/specs/issue_fix/78_properties_that_are_marked_as_readonly_are_not_read_only/mysql/models/base/Payment.php @@ -0,0 +1,31 @@ + [['currency'], 'trim'], + 'currency_string' => [['currency'], 'string'], + ]; + } +} diff --git a/tests/specs/issue_fix/79_response_status_codes_are_not_the_codes_defined_in_spec/index.php b/tests/specs/issue_fix/79_response_status_codes_are_not_the_codes_defined_in_spec/index.php new file mode 100644 index 00000000..cf88e27a --- /dev/null +++ b/tests/specs/issue_fix/79_response_status_codes_are_not_the_codes_defined_in_spec/index.php @@ -0,0 +1,13 @@ + '@specs/issue_fix/79_response_status_codes_are_not_the_codes_defined_in_spec/index.yml', + 'generateUrls' => false, + 'generateModels' => false, + 'excludeModels' => [ + 'Error', + ], + 'generateControllers' => true, + 'generateMigrations' => false, + 'generateModelFaker' => false, +]; diff --git a/tests/specs/issue_fix/79_response_status_codes_are_not_the_codes_defined_in_spec/index.yml b/tests/specs/issue_fix/79_response_status_codes_are_not_the_codes_defined_in_spec/index.yml new file mode 100644 index 00000000..df5b1314 --- /dev/null +++ b/tests/specs/issue_fix/79_response_status_codes_are_not_the_codes_defined_in_spec/index.yml @@ -0,0 +1,36 @@ +openapi: "3.0.0" +info: + version: 1.0.0 + title: 79_response_status_codes_are_not_the_codes_defined_in_spec +paths: + /mango/cake: + get: + responses: + '200': + description: The information + '403': + description: The information + '404': + description: The information + post: + responses: + '201': + description: The information + '403': + description: The information + '404': + description: The information + '422': + description: The information + +components: + schemas: + Address: + type: object + description: desc + properties: + id: + type: integer + name: + type: string + maxLength: 64 diff --git a/tests/specs/issue_fix/79_response_status_codes_are_not_the_codes_defined_in_spec/mysql/controllers/MangoController.php b/tests/specs/issue_fix/79_response_status_codes_are_not_the_codes_defined_in_spec/mysql/controllers/MangoController.php new file mode 100644 index 00000000..46c40ad1 --- /dev/null +++ b/tests/specs/issue_fix/79_response_status_codes_are_not_the_codes_defined_in_spec/mysql/controllers/MangoController.php @@ -0,0 +1,27 @@ + [ + 'class' => \yii\rest\OptionsAction::class, + ], + ]; + } + + /** + * Checks the privilege of the current user. + * + * This method checks whether the current user has the privilege + * to run the specified action against the specified data model. + * If the user does not have access, a [[ForbiddenHttpException]] should be thrown. + * + * @param string $action the ID of the action to be executed + * @param object $model the model to be accessed. If null, it means no specific model is being accessed. + * @param array $params additional parameters + * @throws \yii\web\ForbiddenHttpException if the user does not have access + */ + abstract public function checkAccess($action, $model = null, $params = []); + + abstract public function actionCake(); + + abstract public function actionCreateCake(); + +} diff --git a/tests/specs/issue_fix/88_in_case_of_updating_a_model_generator_creates_redundant_inverse_relations/index.php b/tests/specs/issue_fix/88_in_case_of_updating_a_model_generator_creates_redundant_inverse_relations/index.php new file mode 100644 index 00000000..59eb14b0 --- /dev/null +++ b/tests/specs/issue_fix/88_in_case_of_updating_a_model_generator_creates_redundant_inverse_relations/index.php @@ -0,0 +1,13 @@ + '@specs/issue_fix/88_in_case_of_updating_a_model_generator_creates_redundant_inverse_relations/index.yaml', + 'generateUrls' => false, + 'generateModels' => true, + 'excludeModels' => [ + 'Error', + ], + 'generateControllers' => false, + 'generateMigrations' => false, + 'generateModelFaker' => false, // `generateModels` must be `true` in order to use `generateModelFaker` as `true` +]; diff --git a/tests/specs/issue_fix/88_in_case_of_updating_a_model_generator_creates_redundant_inverse_relations/index.yaml b/tests/specs/issue_fix/88_in_case_of_updating_a_model_generator_creates_redundant_inverse_relations/index.yaml new file mode 100644 index 00000000..8acbf164 --- /dev/null +++ b/tests/specs/issue_fix/88_in_case_of_updating_a_model_generator_creates_redundant_inverse_relations/index.yaml @@ -0,0 +1,31 @@ +openapi: "3.0.0" + +info: + version: 1.0.0 + title: '#88' + +paths: + /: + get: + responses: + '200': + description: The response + +components: + schemas: + Address: + type: object + properties: + id: + type: integer + name: + type: string + Human: + type: object + properties: + id: + type: integer + name: + type: string + address: + $ref: '#/components/schemas/Address' diff --git a/tests/specs/issue_fix/88_in_case_of_updating_a_model_generator_creates_redundant_inverse_relations/mysql/models/Address.php b/tests/specs/issue_fix/88_in_case_of_updating_a_model_generator_creates_redundant_inverse_relations/mysql/models/Address.php new file mode 100644 index 00000000..abfd7d59 --- /dev/null +++ b/tests/specs/issue_fix/88_in_case_of_updating_a_model_generator_creates_redundant_inverse_relations/mysql/models/Address.php @@ -0,0 +1,10 @@ + [['name'], 'trim'], + 'name_string' => [['name'], 'string'], + ]; + } + + # belongs to relation + public function getHuman() + { + return $this->hasOne(\app\models\Human::class, ['address_id' => 'id']); + } +} diff --git a/tests/specs/issue_fix/88_in_case_of_updating_a_model_generator_creates_redundant_inverse_relations/mysql/models/base/Human.php b/tests/specs/issue_fix/88_in_case_of_updating_a_model_generator_creates_redundant_inverse_relations/mysql/models/base/Human.php new file mode 100644 index 00000000..dadaa276 --- /dev/null +++ b/tests/specs/issue_fix/88_in_case_of_updating_a_model_generator_creates_redundant_inverse_relations/mysql/models/base/Human.php @@ -0,0 +1,39 @@ + [['name'], 'trim'], + 'name_string' => [['name'], 'string'], + 'address_id_integer' => [['address_id'], 'integer'], + 'address_id_exist' => [['address_id'], 'exist', 'targetRelation' => 'address'], + ]; + } + + public function getAddress() + { + return $this->hasOne(\app\models\Address::class, ['id' => 'address_id']); + } +} diff --git a/tests/specs/issue_fix/90_implement_belongs_to_relations_in_models/index.php b/tests/specs/issue_fix/90_implement_belongs_to_relations_in_models/index.php new file mode 100644 index 00000000..2c6dfb9b --- /dev/null +++ b/tests/specs/issue_fix/90_implement_belongs_to_relations_in_models/index.php @@ -0,0 +1,13 @@ + '@specs/issue_fix/90_implement_belongs_to_relations_in_models/index.yaml', + 'generateUrls' => false, + 'generateModels' => true, + 'excludeModels' => [ + 'Error', + ], + 'generateControllers' => false, + 'generateMigrations' => false, + 'generateModelFaker' => false, // `generateModels` must be `true` in order to use `generateModelFaker` as `true` +]; diff --git a/tests/specs/issue_fix/90_implement_belongs_to_relations_in_models/index.yaml b/tests/specs/issue_fix/90_implement_belongs_to_relations_in_models/index.yaml new file mode 100644 index 00000000..b62591f0 --- /dev/null +++ b/tests/specs/issue_fix/90_implement_belongs_to_relations_in_models/index.yaml @@ -0,0 +1,26 @@ +openapi: "3.0.0" +info: + version: 1.0.0 + title: \#90 +paths: + /: + get: + responses: + '200': + description: The information + +components: + schemas: + User: + type: object + properties: + id: + type: integer + + Address: + type: object + properties: + id: + type: integer + user: + $ref: '#/components/schemas/User' diff --git a/tests/specs/issue_fix/90_implement_belongs_to_relations_in_models/mysql/models/Address.php b/tests/specs/issue_fix/90_implement_belongs_to_relations_in_models/mysql/models/Address.php new file mode 100644 index 00000000..abfd7d59 --- /dev/null +++ b/tests/specs/issue_fix/90_implement_belongs_to_relations_in_models/mysql/models/Address.php @@ -0,0 +1,10 @@ + [['user_id'], 'integer'], + 'user_id_exist' => [['user_id'], 'exist', 'targetRelation' => 'user'], + ]; + } + + public function getUser() + { + return $this->hasOne(\app\models\User::class, ['id' => 'user_id']); + } +} diff --git a/tests/specs/issue_fix/90_implement_belongs_to_relations_in_models/mysql/models/base/User.php b/tests/specs/issue_fix/90_implement_belongs_to_relations_in_models/mysql/models/base/User.php new file mode 100644 index 00000000..a6a35b66 --- /dev/null +++ b/tests/specs/issue_fix/90_implement_belongs_to_relations_in_models/mysql/models/base/User.php @@ -0,0 +1,32 @@ +hasOne(\app\models\Address::class, ['user_id' => 'id']); + } +} diff --git a/tests/specs/issue_fix/96_component_schema_should_be_optional/index.php b/tests/specs/issue_fix/96_component_schema_should_be_optional/index.php new file mode 100644 index 00000000..34db291a --- /dev/null +++ b/tests/specs/issue_fix/96_component_schema_should_be_optional/index.php @@ -0,0 +1,13 @@ + '@specs/issue_fix/96_component_schema_should_be_optional/index.yml', + 'generateUrls' => false, + 'generateModels' => true, + 'excludeModels' => [ + 'Error', + ], + 'generateControllers' => false, + 'generateMigrations' => false, + 'generateModelFaker' => true, +]; diff --git a/tests/specs/issue_fix/96_component_schema_should_be_optional/index.yml b/tests/specs/issue_fix/96_component_schema_should_be_optional/index.yml new file mode 100644 index 00000000..5dcbc3cb --- /dev/null +++ b/tests/specs/issue_fix/96_component_schema_should_be_optional/index.yml @@ -0,0 +1,13 @@ +openapi: "3.0.0" +info: + version: 1.0.0 + title: 96_component_schema_should_be_optional + +paths: + /: + get: + summary: List + operationId: list + responses: + '200': + description: The information diff --git a/tests/specs/issue_fix/96_component_schema_should_be_optional/mysql/models/BaseModelFaker.php b/tests/specs/issue_fix/96_component_schema_should_be_optional/mysql/models/BaseModelFaker.php new file mode 100644 index 00000000..c367fbb4 --- /dev/null +++ b/tests/specs/issue_fix/96_component_schema_should_be_optional/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/model_name_more_than_once_in_faker_148/app/models/base/Account.php b/tests/specs/issue_fix/model_name_more_than_once_in_faker_148/app/models/base/Account.php index f875eadd..a296c4c0 100644 --- a/tests/specs/issue_fix/model_name_more_than_once_in_faker_148/app/models/base/Account.php +++ b/tests/specs/issue_fix/model_name_more_than_once_in_faker_148/app/models/base/Account.php @@ -29,18 +29,21 @@ public function rules() ]; } + # belongs to relation public function getE123() { - return $this->hasOne(\app\models\E123::class, ['account_id' => 'id'])->inverseOf('account'); + return $this->hasOne(\app\models\E123::class, ['account_id' => 'id']); } + # belongs to relation public function getE1232() { - return $this->hasOne(\app\models\E123::class, ['account_2_id' => 'id'])->inverseOf('account_2'); + return $this->hasOne(\app\models\E123::class, ['account_2_id' => 'id']); } + # belongs to relation public function getE1233() { - return $this->hasOne(\app\models\E123::class, ['account_3_id' => 'id'])->inverseOf('account_3'); + return $this->hasOne(\app\models\E123::class, ['account_3_id' => 'id']); } } diff --git a/tests/specs/petstore/models/base/Store.php b/tests/specs/petstore/models/base/Store.php index 9589d1b8..d9eafaca 100644 --- a/tests/specs/petstore/models/base/Store.php +++ b/tests/specs/petstore/models/base/Store.php @@ -29,8 +29,9 @@ public function rules() ]; } + # belongs to relation public function getPet() { - return $this->hasOne(\app\models\Pet::class, ['store_id' => 'id'])->inverseOf('store'); + return $this->hasOne(\app\models\Pet::class, ['store_id' => 'id']); } } diff --git a/tests/specs/petstore_jsonapi/models/base/Pet.php b/tests/specs/petstore_jsonapi/models/base/Pet.php index 6444cf28..0f057dc1 100644 --- a/tests/specs/petstore_jsonapi/models/base/Pet.php +++ b/tests/specs/petstore_jsonapi/models/base/Pet.php @@ -64,8 +64,9 @@ public function getDuplicates() return $this->hasMany(\app\models\Pet::class, ['tag' => 'tag']); } + # belongs to relation public function getPetStatistic() { - return $this->hasOne(\app\models\PetStatistic::class, ['parentPet_id' => 'id'])->inverseOf('parentPet'); + return $this->hasOne(\app\models\PetStatistic::class, ['parentPet_id' => 'id']); } } diff --git a/tests/specs/petstore_namespace/mymodels/base/Store.php b/tests/specs/petstore_namespace/mymodels/base/Store.php index 24d0f4e4..35aa907f 100644 --- a/tests/specs/petstore_namespace/mymodels/base/Store.php +++ b/tests/specs/petstore_namespace/mymodels/base/Store.php @@ -29,8 +29,9 @@ public function rules() ]; } + # belongs to relation public function getPet() { - return $this->hasOne(\app\mymodels\Pet::class, ['store_id' => 'id'])->inverseOf('store'); + return $this->hasOne(\app\mymodels\Pet::class, ['store_id' => 'id']); } } diff --git a/tests/specs/relations_in_faker/app/models/base/A123.php b/tests/specs/relations_in_faker/app/models/base/A123.php index 0c733222..74d782f2 100644 --- a/tests/specs/relations_in_faker/app/models/base/A123.php +++ b/tests/specs/relations_in_faker/app/models/base/A123.php @@ -37,8 +37,9 @@ public function getB123() return $this->hasOne(\app\models\B123::class, ['id' => 'b123_id']); } + # belongs to relation public function getRouting() { - return $this->hasOne(\app\models\Routing::class, ['a123_id' => 'id'])->inverseOf('a123'); + return $this->hasOne(\app\models\Routing::class, ['a123_id' => 'id']); } } diff --git a/tests/specs/relations_in_faker/app/models/base/Account.php b/tests/specs/relations_in_faker/app/models/base/Account.php index e1198a95..c5bfedb1 100644 --- a/tests/specs/relations_in_faker/app/models/base/Account.php +++ b/tests/specs/relations_in_faker/app/models/base/Account.php @@ -29,8 +29,9 @@ public function rules() ]; } + # belongs to relation public function getDomain() { - return $this->hasOne(\app\models\Domain::class, ['account_id' => 'id'])->inverseOf('account'); + return $this->hasOne(\app\models\Domain::class, ['account_id' => 'id']); } } diff --git a/tests/specs/relations_in_faker/app/models/base/B123.php b/tests/specs/relations_in_faker/app/models/base/B123.php index 7ad4eb77..63727b75 100644 --- a/tests/specs/relations_in_faker/app/models/base/B123.php +++ b/tests/specs/relations_in_faker/app/models/base/B123.php @@ -37,13 +37,15 @@ public function getC123() return $this->hasOne(\app\models\C123::class, ['id' => 'c123_id']); } + # belongs to relation public function getA123() { - return $this->hasOne(\app\models\A123::class, ['b123_id' => 'id'])->inverseOf('b123'); + return $this->hasOne(\app\models\A123::class, ['b123_id' => 'id']); } - public function getE1232() + # belongs to relation + public function getE123() { - return $this->hasOne(\app\models\E123::class, ['b123_id' => 'id'])->inverseOf('b123'); + return $this->hasOne(\app\models\E123::class, ['b123_id' => 'id']); } } diff --git a/tests/specs/relations_in_faker/app/models/base/C123.php b/tests/specs/relations_in_faker/app/models/base/C123.php index 6c5677dd..7f516f78 100644 --- a/tests/specs/relations_in_faker/app/models/base/C123.php +++ b/tests/specs/relations_in_faker/app/models/base/C123.php @@ -28,8 +28,9 @@ public function rules() ]; } + # belongs to relation public function getB123() { - return $this->hasOne(\app\models\B123::class, ['c123_id' => 'id'])->inverseOf('c123'); + return $this->hasOne(\app\models\B123::class, ['c123_id' => 'id']); } } diff --git a/tests/specs/relations_in_faker/app/models/base/D123.php b/tests/specs/relations_in_faker/app/models/base/D123.php index 80a5017d..71555b18 100644 --- a/tests/specs/relations_in_faker/app/models/base/D123.php +++ b/tests/specs/relations_in_faker/app/models/base/D123.php @@ -28,8 +28,9 @@ public function rules() ]; } + # belongs to relation public function getRouting() { - return $this->hasOne(\app\models\Routing::class, ['d123_id' => 'id'])->inverseOf('d123'); + return $this->hasOne(\app\models\Routing::class, ['d123_id' => 'id']); } } diff --git a/tests/specs/relations_in_faker/app/models/base/Domain.php b/tests/specs/relations_in_faker/app/models/base/Domain.php index df6e0822..fa563120 100644 --- a/tests/specs/relations_in_faker/app/models/base/Domain.php +++ b/tests/specs/relations_in_faker/app/models/base/Domain.php @@ -45,8 +45,9 @@ public function getRoutings() return $this->hasMany(\app\models\Routing::class, ['domain_id' => 'id'])->inverseOf('domain'); } + # belongs to relation public function getRouting() { - return $this->hasOne(\app\models\Routing::class, ['domain_id' => 'id'])->inverseOf('domain'); + return $this->hasOne(\app\models\Routing::class, ['domain_id' => 'id']); } } diff --git a/tests/unit/IssueFixTest.php b/tests/unit/IssueFixTest.php index 08d1a00c..8d57cd1b 100644 --- a/tests/unit/IssueFixTest.php +++ b/tests/unit/IssueFixTest.php @@ -1030,6 +1030,20 @@ public function test23ConsiderOpenapiExtensionXNoRelationAlsoInOtherPertinentPla $this->runActualMigrations(); } + // https://github.com/php-openapi/yii2-openapi/issues/79 + public function test79ResponseStatusCodesAreNotTheCodesDefinedInSpec() + { + $testFile = Yii::getAlias("@specs/issue_fix/79_response_status_codes_are_not_the_codes_defined_in_spec/index.php"); + $this->runGenerator($testFile); + $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [ + 'recursive' => true, + ]); + $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/79_response_status_codes_are_not_the_codes_defined_in_spec/mysql"), [ + 'recursive' => true, + ]); + $this->checkFiles($actualFiles, $expectedFiles); + } + // https://github.com/php-openapi/yii2-openapi/issues/87 public function test87ImplementForJsonInIsrefpointertoschema() { @@ -1044,4 +1058,60 @@ public function test87ImplementForJsonInIsrefpointertoschema() $this->checkFiles($actualFiles, $expectedFiles); $this->runActualMigrations(); } + + // https://github.com/php-openapi/yii2-openapi/issues/96 + public function test96ComponentSchemaShouldBeOptional() + { + $testFile = Yii::getAlias("@specs/issue_fix/96_component_schema_should_be_optional/index.php"); + $this->runGenerator($testFile); + $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [ + 'recursive' => true, + ]); + $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/96_component_schema_should_be_optional/mysql"), [ + 'recursive' => true, + ]); + $this->checkFiles($actualFiles, $expectedFiles); + } + + // https://github.com/php-openapi/yii2-openapi/issues/88 + public function test88InCaseOfUpdatingAModelGeneratorCreatesRedundantInverseRelations() + { + $testFile = Yii::getAlias("@specs/issue_fix/88_in_case_of_updating_a_model_generator_creates_redundant_inverse_relations/index.php"); + $this->runGenerator($testFile); + $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [ + 'recursive' => true, + ]); + $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/88_in_case_of_updating_a_model_generator_creates_redundant_inverse_relations/mysql"), [ + 'recursive' => true, + ]); + $this->checkFiles($actualFiles, $expectedFiles); + } + + // https://github.com/php-openapi/yii2-openapi/issues/78 + public function test78PropertiesThatAreMarkedAsReadonlyAreNotReadOnly() + { + $testFile = Yii::getAlias("@specs/issue_fix/78_properties_that_are_marked_as_readonly_are_not_read_only/index.php"); + $this->runGenerator($testFile); + $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [ + 'recursive' => true, + ]); + $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/78_properties_that_are_marked_as_readonly_are_not_read_only/mysql"), [ + 'recursive' => true, + ]); + $this->checkFiles($actualFiles, $expectedFiles); + } + + // https://github.com/php-openapi/yii2-openapi/issues/90 + public function test90ImplementBelongsToRelationsInModels() + { + $testFile = Yii::getAlias("@specs/issue_fix/90_implement_belongs_to_relations_in_models/index.php"); + $this->runGenerator($testFile); + $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [ + 'recursive' => true, + ]); + $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/90_implement_belongs_to_relations_in_models/mysql"), [ + 'recursive' => true, + ]); + $this->checkFiles($actualFiles, $expectedFiles); + } }