Skip to content

Commit

Permalink
Improve code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
fabios committed Oct 1, 2013
1 parent b8f368e commit 59ff676
Show file tree
Hide file tree
Showing 12 changed files with 953 additions and 74 deletions.
4 changes: 0 additions & 4 deletions lib/Doctrine/ORM/Cache/DefaultEntityEntryStructure.php
Expand Up @@ -128,10 +128,6 @@ public function loadCacheEntry(ClassMetadata $metadata, EntityCacheKey $key, Ent
continue;
}

if ( ! $assoc['type'] & ClassMetadata::TO_ONE) {
continue;
}

$assocId = $data[$name];
$assocPersister = $this->uow->getEntityPersister($assoc['targetEntity']);
$assocRegion = $assocPersister->getCacheRegionAcess()->getRegion();
Expand Down
12 changes: 3 additions & 9 deletions lib/Doctrine/ORM/Cache/DefaultQueryCache.php
Expand Up @@ -57,11 +57,6 @@ class DefaultQueryCache implements QueryCache
*/
private $region;

/**
* @var \Doctrine\ORM\Cache\Logging\CacheLogger
*/
private $logger;

/**
* @var \Doctrine\ORM\Cache\QueryCacheValidator
*/
Expand All @@ -81,7 +76,6 @@ public function __construct(EntityManagerInterface $em, Region $region)
$this->em = $em;
$this->region = $region;
$this->uow = $em->getUnitOfWork();
$this->logger = $em->getConfiguration()->getSecondLevelCacheLogger();
$this->validator = $em->getConfiguration()->getSecondLevelCacheQueryValidator();
}

Expand Down Expand Up @@ -158,7 +152,7 @@ public function get(QueryCacheKey $key, ResultSetMapping $rsm)
return null;
}

$element = $this->uow->createEntity($assocEntry->class, $assocEntry->data, self::$hints);;
$element = $this->uow->createEntity($assocEntry->class, $assocEntry->data, self::$hints);

$collection->hydrateSet($assocIndex, $element);
}
Expand Down Expand Up @@ -240,7 +234,7 @@ public function put(QueryCacheKey $key, ResultSetMapping $rsm, array $result)

if (($key->cacheMode & Cache::MODE_REFRESH) || ! $assocRegion->contains($entityKey = new EntityCacheKey($assocMetadata->rootEntityName, $assocIdentifier))) {

// Cancel put result if entity put fail
// Cancel put result if association entity put fail
if ( ! $assocPersister->putEntityCache($assocValue, $entityKey)) {
return false;
}
Expand All @@ -256,7 +250,7 @@ public function put(QueryCacheKey $key, ResultSetMapping $rsm, array $result)
}

