|
| 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 | +} |
0 commit comments