Skip to content

Commit ff51b88

Browse files
committed
Stregthen type checks and backfill tests
1 parent c92a5bd commit ff51b88

File tree

12 files changed

+122
-10
lines changed

12 files changed

+122
-10
lines changed

src/Generators/FactoryGenerator.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,9 @@ protected function buildDefinition(Model $model)
9595
implode(', ', [$scale, 0, (str_repeat(9, $precision - $scale) . '.' . str_repeat(9, $scale))]),
9696
$definition
9797
);
98-
} elseif ($column->dataType() == 'json') {
99-
$definition .= self::INDENT . "'{$column->name()}' => '[]'," . PHP_EOL;
98+
} elseif ($column->dataType() === 'json') {
99+
$default = $column->defaultValue() ?? "'{}'";
100+
$definition .= self::INDENT . "'{$column->name()}' => {$default}," . PHP_EOL;
100101
} else {
101102
$definition .= self::INDENT . "'{$column->name()}' => ";
102103
$faker = self::fakerData($column->name()) ?? self::fakerDataType($column->dataType());
@@ -163,7 +164,7 @@ public static function fakerDataType(string $type)
163164
'datetime' => 'dateTime()',
164165
'timestamp' => 'dateTime()',
165166
'integer' => 'randomNumber()',
166-
'unsignedsmallinteger' => 'randomNumber()',
167+
'unsignedsmallinteger' => 'randomDigitNotNull',
167168
'bigint' => 'randomNumber()',
168169
'smallint' => 'randomNumber()',
169170
'decimal' => 'randomFloat(/** decimal_attributes **/)',

src/Generators/MigrationGenerator.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,11 @@ protected function buildDefinition(Model $model)
5454
/** @var \Blueprint\Models\Column $column */
5555
foreach ($model->columns() as $column) {
5656
$dataType = $column->dataType();
57-
if ($column->name() === 'id' && $dataType != 'uuid') {
57+
if ($column->name() === 'id' && $dataType !== 'uuid') {
5858
$dataType = 'bigIncrements';
5959
} elseif ($column->dataType() === 'id') {
6060
$dataType = 'unsignedBigInteger';
61-
}
62-
63-
if ($column->dataType() === 'uuid') {
61+
} elseif ($column->dataType() === 'uuid') {
6462
$dataType = 'uuid';
6563
}
6664

@@ -80,7 +78,7 @@ protected function buildDefinition(Model $model)
8078

8179
foreach ($column->modifiers() as $modifier) {
8280
if (is_array($modifier)) {
83-
if (key($modifier) == 'foreign') {
81+
if (key($modifier) === 'foreign') {
8482
$foreign =
8583
self::INDENT .
8684
'$table->foreign(' .

src/Models/Column.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,13 @@ public function modifiers()
3636
{
3737
return $this->modifiers;
3838
}
39+
40+
public function defaultValue()
41+
{
42+
return collect($this->modifiers())
43+
->collapse()
44+
->first(function ($value, $key) {
45+
return $key === 'default';
46+
});
47+
}
3948
}

tests/Feature/Generator/FactoryGeneratorTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ public function modelTreeDataProvider()
9292
['definitions/team.bp', 'database/factories/TeamFactory.php', 'factories/team.php'],
9393
['definitions/unconventional.bp', 'database/factories/TeamFactory.php', 'factories/unconventional.php'],
9494
['definitions/nested-components.bp', 'database/factories/Admin/UserFactory.php', 'factories/nested-components.php'],
95-
['definitions/model-modifiers.bp', 'database/factories/ModifierFactory.php', 'factories/model-modifiers.php']
95+
['definitions/model-modifiers.bp', 'database/factories/ModifierFactory.php', 'factories/model-modifiers.php'],
96+
['definitions/model-key-constraints.bp', 'database/factories/OrderFactory.php', 'factories/model-key-constraints.php']
9697
];
9798
}
9899
}

tests/Feature/Generator/MigrationGeneratorTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ public function modelTreeDataProvider()
104104
['definitions/soft-deletes.bp', 'database/migrations/timestamp_create_comments_table.php', 'migrations/soft-deletes.php'],
105105
['definitions/with-timezones.bp', 'database/migrations/timestamp_create_comments_table.php', 'migrations/with-timezones.php'],
106106
['definitions/relationships.bp', 'database/migrations/timestamp_create_comments_table.php', 'migrations/relationships.php'],
107+
['definitions/unconventional.bp', 'database/migrations/timestamp_create_teams_table.php', 'migrations/unconventional.php'],
108+
['definitions/model-key-constraints.bp', 'database/migrations/timestamp_create_orders_table.php', 'migrations/model-key-constraints.php'],
107109
];
108110
}
109111
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
models:
2+
Order:
3+
id: uuid primary
4+
user_id: id foreign:user
5+
external_id: string nullable index
6+
subscription_id: uuid foreign:subscription
7+
expires_at: timestamp nullable index
8+
meta: json default:'[]'

tests/fixtures/definitions/unconventional.bp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ models:
22
Team:
33
name: string
44
owner: id
5-
manager: id:user
5+
manager: id:user
6+
options: json
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
/** @var \Illuminate\Database\Eloquent\Factory $factory */
4+
5+
use App\Order;
6+
use Faker\Generator as Faker;
7+
8+
$factory->define(Order::class, function (Faker $faker) {
9+
return [
10+
'user_id' => factory(\App\User::class),
11+
'external_id' => $faker->word,
12+
'subscription_id' => factory(\App\Subscription::class),
13+
'expires_at' => $faker->dateTime(),
14+
'meta' => '[]',
15+
];
16+
});

tests/fixtures/factories/unconventional.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@
1010
'name' => $faker->name,
1111
'owner' => factory(\App\Owner::class),
1212
'manager' => factory(\App\User::class),
13+
'options' => '{}',
1314
];
1415
});
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateOrdersTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('orders', function (Blueprint $table) {
17+
$table->uuid('id')->primary();
18+
$table->unsignedBigInteger('user_id');
19+
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
20+
$table->string('external_id')->nullable()->index();
21+
$table->uuid('subscription_id');
22+
$table->foreign('subscription_id')->references('id')->on('subscriptions')->onDelete('cascade');
23+
$table->timestamp('expires_at')->nullable()->index();
24+
$table->json('meta')->default('[]');
25+
$table->timestamps();
26+
});
27+
}
28+
29+
/**
30+
* Reverse the migrations.
31+
*
32+
* @return void
33+
*/
34+
public function down()
35+
{
36+
Schema::dropIfExists('orders');
37+
}
38+
}

0 commit comments

Comments
 (0)