2016-07-17 04:04:12 +07:00
|
|
|
<?php
|
|
|
|
|
|
2017-01-17 14:30:19 +00:00
|
|
|
namespace Illuminate\Tests\Database;
|
|
|
|
|
|
2020-10-24 10:46:19 -05:00
|
|
|
use Illuminate\Contracts\Events\Dispatcher;
|
2016-07-17 04:04:12 +07:00
|
|
|
use Illuminate\Database\Console\Migrations\MigrateCommand;
|
|
|
|
|
use Illuminate\Database\Console\Migrations\RefreshCommand;
|
2019-09-10 17:16:05 +02:00
|
|
|
use Illuminate\Database\Console\Migrations\ResetCommand;
|
2016-07-17 04:04:12 +07:00
|
|
|
use Illuminate\Database\Console\Migrations\RollbackCommand;
|
2020-10-24 10:46:19 -05:00
|
|
|
use Illuminate\Database\Events\DatabaseRefreshed;
|
2019-09-10 17:16:05 +02:00
|
|
|
use Illuminate\Foundation\Application;
|
|
|
|
|
use Mockery as m;
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
2016-07-17 04:04:12 +07:00
|
|
|
use Symfony\Component\Console\Application as ConsoleApplication;
|
2019-09-10 17:16:05 +02:00
|
|
|
use Symfony\Component\Console\Input\ArrayInput;
|
|
|
|
|
use Symfony\Component\Console\Output\NullOutput;
|
2016-07-17 04:04:12 +07:00
|
|
|
|
2016-12-30 21:31:11 +01:00
|
|
|
class DatabaseMigrationRefreshCommandTest extends TestCase
|
2016-07-17 04:04:12 +07:00
|
|
|
{
|
2019-02-08 23:05:58 +01:00
|
|
|
protected function tearDown(): void
|
2016-07-17 04:04:12 +07:00
|
|
|
{
|
2024-05-21 14:41:48 -04:00
|
|
|
RefreshCommand::prohibit(false);
|
2026-01-05 18:12:30 +01:00
|
|
|
|
|
|
|
|
parent::tearDown();
|
2016-07-17 04:04:12 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testRefreshCommandCallsCommandsWithProperArguments()
|
|
|
|
|
{
|
2021-03-19 01:05:33 +01:00
|
|
|
$command = new RefreshCommand;
|
2016-07-17 04:04:12 +07:00
|
|
|
|
|
|
|
|
$app = new ApplicationDatabaseRefreshStub(['path.database' => __DIR__]);
|
2020-10-26 11:43:18 -05:00
|
|
|
$dispatcher = $app->instance(Dispatcher::class, $events = m::mock());
|
2016-07-17 04:04:12 +07:00
|
|
|
$console = m::mock(ConsoleApplication::class)->makePartial();
|
|
|
|
|
$console->__construct();
|
|
|
|
|
$command->setLaravel($app);
|
|
|
|
|
$command->setApplication($console);
|
|
|
|
|
|
|
|
|
|
$resetCommand = m::mock(ResetCommand::class);
|
|
|
|
|
$migrateCommand = m::mock(MigrateCommand::class);
|
|
|
|
|
|
|
|
|
|
$console->shouldReceive('find')->with('migrate:reset')->andReturn($resetCommand);
|
|
|
|
|
$console->shouldReceive('find')->with('migrate')->andReturn($migrateCommand);
|
2020-10-24 10:46:19 -05:00
|
|
|
$dispatcher->shouldReceive('dispatch')->once()->with(m::type(DatabaseRefreshed::class));
|
2016-07-17 04:04:12 +07:00
|
|
|
|
2021-03-18 22:27:31 +01:00
|
|
|
$quote = DIRECTORY_SEPARATOR === '\\' ? '"' : "'";
|
2018-12-31 16:25:02 +00:00
|
|
|
$resetCommand->shouldReceive('run')->with(new InputMatcher("--force=1 {$quote}migrate:reset{$quote}"), m::any());
|
|
|
|
|
$migrateCommand->shouldReceive('run')->with(new InputMatcher('--force=1 migrate'), m::any());
|
2016-07-17 04:04:12 +07:00
|
|
|
|
|
|
|
|
$this->runCommand($command);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testRefreshCommandCallsCommandsWithStep()
|
|
|
|
|
{
|
2021-03-19 01:05:33 +01:00
|
|
|
$command = new RefreshCommand;
|
2016-07-17 04:04:12 +07:00
|
|
|
|
|
|
|
|
$app = new ApplicationDatabaseRefreshStub(['path.database' => __DIR__]);
|
2020-10-26 11:43:18 -05:00
|
|
|
$dispatcher = $app->instance(Dispatcher::class, $events = m::mock());
|
2016-07-17 04:04:12 +07:00
|
|
|
$console = m::mock(ConsoleApplication::class)->makePartial();
|
|
|
|
|
$console->__construct();
|
|
|
|
|
$command->setLaravel($app);
|
|
|
|
|
$command->setApplication($console);
|
|
|
|
|
|
|
|
|
|
$rollbackCommand = m::mock(RollbackCommand::class);
|
|
|
|
|
$migrateCommand = m::mock(MigrateCommand::class);
|
|
|
|
|
|
|
|
|
|
$console->shouldReceive('find')->with('migrate:rollback')->andReturn($rollbackCommand);
|
|
|
|
|
$console->shouldReceive('find')->with('migrate')->andReturn($migrateCommand);
|
2020-10-24 10:46:19 -05:00
|
|
|
$dispatcher->shouldReceive('dispatch')->once()->with(m::type(DatabaseRefreshed::class));
|
2016-07-17 04:04:12 +07:00
|
|
|
|
2021-03-18 22:27:31 +01:00
|
|
|
$quote = DIRECTORY_SEPARATOR === '\\' ? '"' : "'";
|
2018-12-31 16:25:02 +00:00
|
|
|
$rollbackCommand->shouldReceive('run')->with(new InputMatcher("--step=2 --force=1 {$quote}migrate:rollback{$quote}"), m::any());
|
|
|
|
|
$migrateCommand->shouldReceive('run')->with(new InputMatcher('--force=1 migrate'), m::any());
|
2016-07-17 04:04:12 +07:00
|
|
|
|
|
|
|
|
$this->runCommand($command, ['--step' => 2]);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-22 16:56:25 +01:00
|
|
|
public function testRefreshCommandExitsWhenProhibited()
|
2024-05-21 14:41:48 -04:00
|
|
|
{
|
|
|
|
|
$command = new RefreshCommand;
|
|
|
|
|
|
|
|
|
|
$app = new ApplicationDatabaseRefreshStub(['path.database' => __DIR__]);
|
|
|
|
|
$dispatcher = $app->instance(Dispatcher::class, $events = m::mock());
|
|
|
|
|
$console = m::mock(ConsoleApplication::class)->makePartial();
|
|
|
|
|
$console->__construct();
|
|
|
|
|
$command->setLaravel($app);
|
|
|
|
|
$command->setApplication($console);
|
|
|
|
|
|
|
|
|
|
RefreshCommand::prohibit();
|
|
|
|
|
|
|
|
|
|
$code = $this->runCommand($command);
|
|
|
|
|
|
|
|
|
|
$this->assertSame(1, $code);
|
|
|
|
|
|
|
|
|
|
$console->shouldNotHaveBeenCalled();
|
|
|
|
|
$dispatcher->shouldNotReceive('dispatch');
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-17 04:04:12 +07:00
|
|
|
protected function runCommand($command, $input = [])
|
|
|
|
|
{
|
2018-10-03 22:05:43 +02:00
|
|
|
return $command->run(new ArrayInput($input), new NullOutput);
|
2016-07-17 04:04:12 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class InputMatcher extends m\Matcher\MatcherAbstract
|
|
|
|
|
{
|
|
|
|
|
/**
|
2016-11-05 00:02:29 -06:00
|
|
|
* @param \Symfony\Component\Console\Input\ArrayInput $actual
|
2016-07-17 04:04:12 +07:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function match(&$actual)
|
|
|
|
|
{
|
|
|
|
|
return (string) $actual == $this->_expected;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function __toString()
|
|
|
|
|
{
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class ApplicationDatabaseRefreshStub extends Application
|
|
|
|
|
{
|
|
|
|
|
public function __construct(array $data = [])
|
|
|
|
|
{
|
|
|
|
|
foreach ($data as $abstract => $instance) {
|
|
|
|
|
$this->instance($abstract, $instance);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-29 21:20:45 +01:00
|
|
|
public function environment(...$environments)
|
2016-07-17 04:04:12 +07:00
|
|
|
{
|
|
|
|
|
return 'development';
|
|
|
|
|
}
|
|
|
|
|
}
|