123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- <?php
- namespace Illuminate\Tests\Database;
- use Illuminate\Contracts\Events\Dispatcher;
- use Illuminate\Database\Console\Migrations\MigrateCommand;
- use Illuminate\Database\Events\SchemaLoaded;
- use Illuminate\Database\Migrations\Migrator;
- use Illuminate\Foundation\Application;
- use Mockery as m;
- use PHPUnit\Framework\TestCase;
- use stdClass;
- use Symfony\Component\Console\Input\ArrayInput;
- use Symfony\Component\Console\Output\NullOutput;
- class DatabaseMigrationMigrateCommandTest extends TestCase
- {
- protected function tearDown(): void
- {
- m::close();
- }
- public function testBasicMigrationsCallMigratorWithProperArguments()
- {
- $command = new MigrateCommand($migrator = m::mock(Migrator::class), $dispatcher = m::mock(Dispatcher::class));
- $app = new ApplicationDatabaseMigrationStub(['path.database' => __DIR__]);
- $app->useDatabasePath(__DIR__);
- $command->setLaravel($app);
- $migrator->shouldReceive('paths')->once()->andReturn([]);
- $migrator->shouldReceive('hasRunAnyMigrations')->andReturn(true);
- $migrator->shouldReceive('usingConnection')->once()->andReturnUsing(function ($name, $callback) {
- return $callback();
- });
- $migrator->shouldReceive('setOutput')->once()->andReturn($migrator);
- $migrator->shouldReceive('run')->once()->with([__DIR__.DIRECTORY_SEPARATOR.'migrations'], ['pretend' => false, 'step' => false]);
- $migrator->shouldReceive('getNotes')->andReturn([]);
- $migrator->shouldReceive('repositoryExists')->once()->andReturn(true);
- $this->runCommand($command);
- }
- public function testMigrationsCanBeRunWithStoredSchema()
- {
- $command = new MigrateCommand($migrator = m::mock(Migrator::class), $dispatcher = m::mock(Dispatcher::class));
- $app = new ApplicationDatabaseMigrationStub(['path.database' => __DIR__]);
- $app->useDatabasePath(__DIR__);
- $command->setLaravel($app);
- $migrator->shouldReceive('paths')->once()->andReturn([]);
- $migrator->shouldReceive('hasRunAnyMigrations')->andReturn(false);
- $migrator->shouldReceive('resolveConnection')->andReturn($connection = m::mock(stdClass::class));
- $connection->shouldReceive('getName')->andReturn('mysql');
- $migrator->shouldReceive('usingConnection')->once()->andReturnUsing(function ($name, $callback) {
- return $callback();
- });
- $migrator->shouldReceive('deleteRepository')->once();
- $connection->shouldReceive('getSchemaState')->andReturn($schemaState = m::mock(stdClass::class));
- $schemaState->shouldReceive('handleOutputUsing')->andReturnSelf();
- $schemaState->shouldReceive('load')->once()->with(__DIR__.'/stubs/schema.sql');
- $dispatcher->shouldReceive('dispatch')->once()->with(m::type(SchemaLoaded::class));
- $migrator->shouldReceive('setOutput')->once()->andReturn($migrator);
- $migrator->shouldReceive('run')->once()->with([__DIR__.DIRECTORY_SEPARATOR.'migrations'], ['pretend' => false, 'step' => false]);
- $migrator->shouldReceive('getNotes')->andReturn([]);
- $migrator->shouldReceive('repositoryExists')->once()->andReturn(true);
- $this->runCommand($command, ['--schema-path' => __DIR__.'/stubs/schema.sql']);
- }
- public function testMigrationRepositoryCreatedWhenNecessary()
- {
- $params = [$migrator = m::mock(Migrator::class), $dispatcher = m::mock(Dispatcher::class)];
- $command = $this->getMockBuilder(MigrateCommand::class)->onlyMethods(['call'])->setConstructorArgs($params)->getMock();
- $app = new ApplicationDatabaseMigrationStub(['path.database' => __DIR__]);
- $app->useDatabasePath(__DIR__);
- $command->setLaravel($app);
- $migrator->shouldReceive('paths')->once()->andReturn([]);
- $migrator->shouldReceive('hasRunAnyMigrations')->andReturn(true);
- $migrator->shouldReceive('usingConnection')->once()->andReturnUsing(function ($name, $callback) {
- return $callback();
- });
- $migrator->shouldReceive('setOutput')->once()->andReturn($migrator);
- $migrator->shouldReceive('run')->once()->with([__DIR__.DIRECTORY_SEPARATOR.'migrations'], ['pretend' => false, 'step' => false]);
- $migrator->shouldReceive('repositoryExists')->once()->andReturn(false);
- $command->expects($this->once())->method('call')->with($this->equalTo('migrate:install'), $this->equalTo([]));
- $this->runCommand($command);
- }
- public function testTheCommandMayBePretended()
- {
- $command = new MigrateCommand($migrator = m::mock(Migrator::class), $dispatcher = m::mock(Dispatcher::class));
- $app = new ApplicationDatabaseMigrationStub(['path.database' => __DIR__]);
- $app->useDatabasePath(__DIR__);
- $command->setLaravel($app);
- $migrator->shouldReceive('paths')->once()->andReturn([]);
- $migrator->shouldReceive('hasRunAnyMigrations')->andReturn(true);
- $migrator->shouldReceive('usingConnection')->once()->andReturnUsing(function ($name, $callback) {
- return $callback();
- });
- $migrator->shouldReceive('setOutput')->once()->andReturn($migrator);
- $migrator->shouldReceive('run')->once()->with([__DIR__.DIRECTORY_SEPARATOR.'migrations'], ['pretend' => true, 'step' => false]);
- $migrator->shouldReceive('repositoryExists')->once()->andReturn(true);
- $this->runCommand($command, ['--pretend' => true]);
- }
- public function testTheDatabaseMayBeSet()
- {
- $command = new MigrateCommand($migrator = m::mock(Migrator::class), $dispatcher = m::mock(Dispatcher::class));
- $app = new ApplicationDatabaseMigrationStub(['path.database' => __DIR__]);
- $app->useDatabasePath(__DIR__);
- $command->setLaravel($app);
- $migrator->shouldReceive('paths')->once()->andReturn([]);
- $migrator->shouldReceive('hasRunAnyMigrations')->andReturn(true);
- $migrator->shouldReceive('usingConnection')->once()->andReturnUsing(function ($name, $callback) {
- return $callback();
- });
- $migrator->shouldReceive('setOutput')->once()->andReturn($migrator);
- $migrator->shouldReceive('run')->once()->with([__DIR__.DIRECTORY_SEPARATOR.'migrations'], ['pretend' => false, 'step' => false]);
- $migrator->shouldReceive('repositoryExists')->once()->andReturn(true);
- $this->runCommand($command, ['--database' => 'foo']);
- }
- public function testStepMayBeSet()
- {
- $command = new MigrateCommand($migrator = m::mock(Migrator::class), $dispatcher = m::mock(Dispatcher::class));
- $app = new ApplicationDatabaseMigrationStub(['path.database' => __DIR__]);
- $app->useDatabasePath(__DIR__);
- $command->setLaravel($app);
- $migrator->shouldReceive('paths')->once()->andReturn([]);
- $migrator->shouldReceive('hasRunAnyMigrations')->andReturn(true);
- $migrator->shouldReceive('usingConnection')->once()->andReturnUsing(function ($name, $callback) {
- return $callback();
- });
- $migrator->shouldReceive('setOutput')->once()->andReturn($migrator);
- $migrator->shouldReceive('run')->once()->with([__DIR__.DIRECTORY_SEPARATOR.'migrations'], ['pretend' => false, 'step' => true]);
- $migrator->shouldReceive('repositoryExists')->once()->andReturn(true);
- $this->runCommand($command, ['--step' => true]);
- }
- protected function runCommand($command, $input = [])
- {
- return $command->run(new ArrayInput($input), new NullOutput);
- }
- }
- class ApplicationDatabaseMigrationStub extends Application
- {
- public function __construct(array $data = [])
- {
- foreach ($data as $abstract => $instance) {
- $this->instance($abstract, $instance);
- }
- }
- public function environment(...$environments)
- {
- return 'development';
- }
- }
|