]> git.mxchange.org Git - friendica.git/blob - tests/src/Console/LockConsoleTest.php
Merge pull request #8269 from MrPetovan/bug/frio-more-actions
[friendica.git] / tests / src / Console / LockConsoleTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Test\src\Console;
23
24 use Friendica\App;
25 use Friendica\App\Mode;
26 use Friendica\Console\Lock;
27 use Friendica\Core\Lock\ILock;
28 use Mockery\MockInterface;
29
30 class LockConsoleTest extends ConsoleTest
31 {
32         /**
33          * @var App\Mode|MockInterface $appMode
34          */
35         private $appMode;
36
37         /**
38          * @var ILock|MockInterface
39          */
40         private $lockMock;
41
42         protected function setUp()
43         {
44                 parent::setUp();
45
46                 \Mockery::getConfiguration()->setConstantsMap([
47                         Mode::class => [
48                                 'DBCONFIGAVAILABLE' => 0
49                         ]
50                 ]);
51
52                 $this->appMode = \Mockery::mock(App\Mode::class);
53                 $this->appMode->shouldReceive('has')
54                         ->andReturn(true);
55
56                 $this->lockMock = \Mockery::mock(ILock::class);
57         }
58
59         public function testList()
60         {
61                 $this->lockMock
62                         ->shouldReceive('getLocks')
63                         ->andReturn(['test', 'test2'])
64                         ->once();
65
66                 $console = new Lock($this->appMode, $this->lockMock, $this->consoleArgv);
67                 $console->setArgument(0, 'list');
68                 $txt = $this->dumpExecute($console);
69                 $this->assertEquals("Listing all Locks:\ntest\ntest2\n2 locks found\n", $txt);
70         }
71
72         public function testListPrefix()
73         {
74                 $this->lockMock
75                         ->shouldReceive('getLocks')
76                         ->with('test')
77                         ->andReturn(['test', 'test2'])
78                         ->once();
79
80                 $console = new Lock($this->appMode, $this->lockMock, $this->consoleArgv);
81                 $console->setArgument(0, 'list');
82                 $console->setArgument(1, 'test');
83                 $txt = $this->dumpExecute($console);
84                 $this->assertEquals("Listing all Locks starting with \"test\":\ntest\ntest2\n2 locks found\n", $txt);
85         }
86
87         public function testDelLock()
88         {
89                 $this->lockMock
90                         ->shouldReceive('release')
91                         ->with('test', true)
92                         ->andReturn(true)
93                         ->once();
94
95                 $console = new Lock($this->appMode, $this->lockMock, $this->consoleArgv);
96                 $console->setArgument(0, 'del');
97                 $console->setArgument(1, 'test');
98                 $txt = $this->dumpExecute($console);
99                 $this->assertEquals("Lock 'test' released.\n", $txt);
100         }
101
102         public function testDelUnknownLock()
103         {
104                 $this->lockMock
105                         ->shouldReceive('release')
106                         ->with('test', true)
107                         ->andReturn(false)
108                         ->once();
109
110                 $console = new Lock($this->appMode, $this->lockMock, $this->consoleArgv);
111                 $console->setArgument(0, 'del');
112                 $console->setArgument(1, 'test');
113                 $txt = $this->dumpExecute($console);
114                 $this->assertEquals("Couldn't release Lock 'test'\n", $txt);
115         }
116
117         public function testSetLock()
118         {
119                 $this->lockMock
120                         ->shouldReceive('isLocked')
121                         ->with('test')
122                         ->andReturn(false)
123                         ->once();
124                 $this->lockMock
125                         ->shouldReceive('acquire')
126                         ->with('test')
127                         ->andReturn(true)
128                         ->once();
129
130                 $console = new Lock($this->appMode, $this->lockMock, $this->consoleArgv);
131                 $console->setArgument(0, 'set');
132                 $console->setArgument(1, 'test');
133                 $txt = $this->dumpExecute($console);
134                 $this->assertEquals("Lock 'test' acquired.\n", $txt);
135         }
136
137         public function testSetLockIsLocked()
138         {
139                 $this->lockMock
140                         ->shouldReceive('isLocked')
141                         ->with('test')
142                         ->andReturn(true)
143                         ->once();
144
145                 $console = new Lock($this->appMode, $this->lockMock, $this->consoleArgv);
146                 $console->setArgument(0, 'set');
147                 $console->setArgument(1, 'test');
148                 $txt = $this->dumpExecute($console);
149                 $this->assertEquals("[Error] 'test' is already set.\n", $txt);
150         }
151
152         public function testSetLockNotWorking()
153         {
154                 $this->lockMock
155                         ->shouldReceive('isLocked')
156                         ->with('test')
157                         ->andReturn(false)
158                         ->once();
159                 $this->lockMock
160                         ->shouldReceive('acquire')
161                         ->with('test')
162                         ->andReturn(false)
163                         ->once();
164
165                 $console = new Lock($this->appMode, $this->lockMock, $this->consoleArgv);
166                 $console->setArgument(0, 'set');
167                 $console->setArgument(1, 'test');
168                 $txt = $this->dumpExecute($console);
169                 $this->assertEquals("[Error] Unable to lock 'test'.\n", $txt);
170         }
171
172         public function testReleaseAll()
173         {
174                 $this->lockMock
175                         ->shouldReceive('releaseAll')
176                         ->andReturn(true)
177                         ->once();
178
179                 $console = new Lock($this->appMode, $this->lockMock, $this->consoleArgv);
180                 $console->setArgument(0, 'clear');
181                 $txt = $this->dumpExecute($console);
182                 $this->assertEquals("Locks successfully cleared.\n", $txt);
183         }
184
185         public function testReleaseAllFailed()
186         {
187                 $this->lockMock
188                         ->shouldReceive('releaseAll')
189                         ->andReturn(false)
190                         ->once();
191
192                 $console = new Lock($this->appMode, $this->lockMock, $this->consoleArgv);
193                 $console->setArgument(0, 'clear');
194                 $txt = $this->dumpExecute($console);
195                 $this->assertEquals("[Error] Unable to clear the locks.\n", $txt);
196         }
197
198         public function testGetHelp()
199         {
200                 // Usable to purposely fail if new commands are added without taking tests into account
201                 $theHelp = <<<HELP
202 console lock - Manage node locks
203 Synopsis
204         bin/console lock list [<prefix>] [-h|--help|-?] [-v]
205         bin/console lock set <lock> [<timeout> [<ttl>]] [-h|--help|-?] [-v]
206         bin/console lock del <lock> [-h|--help|-?] [-v]
207         bin/console lock clear [-h|--help|-?] [-v]
208
209 Description
210         bin/console lock list [<prefix>]
211                 List all locks, optionally filtered by a prefix
212
213         bin/console lock set <lock> [<timeout> [<ttl>]]
214                 Sets manually a lock, optionally with the provided TTL (time to live) with a default of five minutes.
215
216         bin/console lock del <lock>
217                 Deletes a lock.
218
219         bin/console lock clear
220                 Clears all locks
221
222 Options
223     -h|--help|-? Show help information
224     -v           Show more debug information.
225
226 HELP;
227                 $console = new Lock($this->appMode, $this->lockMock, [$this->consoleArgv]);
228                 $console->setOption('help', true);
229
230                 $txt = $this->dumpExecute($console);
231
232                 $this->assertEquals($txt, $theHelp);
233         }
234 }