Update copyright
[friendica.git/.git] / tests / src / Console / LockConsoleTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
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;
29 use Mockery\MockInterface;
30
31 class LockConsoleTest extends ConsoleTest
32 {
33         /**
34          * @var App\Mode|MockInterface $appMode
35          */
36         private $appMode;
37
38         /**
39          * @var ILock|MockInterface
40          */
41         private $lockMock;
42
43         protected function setUp()
44         {
45                 parent::setUp();
46
47                 Mockery::getConfiguration()->setConstantsMap([
48                         Mode::class => [
49                                 'DBCONFIGAVAILABLE' => 0
50                         ]
51                 ]);
52
53                 $this->appMode = Mockery::mock(App\Mode::class);
54                 $this->appMode->shouldReceive('has')
55                         ->andReturn(true);
56
57                 $this->lockMock = Mockery::mock(ILock::class);
58         }
59
60         public function testList()
61         {
62                 $this->lockMock
63                         ->shouldReceive('getLocks')
64                         ->andReturn(['test', 'test2'])
65                         ->once();
66
67                 $console = new Lock($this->appMode, $this->lockMock, $this->consoleArgv);
68                 $console->setArgument(0, 'list');
69                 $txt = $this->dumpExecute($console);
70                 self::assertEquals("Listing all Locks:\ntest\ntest2\n2 locks found\n", $txt);
71         }
72
73         public function testListPrefix()
74         {
75                 $this->lockMock
76                         ->shouldReceive('getLocks')
77                         ->with('test')
78                         ->andReturn(['test', 'test2'])
79                         ->once();
80
81                 $console = new Lock($this->appMode, $this->lockMock, $this->consoleArgv);
82                 $console->setArgument(0, 'list');
83                 $console->setArgument(1, 'test');
84                 $txt = $this->dumpExecute($console);
85                 self::assertEquals("Listing all Locks starting with \"test\":\ntest\ntest2\n2 locks found\n", $txt);
86         }
87
88         public function testDelLock()
89         {
90                 $this->lockMock
91                         ->shouldReceive('release')
92                         ->with('test', true)
93                         ->andReturn(true)
94                         ->once();
95
96                 $console = new Lock($this->appMode, $this->lockMock, $this->consoleArgv);
97                 $console->setArgument(0, 'del');
98                 $console->setArgument(1, 'test');
99                 $txt = $this->dumpExecute($console);
100                 self::assertEquals("Lock 'test' released.\n", $txt);
101         }
102
103         public function testDelUnknownLock()
104         {
105                 $this->lockMock
106                         ->shouldReceive('release')
107                         ->with('test', true)
108                         ->andReturn(false)
109                         ->once();
110
111                 $console = new Lock($this->appMode, $this->lockMock, $this->consoleArgv);
112                 $console->setArgument(0, 'del');
113                 $console->setArgument(1, 'test');
114                 $txt = $this->dumpExecute($console);
115                 self::assertEquals("Couldn't release Lock 'test'\n", $txt);
116         }
117
118         public function testSetLock()
119         {
120                 $this->lockMock
121                         ->shouldReceive('isLocked')
122                         ->with('test')
123                         ->andReturn(false)
124                         ->once();
125                 $this->lockMock
126                         ->shouldReceive('acquire')
127                         ->with('test')
128                         ->andReturn(true)
129                         ->once();
130
131                 $console = new Lock($this->appMode, $this->lockMock, $this->consoleArgv);
132                 $console->setArgument(0, 'set');
133                 $console->setArgument(1, 'test');
134                 $txt = $this->dumpExecute($console);
135                 self::assertEquals("Lock 'test' acquired.\n", $txt);
136         }
137
138         public function testSetLockIsLocked()
139         {
140                 $this->lockMock
141                         ->shouldReceive('isLocked')
142                         ->with('test')
143                         ->andReturn(true)
144                         ->once();
145
146                 $console = new Lock($this->appMode, $this->lockMock, $this->consoleArgv);
147                 $console->setArgument(0, 'set');
148                 $console->setArgument(1, 'test');
149                 $txt = $this->dumpExecute($console);
150                 self::assertEquals("[Error] 'test' is already set.\n", $txt);
151         }
152
153         public function testSetLockNotWorking()
154         {
155                 $this->lockMock
156                         ->shouldReceive('isLocked')
157                         ->with('test')
158                         ->andReturn(false)
159                         ->once();
160                 $this->lockMock
161                         ->shouldReceive('acquire')
162                         ->with('test')
163                         ->andReturn(false)
164                         ->once();
165
166                 $console = new Lock($this->appMode, $this->lockMock, $this->consoleArgv);
167                 $console->setArgument(0, 'set');
168                 $console->setArgument(1, 'test');
169                 $txt = $this->dumpExecute($console);
170                 self::assertEquals("[Error] Unable to lock 'test'.\n", $txt);
171         }
172
173         public function testReleaseAll()
174         {
175                 $this->lockMock
176                         ->shouldReceive('releaseAll')
177                         ->andReturn(true)
178                         ->once();
179
180                 $console = new Lock($this->appMode, $this->lockMock, $this->consoleArgv);
181                 $console->setArgument(0, 'clear');
182                 $txt = $this->dumpExecute($console);
183                 self::assertEquals("Locks successfully cleared.\n", $txt);
184         }
185
186         public function testReleaseAllFailed()
187         {
188                 $this->lockMock
189                         ->shouldReceive('releaseAll')
190                         ->andReturn(false)
191                         ->once();
192
193                 $console = new Lock($this->appMode, $this->lockMock, $this->consoleArgv);
194                 $console->setArgument(0, 'clear');
195                 $txt = $this->dumpExecute($console);
196                 self::assertEquals("[Error] Unable to clear the locks.\n", $txt);
197         }
198
199         public function testGetHelp()
200         {
201                 // Usable to purposely fail if new commands are added without taking tests into account
202                 $theHelp = <<<HELP
203 console lock - Manage node locks
204 Synopsis
205         bin/console lock list [<prefix>] [-h|--help|-?] [-v]
206         bin/console lock set <lock> [<timeout> [<ttl>]] [-h|--help|-?] [-v]
207         bin/console lock del <lock> [-h|--help|-?] [-v]
208         bin/console lock clear [-h|--help|-?] [-v]
209
210 Description
211         bin/console lock list [<prefix>]
212                 List all locks, optionally filtered by a prefix
213
214         bin/console lock set <lock> [<timeout> [<ttl>]]
215                 Sets manually a lock, optionally with the provided TTL (time to live) with a default of five minutes.
216
217         bin/console lock del <lock>
218                 Deletes a lock.
219
220         bin/console lock clear
221                 Clears all locks
222
223 Options
224     -h|--help|-? Show help information
225     -v           Show more debug information.
226
227 HELP;
228                 $console = new Lock($this->appMode, $this->lockMock, [$this->consoleArgv]);
229                 $console->setOption('help', true);
230
231                 $txt = $this->dumpExecute($console);
232
233                 self::assertEquals($txt, $theHelp);
234         }
235 }