Skip to content

Commit f5e594e

Browse files
Fix factory generation for morphs column type (#135)
1 parent 34dc595 commit f5e594e

File tree

6 files changed

+36
-3
lines changed

6 files changed

+36
-3
lines changed

src/Generators/FactoryGenerator.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
namespace Blueprint\Generators;
44

5+
use Blueprint\Contracts\Generator;
6+
use Blueprint\Models\Column;
57
use Blueprint\Models\Model;
68
use Illuminate\Support\Str;
7-
use Blueprint\Models\Column;
8-
use Blueprint\Contracts\Generator;
99

1010
class FactoryGenerator implements Generator
1111
{
@@ -106,6 +106,12 @@ protected function buildDefinition(Model $model)
106106
} elseif ($column->dataType() === 'json') {
107107
$default = $column->defaultValue() ?? "'{}'";
108108
$definition .= self::INDENT . "'{$column->name()}' => {$default}," . PHP_EOL;
109+
} elseif ($column->dataType() === 'morphs') {
110+
if ($column->isNullable()) {
111+
continue;
112+
}
113+
$definition .= sprintf('%s%s => $faker->%s,%s', self::INDENT, "'{$column->name()}_id'", self::fakerDataType('id'), PHP_EOL);
114+
$definition .= sprintf('%s%s => $faker->%s,%s', self::INDENT, "'{$column->name()}_type'", self::fakerDataType('string'), PHP_EOL);
109115
} else {
110116
$definition .= self::INDENT . "'{$column->name()}' => ";
111117
$faker = self::fakerData($column->name()) ?? self::fakerDataType($column->dataType());

src/Generators/MigrationGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ protected function buildDefinition(Model $model)
111111
$dataType = 'unsigned' . ucfirst($dataType);
112112
}
113113

114-
if (in_array($dataType, self::NULLABLE_TYPES) && in_array('nullable', $column->modifiers())) {
114+
if (in_array($dataType, self::NULLABLE_TYPES) && $column->isNullable()) {
115115
$dataType = 'nullable' . ucfirst($dataType);
116116
}
117117

src/Models/Column.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,9 @@ public function defaultValue()
4545
return $key === 'default';
4646
});
4747
}
48+
49+
public function isNullable()
50+
{
51+
return in_array('nullable', $this->modifiers);
52+
}
4853
}

tests/Unit/Models/ColumnTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Tests\Unit\Models;
4+
5+
use Blueprint\Models\Column;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class ColumnTest extends TestCase
9+
{
10+
/** @test */
11+
public function it_knows_if_its_nullable()
12+
{
13+
$this->assertTrue((new Column('foo', 'string', ['nullable']))->isNullable());
14+
15+
$this->assertFalse((new Column('foo', 'string', []))->isNullable());
16+
$this->assertFalse((new Column('foo', 'string', ['something']))->isNullable());
17+
}
18+
}

tests/fixtures/definitions/phone.bp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ models:
55
phone_number: text
66
type: enum:home,cell
77
status: set:archived,deleted
8+
foo: morphs
9+
bar: morphs nullable

tests/fixtures/factories/phone.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,7 @@
1212
'phone_number' => $faker->phoneNumber,
1313
'type' => $faker->randomElement(["home","cell"]),
1414
'status' => $faker->randomElement(["archived","deleted"]),
15+
'foo_id' => $faker->randomDigitNotNull,
16+
'foo_type' => $faker->word,
1517
];
1618
});

0 commit comments

Comments
 (0)