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