2013-02-13 20:42:13 -02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Doctrine\Tests\ORM\Cache;
|
|
|
|
|
2015-01-15 21:01:02 +01:00
|
|
|
use Doctrine\Common\Cache\ArrayCache;
|
2016-06-18 13:01:59 +02:00
|
|
|
use Doctrine\Common\Cache\Cache;
|
2017-05-31 10:58:37 +02:00
|
|
|
use Doctrine\Common\Cache\CacheProvider;
|
2015-04-13 23:31:19 +02:00
|
|
|
use Doctrine\ORM\Cache\CollectionCacheEntry;
|
2013-02-13 20:42:13 -02:00
|
|
|
use Doctrine\ORM\Cache\Region\DefaultRegion;
|
|
|
|
use Doctrine\Tests\Mocks\CacheEntryMock;
|
|
|
|
use Doctrine\Tests\Mocks\CacheKeyMock;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group DDC-2183
|
|
|
|
*/
|
|
|
|
class DefaultRegionTest extends AbstractRegionTest
|
|
|
|
{
|
|
|
|
protected function createRegion()
|
|
|
|
{
|
|
|
|
return new DefaultRegion('default.region.test', $this->cache);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetters()
|
|
|
|
{
|
|
|
|
$this->assertEquals('default.region.test', $this->region->getName());
|
|
|
|
$this->assertSame($this->cache, $this->region->getCache());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSharedRegion()
|
|
|
|
{
|
2017-05-31 10:58:37 +02:00
|
|
|
$cache = new SharedArrayCache();
|
2013-02-13 20:42:13 -02:00
|
|
|
$key = new CacheKeyMock('key');
|
2016-12-07 23:33:41 +01:00
|
|
|
$entry = new CacheEntryMock(['value' => 'foo']);
|
2017-05-31 10:58:37 +02:00
|
|
|
$region1 = new DefaultRegion('region1', $cache->createChild());
|
|
|
|
$region2 = new DefaultRegion('region2', $cache->createChild());
|
2013-02-13 20:42:13 -02:00
|
|
|
|
|
|
|
$this->assertFalse($region1->contains($key));
|
|
|
|
$this->assertFalse($region2->contains($key));
|
|
|
|
|
|
|
|
$region1->put($key, $entry);
|
|
|
|
$region2->put($key, $entry);
|
|
|
|
|
|
|
|
$this->assertTrue($region1->contains($key));
|
|
|
|
$this->assertTrue($region2->contains($key));
|
|
|
|
|
|
|
|
$region1->evictAll();
|
|
|
|
|
|
|
|
$this->assertFalse($region1->contains($key));
|
|
|
|
$this->assertTrue($region2->contains($key));
|
|
|
|
}
|
2015-01-15 21:01:02 +01:00
|
|
|
|
|
|
|
public function testDoesNotModifyCacheNamespace()
|
|
|
|
{
|
|
|
|
$cache = new ArrayCache();
|
|
|
|
|
|
|
|
$cache->setNamespace('foo');
|
|
|
|
|
|
|
|
new DefaultRegion('bar', $cache);
|
|
|
|
new DefaultRegion('baz', $cache);
|
|
|
|
|
|
|
|
$this->assertSame('foo', $cache->getNamespace());
|
|
|
|
}
|
2015-01-17 23:30:20 +01:00
|
|
|
|
|
|
|
public function testEvictAllWithGenericCacheThrowsUnsupportedException()
|
|
|
|
{
|
|
|
|
/* @var $cache \Doctrine\Common\Cache\Cache */
|
2016-06-18 13:01:59 +02:00
|
|
|
$cache = $this->createMock(Cache::class);
|
2015-01-17 23:30:20 +01:00
|
|
|
|
|
|
|
$region = new DefaultRegion('foo', $cache);
|
|
|
|
|
2016-06-18 13:01:59 +02:00
|
|
|
$this->expectException(\BadMethodCallException::class);
|
2015-01-17 23:30:20 +01:00
|
|
|
|
|
|
|
$region->evictAll();
|
|
|
|
}
|
2015-04-13 23:31:19 +02:00
|
|
|
|
|
|
|
public function testGetMulti()
|
|
|
|
{
|
|
|
|
$key1 = new CacheKeyMock('key.1');
|
2016-12-07 23:33:41 +01:00
|
|
|
$value1 = new CacheEntryMock(['id' => 1, 'name' => 'bar']);
|
2015-04-13 23:31:19 +02:00
|
|
|
|
|
|
|
$key2 = new CacheKeyMock('key.2');
|
2016-12-07 23:33:41 +01:00
|
|
|
$value2 = new CacheEntryMock(['id' => 2, 'name' => 'bar']);
|
2015-04-13 23:31:19 +02:00
|
|
|
|
|
|
|
$this->assertFalse($this->region->contains($key1));
|
|
|
|
$this->assertFalse($this->region->contains($key2));
|
|
|
|
|
|
|
|
$this->region->put($key1, $value1);
|
|
|
|
$this->region->put($key2, $value2);
|
|
|
|
|
|
|
|
$this->assertTrue($this->region->contains($key1));
|
|
|
|
$this->assertTrue($this->region->contains($key2));
|
|
|
|
|
2016-12-07 23:33:41 +01:00
|
|
|
$actual = $this->region->getMultiple(new CollectionCacheEntry([$key1, $key2]));
|
2015-04-13 23:31:19 +02:00
|
|
|
|
|
|
|
$this->assertEquals($value1, $actual[0]);
|
|
|
|
$this->assertEquals($value2, $actual[1]);
|
|
|
|
}
|
2016-12-07 23:33:41 +01:00
|
|
|
}
|
2017-05-31 10:58:37 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Cache provider that offers child cache items (sharing the same array)
|
|
|
|
*
|
|
|
|
* Declared as a different class for readability purposes and kept in this file
|
|
|
|
* to keep its monstrosity contained.
|
|
|
|
*
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
final class SharedArrayCache extends ArrayCache
|
|
|
|
{
|
|
|
|
public function createChild(): Cache
|
|
|
|
{
|
|
|
|
return new class ($this) extends CacheProvider
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var ArrayCache
|
|
|
|
*/
|
|
|
|
private $parent;
|
|
|
|
|
|
|
|
public function __construct(ArrayCache $parent)
|
|
|
|
{
|
|
|
|
$this->parent = $parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function doFetch($id)
|
|
|
|
{
|
|
|
|
return $this->parent->doFetch($id);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function doContains($id)
|
|
|
|
{
|
|
|
|
return $this->parent->doContains($id);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function doSave($id, $data, $lifeTime = 0)
|
|
|
|
{
|
|
|
|
return $this->parent->doSave($id, $data, $lifeTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function doDelete($id)
|
|
|
|
{
|
|
|
|
return $this->parent->doDelete($id);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function doFlush()
|
|
|
|
{
|
|
|
|
return $this->parent->doFlush();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function doGetStats()
|
|
|
|
{
|
|
|
|
return $this->parent->doGetStats();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|