diff --git a/src/DistributedMemcacheMutex.php b/src/DistributedMemcacheMutex.php index e0c0f63..e98ce4e 100644 --- a/src/DistributedMemcacheMutex.php +++ b/src/DistributedMemcacheMutex.php @@ -16,7 +16,7 @@ class DistributedMemcacheMutex /** * @var Memcache */ - private $memcache; + protected $memcache; /** * @param Memcache $memcache @@ -44,7 +44,7 @@ public function lock($name, $expirationTimeInSeconds = self::FIVE_MINUTES_SECOND /** * @throws InvalidArgumentException */ - private function assertServersAddedToMemcache() + protected function assertServersAddedToMemcache() { if ($this->memcache->getversion() === false) { throw new InvalidArgumentException(self::MEMCACHE_ERR_MSG); diff --git a/src/DistributedMemcachedMutex.php b/src/DistributedMemcachedMutex.php new file mode 100644 index 0000000..7ea090c --- /dev/null +++ b/src/DistributedMemcachedMutex.php @@ -0,0 +1,28 @@ + + * Class DistributedMemcachedMutex + * @package Assertis\Util + */ +class DistributedMemcachedMutex extends DistributedMemcacheMutex +{ + + /** + * DistributedMemcachedMutex constructor. + * @param Memcached $memcached + */ + public function __construct(Memcached $memcached) + { + $this->memcache = $memcached; + } +} \ No newline at end of file diff --git a/tests/DistributedMemcachedMutexTest.php b/tests/DistributedMemcachedMutexTest.php new file mode 100644 index 0000000..f6577bc --- /dev/null +++ b/tests/DistributedMemcachedMutexTest.php @@ -0,0 +1,104 @@ +mutex = new DistributedMemcachedMutex($memcached->withServersAdded()); + $this->serverlessMutex = new DistributedMemcachedMutex($memcachedWithoutServers); + } + + /** + * @test + */ + public function lockWhenMemcacheKeyDoesNotExist() + { + try { + $this->mutex->lock('some_key'); + $this->success(); + } catch (AlreadyLockedException $exception) { + $this->fail($exception->getMessage()); + } + } + + /** + * @test + */ + public function lockThrowsExceptionWhenMemcacheKeyExists() + { + try { + $this->mutex->lock('some_key'); + $this->mutex->lock('some_key'); + $this->fail('DistributedMemcachedMutex::lock method should throw AlreadyLockedException.'); + } catch (AlreadyLockedException $exception) { + $this->success(); + } + } + + /** + * @test + */ + public function unlockWhenMemcacheKeyExists() + { + try { + $this->mutex->lock('some_key'); + $this->mutex->unlock('some_key'); + $this->mutex->lock('some_key'); + $this->success(); + } catch (AlreadyLockedException $exception) { + $this->fail($exception->getMessage(). ' It should not exists because it was unlocked.'); + } + } + + /** + * @test + */ + public function unlockWhenMemcacheKeyDoesNotExist() + { + try { + $this->mutex->unlock('some_key'); + $this->mutex->lock('some_key'); + $this->success(); + } catch (AlreadyLockedException $exception) { + $this->fail($exception->getMessage(). ' It should not exists because it was not present.'); + } + } + + /** + * @test + */ + public function lockWhenNoServers() + { + try { + $this->serverlessMutex->lock('some_key'); + $this->fail('DistributedMemcachedMutex::lock method should throw InvalidArgumentException.'); + } catch (InvalidArgumentException $exception) { + $this->success(); + } + } + + private function success() + { + $this->assertTrue(true); + } +} diff --git a/tests/MemcacheStub.php b/tests/MemcacheStub.php index ea9d11b..b4794fe 100644 --- a/tests/MemcacheStub.php +++ b/tests/MemcacheStub.php @@ -32,7 +32,7 @@ public function withServersAdded() /** * {@inheritdoc} */ - public function add($key, $var, $flag, $expire) + public function add($key, $var, $expire) { if (array_key_exists($key, $this->cache)) { return false; diff --git a/tests/MemcachedStub.php b/tests/MemcachedStub.php new file mode 100644 index 0000000..5554df5 --- /dev/null +++ b/tests/MemcachedStub.php @@ -0,0 +1,64 @@ + + */ +class MemcachedStub extends Memcached +{ + /** + * @var array + */ + private $cache = []; + /** + * @var bool + */ + private $areServersAdded = false; + + /** + * @return MemcachedStub + */ + public function withServersAdded() + { + $this->areServersAdded = true; + + return $this; + } + + /** + * {@inheritdoc} + */ + public function add($key, $var, $expire = NULL) + { + if (array_key_exists($key, $this->cache)) { + return false; + } + $this->cache[$key] = $var; + + return $this->cache; + } + + /** + * {@inheritdoc} + */ + public function delete($key, $timeout = 0) + { + if (!array_key_exists($key, $this->cache)) { + return false; + } + unset($this->cache[$key]); + + return true; + } + + /** + * {@inheritdoc} + */ + public function getversion() + { + return $this->areServersAdded; + } +}