2013-02-14 02:42:13 +04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Doctrine\Tests\Mocks;
|
|
|
|
|
|
|
|
|
|
|
|
use Doctrine\ORM\Cache\ConcurrentRegion;
|
|
|
|
use Doctrine\ORM\Cache\LockException;
|
|
|
|
use Doctrine\ORM\Cache\CacheEntry;
|
|
|
|
use Doctrine\ORM\Cache\CacheKey;
|
|
|
|
use Doctrine\ORM\Cache\Region;
|
|
|
|
use Doctrine\ORM\Cache\Lock;
|
|
|
|
|
2013-10-23 23:46:45 +04:00
|
|
|
/**
|
|
|
|
* Concurrent region mock
|
|
|
|
*
|
|
|
|
* Used to mock a ConcurrentRegion
|
|
|
|
*/
|
2013-02-14 02:42:13 +04:00
|
|
|
class ConcurrentRegionMock implements ConcurrentRegion
|
|
|
|
{
|
|
|
|
public $calls = array();
|
|
|
|
public $exceptions = array();
|
|
|
|
public $locks = array();
|
2013-10-23 23:46:45 +04:00
|
|
|
|
2013-02-14 02:42:13 +04:00
|
|
|
/**
|
|
|
|
* @var \Doctrine\ORM\Cache\Region
|
|
|
|
*/
|
|
|
|
private $region;
|
|
|
|
|
2013-10-23 23:46:45 +04:00
|
|
|
/**
|
|
|
|
* @param \Doctrine\ORM\Cache\Region $region
|
|
|
|
*/
|
2013-02-14 02:42:13 +04:00
|
|
|
public function __construct(Region $region)
|
|
|
|
{
|
|
|
|
$this->region = $region;
|
|
|
|
}
|
|
|
|
|
2013-10-23 23:46:45 +04:00
|
|
|
/**
|
|
|
|
* Dequeue an exception for a specific method invocation
|
|
|
|
*
|
|
|
|
* @param string $method
|
|
|
|
* @param mixed $default
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2013-02-14 02:42:13 +04:00
|
|
|
private function throwException($method)
|
|
|
|
{
|
|
|
|
if (isset($this->exceptions[$method]) && ! empty($this->exceptions[$method])) {
|
|
|
|
$exception = array_shift($this->exceptions[$method]);
|
|
|
|
|
2013-10-08 02:53:32 +04:00
|
|
|
if ($exception != null) {
|
2013-02-14 02:42:13 +04:00
|
|
|
throw $exception;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-23 23:46:45 +04:00
|
|
|
/**
|
|
|
|
* Queue an exception for the next method invocation
|
|
|
|
*
|
|
|
|
* @param string $method
|
|
|
|
* @param \Exception $e
|
|
|
|
*/
|
2013-02-14 02:42:13 +04:00
|
|
|
public function addException($method, \Exception $e)
|
|
|
|
{
|
|
|
|
$this->exceptions[$method][] = $e;
|
|
|
|
}
|
|
|
|
|
2013-10-23 23:46:45 +04:00
|
|
|
/**
|
|
|
|
* Locks a specific cache entry
|
|
|
|
*
|
|
|
|
* @param \Doctrine\ORM\Cache\CacheKey $key
|
|
|
|
* @param \Doctrine\ORM\Cache\Lock $lock
|
|
|
|
*/
|
2013-02-14 02:42:13 +04:00
|
|
|
public function setLock(CacheKey $key, Lock $lock)
|
|
|
|
{
|
|
|
|
$this->locks[$key->hash] = $lock;
|
|
|
|
}
|
|
|
|
|
2013-10-23 23:46:45 +04:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2013-02-14 02:42:13 +04:00
|
|
|
public function contains(CacheKey $key)
|
|
|
|
{
|
|
|
|
$this->calls[__FUNCTION__][] = array('key' => $key);
|
|
|
|
|
|
|
|
if (isset($this->locks[$key->hash])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->throwException(__FUNCTION__);
|
|
|
|
|
|
|
|
return $this->region->contains($key);
|
|
|
|
}
|
|
|
|
|
2013-10-23 23:46:45 +04:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2013-02-14 02:42:13 +04:00
|
|
|
public function evict(CacheKey $key)
|
|
|
|
{
|
|
|
|
$this->calls[__FUNCTION__][] = array('key' => $key);
|
|
|
|
|
|
|
|
$this->throwException(__FUNCTION__);
|
|
|
|
|
|
|
|
return $this->region->evict($key);
|
|
|
|
}
|
|
|
|
|
2013-10-23 23:46:45 +04:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2013-02-14 02:42:13 +04:00
|
|
|
public function evictAll()
|
|
|
|
{
|
|
|
|
$this->calls[__FUNCTION__][] = array();
|
|
|
|
|
|
|
|
$this->throwException(__FUNCTION__);
|
|
|
|
|
|
|
|
return $this->region->evictAll();
|
|
|
|
}
|
|
|
|
|
2013-10-23 23:46:45 +04:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2013-02-14 02:42:13 +04:00
|
|
|
public function get(CacheKey $key)
|
|
|
|
{
|
|
|
|
$this->calls[__FUNCTION__][] = array('key' => $key);
|
|
|
|
|
|
|
|
$this->throwException(__FUNCTION__);
|
|
|
|
|
|
|
|
if (isset($this->locks[$key->hash])) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->region->get($key);
|
|
|
|
}
|
|
|
|
|
2013-10-23 23:46:45 +04:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2013-02-14 02:42:13 +04:00
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
$this->calls[__FUNCTION__][] = array();
|
|
|
|
|
|
|
|
$this->throwException(__FUNCTION__);
|
|
|
|
|
|
|
|
return $this->region->getName();
|
|
|
|
}
|
|
|
|
|
2013-10-23 23:46:45 +04:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2013-02-14 02:42:13 +04:00
|
|
|
public function put(CacheKey $key, CacheEntry $entry, Lock $lock = null)
|
|
|
|
{
|
|
|
|
$this->calls[__FUNCTION__][] = array('key' => $key, 'entry' => $entry);
|
|
|
|
|
|
|
|
$this->throwException(__FUNCTION__);
|
|
|
|
|
|
|
|
if (isset($this->locks[$key->hash])) {
|
|
|
|
|
|
|
|
if ($lock !== null && $this->locks[$key->hash]->value === $lock->value) {
|
|
|
|
return $this->region->put($key, $entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->region->put($key, $entry);
|
|
|
|
}
|
|
|
|
|
2013-10-23 23:46:45 +04:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2013-02-14 02:42:13 +04:00
|
|
|
public function lock(CacheKey $key)
|
|
|
|
{
|
|
|
|
$this->calls[__FUNCTION__][] = array('key' => $key);
|
|
|
|
|
|
|
|
$this->throwException(__FUNCTION__);
|
|
|
|
|
|
|
|
if (isset($this->locks[$key->hash])) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->locks[$key->hash] = Lock::createLockRead();
|
|
|
|
}
|
|
|
|
|
2013-10-23 23:46:45 +04:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2013-02-14 02:42:13 +04:00
|
|
|
public function unlock(CacheKey $key, Lock $lock)
|
|
|
|
{
|
|
|
|
$this->calls[__FUNCTION__][] = array('key' => $key, 'lock' => $lock);
|
|
|
|
|
|
|
|
$this->throwException(__FUNCTION__);
|
|
|
|
|
|
|
|
if ( ! isset($this->locks[$key->hash])) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->locks[$key->hash]->value !== $lock->value) {
|
|
|
|
throw LockException::unexpectedLockValue($lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
unset($this->locks[$key->hash]);
|
|
|
|
}
|
|
|
|
}
|