3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Test\src\Console;
25 use Friendica\App\Mode;
26 use Friendica\Console\Lock;
27 use Friendica\Core\Lock\Capability\ICanLock;
29 use Mockery\MockInterface;
31 class LockConsoleTest extends ConsoleTest
34 * @var App\Mode|MockInterface $appMode
39 * @var ICanLock|MockInterface
43 protected function setUp() : void
47 Mockery::getConfiguration()->setConstantsMap([
49 'DBCONFIGAVAILABLE' => 0
53 $this->appMode = Mockery::mock(App\Mode::class);
54 $this->appMode->shouldReceive('has')
57 $this->lockMock = Mockery::mock(ICanLock::class);
60 public function testList()
63 ->shouldReceive('getLocks')
64 ->andReturn(['test', 'test2'])
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);
73 public function testListPrefix()
76 ->shouldReceive('getLocks')
78 ->andReturn(['test', 'test2'])
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);
88 public function testDelLock()
91 ->shouldReceive('release')
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);
103 public function testDelUnknownLock()
106 ->shouldReceive('release')
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);
118 public function testSetLock()
121 ->shouldReceive('isLocked')
126 ->shouldReceive('acquire')
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);
138 public function testSetLockIsLocked()
141 ->shouldReceive('isLocked')
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);
153 public function testSetLockNotWorking()
156 ->shouldReceive('isLocked')
161 ->shouldReceive('acquire')
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);
173 public function testReleaseAll()
176 ->shouldReceive('releaseAll')
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);
186 public function testReleaseAllFailed()
189 ->shouldReceive('releaseAll')
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);
199 public function testGetHelp()
201 // Usable to purposely fail if new commands are added without taking tests into account
203 console lock - Manage node locks
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]
211 bin/console lock list [<prefix>]
212 List all locks, optionally filtered by a prefix
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.
217 bin/console lock del <lock>
220 bin/console lock clear
224 -h|--help|-? Show help information
225 -v Show more debug information.
228 $console = new Lock($this->appMode, $this->lockMock, [$this->consoleArgv]);
229 $console->setOption('help', true);
231 $txt = $this->dumpExecute($console);
233 self::assertEquals($txt, $theHelp);