From 4858993024dfc02231cb084a75d144b8de1ba8a3 Mon Sep 17 00:00:00 2001 From: Matthew Exon Date: Mon, 26 Apr 2021 20:20:51 +0200 Subject: [PATCH] Add error message when setting config to existing value --- src/Console/Config.php | 4 ++++ tests/src/Console/ConfigConsoleTest.php | 26 +++++++++++++++++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/Console/Config.php b/src/Console/Config.php index d9fab41c1..a97574c97 100644 --- a/src/Console/Config.php +++ b/src/Console/Config.php @@ -128,6 +128,10 @@ HELP; throw new RuntimeException("$cat.$key is an array and can't be set using this command."); } + if ($this->config->get($cat, $key) == $value) { + throw new RuntimeException("$cat.$key already set to $value."); + } + $result = $this->config->set($cat, $key, $value); if ($result) { $this->out("{$cat}.{$key} <= " . diff --git a/tests/src/Console/ConfigConsoleTest.php b/tests/src/Console/ConfigConsoleTest.php index 5c670aedf..bea4399b3 100644 --- a/tests/src/Console/ConfigConsoleTest.php +++ b/tests/src/Console/ConfigConsoleTest.php @@ -65,8 +65,13 @@ class ConfigConsoleTest extends ConsoleTest $this->configMock ->shouldReceive('get') ->with('config', 'test') - ->andReturn('now') + ->andReturn('old') ->twice(); + $this->configMock + ->shouldReceive('get') + ->with('config', 'test') + ->andReturn('now') + ->once(); $console = new Config($this->appMode, $this->configMock, $this->consoleArgv); $console->setArgument(0, 'config'); @@ -118,6 +123,23 @@ class ConfigConsoleTest extends ConsoleTest self::assertEquals("[Error] config.test is an array and can't be set using this command.\n", $txt); } + public function testSetExistingValue() + { + $this->configMock + ->shouldReceive('get') + ->with('config', 'test') + ->andReturn('now') + ->twice(); + + $console = new Config($this->appMode, $this->configMock, $this->consoleArgv); + $console->setArgument(0, 'config'); + $console->setArgument(1, 'test'); + $console->setArgument(2, 'now'); + $txt = $this->dumpExecute($console); + + self::assertEquals("[Error] config.test already set to now.\n", $txt); + } + public function testTooManyArguments() { $console = new Config($this->appMode, $this->configMock, $this->consoleArgv); @@ -171,7 +193,7 @@ CONF; ->shouldReceive('get') ->with('test', 'it') ->andReturn(null) - ->once(); + ->twice(); $console = new Config($this->appMode, $this->configMock, [$this->consoleArgv]); $console->setArgument(0, 'test'); $console->setArgument(1, 'it');