Skip to content

Commit

Permalink
Add Tests for more drivers and the AbstractClassMetadataFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
beberlei committed Nov 27, 2011
1 parent 6e9967d commit ca3d2f2
Show file tree
Hide file tree
Showing 11 changed files with 326 additions and 32 deletions.
3 changes: 2 additions & 1 deletion lib/Doctrine/Common/Cache/ZendDataCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ protected function doDelete($id)
*/
protected function doFlush()
{
return empty($this->getNamespace()) ? zend_shm_cache_clear() : zend_shm_cache_clear($this->getNamespace());
$namespace = $this->getNamespace();
return empty($namespace) ? zend_shm_cache_clear() : zend_shm_cache_clear($this->getNamespace());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,6 @@ abstract class AbstractClassMetadataFactory implements ClassMetadataFactory
*/
protected $cacheSalt = "\$CLASSMETADATA";

/**
* @var Driver\MappingDriver
*/
protected $driver;

/**
* @var \Doctrine\Common\Cache\Cache
*/
Expand All @@ -60,7 +55,7 @@ abstract class AbstractClassMetadataFactory implements ClassMetadataFactory
* @var bool
*/
protected $initialized = false;

/**
* Sets the cache driver used by the factory to cache ClassMetadata instances.
*
Expand All @@ -81,6 +76,11 @@ public function getCacheDriver()
return $this->cacheDriver;
}

/**
* Return an array of all the loaded metadata currently in memory.
*
* @return array
*/
public function getLoadedMetadata()
{
return $this->loadedMetadata;
Expand All @@ -97,9 +97,10 @@ public function getAllMetadata()
if ( ! $this->initialized) {
$this->initialize();
}


$driver = $this->getDriver();
$metadata = array();
foreach ($this->driver->getAllClassNames() as $className) {
foreach ($driver->getAllClassNames() as $className) {
$metadata[] = $this->getMetadataFor($className);
}

Expand All @@ -109,10 +110,26 @@ public function getAllMetadata()
/**
* Lazy initialization of this stuff, especially the metadata driver,
* since these are not needed at all when a metadata cache is active.
*
* @return void
*/
abstract protected function initialize();

/**
* Get the fully qualified class-name from the namespace alias.
*
* @param string $namespaceAlias
* @param string $simpleClassName
* @return string
*/
abstract protected function getFqcnFromAlias($namespaceAlias, $simpleClassName);

/**
* Return the mapping driver implementation.
*
* @return MappingDriver
*/
abstract protected function getDriver();

/**
* Gets the class metadata descriptor for a class.
Expand All @@ -128,7 +145,7 @@ public function getMetadataFor($className)
// Check for namespace alias
if (strpos($className, ':') !== false) {
list($namespaceAlias, $simpleClassName) = explode(':', $className);
$realClassName = $this->getFqcnFromAlias($namespaceAlias) . '\\' . $simpleClassName;
$realClassName = $this->getFqcnFromAlias($namespaceAlias, $simpleClassName);

if (isset($this->loadedMetadata[$realClassName])) {
// We do not have the alias name in the map, include it
Expand Down Expand Up @@ -196,7 +213,7 @@ protected function getParentClasses($name)
// Collect parent classes, ignoring transient (not-mapped) classes.
$parentClasses = array();
foreach (array_reverse(class_parents($name)) as $parentClass) {
if ( ! $this->driver->isTransient($parentClass)) {
if ( ! $this->getDriver()->isTransient($parentClass)) {
$parentClasses[] = $parentClass;
}
}
Expand Down Expand Up @@ -228,7 +245,7 @@ protected function loadMetadata($name)
foreach ($parentClasses as $className) {
if (isset($this->loadedMetadata[$className])) {
$parent = $this->loadedMetadata[$className];
if ( ! $parent->isMappedSuperclass) {
if (isset($parent->isMappedSuperclass) && $parent->isMappedSuperclass === false) {
$rootEntityFound = true;
array_unshift($visited, $className);
}
Expand All @@ -243,7 +260,7 @@ protected function loadMetadata($name)

$parent = $class;

if ( ! $class->isMappedSuperclass) {
if (isset($parent->isMappedSuperclass) && $class->isMappedSuperclass === false) {
$rootEntityFound = true;
array_unshift($visited, $className);
}
Expand All @@ -254,6 +271,14 @@ protected function loadMetadata($name)
return $loaded;
}

/**
* Actually load the metadata from the underlying metadata
*
* @param ClassMetadata $class
* @param ClassMetadata $parent
* @param bool $rootEntityFound
* @return void
*/
abstract protected function doLoadMetadata($class, $parent, $rootEntityFound);

/**
Expand All @@ -275,7 +300,7 @@ public function isTransient($class)
if ( ! $this->initialized) {
$this->initialize();
}

return $this->driver->isTransient($class);
return $this->getDriver()->isTransient($class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
* @author Jonathan H. Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org>
*/
abstract class AnnotationDriver implements Driver
abstract class AnnotationDriver implements MappingDriver
{
/**
* The AnnotationReader.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
* @author Jonathan H. Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org>
*/
class MappingDriverChain implements Driver
class MappingDriverChain implements MappingDriver
{
/**
* @var array
Expand Down
16 changes: 12 additions & 4 deletions lib/Doctrine/Common/Persistence/Mapping/Driver/PHPDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,32 @@ class PHPDriver extends FileDriver
/**
* {@inheritdoc}
*/
protected $fileExtension = '.php';
protected $metadata;

/**
* {@inheritDoc}
*/
public function __construct($locator, $fileExtension = null)
{
$fileExtension = ".php";
parent::__construct($locator, $fileExtension);
}

/**
* {@inheritdoc}
*/
public function loadMetadataForClass($className, ClassMetadata $metadata)
{
$this->metadata = $metadata;
$this->_loadMappingFile($this->_findMappingFile($className));
$this->loadMappingFile($this->locator->findMappingFile($className));
}

/**
* {@inheritdoc}
*/
protected function _loadMappingFile($file)
protected function loadMappingFile($file)
{
$metadata = $this->metadata;
include $file;
}
}
}
16 changes: 5 additions & 11 deletions lib/Doctrine/Common/Persistence/Mapping/Driver/StaticPHPDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
namespace Doctrine\Common\Persistence\Mapping\Driver;

use Doctrine\Common\Persistence\Mapping\ClassMetadata;
use Doctrine\Common\Persistence\Mapping\MappingException;

/**
* The StaticPHPDriver calls a static loadMetadata() method on your entity
Expand Down Expand Up @@ -49,13 +50,6 @@ class StaticPHPDriver implements MappingDriver
*/
private $classNames;

/**
* The file extension of mapping documents.
*
* @var string
*/
private $fileExtension = '.php';

public function __construct($paths)
{
$this->addPaths((array) $paths);
Expand All @@ -69,9 +63,9 @@ public function addPaths(array $paths)
/**
* {@inheritdoc}
*/
public function loadMetadataForClass($className, ClassMetadataInfo $metadata)
public function loadMetadataForClass($className, ClassMetadata $metadata)
{
call_user_func_array(array($className, 'loadMetadata'), array($metadata));
$className::loadMetadata($metadata);
}

/**
Expand Down Expand Up @@ -102,7 +96,7 @@ public function getAllClassNames()
);

foreach ($iterator as $file) {
if (($fileName = $file->getBasename($this->fileExtension)) == $file->getBasename()) {
if (($fileName = $file->getBasename(".php")) == $file->getBasename()) {
continue;
}

Expand Down Expand Up @@ -132,6 +126,6 @@ public function getAllClassNames()
*/
public function isTransient($className)
{
return method_exists($className, 'loadMetadata') ? false : true;
return ! method_exists($className, 'loadMetadata');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace Doctrine\Tests\Common\Persistence\Mapping;

use Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain;
use Doctrine\Tests\DoctrineTestCase;

class DriverChainTest extends DoctrineTestCase
{
public function testDelegateToMatchingNamespaceDriver()
{
$className = 'Doctrine\Tests\Common\Persistence\Mapping\DriverChainEntity';
$classMetadata = $this->getMock('Doctrine\Common\Persistence\Mapping\ClassMetadata');

$chain = new MappingDriverChain();

$driver1 = $this->getMock('Doctrine\Common\Persistence\Mapping\Driver\MappingDriver');
$driver1->expects($this->never())
->method('loadMetadataForClass');
$driver1->expectS($this->never())
->method('isTransient');

$driver2 = $this->getMock('Doctrine\Common\Persistence\Mapping\Driver\MappingDriver');
$driver2->expects($this->at(0))
->method('loadMetadataForClass')
->with($this->equalTo($className), $this->equalTo($classMetadata));
$driver2->expects($this->at(1))
->method('isTransient')
->with($this->equalTo($className))
->will($this->returnValue( true ));

$chain->addDriver($driver1, 'Doctrine\Tests\Models\Company');
$chain->addDriver($driver2, 'Doctrine\Tests\Common\Persistence\Mapping');

$chain->loadMetadataForClass($className, $classMetadata);

$this->assertTrue( $chain->isTransient($className) );
}

public function testLoadMetadata_NoDelegatorFound_ThrowsMappingException()
{
$className = 'Doctrine\Tests\Common\Persistence\Mapping\DriverChainEntity';
$classMetadata = $this->getMock('Doctrine\Common\Persistence\Mapping\ClassMetadata');

$chain = new MappingDriverChain();

$this->setExpectedException('Doctrine\Common\Persistence\Mapping\MappingException');
$chain->loadMetadataForClass($className, $classMetadata);
}

public function testGatherAllClassNames()
{
$className = 'Doctrine\Tests\Common\Persistence\Mapping\DriverChainEntity';
$classMetadata = $this->getMock('Doctrine\Common\Peristence\ClassMetadata');

$chain = new MappingDriverChain();

$driver1 = $this->getMock('Doctrine\Common\Persistence\Mapping\Driver\MappingDriver');
$driver1->expects($this->once())
->method('getAllClassNames')
->will($this->returnValue(array('Doctrine\Tests\Models\Company\Foo')));

$driver2 = $this->getMock('Doctrine\Common\Persistence\Mapping\Driver\MappingDriver');
$driver2->expects($this->once())
->method('getAllClassNames')
->will($this->returnValue(array('Doctrine\Tests\ORM\Mapping\Bar', 'Doctrine\Tests\ORM\Mapping\Baz', 'FooBarBaz')));

$chain->addDriver($driver1, 'Doctrine\Tests\Models\Company');
$chain->addDriver($driver2, 'Doctrine\Tests\ORM\Mapping');

$this->assertEquals(array(
'Doctrine\Tests\Models\Company\Foo',
'Doctrine\Tests\ORM\Mapping\Bar',
'Doctrine\Tests\ORM\Mapping\Baz'
), $chain->getAllClassNames());
}

/**
* @group DDC-706
*/
public function testIsTransient()
{
$driver1 = $this->getMock('Doctrine\Common\Persistence\Mapping\Driver\MappingDriver');
$chain = new MappingDriverChain();
$chain->addDriver($driver1, 'Doctrine\Tests\Models\CMS');

$this->assertTrue($chain->isTransient('stdClass'), "stdClass isTransient");
}
}

class DriverChainEntity
{

}
Loading

0 comments on commit ca3d2f2

Please sign in to comment.