Skip to content

Commit

Permalink
Extend MigratorTest
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagorb committed Apr 9, 2021
1 parent 9311daf commit 9cdb5cc
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 13 deletions.
74 changes: 61 additions & 13 deletions tests/Integration/Migration/MigratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,29 @@

namespace Illuminate\Tests\Integration\Migration;

use Illuminate\Support\Facades\DB;
use Mockery;
use Mockery\Mock;
use Orchestra\Testbench\TestCase;
use PDOException;
use Symfony\Component\Console\Output\OutputInterface;

class MigratorTest extends TestCase
{
/**
* @var Mock
*/
private $output;

protected function setUp(): void
{
parent::setUp();

$this->output = Mockery::mock(OutputInterface::class);
$this->subject = $this->app->make('migrator');
$this->subject->setOutput($this->output);
$this->subject->getRepository()->createRepository();
}

protected function getEnvironmentSetUp($app)
{
$app['config']->set('app.debug', 'true');
Expand All @@ -19,25 +37,55 @@ protected function getEnvironmentSetUp($app)
]);
}

public function testDontDisplayOutputWhenOutputObjectIsNotAvailable()
public function testMigrate()
{
$this->expectOutput('<comment>Migrating:</comment> 2014_10_12_000000_create_people_table');
$this->expectOutput(Mockery::pattern('#<info>Migrated:</info> 2014_10_12_000000_create_people_table (.*)#'));
$this->expectOutput('<comment>Migrating:</comment> 2015_10_04_000000_modify_people_table');
$this->expectOutput(Mockery::pattern('#<info>Migrated:</info> 2015_10_04_000000_modify_people_table (.*)#'));
$this->expectOutput('<comment>Migrating:</comment> 2016_10_04_000000_modify_people_table');
$this->expectOutput(Mockery::pattern('#<info>Migrated:</info> 2016_10_04_000000_modify_people_table (.*)#'));

$this->subject->run([__DIR__.'/fixtures']);

self::assertTrue(DB::getSchemaBuilder()->hasTable('people'));
self::assertTrue(DB::getSchemaBuilder()->hasColumn('people', 'first_name'));
self::assertTrue(DB::getSchemaBuilder()->hasColumn('people', 'last_name'));
}

public function testRollback()
{
$migrator = $this->app->make('migrator');
$this->getConnection()->statement('CREATE TABLE people(id INT, first_name VARCHAR, last_name VARCHAR);');
$this->subject->getRepository()->log('2014_10_12_000000_create_people_table', 1);
$this->subject->getRepository()->log('2015_10_04_000000_modify_people_table', 1);
$this->subject->getRepository()->log('2016_10_04_000000_modify_people_table', 1);

$migrator->getRepository()->createRepository();
$this->expectOutput('<comment>Rolling back:</comment> 2016_10_04_000000_modify_people_table');
$this->expectOutput(Mockery::pattern('#<info>Rolled back:</info> 2016_10_04_000000_modify_people_table (.*)#'));
$this->expectOutput('<comment>Rolling back:</comment> 2015_10_04_000000_modify_people_table');
$this->expectOutput(Mockery::pattern('#<info>Rolled back:</info> 2015_10_04_000000_modify_people_table (.*)#'));
$this->expectOutput('<comment>Rolling back:</comment> 2014_10_12_000000_create_people_table');
$this->expectOutput(Mockery::pattern('#<info>Rolled back:</info> 2014_10_12_000000_create_people_table (.*)#'));

$migrator->run([__DIR__.'/fixtures']);
$this->subject->rollback([__DIR__.'/fixtures']);

$this->assertTrue($this->tableExists('people'));
self::assertFalse(DB::getSchemaBuilder()->hasTable('people'));
}

private function tableExists($table): bool
public function testPretendMigrate()
{
try {
$this->app->make('db')->select("SELECT COUNT(*) FROM $table");
} catch (PDOException $e) {
return false;
}
$this->expectOutput('<info>2014_10_12_000000_create_people_table:</info> create table "people" ("id" integer not null primary key autoincrement, "name" varchar not null, "email" varchar not null, "password" varchar not null, "remember_token" varchar, "created_at" datetime, "updated_at" datetime)');
$this->expectOutput('<info>2014_10_12_000000_create_people_table:</info> create unique index "people_email_unique" on "people" ("email")');
$this->expectOutput('<info>2015_10_04_000000_modify_people_table:</info> alter table "people" add column "first_name" varchar');
$this->expectOutput('<info>2016_10_04_000000_modify_people_table:</info> alter table "people" add column "last_name" varchar');

$this->subject->run([__DIR__.'/fixtures'], ['pretend' => true]);

return true;
self::assertFalse(DB::getSchemaBuilder()->hasTable('people'));
}

private function expectOutput($argument): void
{
$this->output->shouldReceive('writeln')->once()->with($argument);
}
}
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;

return new class extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('people', function (Blueprint $table) {
$table->string('first_name')->nullable();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('people', function (Blueprint $table) {
$table->dropColumn('first_name');
});
}
};
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;

return new class extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('people', function (Blueprint $table) {
$table->string('last_name')->nullable();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('people', function (Blueprint $table) {
$table->dropColumn('last_name');
});
}
};

0 comments on commit 9cdb5cc

Please sign in to comment.