Skip to content

Commit 497df89

Browse files
authored
Merge pull request #69 from RonasIT/49-add-migration-generator-tests
Add migration generator tests
2 parents 9eed92a + f4eaead commit 497df89

File tree

7 files changed

+214
-14
lines changed

7 files changed

+214
-14
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace RonasIT\Support\Exceptions;
4+
5+
use Exception;
6+
7+
class UnknownFieldTypeException extends Exception
8+
{
9+
public function __construct(
10+
protected string $typeName,
11+
protected string $generatorName,
12+
) {
13+
parent::__construct("Unknown field type {$this->typeName} in {$this->generatorName}.");
14+
}
15+
}

src/Generators/MigrationGenerator.php

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
namespace RonasIT\Support\Generators;
44

55
use Carbon\Carbon;
6-
use Exception;
6+
use Illuminate\Support\Str;
77
use RonasIT\Support\Events\SuccessCreateMessage;
8+
use RonasIT\Support\Exceptions\UnknownFieldTypeException;
89

910
class MigrationGenerator extends EntityGenerator
1011
{
@@ -24,29 +25,30 @@ public function generate(): void
2425
'fields' => $this->fields,
2526
'table' => $this->generateTable($this->fields)
2627
]);
28+
2729
$now = Carbon::now()->format('Y_m_d_His');
2830

2931
$this->saveClass('migrations', "{$now}_{$entities}_create_table", $content);
3032

3133
event(new SuccessCreateMessage("Created a new Migration: {$entities}_create_table"));
3234
}
3335

34-
protected function isJson($typeName): bool
36+
protected function isJson(string $typeName): bool
3537
{
36-
return $typeName == 'json';
38+
return $typeName === 'json';
3739
}
3840

39-
protected function isRequired($typeName): bool
41+
protected function isRequired(string $typeName): bool
4042
{
41-
return !empty(explode('-', $typeName)[1]);
43+
return Str::afterLast($typeName, '-') === 'required';
4244
}
4345

44-
protected function isNullable($typeName): bool
46+
protected function isNullable(string $typeName): bool
4547
{
4648
return empty(explode('-', $typeName)[1]);
4749
}
4850

49-
protected function getJsonLine($fieldName): string
51+
protected function getJsonLine(string $fieldName): string
5052
{
5153
if (env("DB_CONNECTION") == "mysql") {
5254
return "\$table->json('{$fieldName}')->nullable();";
@@ -55,7 +57,7 @@ protected function getJsonLine($fieldName): string
5557
return "\$table->jsonb('{$fieldName}')->default(\"{}\");";
5658
}
5759

58-
protected function getRequiredLine($fieldName, $typeName): string
60+
protected function getRequiredLine(string $fieldName, string $typeName): string
5961
{
6062
$type = explode('-', $typeName)[0];
6163

@@ -66,27 +68,27 @@ protected function getRequiredLine($fieldName, $typeName): string
6668
return "\$table->{$type}('{$fieldName}');";
6769
}
6870

69-
protected function getNonRequiredLine($fieldName, $typeName): string
71+
protected function getNonRequiredLine(string $fieldName, string $typeName): string
7072
{
7173
$type = explode('-', $typeName)[0];
7274

7375
return "\$table->{$type}('{$fieldName}')->nullable();";
7476
}
7577

76-
protected function generateTable($fields): array
78+
protected function generateTable(array $fields): array
7779
{
7880
$resultTable = [];
7981

8082
foreach ($fields as $typeName => $fieldNames) {
8183
foreach ($fieldNames as $fieldName) {
82-
array_push($resultTable, $this->getTableRow($fieldName, $typeName));
84+
$resultTable[] = $this->getTableRow($fieldName, $typeName);
8385
}
8486
}
8587

8688
return $resultTable;
8789
}
8890

89-
protected function getTableRow($fieldName, $typeName): string
91+
protected function getTableRow(string $fieldName, string $typeName): string
9092
{
9193
if ($this->isJson($typeName)) {
9294
return $this->getJsonLine($fieldName);
@@ -100,6 +102,6 @@ protected function getTableRow($fieldName, $typeName): string
100102
return $this->getNonRequiredLine($fieldName, $typeName);
101103
}
102104

103-
throw new Exception('Unknown fieldType in MigrationGenerator');
105+
throw new UnknownFieldTypeException($typeName, 'MigrationGenerator');
104106
}
105107
}

stubs/migration.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,4 @@ public function createTable(): void
6767
});
6868
}
6969
@endif
70-
};
70+
};

tests/MigrationGeneratorTest.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
namespace RonasIT\Support\Tests;
4+
5+
use Illuminate\Support\Carbon;
6+
use RonasIT\Support\Exceptions\UnknownFieldTypeException;
7+
use RonasIT\Support\Generators\MigrationGenerator;
8+
use RonasIT\Support\Tests\Support\Migration\MigrationMockTrait;
9+
10+
class MigrationGeneratorTest extends TestCase
11+
{
12+
use MigrationMockTrait;
13+
14+
public function testSetUnknownFieldType()
15+
{
16+
$this->setupConfigurations();
17+
18+
$this->assertExceptionThrew(
19+
className: UnknownFieldTypeException::class,
20+
message: 'Unknown field type unknown-type in MigrationGenerator.',
21+
);
22+
23+
app(MigrationGenerator::class)
24+
->setModel('Post')
25+
->setRelations([
26+
'belongsTo' => [],
27+
'belongsToMany' => [],
28+
'hasOne' => [],
29+
'hasMany' => [],
30+
])
31+
->setFields([
32+
'integer-required' => ['media_id', 'user_id'],
33+
'unknown-type' => ['title'],
34+
])
35+
->generate();
36+
}
37+
38+
public function testCreateMigration()
39+
{
40+
Carbon::setTestNow('2022-02-02');
41+
42+
$this->mockFilesystem();
43+
$this->setupConfigurations();
44+
45+
app(MigrationGenerator::class)
46+
->setModel('Post')
47+
->setRelations([
48+
'belongsTo' => [],
49+
'belongsToMany' => [],
50+
'hasOne' => [],
51+
'hasMany' => [],
52+
])
53+
->setFields([
54+
'integer-required' => ['media_id', 'user_id'],
55+
'string' => ['title', 'body'],
56+
'json' => ['meta'],
57+
'timestamp' => ['created_at'],
58+
])
59+
->generate();
60+
61+
$this->assertGeneratedFileEquals('migrations.php', 'database/migrations/2022_02_02_000000_posts_create_table.php');
62+
}
63+
64+
public function testCreateMigrationMYSQL()
65+
{
66+
putenv('DB_CONNECTION=mysql');
67+
68+
Carbon::setTestNow('2022-02-02');
69+
70+
$this->mockFilesystem();
71+
$this->setupConfigurations();
72+
73+
app(MigrationGenerator::class)
74+
->setModel('Post')
75+
->setRelations([
76+
'belongsTo' => [],
77+
'belongsToMany' => [],
78+
'hasOne' => [],
79+
'hasMany' => [],
80+
])
81+
->setFields([
82+
'integer-required' => ['media_id', 'user_id'],
83+
'string' => ['title', 'body'],
84+
'json' => ['meta'],
85+
'timestamp' => ['created_at'],
86+
])
87+
->generate();
88+
89+
$this->assertGeneratedFileEquals('migrations_mysql.php', 'database/migrations/2022_02_02_000000_posts_create_table.php');
90+
}
91+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace RonasIT\Support\Tests\Support\Migration;
4+
5+
use org\bovigo\vfs\vfsStream;
6+
use RonasIT\Support\Tests\Support\GeneratorMockTrait;
7+
8+
trait MigrationMockTrait
9+
{
10+
use GeneratorMockTrait;
11+
12+
public function setupConfigurations(): void
13+
{
14+
config([
15+
'entity-generator.stubs.migration' => 'entity-generator::migration',
16+
'entity-generator.paths' => [
17+
'migrations' => 'database/migrations',
18+
]
19+
]);
20+
}
21+
22+
public function mockFilesystem(): void
23+
{
24+
$structure = [
25+
'database' => [
26+
'migrations' => [],
27+
],
28+
];
29+
30+
vfsStream::create($structure);
31+
}
32+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
use RonasIT\Support\Traits\MigrationTrait;
7+
8+
return new class extends Migration
9+
{
10+
use MigrationTrait;
11+
12+
public function up(): void
13+
{
14+
Schema::create('posts', function (Blueprint $table) {
15+
$table->increments('id');
16+
$table->integer('media_id');
17+
$table->integer('user_id');
18+
$table->string('title')->nullable();
19+
$table->string('body')->nullable();
20+
$table->jsonb('meta')->default("{}");
21+
$table->timestamp('created_at')->nullable();
22+
$table->timestamps();
23+
});
24+
}
25+
26+
public function down(): void
27+
{
28+
Schema::dropIfExists('posts');
29+
}
30+
};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
use RonasIT\Support\Traits\MigrationTrait;
7+
8+
return new class extends Migration
9+
{
10+
use MigrationTrait;
11+
12+
public function up(): void
13+
{
14+
Schema::create('posts', function (Blueprint $table) {
15+
$table->increments('id');
16+
$table->integer('media_id');
17+
$table->integer('user_id');
18+
$table->string('title')->nullable();
19+
$table->string('body')->nullable();
20+
$table->json('meta')->nullable();
21+
$table->timestamp('created_at')->nullable();
22+
$table->timestamps();
23+
});
24+
}
25+
26+
public function down(): void
27+
{
28+
Schema::dropIfExists('posts');
29+
}
30+
};

0 commit comments

Comments
 (0)