Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Models/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public function addRelationship(string $type, string $reference)

public function addPivotTable(string $reference)
{
$segments = [$this->name(), $reference];
$segments = [$this->name(), class_basename($reference)];
sort($segments);
$this->pivotTables[] = $segments;
}
Expand All @@ -164,7 +164,7 @@ public function indexes(): array
{
return $this->indexes;
}

public function addIndex(Index $index)
{
$this->indexes[] = $index;
Expand Down
57 changes: 57 additions & 0 deletions tests/Feature/Generators/MigrationGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,63 @@ public function output_also_creates_pivot_table_migration_with_custom_name_larav
$this->assertEquals(['created' => [$model_migration, $pivot_migration]], $this->subject->output($tree));
}

/**
* @test
*/
public function output_creates_pivot_table_migration_correctly_when_model_name_contains_path_prefix()
{
$this->files->expects('stub')
->with('migration.stub')
->andReturn($this->stub('migration.stub'));

$now = Carbon::now();
Carbon::setTestNow($now);

$model_migration = str_replace('timestamp', $now->copy()->subSecond()->format('Y_m_d_His'), 'database/migrations/timestamp_create_regions_table.php');
$pivot_migration = str_replace('timestamp', $now->format('Y_m_d_His'), 'database/migrations/timestamp_create_city_region_table.php');

$this->files->expects('exists')->twice()->andReturn(false);

$this->files->expects('put')
->with($model_migration, $this->fixture('migrations/with-path-prefix-table-name-region.php'));
$this->files->expects('put')
->with($pivot_migration, $this->fixture('migrations/with-path-prefix-table-name-city-region.php'));

$tokens = $this->blueprint->parse($this->fixture('drafts/with-path-prefix.yaml'));
$tree = $this->blueprint->analyze($tokens);

$this->assertEquals(['created' => [$model_migration, $pivot_migration]], $this->subject->output($tree));
}

/**
* @test
* @environment-setup useLaravel6
*/
public function output_creates_pivot_table_migration_correctly_when_model_name_contains_path_prefix_laravel6()
{
$this->files->expects('stub')
->with('migration.stub')
->andReturn($this->stub('migration.stub'));

$now = Carbon::now();
Carbon::setTestNow($now);

$model_migration = str_replace('timestamp', $now->copy()->subSecond()->format('Y_m_d_His'), 'database/migrations/timestamp_create_regions_table.php');
$pivot_migration = str_replace('timestamp', $now->format('Y_m_d_His'), 'database/migrations/timestamp_create_city_region_table.php');

$this->files->expects('exists')->twice()->andReturn(false);

$this->files->expects('put')
->with($model_migration, $this->fixture('migrations/with-path-prefix-table-name-region-laravel6.php'));
$this->files->expects('put')
->with($pivot_migration, $this->fixture('migrations/with-path-prefix-table-name-city-region-laravel6.php'));

$tokens = $this->blueprint->parse($this->fixture('drafts/with-path-prefix.yaml'));
$tree = $this->blueprint->analyze($tokens);

$this->assertEquals(['created' => [$model_migration, $pivot_migration]], $this->subject->output($tree));
}

/**
* @test
*/
Expand Down
6 changes: 6 additions & 0 deletions tests/fixtures/drafts/with-path-prefix.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
models:
Lookup\Region:
name_en: string:255 nullable
softDeletes
relationships:
belongsToMany: Lookup\City
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateCityRegionTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('city_region', function (Blueprint $table) {
$table->unsignedBigInteger('city_id');
$table->unsignedBigInteger('region_id');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('city_region');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateCityRegionTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('city_region', function (Blueprint $table) {
$table->foreignId('city_id');
$table->foreignId('region_id');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('city_region');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateRegionsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('regions', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name_en', 255)->nullable();
$table->softDeletes();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('regions');
}
}
33 changes: 33 additions & 0 deletions tests/fixtures/migrations/with-path-prefix-table-name-region.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateRegionsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('regions', function (Blueprint $table) {
$table->id();
$table->string('name_en', 255)->nullable();
$table->softDeletes();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('regions');
}
}