SIGN IN SIGN UP
laravel / framework UNCLAIMED

Laravel is a web application framework with expressive, elegant syntax.

0 0 2 PHP
2016-05-25 11:21:56 -05:00
<?php
namespace Illuminate\Tests\Database;
use Illuminate\Console\OutputStyle;
use Illuminate\Container\Container;
2016-05-25 11:21:56 -05:00
use Illuminate\Database\Capsule\Manager as DB;
use Illuminate\Database\Migrations\DatabaseMigrationRepository;
use Illuminate\Database\Migrations\Migrator;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Facades\Facade;
use Illuminate\Support\Str;
use Mockery as m;
use PHPUnit\Framework\TestCase;
2016-05-25 11:21:56 -05:00
class DatabaseMigratorIntegrationTest extends TestCase
2016-05-25 11:21:56 -05:00
{
protected $db;
protected $migrator;
2016-05-25 11:21:56 -05:00
/**
* Bootstrap Eloquent.
*
* @return void
*/
2019-02-08 23:05:58 +01:00
protected function setUp(): void
2016-05-25 11:21:56 -05:00
{
$this->db = $db = new DB;
$db->addConnection([
'driver' => 'sqlite',
'database' => ':memory:',
2016-05-25 11:21:56 -05:00
]);
$db->addConnection([
'driver' => 'sqlite',
'database' => ':memory:',
], 'sqlite2');
$db->addConnection([
'driver' => 'sqlite',
'database' => ':memory:',
], 'sqlite3');
2016-05-25 11:21:56 -05:00
$db->setAsGlobal();
$container = new Container;
2016-05-25 13:01:38 -05:00
$container->instance('db', $db->getDatabaseManager());
$container->bind('db.schema', function ($app) {
return $app['db']->connection()->getSchemaBuilder();
});
2018-10-03 15:08:43 -05:00
Facade::setFacadeApplication($container);
2016-05-25 13:01:38 -05:00
2016-05-25 11:21:56 -05:00
$this->migrator = new Migrator(
$repository = new DatabaseMigrationRepository($db->getDatabaseManager(), 'migrations'),
$db->getDatabaseManager(),
new Filesystem
);
$output = m::mock(OutputStyle::class);
[9.x] Introducing a fresh new look for Artisan (#43065) * Improves console output * Apply fixes from StyleCI * Improves confirmable trait wording * Refactors to components * Improves vendor publish * Apply fixes from StyleCI * Fixes displaying line without style * Adds Bullet List componenent * Adds Alert component * Gives access to bullet list and detail on the command * Improves Detail output * Fixes Line info output * Truncates given information to componenents * Improves `migrate:fresh` output * Improves `migrate` output * Improves `make:migrate` output * Improves `migrate:status` output * Improves `migrate` output * Refactors `event:list` output * Improves `queue:work` output * Apply fixes from StyleCI * Fixes static analysis * Improves `queue:listen` output * Swaps dynamic part of the detail to the left. * Apply fixes from StyleCI * Improves `queue:monitor` output * Adds title to migrate status command * Apply fixes from StyleCI * Improves `schedule:test` output * Apply fixes from StyleCI * Fixes length of dots * Fixes extra style * Improves `schedule:run` output * Fixes missing style * Improves `schedule:work` output * Improve "inspire" command * Refactors inspiring * Refactors line * Avoids conflicts by naming "x-line" to "x-illuminate-console-line" * Avoids `ConsoleServiceProvider` * Apply fixes from StyleCI * Adds space to breath * Fixes margin top * Removes `tasks` and `task` from the public api * Apply fixes from StyleCI * Fixes new line written * Remove `bulletList` and `detail` * Renames `Detail` to `TwoColumnDetail` * Apply fixes from StyleCI * Removes unused code * Fixes two columns view * Reverts changes on `$this->alert` * Always finishes with a dot * Uses task component only when is a task * Fixes access to parent question helper * Fixes missing new line * Renames `renderUsing` to `render` * Moves components resources directory * Fixes multiple listeners on `event:list` * Uses `$this->components` to print output * Apply fixes from StyleCI * Removes blade dependency * Apply fixes from StyleCI * Fixes more tests * Fixes more tests * Fixes tests when using PHP 8.0 * Rewords `env` command * Asks previous output if new line was written * Improves migrate command * Rewords `optimize:clear` * Fixes new lines on migrations * Apply fixes from StyleCI * Improves `model:prune` command * Only prints new line when packages found * Improves `list:failed` command * Apply fixes from StyleCI * Improves `queue:retry` and `queue:retry-batch` wording * Improve `schedule:run` command * Makes `schedule:test` very similar to `schedule:run` * Apply fixes from StyleCI * Fixes tests * Improves wording * Removes non-used file * Uses latest version of Termwind * Fixes tests on windows * Adds tests to components * Apply fixes from StyleCI * Updates `env` wording * Updates question first and second color * Adds skipped wording on `vendor:publish` * Adds missing method on interface and tests * Adds test on underlying output * Uses `PHP_EOL` for windows tests * Fixes windows tests * Apply fixes from StyleCI * Displays duration always, except when there is no task at all * Improves `db:seed` output * Improves new line on `seed` command * Fixes seeding tests * Ensures max width * formatting Co-authored-by: StyleCI Bot <bot@styleci.io> Co-authored-by: Taylor Otwell <taylor@laravel.com>
2022-07-14 15:38:53 +01:00
$output->shouldReceive('write');
$output->shouldReceive('writeln');
$output->shouldReceive('newLinesWritten');
$this->migrator->setOutput($output);
2016-05-25 11:21:56 -05:00
if (! $repository->repositoryExists()) {
$repository->createRepository();
}
$repository2 = new DatabaseMigrationRepository($db->getDatabaseManager(), 'migrations');
$repository2->setSource('sqlite2');
if (! $repository2->repositoryExists()) {
$repository2->createRepository();
}
2016-05-25 11:21:56 -05:00
}
2019-02-08 23:05:58 +01:00
protected function tearDown(): void
2016-05-25 11:21:56 -05:00
{
Facade::clearResolvedInstances();
Facade::setFacadeApplication(null);
parent::tearDown();
2016-05-25 11:21:56 -05:00
}
public function testBasicMigrationOfSingleFolder()
{
2016-05-25 11:25:47 -05:00
$ran = $this->migrator->run([__DIR__.'/migrations/one']);
$this->assertTrue($this->db::schema()->hasTable('users'));
$this->assertTrue($this->db::schema()->hasTable('password_resets'));
2016-05-25 11:25:47 -05:00
2022-01-26 16:40:14 +01:00
$this->assertTrue(str_contains($ran[0], 'users'));
$this->assertTrue(str_contains($ran[1], 'password_resets'));
2016-05-25 11:21:56 -05:00
}
public function testMigrationsDefaultConnectionCanBeChanged()
{
$ran = $this->migrator->usingConnection('sqlite2', function () {
return $this->migrator->run([__DIR__.'/migrations/one'], ['database' => 'sqllite3']);
});
$this->assertFalse($this->db::schema()->hasTable('users'));
$this->assertFalse($this->db::schema()->hasTable('password_resets'));
$this->assertTrue($this->db::schema('sqlite2')->hasTable('users'));
$this->assertTrue($this->db::schema('sqlite2')->hasTable('password_resets'));
$this->assertFalse($this->db::schema('sqlite3')->hasTable('users'));
$this->assertFalse($this->db::schema('sqlite3')->hasTable('password_resets'));
$this->assertTrue(Str::contains($ran[0], 'users'));
$this->assertTrue(Str::contains($ran[1], 'password_resets'));
}
public function testMigrationsCanEachDefineConnection()
{
$ran = $this->migrator->run([__DIR__.'/migrations/connection_configured']);
$this->assertFalse($this->db::schema()->hasTable('failed_jobs'));
$this->assertFalse($this->db::schema()->hasTable('jobs'));
$this->assertFalse($this->db::schema('sqlite2')->hasTable('failed_jobs'));
$this->assertFalse($this->db::schema('sqlite2')->hasTable('jobs'));
$this->assertTrue($this->db::schema('sqlite3')->hasTable('failed_jobs'));
$this->assertTrue($this->db::schema('sqlite3')->hasTable('jobs'));
$this->assertTrue(Str::contains($ran[0], 'failed_jobs'));
$this->assertTrue(Str::contains($ran[1], 'jobs'));
}
public function testMigratorCannotChangeDefinedMigrationConnection()
{
$ran = $this->migrator->usingConnection('sqlite2', function () {
return $this->migrator->run([__DIR__.'/migrations/connection_configured']);
});
$this->assertFalse($this->db::schema()->hasTable('failed_jobs'));
$this->assertFalse($this->db::schema()->hasTable('jobs'));
$this->assertFalse($this->db::schema('sqlite2')->hasTable('failed_jobs'));
$this->assertFalse($this->db::schema('sqlite2')->hasTable('jobs'));
$this->assertTrue($this->db::schema('sqlite3')->hasTable('failed_jobs'));
$this->assertTrue($this->db::schema('sqlite3')->hasTable('jobs'));
$this->assertTrue(Str::contains($ran[0], 'failed_jobs'));
$this->assertTrue(Str::contains($ran[1], 'jobs'));
}
2016-05-25 11:21:56 -05:00
public function testMigrationsCanBeRolledBack()
{
$this->migrator->run([__DIR__.'/migrations/one']);
$this->assertTrue($this->db::schema()->hasTable('users'));
$this->assertTrue($this->db::schema()->hasTable('password_resets'));
2016-05-25 11:29:42 -05:00
$rolledBack = $this->migrator->rollback([__DIR__.'/migrations/one']);
$this->assertFalse($this->db::schema()->hasTable('users'));
$this->assertFalse($this->db::schema()->hasTable('password_resets'));
2016-05-25 11:29:42 -05:00
2022-01-26 16:40:14 +01:00
$this->assertTrue(str_contains($rolledBack[0], 'password_resets'));
$this->assertTrue(str_contains($rolledBack[1], 'users'));
2016-05-25 11:21:56 -05:00
}
public function testMigrationsCanBeResetUsingAnString()
{
$this->migrator->run([__DIR__.'/migrations/one']);
$this->assertTrue($this->db::schema()->hasTable('users'));
$this->assertTrue($this->db::schema()->hasTable('password_resets'));
$rolledBack = $this->migrator->reset(__DIR__.'/migrations/one');
$this->assertFalse($this->db::schema()->hasTable('users'));
$this->assertFalse($this->db::schema()->hasTable('password_resets'));
$this->assertTrue(str_contains($rolledBack[0], 'password_resets'));
$this->assertTrue(str_contains($rolledBack[1], 'users'));
}
public function testMigrationsCanBeResetUsingAnArray()
2016-05-25 11:21:56 -05:00
{
$this->migrator->run([__DIR__.'/migrations/one']);
$this->assertTrue($this->db::schema()->hasTable('users'));
$this->assertTrue($this->db::schema()->hasTable('password_resets'));
2016-05-25 11:29:42 -05:00
$rolledBack = $this->migrator->reset([__DIR__.'/migrations/one']);
$this->assertFalse($this->db::schema()->hasTable('users'));
$this->assertFalse($this->db::schema()->hasTable('password_resets'));
2016-05-25 11:29:42 -05:00
2022-01-26 16:40:14 +01:00
$this->assertTrue(str_contains($rolledBack[0], 'password_resets'));
$this->assertTrue(str_contains($rolledBack[1], 'users'));
2016-05-25 11:21:56 -05:00
}
public function testNoErrorIsThrownWhenNoOutstandingMigrationsExist()
{
$this->migrator->run([__DIR__.'/migrations/one']);
$this->assertTrue($this->db::schema()->hasTable('users'));
$this->assertTrue($this->db::schema()->hasTable('password_resets'));
2016-05-25 11:21:56 -05:00
$this->migrator->run([__DIR__.'/migrations/one']);
}
public function testNoErrorIsThrownWhenNothingToRollback()
{
$this->migrator->run([__DIR__.'/migrations/one']);
$this->assertTrue($this->db::schema()->hasTable('users'));
$this->assertTrue($this->db::schema()->hasTable('password_resets'));
2016-05-25 11:21:56 -05:00
$this->migrator->rollback([__DIR__.'/migrations/one']);
$this->assertFalse($this->db::schema()->hasTable('users'));
$this->assertFalse($this->db::schema()->hasTable('password_resets'));
2016-05-25 11:21:56 -05:00
$this->migrator->rollback([__DIR__.'/migrations/one']);
}
public function testMigrationsCanRunAcrossMultiplePaths()
{
$this->migrator->run([__DIR__.'/migrations/one', __DIR__.'/migrations/two']);
$this->assertTrue($this->db::schema()->hasTable('users'));
$this->assertTrue($this->db::schema()->hasTable('password_resets'));
$this->assertTrue($this->db::schema()->hasTable('flights'));
2016-05-25 11:21:56 -05:00
}
public function testMigrationsCanBeRolledBackAcrossMultiplePaths()
{
$this->migrator->run([__DIR__.'/migrations/one', __DIR__.'/migrations/two']);
$this->assertTrue($this->db::schema()->hasTable('users'));
$this->assertTrue($this->db::schema()->hasTable('password_resets'));
$this->assertTrue($this->db::schema()->hasTable('flights'));
2016-05-25 11:21:56 -05:00
$this->migrator->rollback([__DIR__.'/migrations/one', __DIR__.'/migrations/two']);
$this->assertFalse($this->db::schema()->hasTable('users'));
$this->assertFalse($this->db::schema()->hasTable('password_resets'));
$this->assertFalse($this->db::schema()->hasTable('flights'));
2016-05-25 11:21:56 -05:00
}
public function testMigrationsCanBeResetAcrossMultiplePaths()
{
$this->migrator->run([__DIR__.'/migrations/one', __DIR__.'/migrations/two']);
$this->assertTrue($this->db::schema()->hasTable('users'));
$this->assertTrue($this->db::schema()->hasTable('password_resets'));
$this->assertTrue($this->db::schema()->hasTable('flights'));
2016-05-25 11:21:56 -05:00
$this->migrator->reset([__DIR__.'/migrations/one', __DIR__.'/migrations/two']);
$this->assertFalse($this->db::schema()->hasTable('users'));
$this->assertFalse($this->db::schema()->hasTable('password_resets'));
$this->assertFalse($this->db::schema()->hasTable('flights'));
2016-05-25 11:21:56 -05:00
}
public function testMigrationsCanBeProperlySortedAcrossMultiplePaths()
{
$paths = [__DIR__.'/migrations/multi_path/vendor', __DIR__.'/migrations/multi_path/app'];
$migrationsFilesFullPaths = array_values($this->migrator->getMigrationFiles($paths));
$expected = [
__DIR__.'/migrations/multi_path/app/2016_01_01_000000_create_users_table.php', // This file was not created on the "vendor" directory on purpose
__DIR__.'/migrations/multi_path/vendor/2016_01_01_200000_create_flights_table.php', // This file was not created on the "app" directory on purpose
__DIR__.'/migrations/multi_path/app/2019_08_08_000001_rename_table_one.php',
__DIR__.'/migrations/multi_path/app/2019_08_08_000002_rename_table_two.php',
__DIR__.'/migrations/multi_path/app/2019_08_08_000003_rename_table_three.php',
__DIR__.'/migrations/multi_path/app/2019_08_08_000004_rename_table_four.php',
__DIR__.'/migrations/multi_path/app/2019_08_08_000005_create_table_one.php',
__DIR__.'/migrations/multi_path/app/2019_08_08_000006_create_table_two.php',
__DIR__.'/migrations/multi_path/vendor/2019_08_08_000007_create_table_three.php', // This file was not created on the "app" directory on purpose
__DIR__.'/migrations/multi_path/app/2019_08_08_000008_create_table_four.php',
];
$this->assertEquals($expected, $migrationsFilesFullPaths);
}
public function testConnectionPriorToMigrationIsNotChangedAfterMigration()
{
$this->migrator->setConnection('default');
$this->migrator->run([__DIR__.'/migrations/one'], ['database' => 'sqlite2']);
$this->assertSame('default', $this->migrator->getConnection());
}
public function testConnectionPriorToMigrationIsNotChangedAfterRollback()
{
$this->migrator->setConnection('default');
$this->migrator->run([__DIR__.'/migrations/one'], ['database' => 'sqlite2']);
$this->migrator->rollback([__DIR__.'/migrations/one'], ['database' => 'sqlite2']);
$this->assertSame('default', $this->migrator->getConnection());
}
public function testConnectionPriorToMigrationIsNotChangedWhenNoOutstandingMigrationsExist()
{
$this->migrator->setConnection('default');
$this->migrator->run([__DIR__.'/migrations/one'], ['database' => 'sqlite2']);
$this->migrator->setConnection('default');
$this->migrator->run([__DIR__.'/migrations/one'], ['database' => 'sqlite2']);
$this->assertSame('default', $this->migrator->getConnection());
}
public function testConnectionPriorToMigrationIsNotChangedWhenNothingToRollback()
{
$this->migrator->setConnection('default');
$this->migrator->run([__DIR__.'/migrations/one'], ['database' => 'sqlite2']);
$this->migrator->rollback([__DIR__.'/migrations/one'], ['database' => 'sqlite2']);
$this->migrator->rollback([__DIR__.'/migrations/one'], ['database' => 'sqlite2']);
$this->assertSame('default', $this->migrator->getConnection());
}
public function testConnectionPriorToMigrationIsNotChangedAfterMigrateReset()
{
$this->migrator->setConnection('default');
$this->migrator->run([__DIR__.'/migrations/one'], ['database' => 'sqlite2']);
$this->migrator->reset([__DIR__.'/migrations/one'], ['database' => 'sqlite2']);
$this->assertSame('default', $this->migrator->getConnection());
}
2016-05-25 11:21:56 -05:00
}