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