]> git.mxchange.org Git - friendica.git/blob - tests/src/Console/ConfigConsoleTest.php
Merge pull request #9680 from annando/issue-9402
[friendica.git] / tests / src / Console / ConfigConsoleTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
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\IConfig;
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 IConfig|LegacyMockInterface|MockInterface */
39         private $configMock;
40
41         protected function setUp()
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(IConfig::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('now')
69                         ->twice();
70
71                 $console = new Config($this->appMode, $this->configMock, $this->consoleArgv);
72                 $console->setArgument(0, 'config');
73                 $console->setArgument(1, 'test');
74                 $console->setArgument(2, 'now');
75                 $txt = $this->dumpExecute($console);
76                 self::assertEquals("config.test <= now\n", $txt);
77
78                 $this->configMock
79                         ->shouldReceive('get')
80                         ->with('config', 'test')
81                         ->andReturn('now')
82                         ->once();
83
84                 $console = new Config($this->appMode, $this->configMock, [$this->consoleArgv]);
85                 $console->setArgument(0, 'config');
86                 $console->setArgument(1, 'test');
87                 $txt = $this->dumpExecute($console);
88                 self::assertEquals("config.test => now\n", $txt);
89
90                 $this->configMock
91                         ->shouldReceive('get')
92                         ->with('config', 'test')
93                         ->andReturn(null)
94                         ->once();
95
96                 $console = new Config($this->appMode, $this->configMock, $this->consoleArgv);
97                 $console->setArgument(0, 'config');
98                 $console->setArgument(1, 'test');
99                 $txt = $this->dumpExecute($console);
100                 self::assertEquals("config.test => \n", $txt);
101         }
102
103         public function testSetArrayValue()
104         {
105                 $testArray = [1, 2, 3];
106                 $this->configMock
107                         ->shouldReceive('get')
108                         ->with('config', 'test')
109                         ->andReturn($testArray)
110                         ->once();
111
112                 $console = new Config($this->appMode, $this->configMock, $this->consoleArgv);
113                 $console->setArgument(0, 'config');
114                 $console->setArgument(1, 'test');
115                 $console->setArgument(2, 'now');
116                 $txt = $this->dumpExecute($console);
117
118                 self::assertEquals("[Error] config.test is an array and can't be set using this command.\n", $txt);
119         }
120
121         public function testTooManyArguments()
122         {
123                 $console = new Config($this->appMode, $this->configMock, $this->consoleArgv);
124                 $console->setArgument(0, 'config');
125                 $console->setArgument(1, 'test');
126                 $console->setArgument(2, 'it');
127                 $console->setArgument(3, 'now');
128                 $txt       = $this->dumpExecute($console);
129                 $assertion = '[Warning] Too many arguments';
130                 $firstline = substr($txt, 0, strlen($assertion));
131                 self::assertEquals($assertion, $firstline);
132         }
133
134         public function testVerbose()
135         {
136                 $this->configMock
137                         ->shouldReceive('get')
138                         ->with('test', 'it')
139                         ->andReturn('now')
140                         ->once();
141                 $console = new Config($this->appMode, $this->configMock, $this->consoleArgv);
142                 $console->setArgument(0, 'test');
143                 $console->setArgument(1, 'it');
144                 $console->setOption('v', 1);
145                 $executable = $this->consoleArgv[0];
146                 $assertion  = <<<CONF
147 Executable: {$executable}
148 Class: Friendica\Console\Config
149 Arguments: array (
150   0 => 'test',
151   1 => 'it',
152 )
153 Options: array (
154   'v' => 1,
155 )
156 test.it => now
157
158 CONF;
159                 $txt        = $this->dumpExecute($console);
160                 self::assertEquals($assertion, $txt);
161         }
162
163         public function testUnableToSet()
164         {
165                 $this->configMock
166                         ->shouldReceive('set')
167                         ->with('test', 'it', 'now')
168                         ->andReturn(false)
169                         ->once();
170                 $this->configMock
171                         ->shouldReceive('get')
172                         ->with('test', 'it')
173                         ->andReturn(null)
174                         ->once();
175                 $console = new Config($this->appMode, $this->configMock, [$this->consoleArgv]);
176                 $console->setArgument(0, 'test');
177                 $console->setArgument(1, 'it');
178                 $console->setArgument(2, 'now');
179                 $txt = $this->dumpExecute($console);
180                 self::assertSame("Unable to set test.it\n", $txt);
181         }
182
183         public function testGetHelp()
184         {
185                 // Usable to purposely fail if new commands are added without taking tests into account
186                 $theHelp = <<<HELP
187 console config - Manage site configuration
188 Synopsis
189         bin/console config [-h|--help|-?] [-v]
190         bin/console config <category> [-h|--help|-?] [-v]
191         bin/console config <category> <key> [-h|--help|-?] [-v]
192         bin/console config <category> <key> <value> [-h|--help|-?] [-v]
193
194 Description
195         bin/console config
196                 Lists all config values
197
198         bin/console config <category>
199                 Lists all config values in the provided category
200
201         bin/console config <category> <key>
202                 Shows the value of the provided key in the category
203
204         bin/console config <category> <key> <value>
205                 Sets the value of the provided key in the category
206
207 Notes:
208         Setting config entries which are manually set in config/local.config.php may result in
209         conflict between database settings and the manual startup settings.
210
211 Options
212     -h|--help|-? Show help information
213     -v           Show more debug information.
214
215 HELP;
216                 $console = new Config($this->appMode, $this->configMock, [$this->consoleArgv]);
217                 $console->setOption('help', true);
218
219                 $txt = $this->dumpExecute($console);
220
221                 self::assertEquals($txt, $theHelp);
222         }
223 }