// Handle *-to-many associations
if (is_array($assocValue) && ! $assocValue instanceof Collection) {
if ( ! is_array($assocValue) && ! $assocValue instanceof Collection) {
continue;
}

Expand Down
77 changes: 77 additions & 0 deletions tests/Doctrine/Tests/Mocks/CacheRegionMock.php
@@ -0,0 +1,77 @@
<?php

namespace Doctrine\Tests\Mocks;

use Doctrine\ORM\Cache\CacheEntry;
use Doctrine\ORM\Cache\CacheKey;
use Doctrine\ORM\Cache\Lock;
use Doctrine\ORM\Cache\Region;

class CacheRegionMock implements Region
{
public $calls = array();
public $returns = array();
public $name;

public function addReturn($method, $value)
{
$this->returns[$method][] = $value;
}

public function getReturn($method, $datault)
{
if (isset($this->returns[$method]) && ! empty($this->returns[$method])) {
return array_shift($this->returns[$method]);
}

return $datault;
}

public function getName()
{
$this->calls[__FUNCTION__][] = array();

return $this->name;
}

public function contains(CacheKey $key)
{
$this->calls[__FUNCTION__][] = array('key' => $key);

return $this->getReturn(__FUNCTION__, false);
}

public function evict(CacheKey $key)
{
$this->calls[__FUNCTION__][] = array('key' => $key);

return $this->getReturn(__FUNCTION__, true);
}

public function evictAll()
{
$this->calls[__FUNCTION__][] = array();

return $this->getReturn(__FUNCTION__, true);
}

public function get(CacheKey $key)
{
$this->calls[__FUNCTION__][] = array('key' => $key);

return $this->getReturn(__FUNCTION__, null);
}

public function put(CacheKey $key, CacheEntry $entry, Lock $lock = null)
{
$this->calls[__FUNCTION__][] = array('key' => $key, 'entry' => $entry);

return $this->getReturn(__FUNCTION__, true);
}

public function clear()
{
$this->calls = array();
$this->returns = array();
}
}
2 changes: 1 addition & 1 deletion tests/Doctrine/Tests/Models/Cache/City.php
Expand Up @@ -44,7 +44,7 @@ class City
*/
public $attractions;

public function __construct($name, State $state)
public function __construct($name, State $state = null)
{
$this->name = $name;
$this->state = $state;
Expand Down
65 changes: 65 additions & 0 deletions tests/Doctrine/Tests/Models/Cache/Flight.php
@@ -0,0 +1,65 @@
<?php

namespace Doctrine\Tests\Models\Cache;

/**
* @Entity
* @Table("cache_flight")
* @Cache("NONSTRICT_READ_WRITE")
*/
class Flight
{
const CLASSNAME = __CLASS__;

/**
* @Id
* @Cache
* @ManyToOne(targetEntity="City")
* @JoinColumn(name="leaving_from_city_id", referencedColumnName="id")
*/
protected $leavingFrom;

/**
* @Id
* @Cache
* @ManyToOne(targetEntity="City")
* @JoinColumn(name="going_to_city_id", referencedColumnName="id")
*/
protected $goingTo;

/**
* @Column(type="date")
*/
protected $departure;

/**
* @param \Doctrine\Tests\Models\Cache\City $leavingFrom
* @param \Doctrine\Tests\Models\Cache\City $goingTo
*/
public function __construct(City $leavingFrom, City $goingTo)
{
$this->goingTo = $goingTo;
$this->leavingFrom = $leavingFrom;
$this->departure = new \DateTime();
}

public function getLeavingFrom()
{
return $this->leavingFrom;
}

public function getGoingTo()
{
return $this->goingTo;
}

public function getDeparture()
{
return $this->departure;
}

public function setDeparture($departure)
{
$this->departure = $departure;
}
}
2 changes: 1 addition & 1 deletion tests/Doctrine/Tests/Models/Cache/State.php
Expand Up @@ -38,7 +38,7 @@ class State
*/
protected $cities;

public function __construct($name, Country $country)
public function __construct($name, Country $country = null)
{
$this->name = $name;
$this->country = $country;
Expand Down
34 changes: 32 additions & 2 deletions tests/Doctrine/Tests/ORM/Cache/DefaultCacheTest.php
Expand Up @@ -26,6 +26,8 @@ class DefaultCacheTest extends OrmTestCase
*/
private $em;

const NON_CACHEABLE_ENTITY = 'Doctrine\Tests\Models\CMS\CmsUser';

protected function setUp()
{
parent::enableSecondLevelCache();
Expand Down Expand Up @@ -74,13 +76,13 @@ public function testImplementsCache()
public function testGetEntityCacheRegionAccess()
{
$this->assertInstanceOf('Doctrine\ORM\Cache\RegionAccess', $this->cache->getEntityCacheRegionAccess(State::CLASSNAME));
$this->assertNull($this->cache->getEntityCacheRegionAccess('Doctrine\Tests\Models\CMS\CmsUser'));
$this->assertNull($this->cache->getEntityCacheRegionAccess(self::NON_CACHEABLE_ENTITY));
}

public function testGetCollectionCacheRegionAccess()
{
$this->assertInstanceOf('Doctrine\ORM\Cache\RegionAccess', $this->cache->getCollectionCacheRegionAccess(State::CLASSNAME, 'cities'));
$this->assertNull($this->cache->getCollectionCacheRegionAccess('Doctrine\Tests\Models\CMS\CmsUser', 'phonenumbers'));
$this->assertNull($this->cache->getCollectionCacheRegionAccess(self::NON_CACHEABLE_ENTITY, 'phonenumbers'));
}

public function testContainsEntity()
Expand All @@ -94,6 +96,7 @@ public function testContainsEntity()
$this->putEntityCacheEntry($className, $identifier, $cacheEntry);

$this->assertTrue($this->cache->containsEntity(Country::CLASSNAME, 1));
$this->assertFalse($this->cache->containsEntity(self::NON_CACHEABLE_ENTITY, 1));
}

public function testEvictEntity()
Expand All @@ -107,6 +110,7 @@ public function testEvictEntity()
$this->assertTrue($this->cache->containsEntity(Country::CLASSNAME, 1));

$this->cache->evictEntity(Country::CLASSNAME, 1);
$this->cache->evictEntity(self::NON_CACHEABLE_ENTITY, 1);

$this->assertFalse($this->cache->containsEntity(Country::CLASSNAME, 1));
}
Expand All @@ -122,6 +126,7 @@ public function testEvictEntityRegion()
$this->assertTrue($this->cache->containsEntity(Country::CLASSNAME, 1));

$this->cache->evictEntityRegion(Country::CLASSNAME);
$this->cache->evictEntityRegion(self::NON_CACHEABLE_ENTITY);

$this->assertFalse($this->cache->containsEntity(Country::CLASSNAME, 1));
}
Expand Down Expand Up @@ -156,6 +161,7 @@ public function testContainsCollection()
$this->putCollectionCacheEntry($className, $association, $ownerId, $cacheEntry);

$this->assertTrue($this->cache->containsCollection(State::CLASSNAME, $association, 1));
$this->assertFalse($this->cache->containsCollection(self::NON_CACHEABLE_ENTITY, 'phonenumbers', 1));
}

