Skip to content

Commit

Permalink
Merge branch 'fix/#717-do-not-fail-if-cache-instance-is-not-classmeta…
Browse files Browse the repository at this point in the history
…data'

Close #717
  • Loading branch information
Ocramius committed Nov 30, 2016
2 parents f74f44d + 0b52f6f commit 223a292
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public function getMetadataFor($className)

try {
if ($this->cacheDriver) {
if (($cached = $this->cacheDriver->fetch($realClassName . $this->cacheSalt)) !== false) {
if (($cached = $this->cacheDriver->fetch($realClassName . $this->cacheSalt)) instanceof ClassMetadata) {
$this->loadedMetadata[$realClassName] = $cached;

$this->wakeupReflection($cached, $this->getReflectionService());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Doctrine\Tests\Common\Persistence\Mapping;

use Doctrine\Common\Cache\Cache;
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver;
use Doctrine\Common\Persistence\Mapping\MappingException;
use Doctrine\Tests\DoctrineTestCase;
Expand All @@ -10,6 +11,9 @@
use Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory;
use Doctrine\Common\Cache\ArrayCache;

/**
* @covers \Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory
*/
class ClassMetadataFactoryTest extends DoctrineTestCase
{
/**
Expand Down Expand Up @@ -130,6 +134,30 @@ public function testWillFailOnFallbackFailureWithNotLoadedMetadata()

$this->cmf->getMetadataFor('Foo');
}

/**
* @group 717
*/
public function testWillIgnoreCacheEntriesThatAreNotMetadataInstances()
{
/* @var $cacheDriver Cache|\PHPUnit_Framework_MockObject_MockObject */
$cacheDriver = $this->createMock(Cache::class);

$this->cmf->setCacheDriver($cacheDriver);

$cacheDriver->expects(self::once())->method('fetch')->with('Foo$CLASSMETADATA')->willReturn(new \stdClass());

/* @var $metadata ClassMetadata */
$metadata = $this->createMock(ClassMetadata::class);

$fallbackCallback = $this->getMockBuilder(\stdClass::class)->setMethods(['__invoke'])->getMock();

$fallbackCallback->expects(self::any())->method('__invoke')->willReturn($metadata);

$this->cmf->fallbackCallback = $fallbackCallback;

self::assertSame($metadata, $this->cmf->getMetadataFor('Foo'));
}
}

class TestClassMetadataFactory extends AbstractClassMetadataFactory
Expand Down

0 comments on commit 223a292

Please sign in to comment.