1
0
mirror of synced 2024-12-14 07:06:04 +03:00

[2.0] Fixing PhpDriver and covering it with tests

This commit is contained in:
jwage 2010-03-16 22:41:09 +00:00
parent 71b1f709c8
commit a25c704246
6 changed files with 164 additions and 113 deletions

View File

@ -26,7 +26,8 @@ use Doctrine\Common\Cache\ArrayCache,
Doctrine\DBAL\Schema\AbstractSchemaManager,
Doctrine\ORM\Mapping\ClassMetadataInfo,
Doctrine\ORM\Mapping\MappingException,
Doctrine\Common\Util\Inflector;
Doctrine\Common\Util\Inflector,
Doctrine\ORM\Mapping\Driver\AbstractFileDriver;
/**
* The PhpDriver includes php files which just populate ClassMetadataInfo
@ -41,126 +42,30 @@ use Doctrine\Common\Cache\ArrayCache,
* @author Jonathan H. Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org>
*/
class PhpDriver implements Driver
class PhpDriver extends AbstractFileDriver
{
/**
* @var array The array of class names found and the path to the file
*/
private $_classPaths = array();
/**
* The paths where to look for mapping files.
*
* @var array
*/
protected $_paths = array();
/**
* The file extension of mapping documents.
*
* @var string
* {@inheritdoc}
*/
protected $_fileExtension = '.php';
/**
* Initializes a new PhpDriver that looks in the given path(s) for mapping
* documents and operates in the specified operating mode.
*
* @param string|array $paths One or multiple paths where mapping documents can be found.
*/
public function __construct($paths)
{
$this->addPaths((array) $paths);
}
/**
* Append lookup paths to metadata driver.
*
* @param array $paths
*/
public function addPaths(array $paths)
{
$this->_paths = array_unique(array_merge($this->_paths, $paths));
}
/**
* Retrieve the defined metadata lookup paths.
*
* @return array
*/
public function getPaths()
{
return $this->_paths;
}
/**
* Get the file extension used to look for mapping files under
*
* @return void
*/
public function getFileExtension()
{
return $this->_fileExtension;
}
/**
* Set the file extension used to look for mapping files under
*
* @param string $fileExtension The file extension to set
* @return void
*/
public function setFileExtension($fileExtension)
{
$this->_fileExtension = $fileExtension;
}
protected $_metadata;
/**
* {@inheritdoc}
*/
public function loadMetadataForClass($className, ClassMetadataInfo $metadata)
{
$path = $this->_classPaths[$className];
require_once $path;
$this->_metadata = $metadata;
$this->_loadMappingFile($this->_findMappingFile($className));
}
/**
* {@inheritdoc}
*/
public function isTransient($className)
protected function _loadMappingFile($file)
{
return true;
}
/**
* {@inheritDoc}
*/
public function getAllClassNames()
{
$classes = array();
if ($this->_paths) {
foreach ((array) $this->_paths as $path) {
if ( ! is_dir($path)) {
throw MappingException::phpDriverRequiresConfiguredDirectoryPath();
}
$iterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($path),
\RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($iterator as $file) {
if (($fileName = $file->getBasename($this->_fileExtension)) == $file->getBasename()) {
continue;
}
$classes[] = $fileName;
$this->_classPaths[$fileName] = $file->getPathName();
}
}
}
return $classes;
$metadata = $this->_metadata;
require_once $file;
}
}

View File

@ -80,6 +80,14 @@ class PhpExporter extends AbstractExporter
$lines[] = '$metadata->setChangeTrackingPolicy(ClassMetadataInfo::CHANGETRACKING_' . $this->_getChangeTrackingPolicyString($metadata->changeTrackingPolicy) . ');';
}
if ($metadata->lifecycleCallbacks) {
foreach ($metadata->lifecycleCallbacks as $event => $callbacks) {
foreach ($callbacks as $callback) {
$lines[] = "\$metadata->addLifecycleCallback('$callback', '$event');";
}
}
}
foreach ($metadata->fieldMappings as $fieldMapping) {
$lines[] = '$metadata->mapField(' . $this->_varExport($fieldMapping) . ');';
}
@ -89,16 +97,16 @@ class PhpExporter extends AbstractExporter
}
foreach ($metadata->associationMappings as $associationMapping) {
$cascade = array('remove', 'persist', 'refresh', 'merge', 'detach');
foreach ($cascade as $key => $value) {
if ( ! $associationMapping->{'isCascade'.ucfirst($value)}) {
unset($cascade[$key]);
}
}
$associationMappingArray = array(
'fieldName' => $associationMapping->sourceFieldName,
'targetEntity' => $associationMapping->targetEntityName,
'cascade' => array(
'remove' => $associationMapping->isCascadeRemove,
'persist' => $associationMapping->isCascadePersist,
'refresh' => $associationMapping->isCascadeRefresh,
'merge' => $associationMapping->isCascadeMerge,
'detach' => $associationMapping->isCascadeDetach,
),
'cascade' => $cascade,
);
if ($associationMapping instanceof \Doctrine\ORM\Mapping\OneToOneMapping) {
@ -115,6 +123,7 @@ class PhpExporter extends AbstractExporter
$oneToManyMappingArray = array(
'mappedBy' => $associationMapping->mappedBy,
'orphanRemoval' => $associationMapping->orphanRemoval,
'orderBy' => $associationMapping->orderBy
);
$associationMappingArray = array_merge($associationMappingArray, $oneToManyMappingArray);
@ -123,10 +132,12 @@ class PhpExporter extends AbstractExporter
$manyToManyMappingArray = array(
'mappedBy' => $associationMapping->mappedBy,
'joinTable' => $associationMapping->joinTable,
'orderBy' => $associationMapping->orderBy
);
$associationMappingArray = array_merge($associationMappingArray, $manyToManyMappingArray);
}
$lines[] = '$metadata->' . $method . '(' . $this->_varExport($associationMappingArray) . ');';
}

