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