]> git.mxchange.org Git - friendica.git/blob - tests/src/Console/ConfigConsoleTest.php
Merge pull request #11250 from nupplaphil/bug/redis_pw
[friendica.git] / tests / src / Console / ConfigConsoleTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Test\src\Console;
23
24 use Friendica\App;
25 use Friendica\App\Mode;
26 use Friendica\Console\Config;
27 use Friendica\Core\Config\Capability\IManageConfigValues;
28 use Mockery;
29 use Mockery\LegacyMockInterface;
30 use Mockery\MockInterface;
31
32 class ConfigConsoleTest extends ConsoleTest
33 {
34         /**
35          * @var App\Mode|MockInterface $appMode
36          */
37         private $appMode;
38         /** @var IManageConfigValues|LegacyMockInterface|MockInterface */
39         private $configMock;
40
41         protected function setUp() : void
42         {
43                 parent::setUp();
44
45                 Mockery::getConfiguration()->setConstantsMap([
46                         Mode::class => [
47                                 'DBCONFIGAVAILABLE' => 0,
48                         ],
49                 ]);
50
51                 $this->appMode = Mockery::mock(App\Mode::class);
52                 $this->appMode->shouldReceive('has')
53                                           ->andReturn(true);
54
55                 $this->configMock = Mockery::mock(IManageConfigValues::class);
56         }
57
58         public function testSetGetKeyValue()
59         {
60                 $this->configMock
61                         ->shouldReceive('set')
62                         ->with('config', 'test', 'now')
63                         ->andReturn(true)
64                         ->once();
65                 $this->configMock
66                         ->shouldReceive('get')
67                         ->with('config', 'test')
68                         ->andReturn('old')
69                         ->twice();
70                 $this->configMock
71                         ->shouldReceive('get')
72                         ->with('config', 'test')
73                         ->andReturn('now')
74                         ->once();
75
76                 $console = new Config($this->appMode, $this->configMock, $this->consoleArgv);
77                 $console->setArgument(0, 'config');
78                 $console->setArgument(1, 'test');
79                 $console->setArgument(2, 'now');
80                 $txt = $this->dumpExecute($console);
81                 self::assertEquals("config.test <= now\n", $txt);
82
83                 $this->configMock
84                         ->shouldReceive('get')
85                         ->with('config', 'test')
86                         ->andReturn('now')
87                         ->once();
88
89                 $console = new Config($this->appMode, $this->configMock, [$this->consoleArgv]);
90                 $console->setArgument(0, 'config');
91                 $console->setArgument(1, 'test');
92                 $txt = $this->dumpExecute($console);
93                 self::assertEquals("config.test => now\n", $txt);
94
95                 $this->configMock
96                         ->shouldReceive('get')
97                         ->with('config', 'test')
98                         ->andReturn(null)
99                         ->once();
100
101                 $console = new Config($this->appMode, $this->configMock, $this->consoleArgv);
102                 $console->setArgument(0, 'config');
103                 $console->setArgument(1, 'test');
104                 $txt = $this->dumpExecute($console);
105                 self::assertEquals("config.test => NULL\n", $txt);
106         }
107
108         public function testSetArrayValue()
109         {
110                 $testArray = [1, 2, 3];
111                 $this->configMock
112                         ->shouldReceive('get')
113                         ->with('config', 'test')
114                         ->andReturn($testArray)
115                         ->once();
116
117                 $console = new Config($this->appMode, $this->configMock, $this->consoleArgv);
118                 $console->setArgument(0, 'config');
119                 $console->setArgument(1, 'test');
120                 $console->setArgument(2, 'now');
121                 $txt = $this->dumpExecute($console);
122
123                 self::assertEquals("[Error] config.test is an array and can't be set using this command.\n", $txt);
124         }
125
126         public function testSetExistingValue()
127         {
128                 $this->configMock
129                         ->shouldReceive('get')
130                         ->with('config', 'test')
131                         ->andReturn('now')
132                         ->twice();
133
134                 $console = new Config($this->appMode, $this->configMock, $this->consoleArgv);
135                 $console->setArgument(0, 'config');
136                 $console->setArgument(1, 'test');
137                 $console->setArgument(2, 'now');
138                 $txt = $this->dumpExecute($console);
139
140                 self::assertEquals("[Error] config.test already set to now.\n", $txt);
141         }
142
143         public function testTooManyArguments()
144         {
145                 $console = new Config($this->appMode, $this->configMock, $this->consoleArgv);
146                 $console->setArgument(0, 'config');
147                 $console->setArgument(1, 'test');
148                 $console->setArgument(2, 'it');
149                 $console->setArgument(3, 'now');
150                 $txt       = $this->dumpExecute($console);
151                 $assertion = '[Warning] Too many arguments';
152                 $firstline = substr($txt, 0, strlen($assertion));
153                 self::assertEquals($assertion, $firstline);
154         }
155
156         public function testVerbose()
157         {
158                 $this->configMock
159                         ->shouldReceive('get')
160                         ->with('test', 'it')
161                         ->andReturn('now')
162                         ->once();
163                 $console = new Config($this->appMode, $this->configMock, $this->consoleArgv);
164                 $console->setArgument(0, 'test');
165                 $console->setArgument(1, 'it');
166                 $console->setOption('v', 1);
167                 $executable = $this->consoleArgv[0];
168                 $assertion  = <<<CONF
169 Executable: {$executable}
170 Class: Friendica\Console\Config
171 Arguments: array (
172   0 => 'test',
173   1 => 'it',
174 )
175 Options: array (
176   'v' => 1,
177 )
178 test.it => now
179
180 CONF;
181                 $txt        = $this->dumpExecute($console);
182                 self::assertEquals($assertion, $txt);
183         }
184
185         public function testUnableToSet()
186         {
187                 $this->configMock
188                         ->shouldReceive('set')
189                         ->with('test', 'it', 'now')
190                         ->andReturn(false)
191                         ->once();
192                 $this->configMock
193                         ->shouldReceive('get')
194                         ->with('test', 'it')
195                         ->andReturn(null)
196                         ->twice();
197                 $console = new Config($this->appMode, $this->configMock, [$this->consoleArgv]);
198                 $console->setArgument(0, 'test');
199                 $console->setArgument(1, 'it');
200                 $console->setArgument(2, 'now');
201                 $txt = $this->dumpExecute($console);
202                 self::assertSame("Unable to set test.it\n", $txt);
203         }
204
205         public function testGetHelp()
206         {
207                 // Usable to purposely fail if new commands are added without taking tests into account
208                 $theHelp = <<<HELP
209 console config - Manage site configuration
210 Synopsis
211         bin/console config [-h|--help|-?] [-v]
212         bin/console config <category> [-h|--help|-?] [-v]
213         bin/console config <category> <key> [-h|--help|-?] [-v]
214         bin/console config <category> <key> <value> [-h|--help|-?] [-v]
215
216 Description
217         bin/console config
218                 Lists all config values
219
220         bin/console config <category>
221                 Lists all config values in the provided category
222
223         bin/console config <category> <key>
224                 Shows the value of the provided key in the category
225
226         bin/console config <category> <key> <value>
227                 Sets the value of the provided key in the category
228
229 Notes:
230         Setting config entries which are manually set in config/local.config.php may result in
231         conflict between database settings and the manual startup settings.
232
233 Options
234     -h|--help|-? Show help information
235     -v           Show more debug information.
236
237 HELP;
238                 $console = new Config($this->appMode, $this->configMock, [$this->consoleArgv]);
239                 $console->setOption('help', true);
240
241                 $txt = $this->dumpExecute($console);
242
243                 self::assertEquals($txt, $theHelp);
244         }
245 }