diff --git a/src/Generators/FactoryGenerator.php b/src/Generators/FactoryGenerator.php index 65e35f5e..eb0c9b22 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,9 @@ 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); if (!$this->files->exists(dirname($path))) { @@ -53,9 +58,9 @@ 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); 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..607596eb 100644 --- a/stubs/factory.stub +++ b/stubs/factory.stub @@ -2,8 +2,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', + ]; +}