Skip to content

Commit 4471c2f

Browse files
Merge pull request #38 from laravel-shift/axit-joost-master
Generate models for relationships in factory definitions
2 parents 6ca97cd + 51cd1c6 commit 4471c2f

File tree

5 files changed

+34
-6
lines changed

5 files changed

+34
-6
lines changed

src/Generators/FactoryGenerator.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Blueprint\Contracts\Generator;
66
use Blueprint\Models\Model;
7+
use Illuminate\Support\Str;
78

89
class FactoryGenerator implements Generator
910
{
@@ -61,10 +62,19 @@ protected function buildDefinition(Model $model)
6162
continue;
6263
}
6364

64-
$definition .= self::INDENT . "'{$column->name()}' => ";
65-
$faker = $this->fakerData($column->name()) ?? $this->fakerDataType($column->dataType());
66-
$definition .= '$faker->' . $faker;
67-
$definition .= ',' . PHP_EOL;
65+
if ($column->dataType() === 'id') {
66+
$name = Str::substr($column->name(), 0, -3);
67+
$class = Str::studly($column->attributes()[0] ?? $name);
68+
69+
$definition .= self::INDENT . "'{$column->name()}' => ";
70+
$definition .= sprintf("factory(\App\%s::class)", $class);
71+
$definition .= ',' . PHP_EOL;
72+
} else {
73+
$definition .= self::INDENT . "'{$column->name()}' => ";
74+
$faker = $this->fakerData($column->name()) ?? $this->fakerDataType($column->dataType());
75+
$definition .= '$faker->' . $faker;
76+
$definition .= ',' . PHP_EOL;
77+
}
6878
}
6979

7080
return trim($definition);

tests/Feature/Generator/FactoryGeneratorTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ public function output_writes_migration_for_model_tree($definition, $path, $migr
6464
public function modelTreeDataProvider()
6565
{
6666
return [
67-
['definitions/post.bp', 'database/factories/PostFactory.php', 'factories/post.php']
67+
['definitions/post.bp', 'database/factories/PostFactory.php', 'factories/post.php'],
68+
['definitions/team.bp', 'database/factories/TeamFactory.php', 'factories/team.php']
6869
];
6970
}
7071
}

tests/fixtures/definitions/team.bp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
models:
2+
Team:
3+
name: string
4+
owner_id: id:user

tests/fixtures/factories/post.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
$factory->define(Post::class, function (Faker $faker) {
99
return [
1010
'title' => $faker->sentence(4),
11-
'author_id' => $faker->randomDigitNotNull,
11+
'author_id' => factory(\App\Author::class),
1212
'content' => $faker->paragraphs(3, true),
1313
'published_at' => $faker->dateTime(),
1414
'word_count' => $faker->randomNumber(),

tests/fixtures/factories/team.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
/** @var \Illuminate\Database\Eloquent\Factory $factory */
4+
5+
use App\Team;
6+
use Faker\Generator as Faker;
7+
8+
$factory->define(Team::class, function (Faker $faker) {
9+
return [
10+
'name' => $faker->name,
11+
'owner_id' => factory(\App\User::class),
12+
];
13+
});

0 commit comments

Comments
 (0)