View File

@ -157,7 +157,8 @@ abstract class AbstractMappingDriverTest extends \Doctrine\Tests\OrmTestCase
* @depends testManyToManyAssociationWithCascadeAll
* @param ClassMetadata $class
*/
public function testLifecycleCallbacksSupportMultipleMethodNames($class) {
public function testLifecycleCallbacksSupportMultipleMethodNames($class)
{
$this->assertEquals(count($class->lifecycleCallbacks['prePersist']), 2);
$this->assertEquals($class->lifecycleCallbacks['prePersist'][1], 'doOtherStuffOnPrePersistToo');

View File

@ -23,6 +23,7 @@ class AllTests
$suite->addTestSuite('Doctrine\Tests\ORM\Mapping\XmlMappingDriverTest');
$suite->addTestSuite('Doctrine\Tests\ORM\Mapping\YamlMappingDriverTest');
$suite->addTestSuite('Doctrine\Tests\ORM\Mapping\AnnotationDriverTest');
$suite->addTestSuite('Doctrine\Tests\ORM\Mapping\PhpMappingDriverTest');
$suite->addTestSuite('Doctrine\Tests\ORM\Mapping\ClassMetadataFactoryTest');
$suite->addTestSuite('Doctrine\Tests\ORM\Mapping\ClassMetadataLoadEventTest');
$suite->addTestSuite('Doctrine\Tests\ORM\Mapping\BasicInheritanceMappingTest');

View File

@ -0,0 +1,31 @@
<?php
namespace Doctrine\Tests\ORM\Mapping;
use Doctrine\ORM\Mapping\ClassMetadata,
Doctrine\ORM\Mapping\Driver\PhpDriver,
Doctrine\ORM\Tools\Export\ClassMetadataExporter;
require_once __DIR__ . '/../../TestInit.php';
class PhpMappingDriverTest extends AbstractMappingDriverTest
{
protected function _loadDriver()
{
$path = __DIR__ . DIRECTORY_SEPARATOR . 'php';
/*
// Convert YAML mapping information to PHP
// Uncomment this code if the YAML changes and you want to update the PHP code
// for the same mapping information
$cme = new ClassMetadataExporter();
$cme->addMappingSource(__DIR__ . DIRECTORY_SEPARATOR . 'yaml', 'yaml');
$exporter = $cme->getExporter('php', $path);
$exporter->setMetadatas($cme->getMetadatasForMappingSources());
$exporter->export();
*/
return new PhpDriver($path);
}
}

View File

@ -0,0 +1,102 @@
<?php
use Doctrine\ORM\Mapping\ClassMetadataInfo;
$metadata->setInheritanceType(ClassMetadataInfo::INHERITANCE_TYPE_NONE);
$metadata->setPrimaryTable(array(
'name' => 'cms_users',
));
$metadata->setChangeTrackingPolicy(ClassMetadataInfo::CHANGETRACKING_DEFERRED_IMPLICIT);
$metadata->addLifecycleCallback('doStuffOnPrePersist', 'prePersist');
$metadata->addLifecycleCallback('doOtherStuffOnPrePersistToo', 'prePersist');
$metadata->addLifecycleCallback('doStuffOnPostPersist', 'postPersist');
$metadata->mapField(array(
'id' => true,
'fieldName' => 'id',
'type' => 'integer',
'columnName' => 'id',
));
$metadata->mapField(array(
'fieldName' => 'name',
'type' => 'string',
'length' => 50,
'unique' => true,
'nullable' => true,
'columnName' => 'name',
));
$metadata->mapField(array(
'fieldName' => 'email',
'type' => 'string',
'columnName' => 'user_email',
'columnDefinition' => 'CHAR(32) NOT NULL',
));
$metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_AUTO);
$metadata->mapOneToOne(array(
'fieldName' => 'address',
'targetEntity' => 'Doctrine\\Tests\\ORM\\Mapping\\Address',
'cascade' =>
array(
0 => 'remove',
),
'mappedBy' => NULL,
'joinColumns' =>
array(
0 =>
array(
'name' => 'address_id',
'referencedColumnName' => 'id',
),
),
'orphanRemoval' => false,
));
$metadata->mapOneToMany(array(
'fieldName' => 'phonenumbers',
'targetEntity' => 'Doctrine\\Tests\\ORM\\Mapping\\Phonenumber',
'cascade' =>
array(
1 => 'persist',
),
'mappedBy' => 'user',
'orphanRemoval' => false,
'orderBy' =>
array(
'number' => 'ASC',
),
));
$metadata->mapManyToMany(array(
'fieldName' => 'groups',
'targetEntity' => 'Doctrine\\Tests\\ORM\\Mapping\\Group',
'cascade' =>
array(
0 => 'remove',
1 => 'persist',
2 => 'refresh',
3 => 'merge',
4 => 'detach',
),
'mappedBy' => NULL,
'joinTable' =>
array(
'name' => 'cms_users_groups',
'joinColumns' =>
array(
0 =>
array(
'name' => 'user_id',
'referencedColumnName' => 'id',
'unique' => false,
'nullable' => false,
),
),
'inverseJoinColumns' =>
array(
0 =>
array(
'name' => 'group_id',
'referencedColumnName' => 'id',
'columnDefinition' => 'INT NULL',
),
),
),
'orderBy' => NULL,
));