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