From 00f742ba23b95624f6a7573a19a524330c70e6e1 Mon Sep 17 00:00:00 2001 From: Nathan E Date: Fri, 22 May 2020 12:50:15 -0400 Subject: [PATCH 1/4] feat: add support for rememberToken() shorthand - Add the shorthand to `Blueprint::parse` - Update the `MigrationGenerator` for `rememberToken` - Update the `FactoryGenerator` for `remember_token` columns - Update the `ModelGenerator` to hide the attribute Closes #228 --- src/Generators/FactoryGenerator.php | 25 ++++++++++++ src/Generators/MigrationGenerator.php | 2 + src/Generators/ModelGenerator.php | 1 + stubs/factory.stub | 2 +- .../Generator/FactoryGeneratorTest.php | 5 ++- .../Generator/MigrationGeneratorTest.php | 1 + .../Feature/Generator/ModelGeneratorTest.php | 3 +- .../factories/resource-statements.php | 16 ++++++++ .../migrations/resource-statements.php | 35 +++++++++++++++++ tests/fixtures/models/resource-statements.php | 38 +++++++++++++++++++ 10 files changed, 124 insertions(+), 4 deletions(-) create mode 100644 tests/fixtures/factories/resource-statements.php create mode 100644 tests/fixtures/migrations/resource-statements.php create mode 100644 tests/fixtures/models/resource-statements.php diff --git a/src/Generators/FactoryGenerator.php b/src/Generators/FactoryGenerator.php index 65e35f5e..2e3c7c81 100644 --- a/src/Generators/FactoryGenerator.php +++ b/src/Generators/FactoryGenerator.php @@ -14,6 +14,8 @@ class FactoryGenerator implements Generator /** @var \Illuminate\Contracts\Filesystem\Filesystem */ private $files; + private $imports = []; + public function __construct($files) { $this->files = $files; @@ -27,6 +29,8 @@ public function output(array $tree): array /** @var \Blueprint\Models\Model $model */ foreach ($tree['models'] as $model) { + $this->addImport($model, 'Faker\Generator as Faker'); + $path = $this->getPath($model); if (!$this->files->exists(dirname($path))) { @@ -56,6 +60,7 @@ protected function populateStub(string $stub, Model $model) $stub = str_replace('DummyModel', $model->fullyQualifiedClassName(), $stub); $stub = str_replace('DummyClass', $model->name(), $stub); $stub = str_replace('// definition...', $this->buildDefinition($model), $stub); + $stub = str_replace('// imports...', $this->buildImports($model), $stub); return $stub; } @@ -144,6 +149,11 @@ protected function buildDefinition(Model $model) } $definition .= sprintf('%s%s => $faker->%s,%s', self::INDENT, "'{$column->name()}_id'", self::fakerDataType('id'), PHP_EOL); $definition .= sprintf('%s%s => $faker->%s,%s', self::INDENT, "'{$column->name()}_type'", self::fakerDataType('string'), PHP_EOL); + } elseif ($column->dataType() === 'rememberToken') { + $this->addImport($model, 'Illuminate\Support\Str'); + $definition .= self::INDENT . "'{$column->name()}' => "; + $definition .= 'Str::random(10)'; + $definition .= ',' . PHP_EOL; } else { $definition .= self::INDENT . "'{$column->name()}' => "; $faker = self::fakerData($column->name()) ?? self::fakerDataType($column->dataType()); @@ -155,6 +165,21 @@ protected function buildDefinition(Model $model) return trim($definition); } + private function addImport(Model $model, $class) + { + $this->imports[$model->name()][] = $class; + } + + private function buildImports(Model $model) + { + $imports = array_unique($this->imports[$model->name()]); + sort($imports); + + return implode(PHP_EOL, array_map(function ($class) { + return 'use ' . $class . ';'; + }, $imports)); + } + private function fillableColumns(array $columns): array { if (config('blueprint.fake_nullables')) { diff --git a/src/Generators/MigrationGenerator.php b/src/Generators/MigrationGenerator.php index 7e84bf8b..f0f98bb5 100644 --- a/src/Generators/MigrationGenerator.php +++ b/src/Generators/MigrationGenerator.php @@ -111,6 +111,8 @@ protected function buildDefinition(Model $model) $column_definition = self::INDENT; if ($dataType === 'bigIncrements' && $this->isLaravel7orNewer()) { $column_definition .= '$table->id('; + } elseif ($dataType === 'rememberToken') { + $column_definition .= '$table->rememberToken('; } else { $column_definition .= '$table->' . $dataType . "('{$column->name()}'"; } diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php index 99b00f1e..eb26eff5 100644 --- a/src/Generators/ModelGenerator.php +++ b/src/Generators/ModelGenerator.php @@ -204,6 +204,7 @@ private function fillableColumns(array $columns) 'deleted_at', 'created_at', 'updated_at', + 'remember_token', ]); } diff --git a/stubs/factory.stub b/stubs/factory.stub index cb4f3803..46c698d7 100644 --- a/stubs/factory.stub +++ b/stubs/factory.stub @@ -3,7 +3,7 @@ /** @var \Illuminate\Database\Eloquent\Factory $factory */ use DummyModel; -use Faker\Generator as Faker; +// imports... $factory->define(DummyClass::class, function (Faker $faker) { return [ diff --git a/tests/Feature/Generator/FactoryGeneratorTest.php b/tests/Feature/Generator/FactoryGeneratorTest.php index d4c13f53..b231bf09 100644 --- a/tests/Feature/Generator/FactoryGeneratorTest.php +++ b/tests/Feature/Generator/FactoryGeneratorTest.php @@ -48,7 +48,7 @@ public function output_writes_nothing_for_empty_tree() * @test * @dataProvider modelTreeDataProvider */ - public function output_writes_migration_for_model_tree($definition, $path, $migration) + public function output_writes_factory_for_model_tree($definition, $path, $factory) { $this->files->expects('stub') ->with('factory.stub') @@ -59,7 +59,7 @@ public function output_writes_migration_for_model_tree($definition, $path, $migr ->andReturnTrue(); $this->files->expects('put') - ->with($path, $this->fixture($migration)); + ->with($path, $this->fixture($factory)); $tokens = $this->blueprint->parse($this->fixture($definition)); $tree = $this->blueprint->analyze($tokens); @@ -151,6 +151,7 @@ public function modelTreeDataProvider() ['definitions/model-key-constraints.bp', 'database/factories/OrderFactory.php', 'factories/model-key-constraints.php'], ['definitions/unconventional-foreign-key.bp', 'database/factories/StateFactory.php', 'factories/unconventional-foreign-key.php'], ['definitions/foreign-key-shorthand.bp', 'database/factories/CommentFactory.php', 'factories/foreign-key-shorthand.php'], + ['definitions/resource-statements.bp', 'database/factories/UserFactory.php', 'factories/resource-statements.php'], ]; } } diff --git a/tests/Feature/Generator/MigrationGeneratorTest.php b/tests/Feature/Generator/MigrationGeneratorTest.php index 615ac6c5..3f515d3f 100644 --- a/tests/Feature/Generator/MigrationGeneratorTest.php +++ b/tests/Feature/Generator/MigrationGeneratorTest.php @@ -523,6 +523,7 @@ public function modelTreeDataProvider() ['definitions/disable-auto-columns.bp', 'database/migrations/timestamp_create_states_table.php', 'migrations/disable-auto-columns.php'], ['definitions/uuid-shorthand.bp', 'database/migrations/timestamp_create_people_table.php', 'migrations/uuid-shorthand.php'], ['definitions/unconventional-foreign-key.bp', 'database/migrations/timestamp_create_states_table.php', 'migrations/unconventional-foreign-key.php'], + ['definitions/resource-statements.bp', 'database/migrations/timestamp_create_users_table.php', 'migrations/resource-statements.php'], ]; } } diff --git a/tests/Feature/Generator/ModelGeneratorTest.php b/tests/Feature/Generator/ModelGeneratorTest.php index 390c7e27..415844bb 100644 --- a/tests/Feature/Generator/ModelGeneratorTest.php +++ b/tests/Feature/Generator/ModelGeneratorTest.php @@ -55,7 +55,7 @@ public function output_generates_models($definition, $path, $model) ->with('model/fillable.stub') ->andReturn(file_get_contents('stubs/model/fillable.stub')); - if ($definition === 'definitions/nested-components.bp') { + if (in_array($definition, ['definitions/nested-components.bp','definitions/resource-statements.bp'])) { $this->files->expects('stub') ->with('model/hidden.stub') ->andReturn(file_get_contents('stubs/model/hidden.stub')); @@ -415,6 +415,7 @@ public function modelTreeDataProvider() ['definitions/relationships.bp', 'app/Comment.php', 'models/relationships.php'], ['definitions/unconventional.bp', 'app/Team.php', 'models/unconventional.php'], ['definitions/nested-components.bp', 'app/Admin/User.php', 'models/nested-components.php'], + ['definitions/resource-statements.bp', 'app/User.php', 'models/resource-statements.php'], ]; } diff --git a/tests/fixtures/factories/resource-statements.php b/tests/fixtures/factories/resource-statements.php new file mode 100644 index 00000000..567f5d05 --- /dev/null +++ b/tests/fixtures/factories/resource-statements.php @@ -0,0 +1,16 @@ +define(User::class, function (Faker $faker) { + return [ + 'name' => $faker->name, + 'email' => $faker->safeEmail, + 'password' => $faker->password, + 'remember_token' => Str::random(10), + ]; +}); diff --git a/tests/fixtures/migrations/resource-statements.php b/tests/fixtures/migrations/resource-statements.php new file mode 100644 index 00000000..220be996 --- /dev/null +++ b/tests/fixtures/migrations/resource-statements.php @@ -0,0 +1,35 @@ +id(); + $table->string('name'); + $table->string('email'); + $table->string('password'); + $table->rememberToken(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('users'); + } +} diff --git a/tests/fixtures/models/resource-statements.php b/tests/fixtures/models/resource-statements.php new file mode 100644 index 00000000..ff7aa920 --- /dev/null +++ b/tests/fixtures/models/resource-statements.php @@ -0,0 +1,38 @@ + 'integer', + ]; +} From 7f817721623ba501989ae75bbd73a02439ea6230 Mon Sep 17 00:00:00 2001 From: Jason McCreary Date: Fri, 22 May 2020 13:50:32 -0400 Subject: [PATCH 2/4] Import all the things... --- stubs/factory.stub | 1 - 1 file changed, 1 deletion(-) diff --git a/stubs/factory.stub b/stubs/factory.stub index 46c698d7..607596eb 100644 --- a/stubs/factory.stub +++ b/stubs/factory.stub @@ -2,7 +2,6 @@ /** @var \Illuminate\Database\Eloquent\Factory $factory */ -use DummyModel; // imports... $factory->define(DummyClass::class, function (Faker $faker) { From fb1ac841ee68224bb8d3a738d63014a867476cfc Mon Sep 17 00:00:00 2001 From: Jason McCreary Date: Fri, 22 May 2020 13:53:38 -0400 Subject: [PATCH 3/4] Always import model class --- src/Generators/FactoryGenerator.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Generators/FactoryGenerator.php b/src/Generators/FactoryGenerator.php index 2e3c7c81..e127a731 100644 --- a/src/Generators/FactoryGenerator.php +++ b/src/Generators/FactoryGenerator.php @@ -57,7 +57,6 @@ protected function getPath(Model $model) protected function populateStub(string $stub, Model $model) { - $stub = str_replace('DummyModel', $model->fullyQualifiedClassName(), $stub); $stub = str_replace('DummyClass', $model->name(), $stub); $stub = str_replace('// definition...', $this->buildDefinition($model), $stub); $stub = str_replace('// imports...', $this->buildImports($model), $stub); @@ -172,6 +171,8 @@ private function addImport(Model $model, $class) private function buildImports(Model $model) { + $this->addImport($model, $model->fullyQualifiedClassName()); + $imports = array_unique($this->imports[$model->name()]); sort($imports); From 50f5e8613a7e82ea8a47baade309f39d6feec49f Mon Sep 17 00:00:00 2001 From: Jason McCreary Date: Fri, 22 May 2020 13:56:24 -0400 Subject: [PATCH 4/4] Unify import --- src/Generators/FactoryGenerator.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Generators/FactoryGenerator.php b/src/Generators/FactoryGenerator.php index e127a731..eb0c9b22 100644 --- a/src/Generators/FactoryGenerator.php +++ b/src/Generators/FactoryGenerator.php @@ -30,6 +30,7 @@ public function output(array $tree): array /** @var \Blueprint\Models\Model $model */ foreach ($tree['models'] as $model) { $this->addImport($model, 'Faker\Generator as Faker'); + $this->addImport($model, $model->fullyQualifiedClassName()); $path = $this->getPath($model); @@ -171,8 +172,6 @@ private function addImport(Model $model, $class) private function buildImports(Model $model) { - $this->addImport($model, $model->fullyQualifiedClassName()); - $imports = array_unique($this->imports[$model->name()]); sort($imports);