2019-08-15 08:22:29 -04:00
|
|
|
<?php
|
2020-02-09 09:45:36 -05:00
|
|
|
/**
|
2021-03-29 02:40:20 -04:00
|
|
|
* @copyright Copyright (C) 2010-2021, the Friendica project
|
2020-02-09 09:45:36 -05:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
2019-08-15 08:22:29 -04:00
|
|
|
|
|
|
|
namespace Friendica\Test\src\Console;
|
|
|
|
|
|
|
|
use Friendica\App;
|
|
|
|
use Friendica\App\Mode;
|
|
|
|
use Friendica\Console\Lock;
|
|
|
|
use Friendica\Core\Lock\ILock;
|
2020-10-18 14:31:57 -04:00
|
|
|
use Mockery;
|
2019-08-15 08:22:29 -04:00
|
|
|
use Mockery\MockInterface;
|
|
|
|
|
|
|
|
class LockConsoleTest extends ConsoleTest
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var App\Mode|MockInterface $appMode
|
|
|
|
*/
|
|
|
|
private $appMode;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var ILock|MockInterface
|
|
|
|
*/
|
|
|
|
private $lockMock;
|
|
|
|
|
2021-04-01 17:04:30 -04:00
|
|
|
protected function setUp() : void
|
2019-08-15 08:22:29 -04:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
2020-10-18 14:31:57 -04:00
|
|
|
Mockery::getConfiguration()->setConstantsMap([
|
2019-08-15 08:22:29 -04:00
|
|
|
Mode::class => [
|
|
|
|
'DBCONFIGAVAILABLE' => 0
|
|
|
|
]
|
|
|
|
]);
|
|
|
|
|
2020-10-18 14:31:57 -04:00
|
|
|
$this->appMode = Mockery::mock(App\Mode::class);
|
2019-08-15 08:22:29 -04:00
|
|
|
$this->appMode->shouldReceive('has')
|
|
|
|
->andReturn(true);
|
|
|
|
|
2020-10-18 14:31:57 -04:00
|
|
|
$this->lockMock = Mockery::mock(ILock::class);
|
2019-08-15 08:22:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testList()
|
|
|
|
{
|
|
|
|
$this->lockMock
|
|
|
|
->shouldReceive('getLocks')
|
|
|
|
->andReturn(['test', 'test2'])
|
|
|
|
->once();
|
|
|
|
|
|
|
|
$console = new Lock($this->appMode, $this->lockMock, $this->consoleArgv);
|
|
|
|
$console->setArgument(0, 'list');
|
|
|
|
$txt = $this->dumpExecute($console);
|
2020-10-17 08:19:57 -04:00
|
|
|
self::assertEquals("Listing all Locks:\ntest\ntest2\n2 locks found\n", $txt);
|
2019-08-15 08:22:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testListPrefix()
|
|
|
|
{
|
|
|
|
$this->lockMock
|
|
|
|
->shouldReceive('getLocks')
|
|
|
|
->with('test')
|
|
|
|
->andReturn(['test', 'test2'])
|
|
|
|
->once();
|
|
|
|
|
|
|
|
$console = new Lock($this->appMode, $this->lockMock, $this->consoleArgv);
|
|
|
|
$console->setArgument(0, 'list');
|
|
|
|
$console->setArgument(1, 'test');
|
|
|
|
$txt = $this->dumpExecute($console);
|
2020-10-17 08:19:57 -04:00
|
|
|
self::assertEquals("Listing all Locks starting with \"test\":\ntest\ntest2\n2 locks found\n", $txt);
|
2019-08-15 08:22:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testDelLock()
|
|
|
|
{
|
|
|
|
$this->lockMock
|
2020-01-06 18:57:30 -05:00
|
|
|
->shouldReceive('release')
|
2019-08-15 08:22:29 -04:00
|
|
|
->with('test', true)
|
|
|
|
->andReturn(true)
|
|
|
|
->once();
|
|
|
|
|
|
|
|
$console = new Lock($this->appMode, $this->lockMock, $this->consoleArgv);
|
|
|
|
$console->setArgument(0, 'del');
|
|
|
|
$console->setArgument(1, 'test');
|
|
|
|
$txt = $this->dumpExecute($console);
|
2020-10-17 08:19:57 -04:00
|
|
|
self::assertEquals("Lock 'test' released.\n", $txt);
|
2019-08-15 08:22:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testDelUnknownLock()
|
|
|
|
{
|
|
|
|
$this->lockMock
|
2020-01-06 18:57:30 -05:00
|
|
|
->shouldReceive('release')
|
2019-08-15 08:22:29 -04:00
|
|
|
->with('test', true)
|
|
|
|
->andReturn(false)
|
|
|
|
->once();
|
|
|
|
|
|
|
|
$console = new Lock($this->appMode, $this->lockMock, $this->consoleArgv);
|
|
|
|
$console->setArgument(0, 'del');
|
|
|
|
$console->setArgument(1, 'test');
|
|
|
|
$txt = $this->dumpExecute($console);
|
2020-10-17 08:19:57 -04:00
|
|
|
self::assertEquals("Couldn't release Lock 'test'\n", $txt);
|
2019-08-15 08:22:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetLock()
|
|
|
|
{
|
|
|
|
$this->lockMock
|
|
|
|
->shouldReceive('isLocked')
|
|
|
|
->with('test')
|
|
|
|
->andReturn(false)
|
|
|
|
->once();
|
|
|
|
$this->lockMock
|
2020-01-06 18:57:30 -05:00
|
|
|
->shouldReceive('acquire')
|
2019-08-15 08:22:29 -04:00
|
|
|
->with('test')
|
|
|
|
->andReturn(true)
|
|
|
|
->once();
|
|
|
|
|
|
|
|
$console = new Lock($this->appMode, $this->lockMock, $this->consoleArgv);
|
|
|
|
$console->setArgument(0, 'set');
|
|
|
|
$console->setArgument(1, 'test');
|
|
|
|
$txt = $this->dumpExecute($console);
|
2020-10-17 08:19:57 -04:00
|
|
|
self::assertEquals("Lock 'test' acquired.\n", $txt);
|
2019-08-15 08:22:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetLockIsLocked()
|
|
|
|
{
|
|
|
|
$this->lockMock
|
|
|
|
->shouldReceive('isLocked')
|
|
|
|
->with('test')
|
|
|
|
->andReturn(true)
|
|
|
|
->once();
|
|
|
|
|
|
|
|
$console = new Lock($this->appMode, $this->lockMock, $this->consoleArgv);
|
|
|
|
$console->setArgument(0, 'set');
|
|
|
|
$console->setArgument(1, 'test');
|
|
|
|
$txt = $this->dumpExecute($console);
|
2020-10-17 08:19:57 -04:00
|
|
|
self::assertEquals("[Error] 'test' is already set.\n", $txt);
|
2019-08-15 08:22:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetLockNotWorking()
|
|
|
|
{
|
|
|
|
$this->lockMock
|
|
|
|
->shouldReceive('isLocked')
|
|
|
|
->with('test')
|
|
|
|
->andReturn(false)
|
|
|
|
->once();
|
|
|
|
$this->lockMock
|
2020-01-06 18:57:30 -05:00
|
|
|
->shouldReceive('acquire')
|
2019-08-15 08:22:29 -04:00
|
|
|
->with('test')
|
|
|
|
->andReturn(false)
|
|
|
|
->once();
|
|
|
|
|
|
|
|
$console = new Lock($this->appMode, $this->lockMock, $this->consoleArgv);
|
|
|
|
$console->setArgument(0, 'set');
|
|
|
|
$console->setArgument(1, 'test');
|
|
|
|
$txt = $this->dumpExecute($console);
|
2020-10-17 08:19:57 -04:00
|
|
|
self::assertEquals("[Error] Unable to lock 'test'.\n", $txt);
|
2019-08-15 08:22:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testReleaseAll()
|
|
|
|
{
|
|
|
|
$this->lockMock
|
|
|
|
->shouldReceive('releaseAll')
|
|
|
|
->andReturn(true)
|
|
|
|
->once();
|
|
|
|
|
|
|
|
$console = new Lock($this->appMode, $this->lockMock, $this->consoleArgv);
|
|
|
|
$console->setArgument(0, 'clear');
|
|
|
|
$txt = $this->dumpExecute($console);
|
2020-10-17 08:19:57 -04:00
|
|
|
self::assertEquals("Locks successfully cleared.\n", $txt);
|
2019-08-15 08:22:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testReleaseAllFailed()
|
|
|
|
{
|
|
|
|
$this->lockMock
|
|
|
|
->shouldReceive('releaseAll')
|
|
|
|
->andReturn(false)
|
|
|
|
->once();
|
|
|
|
|
|
|
|
$console = new Lock($this->appMode, $this->lockMock, $this->consoleArgv);
|
|
|
|
$console->setArgument(0, 'clear');
|
|
|
|
$txt = $this->dumpExecute($console);
|
2020-10-17 08:19:57 -04:00
|
|
|
self::assertEquals("[Error] Unable to clear the locks.\n", $txt);
|
2019-08-15 08:22:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetHelp()
|
|
|
|
{
|
|
|
|
// Usable to purposely fail if new commands are added without taking tests into account
|
|
|
|
$theHelp = <<<HELP
|
|
|
|
console lock - Manage node locks
|
|
|
|
Synopsis
|
|
|
|
bin/console lock list [<prefix>] [-h|--help|-?] [-v]
|
|
|
|
bin/console lock set <lock> [<timeout> [<ttl>]] [-h|--help|-?] [-v]
|
|
|
|
bin/console lock del <lock> [-h|--help|-?] [-v]
|
|
|
|
bin/console lock clear [-h|--help|-?] [-v]
|
|
|
|
|
|
|
|
Description
|
|
|
|
bin/console lock list [<prefix>]
|
|
|
|
List all locks, optionally filtered by a prefix
|
|
|
|
|
|
|
|
bin/console lock set <lock> [<timeout> [<ttl>]]
|
|
|
|
Sets manually a lock, optionally with the provided TTL (time to live) with a default of five minutes.
|
|
|
|
|
|
|
|
bin/console lock del <lock>
|
|
|
|
Deletes a lock.
|
|
|
|
|
|
|
|
bin/console lock clear
|
|
|
|
Clears all locks
|
|
|
|
|
|
|
|
Options
|
|
|
|
-h|--help|-? Show help information
|
|
|
|
-v Show more debug information.
|
|
|
|
|
|
|
|
HELP;
|
|
|
|
$console = new Lock($this->appMode, $this->lockMock, [$this->consoleArgv]);
|
|
|
|
$console->setOption('help', true);
|
|
|
|
|
|
|
|
$txt = $this->dumpExecute($console);
|
|
|
|
|
2020-10-17 08:19:57 -04:00
|
|
|
self::assertEquals($txt, $theHelp);
|
2019-08-15 08:22:29 -04:00
|
|
|
}
|
|
|
|
}
|