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