From e27d9a0822e169145070ef254fe465c10f334bfc Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Sat, 17 Aug 2024 20:35:41 +0530 Subject: [PATCH 1/4] Initial commit to create PR --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 1cd2090b..3bb79307 100644 --- a/README.md +++ b/README.md @@ -568,3 +568,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 1bfc499f46a3ff538d924140c9acd07f8b5cfba3 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Sun, 18 Aug 2024 19:31:34 +0530 Subject: [PATCH 2/4] Fix issue and other failing tests --- README.md | 1 - TODO.taskpaper | 9 +++++ src/lib/ValidationRulesBuilder.php | 8 ++--- tests/DbTestCase.php | 7 ++-- tests/specs/blog/models/base/Category.php | 2 +- tests/specs/blog/models/base/Comment.php | 2 +- tests/specs/blog/models/base/Post.php | 2 +- tests/specs/blog/models/base/User.php | 2 +- tests/specs/blog_v2/models/base/Category.php | 2 +- tests/specs/blog_v2/models/base/Comment.php | 2 +- tests/specs/blog_v2/models/base/Post.php | 2 +- tests/specs/blog_v2/models/base/Tag.php | 2 +- tests/specs/blog_v2/models/base/User.php | 2 +- .../app/models/base/ColumnNameChange.php | 2 +- .../app/models/base/ColumnNameChange.php | 2 +- .../app/models/base/ColumnNameChange.php | 2 +- .../fk_col_name/app/models/base/User.php | 2 +- .../app/models/base/User.php | 2 +- .../id_not_in_rules/app/models/base/Fruit.php | 2 +- .../app/models/base/Pristine.php | 2 +- .../maria/models/base/Mailing.php | 2 +- .../maria/models/base/Contact.php | 2 +- .../maria/models/base/Mailing.php | 2 +- .../pgsql/models/base/Account.php | 2 +- .../pgsql/models/base/Contact.php | 2 +- .../pgsql/models/base/PaymentMethod.php | 2 +- .../index.php | 13 +++++++ .../index.yaml | 35 +++++++++++++++++++ .../app/models/base/Account.php | 2 +- tests/specs/many2many/models/base/Photo.php | 2 +- tests/specs/many2many/models/base/Post.php | 2 +- tests/specs/many2many/models/base/Tag.php | 2 +- tests/specs/menu/models/base/Menu.php | 2 +- tests/specs/petstore/models/base/Pet.php | 2 +- tests/specs/petstore/models/base/Store.php | 2 +- .../petstore_arrayref/models/base/Pet.php | 2 +- .../petstore_jsonapi/models/base/Doctor.php | 2 +- .../petstore_jsonapi/models/base/Pet.php | 2 +- .../petstore_namespace/mymodels/base/Pet.php | 2 +- .../mymodels/base/Store.php | 2 +- .../petstore_wrapped/models/base/Pet.php | 2 +- .../specs/petstore_xtable/models/base/Pet.php | 2 +- .../app/models/base/Account.php | 2 +- .../app/models/base/Domain.php | 2 +- .../app/models/base/Routing.php | 2 +- .../models/mariamodel/base/Alldbdatatype.php | 2 +- .../app/models/mariamodel/base/Editcolumn.php | 2 +- .../app/models/mariamodel/base/Newcolumn.php | 2 +- .../app/models/mariamodel/base/Pristine.php | 2 +- .../mysql/app/models/base/Alldbdatatype.php | 2 +- .../mysql/app/models/base/Editcolumn.php | 2 +- .../mysql/app/models/base/Newcolumn.php | 2 +- .../mysql/app/models/base/Pristine.php | 2 +- .../models/pgsqlmodel/base/Alldbdatatype.php | 2 +- .../app/models/pgsqlmodel/base/Editcolumn.php | 2 +- .../app/models/pgsqlmodel/base/Newcolumn.php | 2 +- .../app/models/pgsqlmodel/base/Pristine.php | 2 +- tests/unit/IssueFixTest.php | 14 ++++++++ 58 files changed, 129 insertions(+), 60 deletions(-) create mode 100644 TODO.taskpaper create mode 100644 tests/specs/issue_fix/22_bug_rules_required_is_generated_before_default/index.php create mode 100644 tests/specs/issue_fix/22_bug_rules_required_is_generated_before_default/index.yaml diff --git a/README.md b/README.md index 3bb79307..1cd2090b 100644 --- a/README.md +++ b/README.md @@ -568,4 +568,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/TODO.taskpaper b/TODO.taskpaper new file mode 100644 index 00000000..6fc7c133 --- /dev/null +++ b/TODO.taskpaper @@ -0,0 +1,9 @@ +TODO.taskpaper + +### Bug: rules() "required" is generated before "*_default" #22 + ✔ create failing test @done (24-08-18 19:21) + ✔ implement the solution @done (24-08-18 19:21) + ☐ fix failing tests if any + ☐ resolve TODOs if any + ☐ review PR + ☐ delete this file and submit PR diff --git a/src/lib/ValidationRulesBuilder.php b/src/lib/ValidationRulesBuilder.php index 3f3c1398..f0b1f90d 100644 --- a/src/lib/ValidationRulesBuilder.php +++ b/src/lib/ValidationRulesBuilder.php @@ -10,8 +10,6 @@ use cebe\yii2openapi\lib\items\Attribute; use cebe\yii2openapi\lib\items\DbModel; use cebe\yii2openapi\lib\items\ValidationRule; -use yii\helpers\VarDumper; -use yii\validators\DateValidator; use function count; use function implode; use function in_array; @@ -53,9 +51,6 @@ public function build():array $this->rules['trim'] = new ValidationRule($this->typeScope['trim'], 'trim'); } - if (!empty($this->typeScope['required'])) { - $this->rules['required'] = new ValidationRule($this->typeScope['required'], 'required'); - } if (!empty($this->typeScope['ref'])) { $this->addExistRules($this->typeScope['ref']); } @@ -77,6 +72,9 @@ public function build():array if (!empty($this->typeScope['safe'])) { $this->rules['safe'] = new ValidationRule($this->typeScope['safe'], 'safe'); } + if (!empty($this->typeScope['required'])) { + $this->rules['required'] = new ValidationRule($this->typeScope['required'], 'required'); + } return $this->rules; } diff --git a/tests/DbTestCase.php b/tests/DbTestCase.php index ca7b17ff..c28a314c 100644 --- a/tests/DbTestCase.php +++ b/tests/DbTestCase.php @@ -3,12 +3,12 @@ namespace tests; use cebe\yii2openapi\generator\ApiGenerator; +use SamIT\Yii2\MariaDb\Schema as MariaDbSchema; use Yii; -use yii\di\Container; use yii\db\mysql\Schema as MySqlSchema; use yii\db\pgsql\Schema as PgSqlSchema; -use \SamIT\Yii2\MariaDb\Schema as MariaDbSchema; -use yii\helpers\{ArrayHelper, VarDumper, StringHelper, Console}; +use yii\di\Container; +use yii\helpers\{ArrayHelper, StringHelper}; use yii\helpers\FileHelper; class DbTestCase extends \PHPUnit\Framework\TestCase @@ -127,6 +127,7 @@ protected function checkFiles(array $actual, array $expected) ); } + // exec('cp '.$file.' '.$expectedFilePath); $this->assertFileEquals($expectedFilePath, $file, "Failed asserting that file contents of\n$file\nare equal to file contents of\n$expectedFilePath \n\n cp $file $expectedFilePath \n\n "); } } diff --git a/tests/specs/blog/models/base/Category.php b/tests/specs/blog/models/base/Category.php index f22e0014..0debe57d 100644 --- a/tests/specs/blog/models/base/Category.php +++ b/tests/specs/blog/models/base/Category.php @@ -22,11 +22,11 @@ public function rules() { return [ 'trim' => [['title'], 'trim'], - 'required' => [['title', 'active'], 'required'], 'title_unique' => [['title'], 'unique'], 'title_string' => [['title'], 'string', 'max' => 255], 'active_boolean' => [['active'], 'boolean'], 'active_default' => [['active'], 'default', 'value' => false], + 'required' => [['title', 'active'], 'required'], ]; } diff --git a/tests/specs/blog/models/base/Comment.php b/tests/specs/blog/models/base/Comment.php index 44cd42fc..d74131cb 100644 --- a/tests/specs/blog/models/base/Comment.php +++ b/tests/specs/blog/models/base/Comment.php @@ -26,7 +26,6 @@ public function rules() { return [ 'trim' => [['post_id'], 'trim'], - 'required' => [['post_id', 'author_id', 'message', 'created_at'], 'required'], 'post_id_string' => [['post_id'], 'string', 'max' => 128], 'post_id_exist' => [['post_id'], 'exist', 'targetRelation' => 'Post'], 'author_id_integer' => [['author_id'], 'integer'], @@ -35,6 +34,7 @@ public function rules() 'meta_data_default' => [['meta_data'], 'default', 'value' => []], 'created_at_integer' => [['created_at'], 'integer'], 'safe' => [['message', 'meta_data'], 'safe'], + 'required' => [['post_id', 'author_id', 'message', 'created_at'], 'required'], ]; } diff --git a/tests/specs/blog/models/base/Post.php b/tests/specs/blog/models/base/Post.php index 457978b5..c961a2b0 100644 --- a/tests/specs/blog/models/base/Post.php +++ b/tests/specs/blog/models/base/Post.php @@ -28,7 +28,6 @@ public function rules() { return [ 'trim' => [['title', 'slug', 'created_at'], 'trim'], - 'required' => [['title', 'category_id', 'active'], 'required'], 'category_id_integer' => [['category_id'], 'integer'], 'category_id_exist' => [['category_id'], 'exist', 'targetRelation' => 'Category'], 'created_by_id_integer' => [['created_by_id'], 'integer'], @@ -40,6 +39,7 @@ public function rules() 'active_boolean' => [['active'], 'boolean'], 'active_default' => [['active'], 'default', 'value' => false], 'created_at_date' => [['created_at'], 'date', 'format' => 'php:Y-m-d'], + 'required' => [['title', 'category_id', 'active'], 'required'], ]; } diff --git a/tests/specs/blog/models/base/User.php b/tests/specs/blog/models/base/User.php index f0e484e8..bf30a8bd 100644 --- a/tests/specs/blog/models/base/User.php +++ b/tests/specs/blog/models/base/User.php @@ -25,7 +25,6 @@ public function rules() { return [ 'trim' => [['username', 'email', 'password', 'role', 'created_at'], 'trim'], - 'required' => [['username', 'email', 'password'], 'required'], 'username_unique' => [['username'], 'unique'], 'email_unique' => [['email'], 'unique'], 'username_string' => [['username'], 'string', 'max' => 200], @@ -37,6 +36,7 @@ public function rules() 'flags_integer' => [['flags'], 'integer'], 'flags_default' => [['flags'], 'default', 'value' => 0], 'created_at_datetime' => [['created_at'], 'datetime', 'format' => 'php:Y-m-d H:i:s'], + 'required' => [['username', 'email', 'password'], 'required'], ]; } } diff --git a/tests/specs/blog_v2/models/base/Category.php b/tests/specs/blog_v2/models/base/Category.php index a70be3ff..a3ef9567 100644 --- a/tests/specs/blog_v2/models/base/Category.php +++ b/tests/specs/blog_v2/models/base/Category.php @@ -23,10 +23,10 @@ public function rules() { return [ 'trim' => [['title', 'cover'], 'trim'], - 'required' => [['title', 'cover', 'active'], 'required'], 'title_string' => [['title'], 'string', 'max' => 100], 'cover_string' => [['cover'], 'string'], 'active_boolean' => [['active'], 'boolean'], + 'required' => [['title', 'cover', 'active'], 'required'], ]; } diff --git a/tests/specs/blog_v2/models/base/Comment.php b/tests/specs/blog_v2/models/base/Comment.php index a732c5c0..8f634180 100644 --- a/tests/specs/blog_v2/models/base/Comment.php +++ b/tests/specs/blog_v2/models/base/Comment.php @@ -26,7 +26,6 @@ public function rules() { return [ 'trim' => [['message', 'meta_data', 'created_at'], 'trim'], - 'required' => [['post_id', 'message', 'created_at'], 'required'], 'post_id_integer' => [['post_id'], 'integer'], 'post_id_exist' => [['post_id'], 'exist', 'targetRelation' => 'Post'], 'user_id_integer' => [['user_id'], 'integer'], @@ -35,6 +34,7 @@ public function rules() 'meta_data_string' => [['meta_data'], 'string', 'min' => 1, 'max' => 300], 'meta_data_default' => [['meta_data'], 'default', 'value' => ''], 'created_at_datetime' => [['created_at'], 'datetime', 'format' => 'php:Y-m-d H:i:s'], + 'required' => [['post_id', 'message', 'created_at'], 'required'], ]; } diff --git a/tests/specs/blog_v2/models/base/Post.php b/tests/specs/blog_v2/models/base/Post.php index 44fe4275..17f3c8c4 100644 --- a/tests/specs/blog_v2/models/base/Post.php +++ b/tests/specs/blog_v2/models/base/Post.php @@ -30,7 +30,6 @@ public function rules() { return [ 'trim' => [['title', 'slug', 'created_at'], 'trim'], - 'required' => [['title', 'category_id', 'active'], 'required'], 'category_id_integer' => [['category_id'], 'integer'], 'category_id_exist' => [['category_id'], 'exist', 'targetRelation' => 'Category'], 'created_by_id_integer' => [['created_by_id'], 'integer'], @@ -46,6 +45,7 @@ public function rules() 'lang_default' => [['lang'], 'default', 'value' => 'ru'], 'active_boolean' => [['active'], 'boolean'], 'created_at_date' => [['created_at'], 'date', 'format' => 'php:Y-m-d'], + 'required' => [['title', 'category_id', 'active'], 'required'], ]; } diff --git a/tests/specs/blog_v2/models/base/Tag.php b/tests/specs/blog_v2/models/base/Tag.php index f1a57778..2a971ebc 100644 --- a/tests/specs/blog_v2/models/base/Tag.php +++ b/tests/specs/blog_v2/models/base/Tag.php @@ -22,7 +22,6 @@ public function rules() { return [ 'trim' => [['name'], 'trim'], - 'required' => [['name', 'lang'], 'required'], 'name_unique' => [['name'], 'unique'], 'name_string' => [['name'], 'string', 'max' => 100], 'lang_string' => [['lang'], 'string'], @@ -30,6 +29,7 @@ public function rules() 'ru', 'eng', ]], + 'required' => [['name', 'lang'], 'required'], ]; } diff --git a/tests/specs/blog_v2/models/base/User.php b/tests/specs/blog_v2/models/base/User.php index 195b5b5e..eddb7448 100644 --- a/tests/specs/blog_v2/models/base/User.php +++ b/tests/specs/blog_v2/models/base/User.php @@ -25,7 +25,6 @@ public function rules() { return [ 'trim' => [['login', 'email', 'password', 'created_at'], 'trim'], - 'required' => [['login', 'email', 'password'], 'required'], 'login_unique' => [['login'], 'unique'], 'email_unique' => [['email'], 'unique'], 'login_string' => [['login'], 'string'], @@ -41,6 +40,7 @@ public function rules() 'flags_integer' => [['flags'], 'integer'], 'flags_default' => [['flags'], 'default', 'value' => 0], 'created_at_datetime' => [['created_at'], 'datetime', 'format' => 'php:Y-m-d H:i:s'], + 'required' => [['login', 'email', 'password'], 'required'], ]; } } diff --git a/tests/specs/change_column_name/maria/app/models/base/ColumnNameChange.php b/tests/specs/change_column_name/maria/app/models/base/ColumnNameChange.php index a5393546..79574d97 100644 --- a/tests/specs/change_column_name/maria/app/models/base/ColumnNameChange.php +++ b/tests/specs/change_column_name/maria/app/models/base/ColumnNameChange.php @@ -21,8 +21,8 @@ public function rules() { return [ 'trim' => [['name'], 'trim'], - 'required' => [['name'], 'required'], 'name_string' => [['name'], 'string', 'max' => 255], + 'required' => [['name'], 'required'], ]; } } diff --git a/tests/specs/change_column_name/mysql/app/models/base/ColumnNameChange.php b/tests/specs/change_column_name/mysql/app/models/base/ColumnNameChange.php index a5393546..79574d97 100644 --- a/tests/specs/change_column_name/mysql/app/models/base/ColumnNameChange.php +++ b/tests/specs/change_column_name/mysql/app/models/base/ColumnNameChange.php @@ -21,8 +21,8 @@ public function rules() { return [ 'trim' => [['name'], 'trim'], - 'required' => [['name'], 'required'], 'name_string' => [['name'], 'string', 'max' => 255], + 'required' => [['name'], 'required'], ]; } } diff --git a/tests/specs/change_column_name/pgsql/app/models/base/ColumnNameChange.php b/tests/specs/change_column_name/pgsql/app/models/base/ColumnNameChange.php index 426b5a1f..4040250f 100644 --- a/tests/specs/change_column_name/pgsql/app/models/base/ColumnNameChange.php +++ b/tests/specs/change_column_name/pgsql/app/models/base/ColumnNameChange.php @@ -21,8 +21,8 @@ public function rules() { return [ 'trim' => [['name'], 'trim'], - 'required' => [['name'], 'required'], 'name_string' => [['name'], 'string', 'max' => 255], + 'required' => [['name'], 'required'], ]; } } 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 d76c3f4d..3b1bc8d1 100644 --- a/tests/specs/fk_col_name/app/models/base/User.php +++ b/tests/specs/fk_col_name/app/models/base/User.php @@ -20,8 +20,8 @@ public function rules() { return [ 'trim' => [['name'], 'trim'], - 'required' => [['name'], 'required'], 'name_string' => [['name'], 'string'], + 'required' => [['name'], 'required'], ]; } } 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 d76c3f4d..3b1bc8d1 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 @@ -20,8 +20,8 @@ public function rules() { return [ 'trim' => [['name'], 'trim'], - 'required' => [['name'], 'required'], 'name_string' => [['name'], 'string'], + 'required' => [['name'], 'required'], ]; } } diff --git a/tests/specs/id_not_in_rules/app/models/base/Fruit.php b/tests/specs/id_not_in_rules/app/models/base/Fruit.php index 4b1fcd72..9ecdbcf2 100644 --- a/tests/specs/id_not_in_rules/app/models/base/Fruit.php +++ b/tests/specs/id_not_in_rules/app/models/base/Fruit.php @@ -20,8 +20,8 @@ public function rules() { return [ 'trim' => [['name'], 'trim'], - 'required' => [['name'], 'required'], 'name_string' => [['name'], 'string'], + 'required' => [['name'], 'required'], ]; } } diff --git a/tests/specs/issue_fix/153_nullable_false_in_required/app/models/base/Pristine.php b/tests/specs/issue_fix/153_nullable_false_in_required/app/models/base/Pristine.php index 8cc79e6f..58324717 100644 --- a/tests/specs/issue_fix/153_nullable_false_in_required/app/models/base/Pristine.php +++ b/tests/specs/issue_fix/153_nullable_false_in_required/app/models/base/Pristine.php @@ -19,9 +19,9 @@ public static function tableName() public function rules() { return [ - 'required' => [['billing_factor'], 'required'], 'billing_factor_integer' => [['billing_factor'], 'integer'], 'billing_factor_default' => [['billing_factor'], 'default', 'value' => 100], + 'required' => [['billing_factor'], 'required'], ]; } } diff --git a/tests/specs/issue_fix/158_bug_giiapi_generated_rules_enum_with_trim/maria/models/base/Mailing.php b/tests/specs/issue_fix/158_bug_giiapi_generated_rules_enum_with_trim/maria/models/base/Mailing.php index 2839c526..de55cf39 100644 --- a/tests/specs/issue_fix/158_bug_giiapi_generated_rules_enum_with_trim/maria/models/base/Mailing.php +++ b/tests/specs/issue_fix/158_bug_giiapi_generated_rules_enum_with_trim/maria/models/base/Mailing.php @@ -21,7 +21,6 @@ public function rules() { return [ 'trim' => [['name'], 'trim'], - 'required' => [['name'], 'required'], 'name_string' => [['name'], 'string', 'max' => 128], 'paymentMethodName_string' => [['paymentMethodName'], 'string'], 'paymentMethodName_in' => [['paymentMethodName'], 'in', 'range' => [ @@ -29,6 +28,7 @@ public function rules() 'cash', 'ewallet', ]], + 'required' => [['name'], 'required'], ]; } } diff --git a/tests/specs/issue_fix/159_bug_giiapi_generated_rules_emailid/maria/models/base/Contact.php b/tests/specs/issue_fix/159_bug_giiapi_generated_rules_emailid/maria/models/base/Contact.php index efbd8b32..2186badc 100644 --- a/tests/specs/issue_fix/159_bug_giiapi_generated_rules_emailid/maria/models/base/Contact.php +++ b/tests/specs/issue_fix/159_bug_giiapi_generated_rules_emailid/maria/models/base/Contact.php @@ -23,12 +23,12 @@ public function rules() { return [ 'trim' => [['nickname'], 'trim'], - 'required' => [['mailing_id'], 'required'], 'mailing_id_integer' => [['mailing_id'], 'integer'], 'mailing_id_exist' => [['mailing_id'], 'exist', 'targetRelation' => 'Mailing'], 'active_boolean' => [['active'], 'boolean'], 'active_default' => [['active'], 'default', 'value' => false], 'nickname_string' => [['nickname'], 'string'], + 'required' => [['mailing_id'], 'required'], ]; } 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 77049b23..5dcaec74 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 @@ -21,9 +21,9 @@ public function rules() { return [ 'trim' => [['name', 'paymentMethodName'], 'trim'], - 'required' => [['name'], 'required'], 'name_string' => [['name'], 'string', 'max' => 128], 'paymentMethodName_string' => [['paymentMethodName'], 'string'], + 'required' => [['name'], 'required'], ]; } } 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 f3dae5db..c671a556 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 @@ -21,9 +21,9 @@ public function rules() { return [ 'trim' => [['name', 'paymentMethodName'], 'trim'], - 'required' => [['name'], 'required'], 'name_string' => [['name'], 'string', 'max' => 128], 'paymentMethodName_string' => [['paymentMethodName'], 'string'], + 'required' => [['name'], 'required'], ]; } } diff --git a/tests/specs/issue_fix/175_bug_allof_with_multiple_dollarrefs/pgsql/models/base/Contact.php b/tests/specs/issue_fix/175_bug_allof_with_multiple_dollarrefs/pgsql/models/base/Contact.php index 21a2e7f1..c3a42a9a 100644 --- a/tests/specs/issue_fix/175_bug_allof_with_multiple_dollarrefs/pgsql/models/base/Contact.php +++ b/tests/specs/issue_fix/175_bug_allof_with_multiple_dollarrefs/pgsql/models/base/Contact.php @@ -23,12 +23,12 @@ public function rules() { return [ 'trim' => [['nickname'], 'trim'], - 'required' => [['account_id'], 'required'], 'account_id_integer' => [['account_id'], 'integer'], 'account_id_exist' => [['account_id'], 'exist', 'targetRelation' => 'Account'], 'active_boolean' => [['active'], 'boolean'], 'active_default' => [['active'], 'default', 'value' => false], 'nickname_string' => [['nickname'], 'string'], + 'required' => [['account_id'], 'required'], ]; } diff --git a/tests/specs/issue_fix/175_bug_allof_with_multiple_dollarrefs/pgsql/models/base/PaymentMethod.php b/tests/specs/issue_fix/175_bug_allof_with_multiple_dollarrefs/pgsql/models/base/PaymentMethod.php index 79e8c05b..6e18e8cd 100644 --- a/tests/specs/issue_fix/175_bug_allof_with_multiple_dollarrefs/pgsql/models/base/PaymentMethod.php +++ b/tests/specs/issue_fix/175_bug_allof_with_multiple_dollarrefs/pgsql/models/base/PaymentMethod.php @@ -20,9 +20,9 @@ public function rules() { return [ 'trim' => [['name'], 'trim'], - 'required' => [['name'], 'required'], 'name_unique' => [['name'], 'unique'], 'name_string' => [['name'], 'string', 'max' => 150], + 'required' => [['name'], 'required'], ]; } } diff --git a/tests/specs/issue_fix/22_bug_rules_required_is_generated_before_default/index.php b/tests/specs/issue_fix/22_bug_rules_required_is_generated_before_default/index.php new file mode 100644 index 00000000..ebfdd29d --- /dev/null +++ b/tests/specs/issue_fix/22_bug_rules_required_is_generated_before_default/index.php @@ -0,0 +1,13 @@ + '@specs/issue_fix/22_bug_rules_required_is_generated_before_default/index.yaml', + 'generateUrls' => false, + 'generateModels' => true, + 'excludeModels' => [ + 'Error', + ], + 'generateControllers' => false, + 'generateMigrations' => true, + 'generateModelFaker' => true, // `generateModels` must be `true` in order to use `generateModelFaker` as `true` +]; diff --git a/tests/specs/issue_fix/22_bug_rules_required_is_generated_before_default/index.yaml b/tests/specs/issue_fix/22_bug_rules_required_is_generated_before_default/index.yaml new file mode 100644 index 00000000..889ab5b3 --- /dev/null +++ b/tests/specs/issue_fix/22_bug_rules_required_is_generated_before_default/index.yaml @@ -0,0 +1,35 @@ +openapi: 3.0.3 + +info: + title: 'Bug: rules() "required" is generated before "*_default" #22' + version: 1.0.0 + +components: + schemas: + Account: + description: Account + type: object + required: + - id + - name + - verified + properties: + id: + type: integer + readOnly: true + name: + description: account name + type: string + maxLength: 128 + paymentMethodName: + type: string + verified: + type: boolean + default: false + +paths: + '/account': + get: + responses: + '200': + description: Account info 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 0ee4adc0..52b589e1 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 @@ -20,8 +20,8 @@ public function rules() { return [ 'trim' => [['name'], 'trim'], - 'required' => [['name'], 'required'], 'name_string' => [['name'], 'string', 'max' => 40], + 'required' => [['name'], 'required'], ]; } } diff --git a/tests/specs/many2many/models/base/Photo.php b/tests/specs/many2many/models/base/Photo.php index eb08fec7..b7d13e2e 100644 --- a/tests/specs/many2many/models/base/Photo.php +++ b/tests/specs/many2many/models/base/Photo.php @@ -26,8 +26,8 @@ public function rules() { return [ 'trim' => [['filename'], 'trim'], - 'required' => [['filename'], 'required'], 'filename_string' => [['filename'], 'string'], + 'required' => [['filename'], 'required'], ]; } diff --git a/tests/specs/many2many/models/base/Post.php b/tests/specs/many2many/models/base/Post.php index 6ecfc3ce..c9c37e06 100644 --- a/tests/specs/many2many/models/base/Post.php +++ b/tests/specs/many2many/models/base/Post.php @@ -27,8 +27,8 @@ public function rules() { return [ 'trim' => [['title'], 'trim'], - 'required' => [['title'], 'required'], 'title_string' => [['title'], 'string'], + 'required' => [['title'], 'required'], ]; } diff --git a/tests/specs/many2many/models/base/Tag.php b/tests/specs/many2many/models/base/Tag.php index 8d7cfc24..5f15a84c 100644 --- a/tests/specs/many2many/models/base/Tag.php +++ b/tests/specs/many2many/models/base/Tag.php @@ -21,8 +21,8 @@ public function rules() { return [ 'trim' => [['name'], 'trim'], - 'required' => [['name'], 'required'], 'name_string' => [['name'], 'string'], + 'required' => [['name'], 'required'], ]; } diff --git a/tests/specs/menu/models/base/Menu.php b/tests/specs/menu/models/base/Menu.php index 582d952d..f3e10612 100644 --- a/tests/specs/menu/models/base/Menu.php +++ b/tests/specs/menu/models/base/Menu.php @@ -25,7 +25,6 @@ public function rules() { return [ 'trim' => [['name'], 'trim'], - 'required' => [['name'], 'required'], 'parent_id_integer' => [['parent_id'], 'integer'], 'parent_id_exist' => [['parent_id'], 'exist', 'targetRelation' => 'Parent'], 'name_string' => [['name'], 'string', 'min' => 3, 'max' => 100], @@ -43,6 +42,7 @@ public function rules() ], ]], 'safe' => [['args', 'kwargs'], 'safe'], + 'required' => [['name'], 'required'], ]; } diff --git a/tests/specs/petstore/models/base/Pet.php b/tests/specs/petstore/models/base/Pet.php index 039764d2..27566a9b 100644 --- a/tests/specs/petstore/models/base/Pet.php +++ b/tests/specs/petstore/models/base/Pet.php @@ -23,11 +23,11 @@ public function rules() { return [ 'trim' => [['name', 'tag'], 'trim'], - 'required' => [['name'], 'required'], 'store_id_integer' => [['store_id'], 'integer'], 'store_id_exist' => [['store_id'], 'exist', 'targetRelation' => 'Store'], 'name_string' => [['name'], 'string'], 'tag_string' => [['tag'], 'string'], + 'required' => [['name'], 'required'], ]; } diff --git a/tests/specs/petstore/models/base/Store.php b/tests/specs/petstore/models/base/Store.php index b1f6bc0f..432fed31 100644 --- a/tests/specs/petstore/models/base/Store.php +++ b/tests/specs/petstore/models/base/Store.php @@ -20,8 +20,8 @@ public function rules() { return [ 'trim' => [['name'], 'trim'], - 'required' => [['name'], 'required'], 'name_string' => [['name'], 'string'], + 'required' => [['name'], 'required'], ]; } } diff --git a/tests/specs/petstore_arrayref/models/base/Pet.php b/tests/specs/petstore_arrayref/models/base/Pet.php index 660237dd..6e67994d 100644 --- a/tests/specs/petstore_arrayref/models/base/Pet.php +++ b/tests/specs/petstore_arrayref/models/base/Pet.php @@ -22,9 +22,9 @@ public function rules() { return [ 'trim' => [['name', 'tag'], 'trim'], - 'required' => [['name'], 'required'], 'name_string' => [['name'], 'string'], 'tag_string' => [['tag'], 'string'], + 'required' => [['name'], 'required'], ]; } diff --git a/tests/specs/petstore_jsonapi/models/base/Doctor.php b/tests/specs/petstore_jsonapi/models/base/Doctor.php index b13bdaa5..32f6ca70 100644 --- a/tests/specs/petstore_jsonapi/models/base/Doctor.php +++ b/tests/specs/petstore_jsonapi/models/base/Doctor.php @@ -29,10 +29,10 @@ public function rules() { return [ 'trim' => [['name', 'surname'], 'trim'], - 'required' => [['name'], 'required'], 'name_string' => [['name'], 'string', 'max' => 200], 'surname_string' => [['surname'], 'string', 'max' => 200], 'safe' => [['phones'], 'safe'], + 'required' => [['name'], 'required'], ]; } } diff --git a/tests/specs/petstore_jsonapi/models/base/Pet.php b/tests/specs/petstore_jsonapi/models/base/Pet.php index f0e3138a..e75f31ce 100644 --- a/tests/specs/petstore_jsonapi/models/base/Pet.php +++ b/tests/specs/petstore_jsonapi/models/base/Pet.php @@ -48,10 +48,10 @@ public function rules() { return [ 'trim' => [['name', 'tag', 'petCode'], 'trim'], - 'required' => [['name'], 'required'], 'name_string' => [['name'], 'string'], 'tag_string' => [['tag'], 'string'], 'petCode_string' => [['petCode'], 'string', 'max' => 50], + 'required' => [['name'], 'required'], ]; } diff --git a/tests/specs/petstore_namespace/mymodels/base/Pet.php b/tests/specs/petstore_namespace/mymodels/base/Pet.php index 58750645..9a6be041 100644 --- a/tests/specs/petstore_namespace/mymodels/base/Pet.php +++ b/tests/specs/petstore_namespace/mymodels/base/Pet.php @@ -23,11 +23,11 @@ public function rules() { return [ 'trim' => [['name', 'tag'], 'trim'], - 'required' => [['name'], 'required'], 'store_id_integer' => [['store_id'], 'integer'], 'store_id_exist' => [['store_id'], 'exist', 'targetRelation' => 'Store'], 'name_string' => [['name'], 'string'], 'tag_string' => [['tag'], 'string'], + 'required' => [['name'], 'required'], ]; } diff --git a/tests/specs/petstore_namespace/mymodels/base/Store.php b/tests/specs/petstore_namespace/mymodels/base/Store.php index 6ecc3e47..ecce86b6 100644 --- a/tests/specs/petstore_namespace/mymodels/base/Store.php +++ b/tests/specs/petstore_namespace/mymodels/base/Store.php @@ -20,8 +20,8 @@ public function rules() { return [ 'trim' => [['name'], 'trim'], - 'required' => [['name'], 'required'], 'name_string' => [['name'], 'string'], + 'required' => [['name'], 'required'], ]; } } diff --git a/tests/specs/petstore_wrapped/models/base/Pet.php b/tests/specs/petstore_wrapped/models/base/Pet.php index fc84b808..fadf9d48 100644 --- a/tests/specs/petstore_wrapped/models/base/Pet.php +++ b/tests/specs/petstore_wrapped/models/base/Pet.php @@ -21,9 +21,9 @@ public function rules() { return [ 'trim' => [['name', 'tag'], 'trim'], - 'required' => [['name'], 'required'], 'name_string' => [['name'], 'string'], 'tag_string' => [['tag'], 'string'], + 'required' => [['name'], 'required'], ]; } } diff --git a/tests/specs/petstore_xtable/models/base/Pet.php b/tests/specs/petstore_xtable/models/base/Pet.php index fc84b808..fadf9d48 100644 --- a/tests/specs/petstore_xtable/models/base/Pet.php +++ b/tests/specs/petstore_xtable/models/base/Pet.php @@ -21,9 +21,9 @@ public function rules() { return [ 'trim' => [['name', 'tag'], 'trim'], - 'required' => [['name'], 'required'], 'name_string' => [['name'], 'string'], 'tag_string' => [['tag'], 'string'], + 'required' => [['name'], 'required'], ]; } } 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 0ee4adc0..52b589e1 100644 --- a/tests/specs/relations_in_faker/app/models/base/Account.php +++ b/tests/specs/relations_in_faker/app/models/base/Account.php @@ -20,8 +20,8 @@ public function rules() { return [ 'trim' => [['name'], 'trim'], - 'required' => [['name'], 'required'], 'name_string' => [['name'], 'string', 'max' => 40], + 'required' => [['name'], 'required'], ]; } } 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 3f861f9e..c51b5163 100644 --- a/tests/specs/relations_in_faker/app/models/base/Domain.php +++ b/tests/specs/relations_in_faker/app/models/base/Domain.php @@ -24,10 +24,10 @@ public function rules() { return [ 'trim' => [['name'], 'trim'], - 'required' => [['name', 'account_id'], 'required'], 'account_id_integer' => [['account_id'], 'integer'], 'account_id_exist' => [['account_id'], 'exist', 'targetRelation' => 'Account'], 'name_string' => [['name'], 'string', 'max' => 128], + 'required' => [['name', 'account_id'], 'required'], ]; } diff --git a/tests/specs/relations_in_faker/app/models/base/Routing.php b/tests/specs/relations_in_faker/app/models/base/Routing.php index 71a31b81..1731d234 100644 --- a/tests/specs/relations_in_faker/app/models/base/Routing.php +++ b/tests/specs/relations_in_faker/app/models/base/Routing.php @@ -30,7 +30,6 @@ public function rules() { return [ 'trim' => [['path', 'service'], 'trim'], - 'required' => [['domain_id'], 'required'], 'domain_id_integer' => [['domain_id'], 'integer'], 'domain_id_exist' => [['domain_id'], 'exist', 'targetRelation' => 'Domain'], 'd123_id_integer' => [['d123_id'], 'integer'], @@ -41,6 +40,7 @@ public function rules() 'ssl_boolean' => [['ssl'], 'boolean'], 'redirect_to_ssl_boolean' => [['redirect_to_ssl'], 'boolean'], 'service_string' => [['service'], 'string', 'max' => 255], + 'required' => [['domain_id'], 'required'], ]; } diff --git a/tests/specs/x_db_type/rules_and_more/maria/app/models/mariamodel/base/Alldbdatatype.php b/tests/specs/x_db_type/rules_and_more/maria/app/models/mariamodel/base/Alldbdatatype.php index 6a35d601..0077fb3a 100644 --- a/tests/specs/x_db_type/rules_and_more/maria/app/models/mariamodel/base/Alldbdatatype.php +++ b/tests/specs/x_db_type/rules_and_more/maria/app/models/mariamodel/base/Alldbdatatype.php @@ -62,7 +62,6 @@ public function rules() { return [ 'trim' => [['string_col', 'varchar_col', 'text_col', 'varchar_4_col', 'char_4_col', 'char_5_col', 'char_6_col', 'char_7_col', 'char_8_col', 'date_col', 'time_col', 'datetime_col', 'timestamp_col', 'year_col', 'text_def'], 'trim'], - 'required' => [['char_6_col', 'char_7_col'], 'required'], 'string_col_string' => [['string_col'], 'string', 'max' => 255], 'varchar_col_string' => [['varchar_col'], 'string', 'max' => 132], 'text_col_string' => [['text_col'], 'string'], @@ -109,6 +108,7 @@ public function rules() 'a' => 'b', ]], 'safe' => [['varbinary_col', 'blob_col', 'json_col', 'json_col_def', 'json_col_def_2', 'blob_def', 'json_def'], 'safe'], + 'required' => [['char_6_col', 'char_7_col'], 'required'], ]; } } diff --git a/tests/specs/x_db_type/rules_and_more/maria/app/models/mariamodel/base/Editcolumn.php b/tests/specs/x_db_type/rules_and_more/maria/app/models/mariamodel/base/Editcolumn.php index 62f98624..1c49081a 100644 --- a/tests/specs/x_db_type/rules_and_more/maria/app/models/mariamodel/base/Editcolumn.php +++ b/tests/specs/x_db_type/rules_and_more/maria/app/models/mariamodel/base/Editcolumn.php @@ -30,7 +30,6 @@ public function rules() { return [ 'trim' => [['name', 'tag', 'first_name', 'string_col', 'str_col_def', 'json_col'], 'trim'], - 'required' => [['name', 'str_col_def', 'json_col', 'json_col_2'], 'required'], 'name_string' => [['name'], 'string', 'max' => 254], 'name_default' => [['name'], 'default', 'value' => 'Horse-2'], 'tag_string' => [['tag'], 'string'], @@ -46,6 +45,7 @@ public function rules() 'json_col_def_n_default' => [['json_col_def_n'], 'default', 'value' => []], 'json_col_def_n_2_default' => [['json_col_def_n_2'], 'default', 'value' => []], 'safe' => [['json_col_2', 'json_col_def_n', 'json_col_def_n_2'], 'safe'], + 'required' => [['name', 'str_col_def', 'json_col', 'json_col_2'], 'required'], ]; } } diff --git a/tests/specs/x_db_type/rules_and_more/maria/app/models/mariamodel/base/Newcolumn.php b/tests/specs/x_db_type/rules_and_more/maria/app/models/mariamodel/base/Newcolumn.php index 15da0af8..4df93c0c 100644 --- a/tests/specs/x_db_type/rules_and_more/maria/app/models/mariamodel/base/Newcolumn.php +++ b/tests/specs/x_db_type/rules_and_more/maria/app/models/mariamodel/base/Newcolumn.php @@ -26,7 +26,6 @@ public function rules() { return [ 'trim' => [['name', 'last_name', 'varchar_col'], 'trim'], - 'required' => [['name'], 'required'], 'name_string' => [['name'], 'string', 'max' => 255], 'last_name_string' => [['last_name'], 'string'], 'dec_col_double' => [['dec_col'], 'double'], @@ -34,6 +33,7 @@ public function rules() 'numeric_col_double' => [['numeric_col'], 'double'], 'json_col_def_n_default' => [['json_col_def_n'], 'default', 'value' => []], 'safe' => [['json_col', 'json_col_def_n'], 'safe'], + 'required' => [['name'], 'required'], ]; } } diff --git a/tests/specs/x_db_type/rules_and_more/maria/app/models/mariamodel/base/Pristine.php b/tests/specs/x_db_type/rules_and_more/maria/app/models/mariamodel/base/Pristine.php index a5d7c1ef..a8ce93d2 100644 --- a/tests/specs/x_db_type/rules_and_more/maria/app/models/mariamodel/base/Pristine.php +++ b/tests/specs/x_db_type/rules_and_more/maria/app/models/mariamodel/base/Pristine.php @@ -30,7 +30,6 @@ public function rules() { return [ 'trim' => [['name', 'tag', 'new_col', 'col_9', 'col_10', 'col_11'], 'trim'], - 'required' => [['custom_id_col', 'name'], 'required'], 'custom_id_col_integer' => [['custom_id_col'], 'integer'], 'name_string' => [['name'], 'string'], 'tag_string' => [['tag'], 'string'], @@ -45,6 +44,7 @@ public function rules() 'price_double' => [['price'], 'double'], 'price_default' => [['price'], 'default', 'value' => 0], 'safe' => [['col_8'], 'safe'], + 'required' => [['custom_id_col', 'name'], 'required'], ]; } } diff --git a/tests/specs/x_db_type/rules_and_more/mysql/app/models/base/Alldbdatatype.php b/tests/specs/x_db_type/rules_and_more/mysql/app/models/base/Alldbdatatype.php index 41326222..02e39bc6 100644 --- a/tests/specs/x_db_type/rules_and_more/mysql/app/models/base/Alldbdatatype.php +++ b/tests/specs/x_db_type/rules_and_more/mysql/app/models/base/Alldbdatatype.php @@ -62,7 +62,6 @@ public function rules() { return [ 'trim' => [['string_col', 'varchar_col', 'text_col', 'varchar_4_col', 'char_4_col', 'char_5_col', 'char_6_col', 'char_7_col', 'char_8_col', 'date_col', 'time_col', 'datetime_col', 'timestamp_col', 'year_col', 'text_def'], 'trim'], - 'required' => [['char_6_col', 'char_7_col'], 'required'], 'string_col_string' => [['string_col'], 'string', 'max' => 255], 'varchar_col_string' => [['varchar_col'], 'string', 'max' => 132], 'text_col_string' => [['text_col'], 'string'], @@ -109,6 +108,7 @@ public function rules() 'a' => 'b', ]], 'safe' => [['varbinary_col', 'blob_col', 'json_col', 'json_col_def', 'json_col_def_2', 'blob_def', 'json_def'], 'safe'], + 'required' => [['char_6_col', 'char_7_col'], 'required'], ]; } } diff --git a/tests/specs/x_db_type/rules_and_more/mysql/app/models/base/Editcolumn.php b/tests/specs/x_db_type/rules_and_more/mysql/app/models/base/Editcolumn.php index 2b630e67..677d16a9 100644 --- a/tests/specs/x_db_type/rules_and_more/mysql/app/models/base/Editcolumn.php +++ b/tests/specs/x_db_type/rules_and_more/mysql/app/models/base/Editcolumn.php @@ -30,7 +30,6 @@ public function rules() { return [ 'trim' => [['name', 'tag', 'first_name', 'string_col', 'str_col_def', 'json_col'], 'trim'], - 'required' => [['name', 'str_col_def', 'json_col', 'json_col_2'], 'required'], 'name_string' => [['name'], 'string', 'max' => 254], 'name_default' => [['name'], 'default', 'value' => 'Horse-2'], 'tag_string' => [['tag'], 'string'], @@ -46,6 +45,7 @@ public function rules() 'json_col_def_n_default' => [['json_col_def_n'], 'default', 'value' => []], 'json_col_def_n_2_default' => [['json_col_def_n_2'], 'default', 'value' => []], 'safe' => [['json_col_2', 'json_col_def_n', 'json_col_def_n_2'], 'safe'], + 'required' => [['name', 'str_col_def', 'json_col', 'json_col_2'], 'required'], ]; } } diff --git a/tests/specs/x_db_type/rules_and_more/mysql/app/models/base/Newcolumn.php b/tests/specs/x_db_type/rules_and_more/mysql/app/models/base/Newcolumn.php index 186ef3fe..0569715e 100644 --- a/tests/specs/x_db_type/rules_and_more/mysql/app/models/base/Newcolumn.php +++ b/tests/specs/x_db_type/rules_and_more/mysql/app/models/base/Newcolumn.php @@ -26,7 +26,6 @@ public function rules() { return [ 'trim' => [['name', 'last_name', 'varchar_col'], 'trim'], - 'required' => [['name'], 'required'], 'name_string' => [['name'], 'string', 'max' => 255], 'last_name_string' => [['last_name'], 'string'], 'dec_col_double' => [['dec_col'], 'double'], @@ -34,6 +33,7 @@ public function rules() 'numeric_col_double' => [['numeric_col'], 'double'], 'json_col_def_n_default' => [['json_col_def_n'], 'default', 'value' => []], 'safe' => [['json_col', 'json_col_def_n'], 'safe'], + 'required' => [['name'], 'required'], ]; } } diff --git a/tests/specs/x_db_type/rules_and_more/mysql/app/models/base/Pristine.php b/tests/specs/x_db_type/rules_and_more/mysql/app/models/base/Pristine.php index 87c54d6c..891a4ef9 100644 --- a/tests/specs/x_db_type/rules_and_more/mysql/app/models/base/Pristine.php +++ b/tests/specs/x_db_type/rules_and_more/mysql/app/models/base/Pristine.php @@ -30,7 +30,6 @@ public function rules() { return [ 'trim' => [['name', 'tag', 'new_col', 'col_9', 'col_10', 'col_11'], 'trim'], - 'required' => [['custom_id_col', 'name'], 'required'], 'custom_id_col_integer' => [['custom_id_col'], 'integer'], 'name_string' => [['name'], 'string'], 'tag_string' => [['tag'], 'string'], @@ -45,6 +44,7 @@ public function rules() 'price_double' => [['price'], 'double'], 'price_default' => [['price'], 'default', 'value' => 0], 'safe' => [['col_8'], 'safe'], + 'required' => [['custom_id_col', 'name'], 'required'], ]; } } diff --git a/tests/specs/x_db_type/rules_and_more/pgsql/app/models/pgsqlmodel/base/Alldbdatatype.php b/tests/specs/x_db_type/rules_and_more/pgsql/app/models/pgsqlmodel/base/Alldbdatatype.php index 5311af96..74c9dc17 100644 --- a/tests/specs/x_db_type/rules_and_more/pgsql/app/models/pgsqlmodel/base/Alldbdatatype.php +++ b/tests/specs/x_db_type/rules_and_more/pgsql/app/models/pgsqlmodel/base/Alldbdatatype.php @@ -112,7 +112,6 @@ public function rules() { return [ 'trim' => [['string_col', 'varchar_col', 'text_col', 'varchar_4_col', 'varchar_5_col', 'char_4_col', 'char_5_col', 'char_6_col', 'char_7_col', 'char_8_col', 'date_col', 'time_col', 'time_col_2', 'time_col_3', 'time_col_4', 'timetz_col', 'timetz_col_2', 'timestamp_col', 'timestamp_col_2', 'timestamp_col_3', 'timestamp_col_4', 'timestamptz_col', 'timestamptz_col_2', 'date2', 'timestamp_col_z', 'box_col', 'character_col', 'character_n', 'character_varying', 'character_varying_n', 'text_def', 'cidr_col', 'circle_col', 'date_col_z', 'inet_col', 'interval_col', 'interval_col_2', 'interval_col_3', 'line_col', 'lseg_col', 'macaddr_col', 'money_col', 'path_col', 'point_col', 'polygon_col', 'tsquery_col', 'tsvector_col', 'txid_snapshot_col', 'uuid_col', 'xml_col'], 'trim'], - 'required' => [['char_6_col', 'char_7_col', 'smallserial_col', 'serial2_col', 'bigserial_col', 'bigserial_col_2', 'serial_col', 'serial4_col'], 'required'], 'string_col_string' => [['string_col'], 'string'], 'varchar_col_string' => [['varchar_col'], 'string'], 'text_col_string' => [['text_col'], 'string'], @@ -209,6 +208,7 @@ public function rules() 'uuid_col_string' => [['uuid_col'], 'string'], 'xml_col_string' => [['xml_col'], 'string'], 'safe' => [['text_col_array', 'bytea_col_2', 'json_col', 'jsonb_col', 'json_col_def', 'json_col_def_2', 'bytea_def', 'json_def', 'jsonb_def'], 'safe'], + 'required' => [['char_6_col', 'char_7_col', 'smallserial_col', 'serial2_col', 'bigserial_col', 'bigserial_col_2', 'serial_col', 'serial4_col'], 'required'], ]; } } diff --git a/tests/specs/x_db_type/rules_and_more/pgsql/app/models/pgsqlmodel/base/Editcolumn.php b/tests/specs/x_db_type/rules_and_more/pgsql/app/models/pgsqlmodel/base/Editcolumn.php index 730bff5a..94500d7c 100644 --- a/tests/specs/x_db_type/rules_and_more/pgsql/app/models/pgsqlmodel/base/Editcolumn.php +++ b/tests/specs/x_db_type/rules_and_more/pgsql/app/models/pgsqlmodel/base/Editcolumn.php @@ -31,7 +31,6 @@ public function rules() { return [ 'trim' => [['name', 'tag', 'first_name', 'string_col', 'str_col_def', 'json_col'], 'trim'], - 'required' => [['name', 'str_col_def', 'json_col', 'json_col_2'], 'required'], 'name_string' => [['name'], 'string', 'max' => 254], 'name_default' => [['name'], 'default', 'value' => 'Horse-2'], 'tag_string' => [['tag'], 'string'], @@ -47,6 +46,7 @@ public function rules() 'json_col_def_n_default' => [['json_col_def_n'], 'default', 'value' => []], 'json_col_def_n_2_default' => [['json_col_def_n_2'], 'default', 'value' => []], 'safe' => [['json_col_2', 'json_col_def_n', 'json_col_def_n_2', 'text_col_array'], 'safe'], + 'required' => [['name', 'str_col_def', 'json_col', 'json_col_2'], 'required'], ]; } } diff --git a/tests/specs/x_db_type/rules_and_more/pgsql/app/models/pgsqlmodel/base/Newcolumn.php b/tests/specs/x_db_type/rules_and_more/pgsql/app/models/pgsqlmodel/base/Newcolumn.php index 09b54731..35cf841e 100644 --- a/tests/specs/x_db_type/rules_and_more/pgsql/app/models/pgsqlmodel/base/Newcolumn.php +++ b/tests/specs/x_db_type/rules_and_more/pgsql/app/models/pgsqlmodel/base/Newcolumn.php @@ -29,7 +29,6 @@ public function rules() { return [ 'trim' => [['name', 'first_name', 'last_name', 'varchar_col'], 'trim'], - 'required' => [['name'], 'required'], 'name_string' => [['name'], 'string'], 'first_name_string' => [['first_name'], 'string'], 'last_name_string' => [['last_name'], 'string'], @@ -39,6 +38,7 @@ public function rules() 'json_col_def_n_default' => [['json_col_def_n'], 'default', 'value' => []], 'json_col_def_n_2_default' => [['json_col_def_n_2'], 'default', 'value' => []], 'safe' => [['json_col', 'json_col_def_n', 'json_col_def_n_2', 'text_col_array'], 'safe'], + 'required' => [['name'], 'required'], ]; } } diff --git a/tests/specs/x_db_type/rules_and_more/pgsql/app/models/pgsqlmodel/base/Pristine.php b/tests/specs/x_db_type/rules_and_more/pgsql/app/models/pgsqlmodel/base/Pristine.php index c24d65b9..430adcec 100644 --- a/tests/specs/x_db_type/rules_and_more/pgsql/app/models/pgsqlmodel/base/Pristine.php +++ b/tests/specs/x_db_type/rules_and_more/pgsql/app/models/pgsqlmodel/base/Pristine.php @@ -30,7 +30,6 @@ public function rules() { return [ 'trim' => [['name', 'tag', 'new_col', 'col_9', 'col_10', 'col_11'], 'trim'], - 'required' => [['custom_id_col', 'name'], 'required'], 'custom_id_col_integer' => [['custom_id_col'], 'integer'], 'name_string' => [['name'], 'string'], 'tag_string' => [['tag'], 'string'], @@ -45,6 +44,7 @@ public function rules() 'price_double' => [['price'], 'double'], 'price_default' => [['price'], 'default', 'value' => 0], 'safe' => [['col_8'], 'safe'], + 'required' => [['custom_id_col', 'name'], 'required'], ]; } } diff --git a/tests/unit/IssueFixTest.php b/tests/unit/IssueFixTest.php index b6c7abdb..eeeeffad 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/22 + public function test22BugRulesRequiredIsGeneratedBeforeDefault() + { + $testFile = Yii::getAlias("@specs/issue_fix/22_bug_rules_required_is_generated_before_default/index.php"); + $this->runGenerator($testFile); + // $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [ + // 'recursive' => true, + // ]); + // $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/22_bug_rules_required_is_generated_before_default/mysql"), [ + // 'recursive' => true, + // ]); + // $this->checkFiles($actualFiles, $expectedFiles); + } } From 1beb4d67e30375c09d47b27c0d28e226bb6317df Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Sun, 18 Aug 2024 19:41:54 +0530 Subject: [PATCH 3/4] Complete test --- TODO.taskpaper | 2 +- tests/DbTestCase.php | 1 - .../m200000_000000_create_table_accounts.php | 22 +++ .../mysql/models/Account.php | 10 ++ .../mysql/models/AccountFaker.php | 43 ++++++ .../mysql/models/BaseModelFaker.php | 144 ++++++++++++++++++ .../mysql/models/base/Account.php | 32 ++++ tests/unit/IssueFixTest.php | 14 +- 8 files changed, 259 insertions(+), 9 deletions(-) create mode 100644 tests/specs/issue_fix/22_bug_rules_required_is_generated_before_default/mysql/migrations_mysql_db/m200000_000000_create_table_accounts.php create mode 100644 tests/specs/issue_fix/22_bug_rules_required_is_generated_before_default/mysql/models/Account.php create mode 100644 tests/specs/issue_fix/22_bug_rules_required_is_generated_before_default/mysql/models/AccountFaker.php create mode 100644 tests/specs/issue_fix/22_bug_rules_required_is_generated_before_default/mysql/models/BaseModelFaker.php create mode 100644 tests/specs/issue_fix/22_bug_rules_required_is_generated_before_default/mysql/models/base/Account.php diff --git a/TODO.taskpaper b/TODO.taskpaper index 6fc7c133..54e63549 100644 --- a/TODO.taskpaper +++ b/TODO.taskpaper @@ -3,7 +3,7 @@ TODO.taskpaper ### Bug: rules() "required" is generated before "*_default" #22 ✔ create failing test @done (24-08-18 19:21) ✔ implement the solution @done (24-08-18 19:21) - ☐ fix failing tests if any + ✔ fix failing tests if any @done (24-08-18 19:39) ☐ resolve TODOs if any ☐ review PR ☐ delete this file and submit PR diff --git a/tests/DbTestCase.php b/tests/DbTestCase.php index c28a314c..d3e466aa 100644 --- a/tests/DbTestCase.php +++ b/tests/DbTestCase.php @@ -101,7 +101,6 @@ protected function compareFiles(array $actual, string $testFile) foreach ($actual as $file) { $expectedFile = str_replace('@app', substr($testFile, 0, -4), $file); $actualFile = str_replace('@app', Yii::getAlias('@app'), $file); - // exec('cp '.$actualFile.' '.$expectedFile); $this->checkFiles([$actualFile], [$expectedFile]); } } diff --git a/tests/specs/issue_fix/22_bug_rules_required_is_generated_before_default/mysql/migrations_mysql_db/m200000_000000_create_table_accounts.php b/tests/specs/issue_fix/22_bug_rules_required_is_generated_before_default/mysql/migrations_mysql_db/m200000_000000_create_table_accounts.php new file mode 100644 index 00000000..e59c2946 --- /dev/null +++ b/tests/specs/issue_fix/22_bug_rules_required_is_generated_before_default/mysql/migrations_mysql_db/m200000_000000_create_table_accounts.php @@ -0,0 +1,22 @@ +createTable('{{%accounts}}', [ + 'id' => $this->primaryKey(), + 'name' => $this->string(128)->notNull(), + 'paymentMethodName' => $this->text()->null(), + 'verified' => $this->boolean()->notNull()->defaultValue(false), + ]); + } + + public function down() + { + $this->dropTable('{{%accounts}}'); + } +} diff --git a/tests/specs/issue_fix/22_bug_rules_required_is_generated_before_default/mysql/models/Account.php b/tests/specs/issue_fix/22_bug_rules_required_is_generated_before_default/mysql/models/Account.php new file mode 100644 index 00000000..2d25d7fc --- /dev/null +++ b/tests/specs/issue_fix/22_bug_rules_required_is_generated_before_default/mysql/models/Account.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 Account(); + //$model->id = $uniqueFaker->numberBetween(0, 1000000); + $model->name = substr($faker->text(128), 0, 128); + $model->paymentMethodName = $faker->sentence; + $model->verified = $faker->boolean; + if (!is_callable($attributes)) { + $model->setAttributes($attributes, false); + } else { + $model = $attributes($model, $faker, $uniqueFaker); + } + return $model; + } +} diff --git a/tests/specs/issue_fix/22_bug_rules_required_is_generated_before_default/mysql/models/BaseModelFaker.php b/tests/specs/issue_fix/22_bug_rules_required_is_generated_before_default/mysql/models/BaseModelFaker.php new file mode 100644 index 00000000..c367fbb4 --- /dev/null +++ b/tests/specs/issue_fix/22_bug_rules_required_is_generated_before_default/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/22_bug_rules_required_is_generated_before_default/mysql/models/base/Account.php b/tests/specs/issue_fix/22_bug_rules_required_is_generated_before_default/mysql/models/base/Account.php new file mode 100644 index 00000000..96b515cf --- /dev/null +++ b/tests/specs/issue_fix/22_bug_rules_required_is_generated_before_default/mysql/models/base/Account.php @@ -0,0 +1,32 @@ + [['name', 'paymentMethodName'], 'trim'], + 'name_string' => [['name'], 'string', 'max' => 128], + 'paymentMethodName_string' => [['paymentMethodName'], 'string'], + 'verified_boolean' => [['verified'], 'boolean'], + 'verified_default' => [['verified'], 'default', 'value' => false], + 'required' => [['name', 'verified'], 'required'], + ]; + } +} diff --git a/tests/unit/IssueFixTest.php b/tests/unit/IssueFixTest.php index eeeeffad..30968b32 100644 --- a/tests/unit/IssueFixTest.php +++ b/tests/unit/IssueFixTest.php @@ -366,12 +366,12 @@ public function test22BugRulesRequiredIsGeneratedBeforeDefault() { $testFile = Yii::getAlias("@specs/issue_fix/22_bug_rules_required_is_generated_before_default/index.php"); $this->runGenerator($testFile); - // $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [ - // 'recursive' => true, - // ]); - // $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/22_bug_rules_required_is_generated_before_default/mysql"), [ - // 'recursive' => true, - // ]); - // $this->checkFiles($actualFiles, $expectedFiles); + $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [ + 'recursive' => true, + ]); + $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/22_bug_rules_required_is_generated_before_default/mysql"), [ + 'recursive' => true, + ]); + $this->checkFiles($actualFiles, $expectedFiles); } } From 99e51928a881be53e20ed1c423b399233b86f77a Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Sun, 18 Aug 2024 19:44:10 +0530 Subject: [PATCH 4/4] Delete TODO.taskpaper file --- TODO.taskpaper | 9 --------- 1 file changed, 9 deletions(-) delete mode 100644 TODO.taskpaper diff --git a/TODO.taskpaper b/TODO.taskpaper deleted file mode 100644 index 54e63549..00000000 --- a/TODO.taskpaper +++ /dev/null @@ -1,9 +0,0 @@ -TODO.taskpaper - -### Bug: rules() "required" is generated before "*_default" #22 - ✔ create failing test @done (24-08-18 19:21) - ✔ implement the solution @done (24-08-18 19:21) - ✔ fix failing tests if any @done (24-08-18 19:39) - ☐ resolve TODOs if any - ☐ review PR - ☐ delete this file and submit PR