3 * @copyright Copyright (C) 2020, Friendica
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\ILock;
28 use Mockery\MockInterface;
30 class LockConsoleTest extends ConsoleTest
33 * @var App\Mode|MockInterface $appMode
38 * @var ILock|MockInterface
42 protected function setUp()
46 \Mockery::getConfiguration()->setConstantsMap([
48 'DBCONFIGAVAILABLE' => 0
52 $this->appMode = \Mockery::mock(App\Mode::class);
53 $this->appMode->shouldReceive('has')
56 $this->lockMock = \Mockery::mock(ILock::class);
59 public function testList()
62 ->shouldReceive('getLocks')
63 ->andReturn(['test', 'test2'])
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);
72 public function testListPrefix()
75 ->shouldReceive('getLocks')
77 ->andReturn(['test', 'test2'])
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);
87 public function testDelLock()
90 ->shouldReceive('release')
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);
102 public function testDelUnknownLock()
105 ->shouldReceive('release')
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);
117 public function testSetLock()
120 ->shouldReceive('isLocked')
125 ->shouldReceive('acquire')
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);
137 public function testSetLockIsLocked()
140 ->shouldReceive('isLocked')
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);
152 public function testSetLockNotWorking()
155 ->shouldReceive('isLocked')
160 ->shouldReceive('acquire')
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);
172 public function testReleaseAll()
175 ->shouldReceive('releaseAll')
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);
185 public function testReleaseAllFailed()
188 ->shouldReceive('releaseAll')
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);
198 public function testGetHelp()
200 // Usable to purposely fail if new commands are added without taking tests into account
202 console lock - Manage node locks
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]
210 bin/console lock list [<prefix>]
211 List all locks, optionally filtered by a prefix
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.
216 bin/console lock del <lock>
219 bin/console lock clear
223 -h|--help|-? Show help information
224 -v Show more debug information.
227 $console = new Lock($this->appMode, $this->lockMock, [$this->consoleArgv]);
228 $console->setOption('help', true);
230 $txt = $this->dumpExecute($console);
232 $this->assertEquals($txt, $theHelp);