Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion src/Generators/FactoryGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class FactoryGenerator implements Generator
/** @var \Illuminate\Contracts\Filesystem\Filesystem */
private $files;

private $imports = [];

public function __construct($files)
{
$this->files = $files;
Expand All @@ -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))) {
Expand All @@ -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;
}
Expand Down Expand Up @@ -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());
Expand All @@ -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')) {
Expand Down
2 changes: 2 additions & 0 deletions src/Generators/MigrationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()}'";
}
Expand Down
1 change: 1 addition & 0 deletions src/Generators/ModelGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ private function fillableColumns(array $columns)
'deleted_at',
'created_at',
'updated_at',
'remember_token',
]);
}

Expand Down
3 changes: 1 addition & 2 deletions stubs/factory.stub
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand Down
5 changes: 3 additions & 2 deletions tests/Feature/Generator/FactoryGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for scouting this.

{
$this->files->expects('stub')
->with('factory.stub')
Expand All @@ -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);
Expand Down Expand Up @@ -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'],
];
}
}
1 change: 1 addition & 0 deletions tests/Feature/Generator/MigrationGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
];
}
}
3 changes: 2 additions & 1 deletion tests/Feature/Generator/ModelGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down Expand Up @@ -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'],
];
}

Expand Down
16 changes: 16 additions & 0 deletions tests/fixtures/factories/resource-statements.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/** @var \Illuminate\Database\Eloquent\Factory $factory */

use App\User;
use Faker\Generator as Faker;
use Illuminate\Support\Str;

$factory->define(User::class, function (Faker $faker) {
return [
'name' => $faker->name,
'email' => $faker->safeEmail,
'password' => $faker->password,
'remember_token' => Str::random(10),
];
});
35 changes: 35 additions & 0 deletions tests/fixtures/migrations/resource-statements.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->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');
}
}
38 changes: 38 additions & 0 deletions tests/fixtures/models/resource-statements.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
'email',
'password',
];

/**
* The attributes that should be hidden for serialization.
*
* @var array
*/
protected $hidden = [
'password',
'remember_token',
];

/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'id' => 'integer',
];
}