-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Paweł Brzozowski
committed
Sep 10, 2018
1 parent
6045f03
commit 05cf273
Showing
9 changed files
with
194 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace bizley\migration\tests; | ||
|
||
use bizley\migration\controllers\MigrationController; | ||
|
||
/** | ||
* Class MockMigrationController | ||
* @package bizley\migration\tests | ||
*/ | ||
class MockMigrationController extends MigrationController | ||
{ | ||
/** | ||
* @var string output buffer. | ||
*/ | ||
private $stdOutBuffer = ''; | ||
|
||
/** | ||
* @param string $string | ||
*/ | ||
public function stdout($string): void // BC declaration | ||
{ | ||
$this->stdOutBuffer .= $string; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function flushStdOutBuffer(): string | ||
{ | ||
$result = $this->stdOutBuffer; | ||
$this->stdOutBuffer = ''; | ||
return $result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace bizley\migration\tests\mysql; | ||
|
||
use bizley\migration\tests\MockMigrationController; | ||
use Yii; | ||
use yii\console\ExitCode; | ||
|
||
class MigrationControllerTest extends MysqlDbUpdaterTestCase | ||
{ | ||
protected function tearDown() // BC declaration | ||
{ | ||
$this->dbDown('ALL'); | ||
parent::tearDown(); | ||
} | ||
|
||
public function testCreateNonExisting(): void | ||
{ | ||
$controller = new MockMigrationController('migration', Yii::$app); | ||
|
||
$this->assertEquals(ExitCode::DATAERR, $controller->runAction('create', ['non-existing-table'])); | ||
|
||
$output = $controller->flushStdOutBuffer(); | ||
|
||
$this->assertContains('> Generating create migration for table \'non-existing-table\' ...ERROR!', $output); | ||
$this->assertContains('Table \'non-existing-table\' does not exist!', $output); | ||
} | ||
|
||
public function testUpdateNonExisting(): void | ||
{ | ||
$controller = new MockMigrationController('migration', Yii::$app); | ||
|
||
$this->assertEquals(ExitCode::DATAERR, $controller->runAction('update', ['non-existing-table'])); | ||
|
||
$output = $controller->flushStdOutBuffer(); | ||
|
||
$this->assertContains('> Generating update migration for table \'non-existing-table\' ...ERROR!', $output); | ||
$this->assertContains('Table \'non-existing-table\' does not exist!', $output); | ||
} | ||
|
||
public function testCreateFileFail(): void | ||
{ | ||
$this->dbUp('test_pk'); | ||
|
||
$mock = $this->getMockBuilder(MockMigrationController::class) | ||
->setConstructorArgs(['migration', Yii::$app])->setMethods(['generateFile'])->getMock(); | ||
$mock->method('generateFile')->willReturn(false); | ||
|
||
$this->assertEquals(ExitCode::SOFTWARE, $mock->runAction('create', ['test_pk'])); | ||
|
||
$output = $mock->flushStdOutBuffer(); | ||
|
||
$this->assertContains('> Generating create migration for table \'test_pk\' ...ERROR!', $output); | ||
$this->assertContains('Migration file for table \'test_pk\' can not be generated!', $output); | ||
} | ||
|
||
/** | ||
* @runInSeparateProcess | ||
* @preserveGlobalState disabled | ||
*/ | ||
public function testUpdateFileFail(): void | ||
{ | ||
$this->dbUp('test_pk'); | ||
Yii::$app->db->createCommand()->addColumn('test_pk', 'col_new', 'INT(11)')->execute(); | ||
|
||
$mock = $this->getMockBuilder(MockMigrationController::class) | ||
->setConstructorArgs(['migration', Yii::$app])->setMethods(['generateFile'])->getMock(); | ||
$mock->method('generateFile')->willReturn(false); | ||
|
||
$this->assertEquals(ExitCode::SOFTWARE, $mock->runAction('update', ['test_pk'])); | ||
|
||
$output = $mock->flushStdOutBuffer(); | ||
|
||
$this->assertContains('> Generating update migration for table \'test_pk\' ...ERROR!', $output); | ||
$this->assertContains('Migration file for table \'test_pk\' can not be generated!', $output); | ||
} | ||
|
||
public function testCreateSuccess(): void | ||
{ | ||
$this->dbUp('test_pk'); | ||
|
||
$mock = $this->getMockBuilder(MockMigrationController::class) | ||
->setConstructorArgs(['migration', Yii::$app])->setMethods(['generateFile'])->getMock(); | ||
$mock->method('generateFile')->willReturn(true); | ||
|
||
$this->assertEquals(ExitCode::OK, $mock->runAction('create', ['test_pk'])); | ||
|
||
$output = $mock->flushStdOutBuffer(); | ||
|
||
$this->assertContains('> Generating create migration for table \'test_pk\' ...DONE!', $output); | ||
$this->assertContains('Generated 1 file(s).', $output); | ||
} | ||
|
||
/** | ||
* @runInSeparateProcess | ||
* @preserveGlobalState disabled | ||
*/ | ||
public function testUpdateNoNeeded(): void | ||
{ | ||
$this->dbUp('test_pk'); | ||
|
||
$controller = new MockMigrationController('migration', Yii::$app); | ||
|
||
$this->assertEquals(ExitCode::OK, $controller->runAction('update', ['test_pk'])); | ||
|
||
$output = $controller->flushStdOutBuffer(); | ||
|
||
$this->assertContains('> Generating update migration for table \'test_pk\' ...UPDATE NOT REQUIRED.', $output); | ||
$this->assertContains('No files generated.', $output); | ||
} | ||
|
||
/** | ||
* @runInSeparateProcess | ||
* @preserveGlobalState disabled | ||
*/ | ||
public function testUpdateSuccess(): void | ||
{ | ||
$this->dbUp('test_pk'); | ||
Yii::$app->db->createCommand()->addColumn('test_pk', 'col_new', 'INT(11)')->execute(); | ||
|
||
$mock = $this->getMockBuilder(MockMigrationController::class) | ||
->setConstructorArgs(['migration', Yii::$app])->setMethods(['generateFile'])->getMock(); | ||
$mock->method('generateFile')->willReturn(true); | ||
|
||
$this->assertEquals(ExitCode::OK, $mock->runAction('update', ['test_pk'])); | ||
|
||
$output = $mock->flushStdOutBuffer(); | ||
|
||
$this->assertContains('> Generating update migration for table \'test_pk\' ...DONE!', $output); | ||
$this->assertContains('Generated 1 file(s).', $output); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters