|
3 | 3 |
|
4 | 4 | use Gt\Cli\Argument\ArgumentValueList; |
5 | 5 | use Gt\Cli\Stream; |
| 6 | +use Gt\Config\Config; |
| 7 | +use Gt\Config\ConfigSection; |
6 | 8 | use Gt\Database\Cli\ExecuteCommand; |
7 | 9 | use Gt\Database\Connection\Settings; |
8 | 10 | use Gt\Database\Database; |
9 | 11 | use Gt\Database\Test\Helper\Helper; |
| 12 | +use Gt\Cli\Parameter\Parameter; |
10 | 13 | use PHPUnit\Framework\TestCase; |
11 | 14 | use SplFileObject; |
12 | 15 |
|
@@ -189,4 +192,99 @@ public function testExecuteWithResetWithNumber():void { |
189 | 192 | chdir($cwdBackup); |
190 | 193 | } |
191 | 194 | } |
| 195 | + |
| 196 | + public function testOptionalParameterListContainsCliOverrides():void { |
| 197 | + $command = new ExecuteCommand(); |
| 198 | + $parameterNames = array_map( |
| 199 | + fn(Parameter $parameter) => $parameter->getLongOption(), |
| 200 | + $command->getOptionalParameterList() |
| 201 | + ); |
| 202 | + |
| 203 | + self::assertContains("base-directory", $parameterNames); |
| 204 | + self::assertContains("driver", $parameterNames); |
| 205 | + self::assertContains("database", $parameterNames); |
| 206 | + self::assertContains("host", $parameterNames); |
| 207 | + self::assertContains("port", $parameterNames); |
| 208 | + self::assertContains("username", $parameterNames); |
| 209 | + self::assertContains("password", $parameterNames); |
| 210 | + self::assertContains("force", $parameterNames); |
| 211 | + self::assertContains("reset", $parameterNames); |
| 212 | + } |
| 213 | + |
| 214 | + public function testCliArgumentsOverrideConfigValuesWhenBuildingSettings():void { |
| 215 | + $repoBasePath = "/tmp/project-root"; |
| 216 | + $config = new Config( |
| 217 | + new ConfigSection("database", [ |
| 218 | + "query_path" => "query", |
| 219 | + "driver" => "mysql", |
| 220 | + "schema" => "config-db", |
| 221 | + "host" => "config-host", |
| 222 | + "port" => "3306", |
| 223 | + "username" => "config-user", |
| 224 | + "password" => "config-pass", |
| 225 | + "migration_path" => "_migration", |
| 226 | + "migration_table" => "_migration", |
| 227 | + ]) |
| 228 | + ); |
| 229 | + $args = new ArgumentValueList(); |
| 230 | + $args->set("base-directory", "custom-query"); |
| 231 | + $args->set("driver", "sqlite"); |
| 232 | + $args->set("database", "/tmp/override.db"); |
| 233 | + $args->set("host", "override-host"); |
| 234 | + $args->set("port", "1234"); |
| 235 | + $args->set("username", "override-user"); |
| 236 | + $args->set("password", "override-pass"); |
| 237 | + |
| 238 | + $command = $this->createCommandProbe(); |
| 239 | + $settings = $command->buildSettingsForTest($config, $repoBasePath, $args); |
| 240 | + |
| 241 | + self::assertSame("/tmp/project-root/custom-query", $settings->getBaseDirectory()); |
| 242 | + self::assertSame("sqlite", $settings->getDriver()); |
| 243 | + self::assertSame("/tmp/override.db", $settings->getSchema()); |
| 244 | + self::assertSame("override-host", $settings->getHost()); |
| 245 | + self::assertSame(1234, $settings->getPort()); |
| 246 | + self::assertSame("override-user", $settings->getUsername()); |
| 247 | + self::assertSame("override-pass", $settings->getPassword()); |
| 248 | + } |
| 249 | + |
| 250 | + public function testBaseDirectoryOverrideIsUsedForMigrationLocation():void { |
| 251 | + $repoBasePath = "/tmp/project-root"; |
| 252 | + $config = new Config( |
| 253 | + new ConfigSection("database", [ |
| 254 | + "query_path" => "query", |
| 255 | + "migration_path" => "_migration", |
| 256 | + "migration_table" => "migration_log", |
| 257 | + ]) |
| 258 | + ); |
| 259 | + $args = new ArgumentValueList(); |
| 260 | + $args->set("base-directory", "alt-query"); |
| 261 | + |
| 262 | + $command = $this->createCommandProbe(); |
| 263 | + [$migrationPath, $migrationTable] = $command->getMigrationLocationForTest($config, $repoBasePath, $args); |
| 264 | + |
| 265 | + self::assertSame("/tmp/project-root/alt-query/_migration", $migrationPath); |
| 266 | + self::assertSame("migration_log", $migrationTable); |
| 267 | + } |
| 268 | + |
| 269 | + private function createCommandProbe():ExecuteCommand { |
| 270 | + return new class extends ExecuteCommand { |
| 271 | + public function buildSettingsForTest( |
| 272 | + Config $config, |
| 273 | + string $repoBasePath, |
| 274 | + ?ArgumentValueList $arguments = null |
| 275 | + ): Settings { |
| 276 | + return $this->buildSettingsFromConfig($config, $repoBasePath, $arguments); |
| 277 | + } |
| 278 | + |
| 279 | + /** @return list<string> */ |
| 280 | + public function getMigrationLocationForTest( |
| 281 | + Config $config, |
| 282 | + string $repoBasePath, |
| 283 | + ?ArgumentValueList $arguments = null |
| 284 | + ): array { |
| 285 | + return $this->getMigrationLocation($config, $repoBasePath, $arguments); |
| 286 | + } |
| 287 | + }; |
| 288 | + } |
| 289 | + |
192 | 290 | } |
0 commit comments