Added unit test of migrate:reset command

This commit is contained in:
huangzhhui 2019-05-31 17:47:56 +08:00
parent 8bdf6a1a04
commit e9fef2743a

View File

@ -0,0 +1,69 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://hyperf.io
* @document https://doc.hyperf.io
* @contact group@hyperf.io
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\Database;
use Hyperf\Database\Commands\Migrations\ResetCommand;
use Hyperf\Database\Migrations\Migrator;
use Mockery;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;
/**
* @internal
* @coversNothing
*/
class DatabaseMigrationResetCommandTest 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 testResetCommandCallsMigratorWithProperArguments()
{
$command = new ResetCommand($migrator = Mockery::mock(Migrator::class));
$migrator->shouldReceive('paths')->once()->andReturn([]);
$migrator->shouldReceive('setConnection')->once()->with('default');
$migrator->shouldReceive('repositoryExists')->once()->andReturn(true);
$migrator->shouldReceive('setOutput')->once()->andReturn($migrator);
$migrator->shouldReceive('reset')->once()->with([__DIR__ . DIRECTORY_SEPARATOR . 'migrations'], false);
$this->runCommand($command);
}
public function testResetCommandCanBePretended()
{
$command = new ResetCommand($migrator = Mockery::mock(Migrator::class));
$migrator->shouldReceive('paths')->once()->andReturn([]);
$migrator->shouldReceive('setConnection')->once()->with('foo');
$migrator->shouldReceive('repositoryExists')->once()->andReturn(true);
$migrator->shouldReceive('setOutput')->once()->andReturn($migrator);
$migrator->shouldReceive('reset')->once()->with([__DIR__ . DIRECTORY_SEPARATOR . 'migrations'], true);
$this->runCommand($command, ['--pretend' => true, '--database' => 'foo']);
}
protected function runCommand($command, $input = [])
{
return $command->run(new ArrayInput($input), new NullOutput());
}
}