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