Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Paweł Brzozowski committed Sep 10, 2018
1 parent 6045f03 commit 05cf273
Show file tree
Hide file tree
Showing 9 changed files with 194 additions and 8 deletions.
15 changes: 13 additions & 2 deletions controllers/MigrationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,17 @@ public function actionList(): int
return ExitCode::OK;
}

/**
* @param $path
* @param $content
* @return bool|int
* @since 3.0.2
*/
public function generateFile($path, $content)
{
return file_put_contents($path, $content);
}

/**
* Creates new migration for the given tables.
* @param string $table Table names separated by commas.
Expand Down Expand Up @@ -332,7 +343,7 @@ public function actionCreate($table): int
return ExitCode::DATAERR;
}

if (file_put_contents($file, $generator->generateMigration()) === false) {
if ($this->generateFile($file, $generator->generateMigration()) === false) {
$this->stdout("ERROR!\n > Migration file for table '{$name}' can not be generated!\n\n", Console::FG_RED);
return ExitCode::SOFTWARE;
}
Expand Down Expand Up @@ -435,7 +446,7 @@ public function actionUpdate($table): int
}

if (!$this->showOnly) {
if (file_put_contents($file, $updater->generateMigration()) === false) {
if ($this->generateFile($file, $updater->generateMigration()) === false) {
$this->stdout("ERROR!\n > Migration file for table '{$name}' can not be generated!\n\n", Console::FG_RED);
return ExitCode::SOFTWARE;
}
Expand Down
5 changes: 3 additions & 2 deletions tests/DbTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static function getParam(string $name, $default = null)
* @throws \yii\console\Exception
* @throws \yii\db\Exception
*/
public static function setUpBeforeClass()
public static function setUpBeforeClass() // BC declaration
{
static::mockApplication();
if (static::$runMigrations) {
Expand Down Expand Up @@ -99,7 +99,7 @@ protected static function runSilentMigration($route, array $params = []): void
* @throws \yii\base\InvalidRouteException
* @throws \yii\console\Exception
*/
public static function tearDownAfterClass(): void
public static function tearDownAfterClass() // BC declaration
{
static::runSilentMigration('migrate/down', ['all']);
if (static::$db) {
Expand All @@ -117,6 +117,7 @@ public static function getConnection(): Connection
if (static::$db === null) {
$db = new Connection();
$db->dsn = static::$database['dsn'];
$db->charset = static::$database['charset'];
if (isset(static::$database['username'])) {
$db->username = static::$database['username'];
$db->password = static::$database['password'];
Expand Down
37 changes: 37 additions & 0 deletions tests/MockMigrationController.php
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;
}
}
1 change: 1 addition & 0 deletions tests/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
'dsn' => 'mysql:host=127.0.0.1;dbname=migrationtest',
'username' => 'migration',
'password' => 'migration',
'charset' => 'utf8',
],
];
if (is_file(__DIR__ . '/config.local.php')) {
Expand Down
134 changes: 134 additions & 0 deletions tests/mysql/MigrationControllerTest.php
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);
}
}
2 changes: 1 addition & 1 deletion tests/mysql/MysqlDbTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

abstract class MysqlDbTestCase extends DbTestCase
{
public static function setUpBeforeClass()
public static function setUpBeforeClass() // BC declaration
{
static::$database = static::getParam('mysql');
parent::setUpBeforeClass();
Expand Down
4 changes: 3 additions & 1 deletion tests/mysql/MysqlDbUpdaterTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,11 @@ protected function getUpdater($tableName, $generalSchema = true, array $skip = [
}

/**
* @throws \yii\base\InvalidRouteException
* @throws \yii\console\Exception
* @throws \yii\db\Exception
*/
public static function setUpBeforeClass()
public static function setUpBeforeClass() // BC declaration
{
parent::setUpBeforeClass();
if (!\in_array('migration', Yii::$app->db->schema->tableNames, true)) {
Expand Down
2 changes: 1 addition & 1 deletion tests/mysql/UpdaterColumnsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class UpdaterColumnsTest extends MysqlDbUpdaterTestCase
{
protected function tearDown()
protected function tearDown() // BC declaration
{
$this->dbDown('ALL');
parent::tearDown();
Expand Down
2 changes: 1 addition & 1 deletion tests/mysql/UpdaterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class UpdaterTest extends MysqlDbUpdaterTestCase
{
protected function tearDown()
protected function tearDown() // BC declaration
{
$this->dbDown('ALL');
parent::tearDown();
Expand Down

0 comments on commit 05cf273

Please sign in to comment.