Skip to content

Commit 5f6cda5

Browse files
committed
Backfill tests for id migration method
1 parent 23aa5a0 commit 5f6cda5

12 files changed

+75
-18
lines changed

src/Generators/MigrationGenerator.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Blueprint\Contracts\Generator;
66
use Blueprint\Models\Model;
77
use Carbon\Carbon;
8+
use Illuminate\Support\Facades\App;
89
use Illuminate\Support\Str;
910

1011
class MigrationGenerator implements Generator
@@ -62,7 +63,7 @@ protected function buildDefinition(Model $model)
6263
$dataType = 'uuid';
6364
}
6465

65-
if ($this->isLaravel7orNewer() && $dataType === 'bigIncrements') {
66+
if ($dataType === 'bigIncrements' && $this->isLaravel7orNewer()) {
6667
$definition .= self::INDENT . '$table->id(';
6768
} else {
6869
$definition .= self::INDENT . '$table->' . $dataType . "('{$column->name()}'";
@@ -83,13 +84,7 @@ protected function buildDefinition(Model $model)
8384
foreach ($column->modifiers() as $modifier) {
8485
if (is_array($modifier)) {
8586
if (key($modifier) === 'foreign') {
86-
$foreign =
87-
self::INDENT .
88-
'$table->foreign(' .
89-
"'{$column->name()}')->references('id')->on('" .
90-
Str::lower(Str::plural(current($modifier))) .
91-
"')->onDelete('cascade');" .
92-
PHP_EOL;
87+
$foreign = self::INDENT . '$table->foreign(' . "'{$column->name()}')->references('id')->on('" . Str::lower(Str::plural(current($modifier))) . "')->onDelete('cascade');" . PHP_EOL;
9388
} else {
9489
$definition .= '->' . key($modifier) . '(' . current($modifier) . ')';
9590
}
@@ -124,6 +119,6 @@ protected function getPath(Model $model, Carbon $timestamp)
124119

125120
protected function isLaravel7orNewer()
126121
{
127-
return version_compare(app()->version(), '7.0.0', '>=');
122+
return version_compare(App::version(), '7.0.0', '>=');
128123
}
129124
}

tests/Feature/Generator/MigrationGeneratorTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Blueprint\Blueprint;
66
use Blueprint\Generators\MigrationGenerator;
77
use Carbon\Carbon;
8+
use Illuminate\Support\Facades\App;
89
use Tests\TestCase;
910

1011
/**
@@ -95,6 +96,35 @@ public function output_uses_past_timestamp_for_multiple_migrations()
9596
$this->assertEquals(['created' => [$post_path, $comment_path]], $this->subject->output($tree));
9697
}
9798

99+
/**
100+
* @test
101+
*/
102+
public function output_uses_big_increments_for_id_column()
103+
{
104+
$app = \Mockery::mock();
105+
$app->expects('version')
106+
->withNoArgs()
107+
->andReturn('6.0.0');
108+
App::swap($app);
109+
110+
$this->files->expects('stub')
111+
->with('migration.stub')
112+
->andReturn(file_get_contents('stubs/migration.stub'));
113+
114+
$now = Carbon::now();
115+
Carbon::setTestNow($now);
116+
117+
$timestamp_path = str_replace('timestamp', $now->format('Y_m_d_His'), 'database/migrations/timestamp_create_relationships_table.php');
118+
119+
$this->files->expects('put')
120+
->with($timestamp_path, $this->fixture('migrations/identity-columns-big-increments.php'));
121+
122+
$tokens = $this->blueprint->parse($this->fixture('definitions/model-identities.bp'));
123+
$tree = $this->blueprint->analyze($tokens);
124+
125+
$this->assertEquals(['created' => [$timestamp_path]], $this->subject->output($tree));
126+
}
127+
98128
public function modelTreeDataProvider()
99129
{
100130
return [

tests/fixtures/migrations/comments.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class CreateCommentsTable extends Migration
1414
public function up()
1515
{
1616
Schema::create('comments', function (Blueprint $table) {
17-
$table->bigIncrements('id');
17+
$table->id();
1818
$table->text('content');
1919
$table->timestamps();
2020
});
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateRelationshipsTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('relationships', function (Blueprint $table) {
17+
$table->bigIncrements('id');
18+
$table->unsignedBigInteger('post_id');
19+
$table->unsignedBigInteger('another');
20+
});
21+
}
22+
23+
/**
24+
* Reverse the migrations.
25+
*
26+
* @return void
27+
*/
28+
public function down()
29+
{
30+
Schema::dropIfExists('relationships');
31+
}
32+
}

tests/fixtures/migrations/identity-columns.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class CreateRelationshipsTable extends Migration
1414
public function up()
1515
{
1616
Schema::create('relationships', function (Blueprint $table) {
17-
$table->bigIncrements('id');
17+
$table->id();
1818
$table->unsignedBigInteger('post_id');
1919
$table->unsignedBigInteger('another');
2020
});

tests/fixtures/migrations/model-modifiers.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class CreateModifiersTable extends Migration
1414
public function up()
1515
{
1616
Schema::create('modifiers', function (Blueprint $table) {
17-
$table->bigIncrements('id');
17+
$table->id();
1818
$table->string('title')->nullable();
1919
$table->string('name', 1000)->unique()->charset('utf8');
2020
$table->string('content')->default('');

tests/fixtures/migrations/posts.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class CreatePostsTable extends Migration
1414
public function up()
1515
{
1616
Schema::create('posts', function (Blueprint $table) {
17-
$table->bigIncrements('id');
17+
$table->id();
1818
$table->string('title');
1919
$table->timestamps();
2020
});

tests/fixtures/migrations/readme-example.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class CreatePostsTable extends Migration
1414
public function up()
1515
{
1616
Schema::create('posts', function (Blueprint $table) {
17-
$table->bigIncrements('id');
17+
$table->id();
1818
$table->string('title', 400);
1919
$table->longText('content');
2020
$table->timestamp('published_at')->nullable();

tests/fixtures/migrations/relationships.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class CreateCommentsTable extends Migration
1414
public function up()
1515
{
1616
Schema::create('comments', function (Blueprint $table) {
17-
$table->bigIncrements('id');
17+
$table->id();
1818
$table->unsignedBigInteger('post_id');
1919
$table->unsignedBigInteger('author_id');
2020
$table->timestamps();

tests/fixtures/migrations/soft-deletes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class CreateCommentsTable extends Migration
1414
public function up()
1515
{
1616
Schema::create('comments', function (Blueprint $table) {
17-
$table->bigIncrements('id');
17+
$table->id();
1818
$table->unsignedBigInteger('post_id');
1919
$table->softDeletes();
2020
$table->timestamps();

0 commit comments

Comments
 (0)