mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-12-02 11:48:08 +08:00
Added migrate command unit test
This commit is contained in:
parent
68e51448f6
commit
298d036170
117
src/database/tests/DatabaseMigrationMigrateCommandTest.php
Executable file
117
src/database/tests/DatabaseMigrationMigrateCommandTest.php
Executable file
@ -0,0 +1,117 @@
|
||||
<?php
|
||||
|
||||
namespace HyperfTest\Database;
|
||||
|
||||
use Mockery;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Hyperf\Database\Migrations\Migrator;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Symfony\Component\Console\Input\ArrayInput;
|
||||
use Symfony\Component\Console\Output\NullOutput;
|
||||
use Hyperf\Database\Commands\Migrations\MigrateCommand;
|
||||
|
||||
class DatabaseMigrationMigrateCommandTest extends TestCase
|
||||
{
|
||||
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
! defined('BASE_PATH') && define('BASE_PATH', __DIR__);
|
||||
}
|
||||
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
Mockery::close();
|
||||
}
|
||||
|
||||
public function testBasicMigrationsCallMigratorWithProperArguments()
|
||||
{
|
||||
$command = new MigrateCommand($migrator = Mockery::mock(Migrator::class));
|
||||
$migrator->shouldReceive('paths')->once()->andReturn([]);
|
||||
$migrator->shouldReceive('setConnection')->once()->with('default');
|
||||
$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 testMigrationRepositoryCreatedWhenNecessary()
|
||||
{
|
||||
$params = [$migrator = Mockery::mock(Migrator::class)];
|
||||
$command = $this->getMockBuilder(MigrateCommand::class)->setMethods(['call'])->setConstructorArgs($params)->getMock();
|
||||
$migrator->shouldReceive('paths')->once()->andReturn([]);
|
||||
$migrator->shouldReceive('setConnection')->once()->with('default');
|
||||
$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 = Mockery::mock(Migrator::class));
|
||||
$migrator->shouldReceive('paths')->once()->andReturn([]);
|
||||
$migrator->shouldReceive('setConnection')->once()->with('default');
|
||||
$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 = Mockery::mock(Migrator::class));
|
||||
$migrator->shouldReceive('paths')->once()->andReturn([]);
|
||||
$migrator->shouldReceive('setConnection')->once()->with('foo');
|
||||
$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 = Mockery::mock(Migrator::class));
|
||||
$migrator->shouldReceive('paths')->once()->andReturn([]);
|
||||
$migrator->shouldReceive('setConnection')->once()->with('default');
|
||||
$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
|
||||
{
|
||||
/**
|
||||
* @var \Psr\Container\ContainerInterface
|
||||
*/
|
||||
private $container;
|
||||
|
||||
public function __construct(array $data = [])
|
||||
{
|
||||
$this->container = Mockery::mock(ContainerInterface::class);
|
||||
foreach ($data as $abstract => $instance) {
|
||||
$this->container->shouldReceive('get')->with($abstract)->andReturn(class_exists($instance) ? new $instance : $instance);
|
||||
}
|
||||
}
|
||||
|
||||
public function environment(...$environments)
|
||||
{
|
||||
return 'development';
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user