diff --git a/src/Config/Builder.php b/src/Config/Builder.php index 3a2bdd8..b1a8e40 100644 --- a/src/Config/Builder.php +++ b/src/Config/Builder.php @@ -17,7 +17,7 @@ public function build(InputInterface $input): Config { $valuesFromFile = $this->getValuesFromConfigFile($input); $commandLineValues = $this->valuesFromCommandLineArgs($input); - $configValues = array_replace_recursive($valuesFromFile, $commandLineValues); + $configValues = $this->mergeConfigValues($valuesFromFile, $commandLineValues); return new Config( $configValues['script'], @@ -93,4 +93,18 @@ private function getValuesFromConfigFile(InputInterface $input): array } return $valuesFromFile; } + + private function mergeConfigValues(array $valuesFromFile, array $commandLineValues): array + { + $configValues = []; + foreach ($commandLineValues as $key => $value) { + if (empty($value) && isset($valuesFromFile[$key])) { + $configValues[$key] = $valuesFromFile[$key]; + } else { + $configValues[$key] = $commandLineValues[$key]; + } + } + + return $configValues; + } } diff --git a/tests/ConfigTest.php b/tests/ConfigTest.php index 0e82703..d81d52f 100644 --- a/tests/ConfigTest.php +++ b/tests/ConfigTest.php @@ -31,6 +31,32 @@ public function it_can_use_config_path_from_command_line_arg(): void $this->wait(); $output = $watcher->getOutput(); - $this->assertStringContainsString('directory-to-watch', $output); + $this->assertStringContainsString('watching: directory-to-watch', $output); + } + + /** @test */ + public function it_uses_values_from_config(): void + { + $configFile = Filesystem::createConfigFile(['watch' => ['first', 'second']]); + $fileToWatch = Filesystem::createHelloWorldPHPFile(); + + $watcher = (new WatcherRunner())->run($fileToWatch, ['--config', $configFile]); + $this->wait(); + + $output = $watcher->getOutput(); + $this->assertStringContainsString('watching: first, second', $output); + } + + /** @test */ + public function command_line_options_override_values_from_config(): void + { + $configFile = Filesystem::createConfigFile(['watch' => ['directory-to-watch']]); + $fileToWatch = Filesystem::createHelloWorldPHPFile(); + + $watcher = (new WatcherRunner())->run($fileToWatch, ['--watch', $configFile]); + $this->wait(); + + $output = $watcher->getOutput(); + $this->assertStringContainsString("watching: $configFile", $output); } }