From d95457cf611881a9dde534bacfe368661553e20f Mon Sep 17 00:00:00 2001 From: Philipp Holzer Date: Thu, 15 Aug 2019 14:22:29 +0200 Subject: [PATCH] Add Lock test --- src/Console/Lock.php | 18 +-- tests/src/Console/LockConsoleTest.php | 215 ++++++++++++++++++++++++++ 2 files changed, 224 insertions(+), 9 deletions(-) create mode 100644 tests/src/Console/LockConsoleTest.php diff --git a/src/Console/Lock.php b/src/Console/Lock.php index fe9132b7cb..46826cc8c9 100644 --- a/src/Console/Lock.php +++ b/src/Console/Lock.php @@ -32,7 +32,7 @@ class Lock extends \Asika\SimpleConsole\Console protected function getHelp() { $help = <<] [-h|--help|-?] [-v] bin/console lock set [ []] [-h|--help|-?] [-v] @@ -131,9 +131,9 @@ HELP; private function executeDel() { if (count($this->args) >= 2) { - $lock = $this->getArgument(1); + $lock = $this->getArgument(1); - if ($this->lock->releaseLock($lock, true)){ + if ($this->lock->releaseLock($lock, true)) { $this->out(sprintf('Lock \'%s\' released.', $lock)); } else { $this->out(sprintf('Couldn\'t release Lock \'%s\'', $lock)); @@ -147,11 +147,11 @@ HELP; private function executeSet() { if (count($this->args) >= 2) { - $lock = $this->getArgument(1); + $lock = $this->getArgument(1); $timeout = intval($this->getArgument(2, false)); - $ttl = intval($this->getArgument(3, false)); + $ttl = intval($this->getArgument(3, false)); - if (is_array($this->lock->isLocked($lock))) { + if ($this->lock->isLocked($lock)) { throw new RuntimeException(sprintf('\'%s\' is already set.', $lock)); } @@ -166,7 +166,7 @@ HELP; if ($result) { $this->out(sprintf('Lock \'%s\' acquired.', $lock)); } else { - $this->out(sprintf('Unable to lock \'%s\'', $lock)); + throw new RuntimeException(sprintf('Unable to lock \'%s\'.', $lock)); } } else { throw new CommandArgsException('Too few arguments for set.'); @@ -177,9 +177,9 @@ HELP; { $result = $this->lock->releaseAll(true); if ($result) { - $this->out('Locks successfully cleared,'); + $this->out('Locks successfully cleared.'); } else { - $this->out('Unable to clear the locks.'); + throw new RuntimeException('Unable to clear the locks.'); } } } diff --git a/tests/src/Console/LockConsoleTest.php b/tests/src/Console/LockConsoleTest.php new file mode 100644 index 0000000000..51c05b5c37 --- /dev/null +++ b/tests/src/Console/LockConsoleTest.php @@ -0,0 +1,215 @@ +setConstantsMap([ + Mode::class => [ + 'DBCONFIGAVAILABLE' => 0 + ] + ]); + + $this->appMode = \Mockery::mock(App\Mode::class); + $this->appMode->shouldReceive('has') + ->andReturn(true); + + $this->lockMock = \Mockery::mock(ILock::class); + } + + 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); + $this->assertEquals("Listing all Locks:\ntest\ntest2\n2 locks found\n", $txt); + } + + 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); + $this->assertEquals("Listing all Locks starting with \"test\":\ntest\ntest2\n2 locks found\n", $txt); + } + + public function testDelLock() + { + $this->lockMock + ->shouldReceive('releaseLock') + ->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); + $this->assertEquals("Lock 'test' released.\n", $txt); + } + + public function testDelUnknownLock() + { + $this->lockMock + ->shouldReceive('releaseLock') + ->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); + $this->assertEquals("Couldn't release Lock 'test'\n", $txt); + } + + public function testSetLock() + { + $this->lockMock + ->shouldReceive('isLocked') + ->with('test') + ->andReturn(false) + ->once(); + $this->lockMock + ->shouldReceive('acquireLock') + ->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); + $this->assertEquals("Lock 'test' acquired.\n", $txt); + } + + 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); + $this->assertEquals("[Error] 'test' is already set.\n", $txt); + } + + public function testSetLockNotWorking() + { + $this->lockMock + ->shouldReceive('isLocked') + ->with('test') + ->andReturn(false) + ->once(); + $this->lockMock + ->shouldReceive('acquireLock') + ->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); + $this->assertEquals("[Error] Unable to lock 'test'.\n", $txt); + } + + 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); + $this->assertEquals("Locks successfully cleared.\n", $txt); + } + + 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); + $this->assertEquals("[Error] Unable to clear the locks.\n", $txt); + } + + public function testGetHelp() + { + // Usable to purposely fail if new commands are added without taking tests into account + $theHelp = <<] [-h|--help|-?] [-v] + bin/console lock set [ []] [-h|--help|-?] [-v] + bin/console lock del [-h|--help|-?] [-v] + bin/console lock clear [-h|--help|-?] [-v] + +Description + bin/console lock list [] + List all locks, optionally filtered by a prefix + + bin/console lock set [ []] + Sets manually a lock, optionally with the provided TTL (time to live) with a default of five minutes. + + bin/console lock del + 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); + + $this->assertEquals($txt, $theHelp); + } +}