|
| 1 | +<?php |
| 2 | +namespace Gt\Database\Test\Cli; |
| 3 | + |
| 4 | +use Gt\Cli\Argument\ArgumentValueList; |
| 5 | +use Gt\Cli\Stream; |
| 6 | +use Gt\Database\Cli\ExecuteCommand; |
| 7 | +use Gt\Database\Connection\Settings; |
| 8 | +use Gt\Database\Database; |
| 9 | +use Gt\Database\Test\Helper\Helper; |
| 10 | +use PHPUnit\Framework\TestCase; |
| 11 | +use SplFileObject; |
| 12 | + |
| 13 | +class ExecuteCommandTest extends TestCase { |
| 14 | + const MIGRATION_CREATE |
| 15 | + = "create table `test` (`id` int primary key, `name` varchar(32))"; |
| 16 | + const MIGRATION_ALTER = "alter table `test` add `new_column` varchar(32)"; |
| 17 | + |
| 18 | + private function createProjectDir():string { |
| 19 | + $root = Helper::getTmpDir(); |
| 20 | + // Ensure empty directory for each test run |
| 21 | + $project = implode(DIRECTORY_SEPARATOR, [$root, uniqid("proj-")]); |
| 22 | + mkdir($project, 0775, true); |
| 23 | + return $project; |
| 24 | + } |
| 25 | + |
| 26 | + private function writeConfigIni(string $projectRoot, string $sqlitePath, string $queryPath = "query", string $migrationPath = "_migration"):void { |
| 27 | + $config = []; |
| 28 | + $config[] = "[database]"; |
| 29 | + $config[] = "driver = sqlite"; |
| 30 | + $config[] = "schema = \"" . str_replace("\\", "/", $sqlitePath) . "\""; |
| 31 | + $config[] = "query_path = $queryPath"; |
| 32 | + $config[] = "migration_path = $migrationPath"; |
| 33 | + $config[] = "username = \"\""; |
| 34 | + $config[] = "password = \"\""; |
| 35 | + file_put_contents($projectRoot . DIRECTORY_SEPARATOR . "config.ini", implode(PHP_EOL, $config)); |
| 36 | + } |
| 37 | + |
| 38 | + private function createMigrations(string $projectRoot, int $count):array { |
| 39 | + $queryDir = $projectRoot . DIRECTORY_SEPARATOR . "query"; |
| 40 | + $migDir = $queryDir . DIRECTORY_SEPARATOR . "_migration"; |
| 41 | + mkdir($migDir, 0775, true); |
| 42 | + |
| 43 | + $fileList = []; |
| 44 | + for($i = 1; $i <= $count; $i++) { |
| 45 | + $filename = str_pad((string)$i, 4, "0", STR_PAD_LEFT) . "-" . uniqid() . ".sql"; |
| 46 | + $path = $migDir . DIRECTORY_SEPARATOR . $filename; |
| 47 | + if($i === 1) { |
| 48 | + $sql = self::MIGRATION_CREATE; |
| 49 | + } |
| 50 | + else { |
| 51 | + $sql = str_replace("`new_column`", "`new_column_{$i}`", self::MIGRATION_ALTER); |
| 52 | + } |
| 53 | + file_put_contents($path, $sql); |
| 54 | + $fileList[] = $path; |
| 55 | + } |
| 56 | + return $fileList; |
| 57 | + } |
| 58 | + |
| 59 | + private function makeStreamFiles():array { |
| 60 | + $dir = Helper::getTmpDir(); |
| 61 | + // Ensure the directory exists to prevent tempnam() notices |
| 62 | + if(!is_dir($dir)) { |
| 63 | + mkdir($dir, 0775, true); |
| 64 | + } |
| 65 | + $in = tempnam($dir, "cli-in-"); |
| 66 | + $out = tempnam($dir, "cli-out-"); |
| 67 | + $err = tempnam($dir, "cli-err-"); |
| 68 | + // Ensure files exist |
| 69 | + file_put_contents($in, ""); |
| 70 | + file_put_contents($out, ""); |
| 71 | + file_put_contents($err, ""); |
| 72 | + $stream = new Stream($in, $out, $err); |
| 73 | + return [ |
| 74 | + "stream" => $stream, |
| 75 | + "in" => $in, |
| 76 | + "out" => $out, |
| 77 | + "err" => $err, |
| 78 | + ]; |
| 79 | + } |
| 80 | + |
| 81 | + private function readFromFiles(string $outPath, string $errPath):array { |
| 82 | + $out = new SplFileObject($outPath, "r"); |
| 83 | + $err = new SplFileObject($errPath, "r"); |
| 84 | + $out->rewind(); |
| 85 | + $err->rewind(); |
| 86 | + return [ |
| 87 | + "out" => $out->fread(8192), |
| 88 | + "err" => $err->fread(8192), |
| 89 | + ]; |
| 90 | + } |
| 91 | + |
| 92 | + public function testExecuteMigratesAll():void { |
| 93 | + $project = $this->createProjectDir(); |
| 94 | + $sqlitePath = str_replace("\\", "/", $project . DIRECTORY_SEPARATOR . "cli-test.db"); |
| 95 | + $this->writeConfigIni($project, $sqlitePath); |
| 96 | + $this->createMigrations($project, 3); |
| 97 | + |
| 98 | + $cwdBackup = getcwd(); |
| 99 | + chdir($project); |
| 100 | + try { |
| 101 | + $cmd = new ExecuteCommand(); |
| 102 | + $streams = $this->makeStreamFiles(); |
| 103 | + $cmd->setStream($streams["stream"]); |
| 104 | + |
| 105 | + $args = new ArgumentValueList(); |
| 106 | + // No additional params; simply run |
| 107 | + $cmd->run($args); |
| 108 | + |
| 109 | + list("out" => $out) = $this->readFromFiles($streams["out"], $streams["err"]); |
| 110 | + self::assertStringContainsString("Migration 1:", $out); |
| 111 | + self::assertStringContainsString("3 migrations were completed successfully.", $out); |
| 112 | + |
| 113 | + // Verify DB state |
| 114 | + $settings = new Settings($project . DIRECTORY_SEPARATOR . "query", Settings::DRIVER_SQLITE, $sqlitePath); |
| 115 | + $db = new Database($settings); |
| 116 | + $result = $db->executeSql("PRAGMA table_info(test);"); |
| 117 | + self::assertGreaterThanOrEqual(4, count($result->fetchAll())); |
| 118 | + } |
| 119 | + finally { |
| 120 | + chdir($cwdBackup); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + public function testExecuteWithResetWithoutNumber():void { |
| 125 | + $project = $this->createProjectDir(); |
| 126 | + $sqlitePath = str_replace("\\", "/", $project . DIRECTORY_SEPARATOR . "cli-test.db"); |
| 127 | + $this->writeConfigIni($project, $sqlitePath); |
| 128 | + $migrations = $this->createMigrations($project, 4); |
| 129 | + |
| 130 | + // Prepare base state: create table so we can skip first migration safely. |
| 131 | + $settings = new Settings($project . DIRECTORY_SEPARATOR . "query", Settings::DRIVER_SQLITE, $sqlitePath); |
| 132 | + $db = new Database($settings); |
| 133 | + $db->executeSql(self::MIGRATION_CREATE); |
| 134 | + |
| 135 | + $cwdBackup = getcwd(); |
| 136 | + chdir($project); |
| 137 | + try { |
| 138 | + $cmd = new ExecuteCommand(); |
| 139 | + $streams = $this->makeStreamFiles(); |
| 140 | + $cmd->setStream($streams["stream"]); |
| 141 | + |
| 142 | + $args = new ArgumentValueList(); |
| 143 | + $args->set("reset"); // No number provided: should reset to latest migration number |
| 144 | + |
| 145 | + $cmd->run($args); |
| 146 | + |
| 147 | + list("out" => $out) = $this->readFromFiles($streams["out"], $streams["err"]); |
| 148 | + // Should only execute the last migration |
| 149 | + self::assertMatchesRegularExpression("/Migration\\s+4:/", $out); |
| 150 | + self::assertStringContainsString("1 migrations were completed successfully.", $out); |
| 151 | + } |
| 152 | + finally { |
| 153 | + chdir($cwdBackup); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + public function testExecuteWithResetWithNumber():void { |
| 158 | + $project = $this->createProjectDir(); |
| 159 | + $sqlitePath = str_replace("\\", "/", $project . DIRECTORY_SEPARATOR . "cli-test.db"); |
| 160 | + $this->writeConfigIni($project, $sqlitePath); |
| 161 | + $migrations = $this->createMigrations($project, 5); |
| 162 | + |
| 163 | + // Prepare base state when skipping the initial migrations. |
| 164 | + $settings = new Settings($project . DIRECTORY_SEPARATOR . "query", Settings::DRIVER_SQLITE, $sqlitePath); |
| 165 | + $db = new Database($settings); |
| 166 | + $db->executeSql(self::MIGRATION_CREATE); |
| 167 | + |
| 168 | + $cwdBackup = getcwd(); |
| 169 | + chdir($project); |
| 170 | + try { |
| 171 | + $cmd = new ExecuteCommand(); |
| 172 | + $streams = $this->makeStreamFiles(); |
| 173 | + $cmd->setStream($streams["stream"]); |
| 174 | + |
| 175 | + $args = new ArgumentValueList(); |
| 176 | + $args->set("reset", "3"); // Should migrate from 4 and 5 only |
| 177 | + |
| 178 | + $cmd->run($args); |
| 179 | + |
| 180 | + list("out" => $out) = $this->readFromFiles($streams["out"], $streams["err"]); |
| 181 | + self::assertStringNotContainsString("Migration 1:", $out); |
| 182 | + self::assertStringNotContainsString("Migration 2:", $out); |
| 183 | + self::assertStringNotContainsString("Migration 3:", $out); |
| 184 | + self::assertStringContainsString("Migration 4:", $out); |
| 185 | + self::assertStringContainsString("Migration 5:", $out); |
| 186 | + self::assertStringContainsString("2 migrations were completed successfully.", $out); |
| 187 | + } |
| 188 | + finally { |
| 189 | + chdir($cwdBackup); |
| 190 | + } |
| 191 | + } |
| 192 | +} |
0 commit comments