2013-02-13 20:42:13 -02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Doctrine\Tests\ORM\Cache;
|
|
|
|
|
2016-12-08 18:01:04 +01:00
|
|
|
use Doctrine\ORM\Cache;
|
|
|
|
use Doctrine\Tests\Models\CMS\CmsUser;
|
2013-02-13 20:42:13 -02:00
|
|
|
use Doctrine\Tests\OrmTestCase;
|
|
|
|
use Doctrine\ORM\Cache\DefaultCache;
|
|
|
|
use Doctrine\Tests\Models\Cache\State;
|
|
|
|
use Doctrine\Tests\Models\Cache\Country;
|
|
|
|
use Doctrine\ORM\Cache\EntityCacheKey;
|
|
|
|
use Doctrine\ORM\Cache\EntityCacheEntry;
|
|
|
|
use Doctrine\ORM\Cache\CollectionCacheKey;
|
|
|
|
use Doctrine\ORM\Cache\CollectionCacheEntry;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group DDC-2183
|
|
|
|
*/
|
|
|
|
class DefaultCacheTest extends OrmTestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var \Doctrine\ORM\Cache
|
|
|
|
*/
|
|
|
|
private $cache;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Doctrine\ORM\EntityManagerInterface
|
|
|
|
*/
|
|
|
|
private $em;
|
|
|
|
|
|
|
|
protected function setUp()
|
|
|
|
{
|
|
|
|
parent::enableSecondLevelCache();
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->em = $this->_getTestEntityManager();
|
|
|
|
$this->cache = new DefaultCache($this->em);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $className
|
|
|
|
* @param array $identifier
|
|
|
|
* @param array $data
|
|
|
|
*/
|
|
|
|
private function putEntityCacheEntry($className, array $identifier, array $data)
|
|
|
|
{
|
|
|
|
$metadata = $this->em->getClassMetadata($className);
|
|
|
|
$cacheKey = new EntityCacheKey($metadata->name, $identifier);
|
|
|
|
$cacheEntry = new EntityCacheEntry($metadata->name, $data);
|
|
|
|
$persister = $this->em->getUnitOfWork()->getEntityPersister($metadata->rootEntityName);
|
|
|
|
|
|
|
|
$persister->getCacheRegion()->put($cacheKey, $cacheEntry);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $className
|
|
|
|
* @param string $association
|
|
|
|
* @param array $ownerIdentifier
|
|
|
|
* @param array $data
|
|
|
|
*/
|
|
|
|
private function putCollectionCacheEntry($className, $association, array $ownerIdentifier, array $data)
|
|
|
|
{
|
|
|
|
$metadata = $this->em->getClassMetadata($className);
|
|
|
|
$cacheKey = new CollectionCacheKey($metadata->name, $association, $ownerIdentifier);
|
|
|
|
$cacheEntry = new CollectionCacheEntry($data);
|
|
|
|
$persister = $this->em->getUnitOfWork()->getCollectionPersister($metadata->getAssociationMapping($association));
|
|
|
|
|
|
|
|
$persister->getCacheRegion()->put($cacheKey, $cacheEntry);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testImplementsCache()
|
|
|
|
{
|
2016-12-08 18:01:04 +01:00
|
|
|
$this->assertInstanceOf(Cache::class, $this->cache);
|
2013-02-13 20:42:13 -02:00
|
|
|
}
|
2016-12-07 23:33:41 +01:00
|
|
|
|
2013-02-13 20:42:13 -02:00
|
|
|
public function testGetEntityCacheRegionAccess()
|
|
|
|
{
|
2016-12-08 18:01:04 +01:00
|
|
|
$this->assertInstanceOf(Cache\Region::class, $this->cache->getEntityCacheRegion(State::class));
|
|
|
|
$this->assertNull($this->cache->getEntityCacheRegion(CmsUser::class));
|
2013-02-13 20:42:13 -02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetCollectionCacheRegionAccess()
|
|
|
|
{
|
2016-12-08 18:01:04 +01:00
|
|
|
$this->assertInstanceOf(Cache\Region::class, $this->cache->getCollectionCacheRegion(State::class, 'cities'));
|
|
|
|
$this->assertNull($this->cache->getCollectionCacheRegion(CmsUser::class, 'phonenumbers'));
|
2013-02-13 20:42:13 -02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testContainsEntity()
|
|
|
|
{
|
2016-12-07 23:33:41 +01:00
|
|
|
$identifier = ['id'=>1];
|
2016-12-08 18:01:04 +01:00
|
|
|
$className = Country::class;
|
2016-12-07 23:33:41 +01:00
|
|
|
$cacheEntry = array_merge($identifier, ['name' => 'Brazil']);
|
2013-02-13 20:42:13 -02:00
|
|
|
|
2016-12-08 18:01:04 +01:00
|
|
|
$this->assertFalse($this->cache->containsEntity(Country::class, 1));
|
2013-02-13 20:42:13 -02:00
|
|
|
|
|
|
|
$this->putEntityCacheEntry($className, $identifier, $cacheEntry);
|
|
|
|
|
2016-12-08 18:01:04 +01:00
|
|
|
$this->assertTrue($this->cache->containsEntity(Country::class, 1));
|
|
|
|
$this->assertFalse($this->cache->containsEntity(CmsUser::class, 1));
|
2013-02-13 20:42:13 -02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testEvictEntity()
|
|
|
|
{
|
2016-12-07 23:33:41 +01:00
|
|
|
$identifier = ['id'=>1];
|
2016-12-08 18:01:04 +01:00
|
|
|
$className = Country::class;
|
2016-12-07 23:33:41 +01:00
|
|
|
$cacheEntry = array_merge($identifier, ['name' => 'Brazil']);
|
2013-02-13 20:42:13 -02:00
|
|
|
|
|
|
|
$this->putEntityCacheEntry($className, $identifier, $cacheEntry);
|
|
|
|
|
2016-12-08 18:01:04 +01:00
|
|
|
$this->assertTrue($this->cache->containsEntity(Country::class, 1));
|
2013-02-13 20:42:13 -02:00
|
|
|
|
2016-12-08 18:01:04 +01:00
|
|
|
$this->cache->evictEntity(Country::class, 1);
|
|
|
|
$this->cache->evictEntity(CmsUser::class, 1);
|
2013-02-13 20:42:13 -02:00
|
|
|
|
2016-12-08 18:01:04 +01:00
|
|
|
$this->assertFalse($this->cache->containsEntity(Country::class, 1));
|
2013-02-13 20:42:13 -02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testEvictEntityRegion()
|
|
|
|
{
|
2016-12-07 23:33:41 +01:00
|
|
|
$identifier = ['id'=>1];
|
2016-12-08 18:01:04 +01:00
|
|
|
$className = Country::class;
|
2016-12-07 23:33:41 +01:00
|
|
|
$cacheEntry = array_merge($identifier, ['name' => 'Brazil']);
|
2013-02-13 20:42:13 -02:00
|
|
|
|
|
|
|
$this->putEntityCacheEntry($className, $identifier, $cacheEntry);
|
|
|
|
|
2016-12-08 18:01:04 +01:00
|
|
|
$this->assertTrue($this->cache->containsEntity(Country::class, 1));
|
2013-02-13 20:42:13 -02:00
|
|
|
|
2016-12-08 18:01:04 +01:00
|
|
|
$this->cache->evictEntityRegion(Country::class);
|
|
|
|
$this->cache->evictEntityRegion(CmsUser::class);
|
2013-02-13 20:42:13 -02:00
|
|
|
|
2016-12-08 18:01:04 +01:00
|
|
|
$this->assertFalse($this->cache->containsEntity(Country::class, 1));
|
2013-02-13 20:42:13 -02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testEvictEntityRegions()
|
|
|
|
{
|
2016-12-07 23:33:41 +01:00
|
|
|
$identifier = ['id'=>1];
|
2016-12-08 18:01:04 +01:00
|
|
|
$className = Country::class;
|
2016-12-07 23:33:41 +01:00
|
|
|
$cacheEntry = array_merge($identifier, ['name' => 'Brazil']);
|
2013-02-13 20:42:13 -02:00
|
|
|
|
|
|
|
$this->putEntityCacheEntry($className, $identifier, $cacheEntry);
|
|
|
|
|
2016-12-08 18:01:04 +01:00
|
|
|
$this->assertTrue($this->cache->containsEntity(Country::class, 1));
|
2013-02-13 20:42:13 -02:00
|
|
|
|
|
|
|
$this->cache->evictEntityRegions();
|
|
|
|
|
2016-12-08 18:01:04 +01:00
|
|
|
$this->assertFalse($this->cache->containsEntity(Country::class, 1));
|
2013-02-13 20:42:13 -02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testContainsCollection()
|
|
|
|
{
|
2016-12-07 23:33:41 +01:00
|
|
|
$ownerId = ['id'=>1];
|
2016-12-08 18:01:04 +01:00
|
|
|
$className = State::class;
|
2013-02-13 20:42:13 -02:00
|
|
|
$association = 'cities';
|
2016-12-07 23:33:41 +01:00
|
|
|
$cacheEntry = [
|
|
|
|
['id' => 11],
|
|
|
|
['id' => 12],
|
|
|
|
];
|
2013-02-13 20:42:13 -02:00
|
|
|
|
2016-12-08 18:01:04 +01:00
|
|
|
$this->assertFalse($this->cache->containsCollection(State::class, $association, 1));
|
2013-02-13 20:42:13 -02:00
|
|
|
|
|
|
|
$this->putCollectionCacheEntry($className, $association, $ownerId, $cacheEntry);
|
|
|
|
|
2016-12-08 18:01:04 +01:00
|
|
|
$this->assertTrue($this->cache->containsCollection(State::class, $association, 1));
|
|
|
|
$this->assertFalse($this->cache->containsCollection(CmsUser::class, 'phonenumbers', 1));
|
2013-02-13 20:42:13 -02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testEvictCollection()
|
|
|
|
{
|
2016-12-07 23:33:41 +01:00
|
|
|
$ownerId = ['id'=>1];
|
2016-12-08 18:01:04 +01:00
|
|
|
$className = State::class;
|
2013-02-13 20:42:13 -02:00
|
|
|
$association = 'cities';
|
2016-12-07 23:33:41 +01:00
|
|
|
$cacheEntry = [
|
|
|
|
['id' => 11],
|
|
|
|
['id' => 12],
|
|
|
|
];
|
2013-02-13 20:42:13 -02:00
|
|
|
|
|
|
|
$this->putCollectionCacheEntry($className, $association, $ownerId, $cacheEntry);
|
|
|
|
|
2016-12-08 18:01:04 +01:00
|
|
|
$this->assertTrue($this->cache->containsCollection(State::class, $association, 1));
|
2013-02-13 20:42:13 -02:00
|
|
|
|
|
|
|
$this->cache->evictCollection($className, $association, $ownerId);
|
2016-12-08 18:01:04 +01:00
|
|
|
$this->cache->evictCollection(CmsUser::class, 'phonenumbers', 1);
|
2013-02-13 20:42:13 -02:00
|
|
|
|
2016-12-08 18:01:04 +01:00
|
|
|
$this->assertFalse($this->cache->containsCollection(State::class, $association, 1));
|
2013-02-13 20:42:13 -02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testEvictCollectionRegion()
|
|
|
|
{
|
2016-12-07 23:33:41 +01:00
|
|
|
$ownerId = ['id'=>1];
|
2016-12-08 18:01:04 +01:00
|
|
|
$className = State::class;
|
2013-02-13 20:42:13 -02:00
|
|
|
$association = 'cities';
|
2016-12-07 23:33:41 +01:00
|
|
|
$cacheEntry = [
|
|
|
|
['id' => 11],
|
|
|
|
['id' => 12],
|
|
|
|
];
|
2013-02-13 20:42:13 -02:00
|
|
|
|
|
|
|
$this->putCollectionCacheEntry($className, $association, $ownerId, $cacheEntry);
|
|
|
|
|
2016-12-08 18:01:04 +01:00
|
|
|
$this->assertTrue($this->cache->containsCollection(State::class, $association, 1));
|
2013-02-13 20:42:13 -02:00
|
|
|
|
|
|
|
$this->cache->evictCollectionRegion($className, $association);
|
2016-12-08 18:01:04 +01:00
|
|
|
$this->cache->evictCollectionRegion(CmsUser::class, 'phonenumbers');
|
2013-02-13 20:42:13 -02:00
|
|
|
|
2016-12-08 18:01:04 +01:00
|
|
|
$this->assertFalse($this->cache->containsCollection(State::class, $association, 1));
|
2013-02-13 20:42:13 -02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testEvictCollectionRegions()
|
|
|
|
{
|
2016-12-07 23:33:41 +01:00
|
|
|
$ownerId = ['id'=>1];
|
2016-12-08 18:01:04 +01:00
|
|
|
$className = State::class;
|
2013-02-13 20:42:13 -02:00
|
|
|
$association = 'cities';
|
2016-12-07 23:33:41 +01:00
|
|
|
$cacheEntry = [
|
|
|
|
['id' => 11],
|
|
|
|
['id' => 12],
|
|
|
|
];
|
2013-02-13 20:42:13 -02:00
|
|
|
|
|
|
|
$this->putCollectionCacheEntry($className, $association, $ownerId, $cacheEntry);
|
|
|
|
|
2016-12-08 18:01:04 +01:00
|
|
|
$this->assertTrue($this->cache->containsCollection(State::class, $association, 1));
|
2013-02-13 20:42:13 -02:00
|
|
|
|
|
|
|
$this->cache->evictCollectionRegions();
|
|
|
|
|
2016-12-08 18:01:04 +01:00
|
|
|
$this->assertFalse($this->cache->containsCollection(State::class, $association, 1));
|
2013-02-13 20:42:13 -02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testQueryCache()
|
|
|
|
{
|
|
|
|
$this->assertFalse($this->cache->containsQuery('foo'));
|
|
|
|
|
|
|
|
$defaultQueryCache = $this->cache->getQueryCache();
|
|
|
|
$fooQueryCache = $this->cache->getQueryCache('foo');
|
|
|
|
|
2016-12-08 18:01:04 +01:00
|
|
|
$this->assertInstanceOf(Cache\QueryCache::class, $defaultQueryCache);
|
|
|
|
$this->assertInstanceOf(Cache\QueryCache::class, $fooQueryCache);
|
2013-02-13 20:42:13 -02:00
|
|
|
$this->assertSame($defaultQueryCache, $this->cache->getQueryCache());
|
|
|
|
$this->assertSame($fooQueryCache, $this->cache->getQueryCache('foo'));
|
|
|
|
|
|
|
|
$this->cache->evictQueryRegion();
|
|
|
|
$this->cache->evictQueryRegion('foo');
|
|
|
|
$this->cache->evictQueryRegions();
|
|
|
|
|
|
|
|
$this->assertTrue($this->cache->containsQuery('foo'));
|
|
|
|
|
|
|
|
$this->assertSame($defaultQueryCache, $this->cache->getQueryCache());
|
|
|
|
$this->assertSame($fooQueryCache, $this->cache->getQueryCache('foo'));
|
|
|
|
}
|
|
|
|
|
2013-12-16 15:55:54 -05:00
|
|
|
public function testToIdentifierArrayShouldLookupForEntityIdentifier()
|
2013-02-13 20:42:13 -02:00
|
|
|
{
|
|
|
|
$identifier = 123;
|
|
|
|
$entity = new Country('Foo');
|
2016-12-08 18:01:04 +01:00
|
|
|
$metadata = $this->em->getClassMetadata(Country::class);
|
2013-02-13 20:42:13 -02:00
|
|
|
$method = new \ReflectionMethod($this->cache, 'toIdentifierArray');
|
|
|
|
$property = new \ReflectionProperty($entity, 'id');
|
|
|
|
|
|
|
|
$property->setAccessible(true);
|
|
|
|
$method->setAccessible(true);
|
|
|
|
$property->setValue($entity, $identifier);
|
|
|
|
|
2016-12-07 23:33:41 +01:00
|
|
|
$this->assertEquals(['id'=>$identifier], $method->invoke($this->cache, $metadata, $identifier));
|
2013-02-13 20:42:13 -02:00
|
|
|
}
|
|
|
|
|
2016-12-07 23:33:41 +01:00
|
|
|
}
|