|
| 1 | +<?php |
| 2 | +namespace WebFiori\Framework\Test\Cli; |
| 3 | + |
| 4 | +use WebFiori\Database\ConnectionInfo; |
| 5 | +use WebFiori\Database\Schema\SchemaRunner; |
| 6 | +use WebFiori\Framework\App; |
| 7 | +use WebFiori\Framework\Cli\CLITestCase; |
| 8 | +use WebFiori\Framework\Cli\Commands\StepMigrationsCommand; |
| 9 | + |
| 10 | +class StepMigrationsCommandTest extends CLITestCase { |
| 11 | + private ConnectionInfo $testConnection; |
| 12 | + |
| 13 | + protected function setUp(): void { |
| 14 | + parent::setUp(); |
| 15 | + $this->setupTestConnection(); |
| 16 | + $this->dropSchemaTable(); |
| 17 | + $this->cleanupMigrations(); |
| 18 | + } |
| 19 | + |
| 20 | + protected function tearDown(): void { |
| 21 | + $this->cleanupMigrations(); |
| 22 | + $this->dropSchemaTable(); |
| 23 | + App::getConfig()->removeAllDBConnections(); |
| 24 | + parent::tearDown(); |
| 25 | + } |
| 26 | + |
| 27 | + /** @test */ |
| 28 | + public function testNoConnectionsConfigured() { |
| 29 | + App::getConfig()->removeAllDBConnections(); |
| 30 | + |
| 31 | + $output = $this->executeMultiCommand([ |
| 32 | + StepMigrationsCommand::class |
| 33 | + ]); |
| 34 | + |
| 35 | + $this->assertEquals([ |
| 36 | + "Info: No database connections configured.\n" |
| 37 | + ], $output); |
| 38 | + $this->assertEquals(1, $this->getExitCode()); |
| 39 | + } |
| 40 | + |
| 41 | + /** @test */ |
| 42 | + public function testNoPendingMigrations() { |
| 43 | + $this->initMigrations(); |
| 44 | + |
| 45 | + $output = $this->executeMultiCommand([ |
| 46 | + StepMigrationsCommand::class, |
| 47 | + '--connection' => 'test-connection' |
| 48 | + ]); |
| 49 | + |
| 50 | + $outputStr = implode('', $output); |
| 51 | + $this->assertStringContainsString('No pending migrations.', $outputStr); |
| 52 | + $this->assertEquals(0, $this->getExitCode()); |
| 53 | + } |
| 54 | + |
| 55 | + /** @test */ |
| 56 | + public function testApplyOneMigration() { |
| 57 | + $this->createTestMigration('StepApply1'); |
| 58 | + $this->initMigrations(); |
| 59 | + |
| 60 | + $output = $this->executeMultiCommand([ |
| 61 | + StepMigrationsCommand::class, |
| 62 | + '--connection' => 'test-connection' |
| 63 | + ], [ |
| 64 | + '0' // Select "Apply" |
| 65 | + ]); |
| 66 | + |
| 67 | + $outputStr = implode('', $output); |
| 68 | + $this->assertStringContainsString('Applied:', $outputStr); |
| 69 | + $this->assertStringContainsString('1 applied, 0 skipped', $outputStr); |
| 70 | + $this->assertEquals(0, $this->getExitCode()); |
| 71 | + } |
| 72 | + |
| 73 | + /** @test */ |
| 74 | + public function testSkipOneMigration() { |
| 75 | + $this->createTestMigration('StepSkip1'); |
| 76 | + $this->initMigrations(); |
| 77 | + |
| 78 | + $output = $this->executeMultiCommand([ |
| 79 | + StepMigrationsCommand::class, |
| 80 | + '--connection' => 'test-connection' |
| 81 | + ], [ |
| 82 | + '1' // Select "Skip" |
| 83 | + ]); |
| 84 | + |
| 85 | + $outputStr = implode('', $output); |
| 86 | + $this->assertStringContainsString('Skipped:', $outputStr); |
| 87 | + $this->assertStringContainsString('0 applied, 1 skipped', $outputStr); |
| 88 | + $this->assertEquals(0, $this->getExitCode()); |
| 89 | + } |
| 90 | + |
| 91 | + /** @test */ |
| 92 | + public function testQuitBeforeProcessingAll() { |
| 93 | + $this->createTestMigration('StepQuit1'); |
| 94 | + $this->createTestMigration('StepQuit2'); |
| 95 | + $this->initMigrations(); |
| 96 | + |
| 97 | + $output = $this->executeMultiCommand([ |
| 98 | + StepMigrationsCommand::class, |
| 99 | + '--connection' => 'test-connection' |
| 100 | + ], [ |
| 101 | + '0', // Apply first |
| 102 | + '2' // Quit on second |
| 103 | + ]); |
| 104 | + |
| 105 | + $outputStr = implode('', $output); |
| 106 | + $this->assertStringContainsString('Applied:', $outputStr); |
| 107 | + $this->assertStringContainsString('1 applied, 0 skipped', $outputStr); |
| 108 | + $this->assertEquals(0, $this->getExitCode()); |
| 109 | + } |
| 110 | + |
| 111 | + /** @test */ |
| 112 | + public function testMixedApplyAndSkip() { |
| 113 | + $this->createTestMigration('StepMix1'); |
| 114 | + $this->createTestMigration('StepMix2'); |
| 115 | + $this->initMigrations(); |
| 116 | + |
| 117 | + $output = $this->executeMultiCommand([ |
| 118 | + StepMigrationsCommand::class, |
| 119 | + '--connection' => 'test-connection' |
| 120 | + ], [ |
| 121 | + '0', // Apply first |
| 122 | + '1' // Skip second |
| 123 | + ]); |
| 124 | + |
| 125 | + $outputStr = implode('', $output); |
| 126 | + $this->assertStringContainsString('1 applied, 1 skipped', $outputStr); |
| 127 | + $this->assertEquals(0, $this->getExitCode()); |
| 128 | + } |
| 129 | + |
| 130 | + private function initMigrations(): void { |
| 131 | + $this->executeMultiCommand([ |
| 132 | + 'WebFiori\\Framework\\Cli\\Commands\\InitMigrationsCommand', |
| 133 | + '--connection' => 'test-connection' |
| 134 | + ]); |
| 135 | + } |
| 136 | + |
| 137 | + private function createTestMigration(string $name): void { |
| 138 | + $dir = APP_PATH.'Database'.DS.'Migrations'; |
| 139 | + |
| 140 | + if (!is_dir($dir)) { |
| 141 | + mkdir($dir, 0755, true); |
| 142 | + } |
| 143 | + |
| 144 | + $content = <<<PHP |
| 145 | +<?php |
| 146 | +namespace App\Database\Migrations; |
| 147 | +
|
| 148 | +use WebFiori\Database\Database; |
| 149 | +use WebFiori\Database\Schema\AbstractMigration; |
| 150 | +
|
| 151 | +class $name extends AbstractMigration { |
| 152 | + public function up(Database \$db): void { |
| 153 | + // Test migration |
| 154 | + } |
| 155 | +
|
| 156 | + public function down(Database \$db): void { |
| 157 | + // Test rollback |
| 158 | + } |
| 159 | +} |
| 160 | +PHP; |
| 161 | + |
| 162 | + file_put_contents($dir.DS.$name.'.php', $content); |
| 163 | + } |
| 164 | + |
| 165 | + private function cleanupMigrations(): void { |
| 166 | + $dir = APP_PATH.'Database'.DS.'Migrations'; |
| 167 | + |
| 168 | + if (!is_dir($dir)) { |
| 169 | + return; |
| 170 | + } |
| 171 | + |
| 172 | + foreach (glob($dir.DS.'*.php') as $file) { |
| 173 | + unlink($file); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + private function setupTestConnection(): void { |
| 178 | + $this->testConnection = new ConnectionInfo('mysql', 'root', MYSQL_ROOT_PASSWORD, 'testing_db', '127.0.0.1', 3306); |
| 179 | + $this->testConnection->setName('test-connection'); |
| 180 | + App::getConfig()->addOrUpdateDBConnection($this->testConnection); |
| 181 | + } |
| 182 | + |
| 183 | + private function dropSchemaTable(): void { |
| 184 | + try { |
| 185 | + $connection = App::getConfig()->getDBConnection('test-connection'); |
| 186 | + |
| 187 | + if ($connection !== null) { |
| 188 | + $runner = new SchemaRunner($connection); |
| 189 | + $runner->dropSchemaTable(); |
| 190 | + } |
| 191 | + } catch (\Throwable $e) { |
| 192 | + // Ignore errors during cleanup |
| 193 | + } |
| 194 | + } |
| 195 | +} |
0 commit comments