3 namespace Friendica\Test\src\Console;
6 use Friendica\App\Mode;
7 use Friendica\Console\Lock;
8 use Friendica\Core\Lock\ILock;
9 use Mockery\MockInterface;
11 class LockConsoleTest extends ConsoleTest
14 * @var App\Mode|MockInterface $appMode
19 * @var ILock|MockInterface
23 protected function setUp()
27 \Mockery::getConfiguration()->setConstantsMap([
29 'DBCONFIGAVAILABLE' => 0
33 $this->appMode = \Mockery::mock(App\Mode::class);
34 $this->appMode->shouldReceive('has')
37 $this->lockMock = \Mockery::mock(ILock::class);
40 public function testList()
43 ->shouldReceive('getLocks')
44 ->andReturn(['test', 'test2'])
47 $console = new Lock($this->appMode, $this->lockMock, $this->consoleArgv);
48 $console->setArgument(0, 'list');
49 $txt = $this->dumpExecute($console);
50 $this->assertEquals("Listing all Locks:\ntest\ntest2\n2 locks found\n", $txt);
53 public function testListPrefix()
56 ->shouldReceive('getLocks')
58 ->andReturn(['test', 'test2'])
61 $console = new Lock($this->appMode, $this->lockMock, $this->consoleArgv);
62 $console->setArgument(0, 'list');
63 $console->setArgument(1, 'test');
64 $txt = $this->dumpExecute($console);
65 $this->assertEquals("Listing all Locks starting with \"test\":\ntest\ntest2\n2 locks found\n", $txt);
68 public function testDelLock()
71 ->shouldReceive('releaseLock')
76 $console = new Lock($this->appMode, $this->lockMock, $this->consoleArgv);
77 $console->setArgument(0, 'del');
78 $console->setArgument(1, 'test');
79 $txt = $this->dumpExecute($console);
80 $this->assertEquals("Lock 'test' released.\n", $txt);
83 public function testDelUnknownLock()
86 ->shouldReceive('releaseLock')
91 $console = new Lock($this->appMode, $this->lockMock, $this->consoleArgv);
92 $console->setArgument(0, 'del');
93 $console->setArgument(1, 'test');
94 $txt = $this->dumpExecute($console);
95 $this->assertEquals("Couldn't release Lock 'test'\n", $txt);
98 public function testSetLock()
101 ->shouldReceive('isLocked')
106 ->shouldReceive('acquireLock')
111 $console = new Lock($this->appMode, $this->lockMock, $this->consoleArgv);
112 $console->setArgument(0, 'set');
113 $console->setArgument(1, 'test');
114 $txt = $this->dumpExecute($console);
115 $this->assertEquals("Lock 'test' acquired.\n", $txt);
118 public function testSetLockIsLocked()
121 ->shouldReceive('isLocked')
126 $console = new Lock($this->appMode, $this->lockMock, $this->consoleArgv);
127 $console->setArgument(0, 'set');
128 $console->setArgument(1, 'test');
129 $txt = $this->dumpExecute($console);
130 $this->assertEquals("[Error] 'test' is already set.\n", $txt);
133 public function testSetLockNotWorking()
136 ->shouldReceive('isLocked')
141 ->shouldReceive('acquireLock')
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 $this->assertEquals("[Error] Unable to lock 'test'.\n", $txt);
153 public function testReleaseAll()
156 ->shouldReceive('releaseAll')
160 $console = new Lock($this->appMode, $this->lockMock, $this->consoleArgv);
161 $console->setArgument(0, 'clear');
162 $txt = $this->dumpExecute($console);
163 $this->assertEquals("Locks successfully cleared.\n", $txt);
166 public function testReleaseAllFailed()
169 ->shouldReceive('releaseAll')
173 $console = new Lock($this->appMode, $this->lockMock, $this->consoleArgv);
174 $console->setArgument(0, 'clear');
175 $txt = $this->dumpExecute($console);
176 $this->assertEquals("[Error] Unable to clear the locks.\n", $txt);
179 public function testGetHelp()
181 // Usable to purposely fail if new commands are added without taking tests into account
183 console lock - Manage node locks
185 bin/console lock list [<prefix>] [-h|--help|-?] [-v]
186 bin/console lock set <lock> [<timeout> [<ttl>]] [-h|--help|-?] [-v]
187 bin/console lock del <lock> [-h|--help|-?] [-v]
188 bin/console lock clear [-h|--help|-?] [-v]
191 bin/console lock list [<prefix>]
192 List all locks, optionally filtered by a prefix
194 bin/console lock set <lock> [<timeout> [<ttl>]]
195 Sets manually a lock, optionally with the provided TTL (time to live) with a default of five minutes.
197 bin/console lock del <lock>
200 bin/console lock clear
204 -h|--help|-? Show help information
205 -v Show more debug information.
208 $console = new Lock($this->appMode, $this->lockMock, [$this->consoleArgv]);
209 $console->setOption('help', true);
211 $txt = $this->dumpExecute($console);
213 $this->assertEquals($txt, $theHelp);