public function testEvictCollection()
Expand All @@ -173,6 +179,7 @@ public function testEvictCollection()
$this->assertTrue($this->cache->containsCollection(State::CLASSNAME, $association, 1));

$this->cache->evictCollection($className, $association, $ownerId);
$this->cache->evictCollection(self::NON_CACHEABLE_ENTITY, 'phonenumbers', 1);

$this->assertFalse($this->cache->containsCollection(State::CLASSNAME, $association, 1));
}
Expand All @@ -192,6 +199,7 @@ public function testEvictCollectionRegion()
$this->assertTrue($this->cache->containsCollection(State::CLASSNAME, $association, 1));

$this->cache->evictCollectionRegion($className, $association);
$this->cache->evictCollectionRegion(self::NON_CACHEABLE_ENTITY, 'phonenumbers');

$this->assertFalse($this->cache->containsCollection(State::CLASSNAME, $association, 1));
}
Expand All @@ -215,6 +223,28 @@ public function testEvictCollectionRegions()
$this->assertFalse($this->cache->containsCollection(State::CLASSNAME, $association, 1));
}

public function testQueryCache()
{
$this->assertFalse($this->cache->containsQuery('foo'));

$defaultQueryCache = $this->cache->getQueryCache();
$fooQueryCache = $this->cache->getQueryCache('foo');

$this->assertInstanceOf('Doctrine\ORM\Cache\QueryCache', $defaultQueryCache);
$this->assertInstanceOf('Doctrine\ORM\Cache\QueryCache', $fooQueryCache);
$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'));
}

public function testToIdentifierArrayShoudLookupForEntityIdentifier()
{
$identifier = 123;
Expand Down

0 comments on commit 59ff676

Please sign in to comment.