Skip to content

Commit 05cf273

Browse files
author
Paweł Brzozowski
committed
tests
1 parent 6045f03 commit 05cf273

File tree

9 files changed

+194
-8
lines changed

9 files changed

+194
-8
lines changed

controllers/MigrationController.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,17 @@ public function actionList(): int
296296
return ExitCode::OK;
297297
}
298298

299+
/**
300+
* @param $path
301+
* @param $content
302+
* @return bool|int
303+
* @since 3.0.2
304+
*/
305+
public function generateFile($path, $content)
306+
{
307+
return file_put_contents($path, $content);
308+
}
309+
299310
/**
300311
* Creates new migration for the given tables.
301312
* @param string $table Table names separated by commas.
@@ -332,7 +343,7 @@ public function actionCreate($table): int
332343
return ExitCode::DATAERR;
333344
}
334345

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

437448
if (!$this->showOnly) {
438-
if (file_put_contents($file, $updater->generateMigration()) === false) {
449+
if ($this->generateFile($file, $updater->generateMigration()) === false) {
439450
$this->stdout("ERROR!\n > Migration file for table '{$name}' can not be generated!\n\n", Console::FG_RED);
440451
return ExitCode::SOFTWARE;
441452
}

tests/DbTestCase.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public static function getParam(string $name, $default = null)
4040
* @throws \yii\console\Exception
4141
* @throws \yii\db\Exception
4242
*/
43-
public static function setUpBeforeClass()
43+
public static function setUpBeforeClass() // BC declaration
4444
{
4545
static::mockApplication();
4646
if (static::$runMigrations) {
@@ -99,7 +99,7 @@ protected static function runSilentMigration($route, array $params = []): void
9999
* @throws \yii\base\InvalidRouteException
100100
* @throws \yii\console\Exception
101101
*/
102-
public static function tearDownAfterClass(): void
102+
public static function tearDownAfterClass() // BC declaration
103103
{
104104
static::runSilentMigration('migrate/down', ['all']);
105105
if (static::$db) {
@@ -117,6 +117,7 @@ public static function getConnection(): Connection
117117
if (static::$db === null) {
118118
$db = new Connection();
119119
$db->dsn = static::$database['dsn'];
120+
$db->charset = static::$database['charset'];
120121
if (isset(static::$database['username'])) {
121122
$db->username = static::$database['username'];
122123
$db->password = static::$database['password'];

tests/MockMigrationController.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace bizley\migration\tests;
6+
7+
use bizley\migration\controllers\MigrationController;
8+
9+
/**
10+
* Class MockMigrationController
11+
* @package bizley\migration\tests
12+
*/
13+
class MockMigrationController extends MigrationController
14+
{
15+
/**
16+
* @var string output buffer.
17+
*/
18+
private $stdOutBuffer = '';
19+
20+
/**
21+
* @param string $string
22+
*/
23+
public function stdout($string): void // BC declaration
24+
{
25+
$this->stdOutBuffer .= $string;
26+
}
27+
28+
/**
29+
* @return string
30+
*/
31+
public function flushStdOutBuffer(): string
32+
{
33+
$result = $this->stdOutBuffer;
34+
$this->stdOutBuffer = '';
35+
return $result;
36+
}
37+
}

tests/config.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
'dsn' => 'mysql:host=127.0.0.1;dbname=migrationtest',
1010
'username' => 'migration',
1111
'password' => 'migration',
12+
'charset' => 'utf8',
1213
],
1314
];
1415
if (is_file(__DIR__ . '/config.local.php')) {
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace bizley\migration\tests\mysql;
6+
7+
use bizley\migration\tests\MockMigrationController;
8+
use Yii;
9+
use yii\console\ExitCode;
10+
11+
class MigrationControllerTest extends MysqlDbUpdaterTestCase
12+
{
13+
protected function tearDown() // BC declaration
14+
{
15+
$this->dbDown('ALL');
16+
parent::tearDown();
17+
}
18+
19+
public function testCreateNonExisting(): void
20+
{
21+
$controller = new MockMigrationController('migration', Yii::$app);
22+
23+
$this->assertEquals(ExitCode::DATAERR, $controller->runAction('create', ['non-existing-table']));
24+
25+
$output = $controller->flushStdOutBuffer();
26+
27+
$this->assertContains('> Generating create migration for table \'non-existing-table\' ...ERROR!', $output);
28+
$this->assertContains('Table \'non-existing-table\' does not exist!', $output);
29+
}
30+
31+
public function testUpdateNonExisting(): void
32+
{
33+
$controller = new MockMigrationController('migration', Yii::$app);
34+
35+
$this->assertEquals(ExitCode::DATAERR, $controller->runAction('update', ['non-existing-table']));
36+
37+
$output = $controller->flushStdOutBuffer();
38+
39+
$this->assertContains('> Generating update migration for table \'non-existing-table\' ...ERROR!', $output);
40+
$this->assertContains('Table \'non-existing-table\' does not exist!', $output);
41+
}
42+
43+
public function testCreateFileFail(): void
44+
{
45+
$this->dbUp('test_pk');
46+
47+
$mock = $this->getMockBuilder(MockMigrationController::class)
48+
->setConstructorArgs(['migration', Yii::$app])->setMethods(['generateFile'])->getMock();
49+
$mock->method('generateFile')->willReturn(false);
50+
51+
$this->assertEquals(ExitCode::SOFTWARE, $mock->runAction('create', ['test_pk']));
52+
53+
$output = $mock->flushStdOutBuffer();
54+
55+
$this->assertContains('> Generating create migration for table \'test_pk\' ...ERROR!', $output);
56+
$this->assertContains('Migration file for table \'test_pk\' can not be generated!', $output);
57+
}
58+
59+
/**
60+
* @runInSeparateProcess
61+
* @preserveGlobalState disabled
62+
*/
63+
public function testUpdateFileFail(): void
64+
{
65+
$this->dbUp('test_pk');
66+
Yii::$app->db->createCommand()->addColumn('test_pk', 'col_new', 'INT(11)')->execute();
67+
68+
$mock = $this->getMockBuilder(MockMigrationController::class)
69+
->setConstructorArgs(['migration', Yii::$app])->setMethods(['generateFile'])->getMock();
70+
$mock->method('generateFile')->willReturn(false);
71+
72+
$this->assertEquals(ExitCode::SOFTWARE, $mock->runAction('update', ['test_pk']));
73+
74+
$output = $mock->flushStdOutBuffer();
75+
76+
$this->assertContains('> Generating update migration for table \'test_pk\' ...ERROR!', $output);
77+
$this->assertContains('Migration file for table \'test_pk\' can not be generated!', $output);
78+
}
79+
80+
public function testCreateSuccess(): void
81+
{
82+
$this->dbUp('test_pk');
83+
84+
$mock = $this->getMockBuilder(MockMigrationController::class)
85+
->setConstructorArgs(['migration', Yii::$app])->setMethods(['generateFile'])->getMock();
86+
$mock->method('generateFile')->willReturn(true);
87+
88+
$this->assertEquals(ExitCode::OK, $mock->runAction('create', ['test_pk']));
89+
90+
$output = $mock->flushStdOutBuffer();
91+
92+
$this->assertContains('> Generating create migration for table \'test_pk\' ...DONE!', $output);
93+
$this->assertContains('Generated 1 file(s).', $output);
94+
}
95+
96+
/**
97+
* @runInSeparateProcess
98+
* @preserveGlobalState disabled
99+
*/
100+
public function testUpdateNoNeeded(): void
101+
{
102+
$this->dbUp('test_pk');
103+
104+
$controller = new MockMigrationController('migration', Yii::$app);
105+
106+
$this->assertEquals(ExitCode::OK, $controller->runAction('update', ['test_pk']));
107+
108+
$output = $controller->flushStdOutBuffer();
109+
110+
$this->assertContains('> Generating update migration for table \'test_pk\' ...UPDATE NOT REQUIRED.', $output);
111+
$this->assertContains('No files generated.', $output);
112+
}
113+
114+
/**
115+
* @runInSeparateProcess
116+
* @preserveGlobalState disabled
117+
*/
118+
public function testUpdateSuccess(): void
119+
{
120+
$this->dbUp('test_pk');
121+
Yii::$app->db->createCommand()->addColumn('test_pk', 'col_new', 'INT(11)')->execute();
122+
123+
$mock = $this->getMockBuilder(MockMigrationController::class)
124+
->setConstructorArgs(['migration', Yii::$app])->setMethods(['generateFile'])->getMock();
125+
$mock->method('generateFile')->willReturn(true);
126+
127+
$this->assertEquals(ExitCode::OK, $mock->runAction('update', ['test_pk']));
128+
129+
$output = $mock->flushStdOutBuffer();
130+
131+
$this->assertContains('> Generating update migration for table \'test_pk\' ...DONE!', $output);
132+
$this->assertContains('Generated 1 file(s).', $output);
133+
}
134+
}

tests/mysql/MysqlDbTestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
abstract class MysqlDbTestCase extends DbTestCase
1010
{
11-
public static function setUpBeforeClass()
11+
public static function setUpBeforeClass() // BC declaration
1212
{
1313
static::$database = static::getParam('mysql');
1414
parent::setUpBeforeClass();

tests/mysql/MysqlDbUpdaterTestCase.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,11 @@ protected function getUpdater($tableName, $generalSchema = true, array $skip = [
5454
}
5555

5656
/**
57+
* @throws \yii\base\InvalidRouteException
58+
* @throws \yii\console\Exception
5759
* @throws \yii\db\Exception
5860
*/
59-
public static function setUpBeforeClass()
61+
public static function setUpBeforeClass() // BC declaration
6062
{
6163
parent::setUpBeforeClass();
6264
if (!\in_array('migration', Yii::$app->db->schema->tableNames, true)) {

tests/mysql/UpdaterColumnsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
class UpdaterColumnsTest extends MysqlDbUpdaterTestCase
1111
{
12-
protected function tearDown()
12+
protected function tearDown() // BC declaration
1313
{
1414
$this->dbDown('ALL');
1515
parent::tearDown();

tests/mysql/UpdaterTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
class UpdaterTest extends MysqlDbUpdaterTestCase
1111
{
12-
protected function tearDown()
12+
protected function tearDown() // BC declaration
1313
{
1414
$this->dbDown('ALL');
1515
parent::tearDown();

0 commit comments

Comments
 (0)