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