From 770d00abe91220323680aa97ea82f9eae9479ef5 Mon Sep 17 00:00:00 2001 From: jwage Date: Wed, 17 Mar 2010 21:25:19 +0000 Subject: [PATCH] [2.0] Improving test coverage for mapping exporters as well as adding missing functionality --- lib/Doctrine/ORM/Mapping/Driver/PhpDriver.php | 2 +- .../Tools/Export/Driver/AbstractExporter.php | 2 +- .../Export/Driver/AnnotationExporter.php | 73 ++++- .../ORM/Tools/Export/Driver/XmlExporter.php | 70 ++++- .../ORM/Tools/Export/Driver/YamlExporter.php | 35 ++- .../Tools/Export/Driver/annotation.tpl.php | 10 +- .../ORM/Mapping/AbstractMappingDriverTest.php | 2 +- tests/Doctrine/Tests/ORM/Tools/AllTests.php | 5 +- .../AbstractClassMetadataExporterTest.php | 281 ++++++++++++++++++ .../AnnotationClassMetadataExporterTest.php | 42 +++ .../Export/ClassMetadataExporterTest.php | 151 ---------- .../Export/PhpClassMetadataExporterTEst.php | 42 +++ .../Export/XmlClassMetadataExporterTest.php | 42 +++ .../Export/YamlClassMetadataExporterTest.php | 42 +++ .../Export/annotation/AnnotationTest.php | 52 ---- .../Doctrine.Tests.ORM.Tools.Export.User.php | 68 +++++ .../Doctrine.Tests.ORM.Tools.Export.User.php | 104 +++++++ .../Tests/ORM/Tools/Export/php/PhpTest.php | 23 -- ...ctrine.Tests.ORM.Tools.Export.User.dcm.xml | 53 ++++ .../ORM/Tools/Export/xml/XmlTest.dcm.xml | 2 - ...ctrine.Tests.ORM.Tools.Export.User.dcm.yml | 53 ++++ .../ORM/Tools/Export/yml/YmlTest.dcm.yml | 12 - 22 files changed, 901 insertions(+), 265 deletions(-) create mode 100644 tests/Doctrine/Tests/ORM/Tools/Export/AbstractClassMetadataExporterTest.php create mode 100644 tests/Doctrine/Tests/ORM/Tools/Export/AnnotationClassMetadataExporterTest.php create mode 100644 tests/Doctrine/Tests/ORM/Tools/Export/PhpClassMetadataExporterTEst.php create mode 100644 tests/Doctrine/Tests/ORM/Tools/Export/XmlClassMetadataExporterTest.php create mode 100644 tests/Doctrine/Tests/ORM/Tools/Export/YamlClassMetadataExporterTest.php delete mode 100644 tests/Doctrine/Tests/ORM/Tools/Export/annotation/AnnotationTest.php create mode 100644 tests/Doctrine/Tests/ORM/Tools/Export/annotation/Doctrine.Tests.ORM.Tools.Export.User.php create mode 100644 tests/Doctrine/Tests/ORM/Tools/Export/php/Doctrine.Tests.ORM.Tools.Export.User.php delete mode 100644 tests/Doctrine/Tests/ORM/Tools/Export/php/PhpTest.php create mode 100644 tests/Doctrine/Tests/ORM/Tools/Export/xml/Doctrine.Tests.ORM.Tools.Export.User.dcm.xml delete mode 100644 tests/Doctrine/Tests/ORM/Tools/Export/xml/XmlTest.dcm.xml create mode 100644 tests/Doctrine/Tests/ORM/Tools/Export/yaml/Doctrine.Tests.ORM.Tools.Export.User.dcm.yml delete mode 100644 tests/Doctrine/Tests/ORM/Tools/Export/yml/YmlTest.dcm.yml diff --git a/lib/Doctrine/ORM/Mapping/Driver/PhpDriver.php b/lib/Doctrine/ORM/Mapping/Driver/PhpDriver.php index 5dd363698..ecc4dd896 100644 --- a/lib/Doctrine/ORM/Mapping/Driver/PhpDriver.php +++ b/lib/Doctrine/ORM/Mapping/Driver/PhpDriver.php @@ -66,6 +66,6 @@ class PhpDriver extends AbstractFileDriver protected function _loadMappingFile($file) { $metadata = $this->_metadata; - require_once $file; + include $file; } } \ No newline at end of file diff --git a/lib/Doctrine/ORM/Tools/Export/Driver/AbstractExporter.php b/lib/Doctrine/ORM/Tools/Export/Driver/AbstractExporter.php index b8321ec44..12a2f18c2 100644 --- a/lib/Doctrine/ORM/Tools/Export/Driver/AbstractExporter.php +++ b/lib/Doctrine/ORM/Tools/Export/Driver/AbstractExporter.php @@ -80,7 +80,7 @@ abstract class AbstractExporter public function export() { if ( ! is_dir($this->_outputDir)) { - mkdir($this->_outputDir, 0777); + mkdir($this->_outputDir, 0777, true); } foreach ($this->_metadatas as $metadata) { diff --git a/lib/Doctrine/ORM/Tools/Export/Driver/AnnotationExporter.php b/lib/Doctrine/ORM/Tools/Export/Driver/AnnotationExporter.php index 45babb405..045e3fd97 100644 --- a/lib/Doctrine/ORM/Tools/Export/Driver/AnnotationExporter.php +++ b/lib/Doctrine/ORM/Tools/Export/Driver/AnnotationExporter.php @@ -232,17 +232,40 @@ class AnnotationExporter extends AbstractExporter private function _getEntityAnnotation($metadata) { - if ($metadata->isMappedSuperclass) { - return '@MappedSupperClass'; + $lines = array(); + $lines[] = '/**'; + + $methods = array( + '_getTableAnnotation', + '_getInheritanceAnnotation', + '_getDiscriminatorColumnAnnotation', + '_getDiscriminatorMapAnnotation' + ); + + foreach ($methods as $method) { + if ($code = $this->$method($metadata)) { + $lines[] = ' * ' . $code; + } } - $str = '@Entity'; + if ($metadata->isMappedSuperclass) { + $lines[] = ' * @MappedSupperClass'; + } else { + $lines[] = ' * @Entity'; + } if ($metadata->customRepositoryClassName) { - $str .= '(repositoryClass="' . $metadata->customRepositoryClassName . '")'; + $lines[count($lines) - 1] .= '(repositoryClass="' . $metadata->customRepositoryClassName . '")'; } - return $str; + if (isset($metadata->lifecycleCallbacks) && $metadata->lifecycleCallbacks) { + $lines[] = ' * @HasLifecycleCallbacks'; + } + + $lines[] = ' */'; + $lines[] = ''; + + return implode("\n", $lines); } private function _getTableAnnotation($metadata) @@ -350,6 +373,23 @@ class AnnotationExporter extends AbstractExporter $methods[] = implode("\n", $method); } + private function _addLifecycleCallbackMethod($name, $methodName, $metadata, array &$methods) + { + if ($this->_hasMethod($methodName, $metadata)) { + return false; + } + + $method = array(); + $method[] = $this->_spaces . '/**'; + $method[] = $this->_spaces . ' * @'.$name; + $method[] = $this->_spaces . ' */'; + $method[] = $this->_spaces . 'public function ' . $methodName . '()'; + $method[] = $this->_spaces . '{'; + $method[] = $this->_spaces . '}'; + + $methods[] = implode("\n", $method)."\n\n"; + } + private function _getMethods($metadata) { $methods = array(); @@ -380,6 +420,14 @@ class AnnotationExporter extends AbstractExporter } } + if (isset($metadata->lifecycleCallbacks) && $metadata->lifecycleCallbacks) { + foreach ($metadata->lifecycleCallbacks as $name => $callbacks) { + foreach ($callbacks as $callback) { + $this->_addLifecycleCallbackMethod($name, $callback, $metadata, $methods); + } + } + } + return $methods; } @@ -404,6 +452,9 @@ class AnnotationExporter extends AbstractExporter if (isset($joinColumn['onUpdate'])) { $joinColumnAnnot[] = 'onUpdate=' . ($joinColumn['onUpdate'] ? 'true' : 'false'); } + if (isset($joinColumn['columnDefinition'])) { + $joinColumnAnnot[] = 'columnDefinition="' . $joinColumn['columnDefinition'] . '"'; + } return '@JoinColumn(' . implode(', ', $joinColumnAnnot) . ')'; } @@ -472,6 +523,15 @@ class AnnotationExporter extends AbstractExporter $lines[] = $this->_spaces . ' * )'; } + if (isset($associationMapping->orderBy)) { + $lines[] = $this->_spaces . ' * @OrderBy({'; + foreach ($associationMapping->orderBy as $name => $direction) { + $lines[] = $this->_spaces . ' * "' . $name . '"="' . $direction . '",'; + } + $lines[count($lines) - 1] = substr($lines[count($lines) - 1], 0, strlen($lines[count($lines) - 1]) - 1); + $lines[] = $this->_spaces . ' * })'; + } + $lines[] = $this->_spaces . ' */'; return implode("\n", $lines); @@ -501,6 +561,9 @@ class AnnotationExporter extends AbstractExporter if (isset($fieldMapping['nullable'])) { $column[] = 'nullable=' . var_export($fieldMapping['nullable'], true); } + if (isset($fieldMapping['columnDefinition'])) { + $column[] = 'columnDefinition="' . $fieldMapping['columnDefinition'] . '"'; + } if (isset($fieldMapping['options'])) { $options = array(); foreach ($fieldMapping['options'] as $key => $value) { diff --git a/lib/Doctrine/ORM/Tools/Export/Driver/XmlExporter.php b/lib/Doctrine/ORM/Tools/Export/Driver/XmlExporter.php index 9857e390e..982755db5 100644 --- a/lib/Doctrine/ORM/Tools/Export/Driver/XmlExporter.php +++ b/lib/Doctrine/ORM/Tools/Export/Driver/XmlExporter.php @@ -160,6 +160,9 @@ class XmlExporter extends AbstractExporter if (isset($field['version'])) { $fieldXml->addAttribute('version', $field['version']); } + if (isset($field['columnDefinition'])) { + $fieldXml->addAttribute('column-definition', $field['columnDefinition']); + } } } @@ -204,9 +207,63 @@ class XmlExporter extends AbstractExporter $joinColumnXml = $joinColumnsXml->addChild('join-column'); $joinColumnXml->addAttribute('name', $joinColumn['name']); $joinColumnXml->addAttribute('referenced-column-name', $joinColumn['referencedColumnName']); + if (isset($joinColumn['onDelete'])) { + $joinColumnXml->addAttribute('on-delete', $joinColumn['onDelete']); + } + if (isset($joinColumn['onUpdate'])) { + $joinColumnXml->addAttribute('on-update', $joinColumn['onUpdate']); + } + } + $inverseJoinColumnsXml = $joinTableXml->addChild('inverse-join-columns'); + foreach ($associationMapping->joinTable['inverseJoinColumns'] as $inverseJoinColumn) { + $inverseJoinColumnXml = $inverseJoinColumnsXml->addChild('join-column'); + $inverseJoinColumnXml->addAttribute('name', $inverseJoinColumn['name']); + $inverseJoinColumnXml->addAttribute('referenced-column-name', $inverseJoinColumn['referencedColumnName']); + if (isset($inverseJoinColumn['onDelete'])) { + $inverseJoinColumnXml->addAttribute('on-delete', $inverseJoinColumn['onDelete']); + } + if (isset($inverseJoinColumn['onUpdate'])) { + $inverseJoinColumnXml->addAttribute('on-update', $inverseJoinColumn['onUpdate']); + } + if (isset($inverseJoinColumn['columnDefinition'])) { + $inverseJoinColumnXml->addAttribute('column-definition', $inverseJoinColumn['columnDefinition']); + } + if (isset($inverseJoinColumn['nullable'])) { + $inverseJoinColumnXml->addAttribute('nullable', $inverseJoinColumn['nullable']); + } + if (isset($inverseJoinColumn['orderBy'])) { + $inverseJoinColumnXml->addAttribute('order-by', $inverseJoinColumn['orderBy']); + } + } + } + if (isset($associationMapping->joinColumns)) { + $joinColumnsXml = $associationMappingXml->addChild('join-columns'); + foreach ($associationMapping->joinColumns as $joinColumn) { + $joinColumnXml = $joinColumnsXml->addChild('join-column'); + $joinColumnXml->addAttribute('name', $joinColumn['name']); + $joinColumnXml->addAttribute('referenced-column-name', $joinColumn['referencedColumnName']); + if (isset($joinColumn['onDelete'])) { + $joinColumnXml->addAttribute('on-delete', $joinColumn['onDelete']); + } + if (isset($joinColumn['onUpdate'])) { + $joinColumnXml->addAttribute('on-update', $joinColumn['onUpdate']); + } + if (isset($joinColumn['columnDefinition'])) { + $joinColumnXml->addAttribute('column-definition', $joinColumn['columnDefinition']); + } + if (isset($joinColumn['nullable'])) { + $joinColumnXml->addAttribute('nullable', $joinColumn['nullable']); + } + } + } + if (isset($associationMapping->orderBy)) { + $orderByXml = $associationMappingXml->addChild('order-by'); + foreach ($associationMapping->orderBy as $name => $direction) { + $orderByFieldXml = $orderByXml->addChild('order-by-field'); + $orderByFieldXml->addAttribute('name', $name); + $orderByFieldXml->addAttribute('direction', $direction); } } - $cascade = array(); if ($associationMapping->isCascadeRemove) { $cascade[] = 'remove'; @@ -231,6 +288,17 @@ class XmlExporter extends AbstractExporter } } + if (isset($metadata->lifecycleCallbacks)) { + $lifecycleCallbacksXml = $root->addChild('lifecycle-callbacks'); + foreach ($metadata->lifecycleCallbacks as $name => $methods) { + foreach ($methods as $method) { + $lifecycleCallbackXml = $lifecycleCallbacksXml->addChild('lifecycle-callback'); + $lifecycleCallbackXml->addAttribute('type', $name); + $lifecycleCallbackXml->addAttribute('method', $method); + } + } + } + return $this->_asXml($xml); } diff --git a/lib/Doctrine/ORM/Tools/Export/Driver/YamlExporter.php b/lib/Doctrine/ORM/Tools/Export/Driver/YamlExporter.php index bc9cc108d..eac5f7bdc 100644 --- a/lib/Doctrine/ORM/Tools/Export/Driver/YamlExporter.php +++ b/lib/Doctrine/ORM/Tools/Export/Driver/YamlExporter.php @@ -133,15 +133,25 @@ class YamlExporter extends AbstractExporter $associations = array(); foreach ($metadata->associationMappings as $name => $associationMapping) { + $cascade = array(); + if ($associationMapping->isCascadeRemove) { + $cascade[] = 'remove'; + } + if ($associationMapping->isCascadePersist) { + $cascade[] = 'persist'; + } + if ($associationMapping->isCascadeRefresh) { + $cascade[] = 'refresh'; + } + if ($associationMapping->isCascadeMerge) { + $cascade[] = 'merge'; + } + if ($associationMapping->isCascadeDetach) { + $cascade[] = 'detach'; + } $associationMappingArray = array( '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 OneToOneMapping) { @@ -149,6 +159,12 @@ class YamlExporter extends AbstractExporter $newJoinColumns = array(); foreach ($joinColumns as $joinColumn) { $newJoinColumns[$joinColumn['name']]['referencedColumnName'] = $joinColumn['referencedColumnName']; + if (isset($joinColumn['onDelete'])) { + $newJoinColumns[$joinColumn['name']]['onDelete'] = $joinColumn['onDelete']; + } + if (isset($joinColumn['onUpdate'])) { + $newJoinColumns[$joinColumn['name']]['onUpdate'] = $joinColumn['onUpdate']; + } } $oneToOneMappingArray = array( 'mappedBy' => $associationMapping->mappedBy, @@ -162,6 +178,7 @@ class YamlExporter extends AbstractExporter $oneToManyMappingArray = array( 'mappedBy' => $associationMapping->mappedBy, 'orphanRemoval' => $associationMapping->orphanRemoval, + 'orderBy' => $associationMapping->orderBy ); $associationMappingArray = array_merge($associationMappingArray, $oneToManyMappingArray); @@ -170,12 +187,16 @@ class YamlExporter extends AbstractExporter $manyToManyMappingArray = array( 'mappedBy' => $associationMapping->mappedBy, 'joinTable' => $associationMapping->joinTable, + 'orderBy' => $associationMapping->orderBy ); $associationMappingArray = array_merge($associationMappingArray, $manyToManyMappingArray); $array['manyToMany'][$name] = $associationMappingArray; } } + if (isset($metadata->lifecycleCallbacks)) { + $array['lifecycleCallbacks'] = $metadata->lifecycleCallbacks; + } return \Symfony\Components\Yaml\Yaml::dump(array($metadata->name => $array), 10); } diff --git a/lib/Doctrine/ORM/Tools/Export/Driver/annotation.tpl.php b/lib/Doctrine/ORM/Tools/Export/Driver/annotation.tpl.php index 8439a0e3e..cabb25d24 100644 --- a/lib/Doctrine/ORM/Tools/Export/Driver/annotation.tpl.php +++ b/lib/Doctrine/ORM/Tools/Export/Driver/annotation.tpl.php @@ -8,14 +8,8 @@ namespace _getNamespace($metadata) ?>; use _getClassToExtendNamespace() ?>; -/** - * _getEntityAnnotation($metadata)."\n"; ?> - * _getTableAnnotation($metadata)."\n" ?> - * _getInheritanceAnnotation($metadata)."\n" ?> - * _getDiscriminatorColumnAnnotation($metadata)."\n" ?> - * _getDiscriminatorMapAnnotation($metadata)."\n" ?> - */ -class _getClassName($metadata); ?>_extendsClass()): ?> extends _getClassToExtendName() ?> +_getEntityAnnotation($metadata) ?> +class _getClassName($metadata); ?>_extendsClass()): ?> extends _getClassToExtendName() ?> { } \ No newline at end of file diff --git a/tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php b/tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php index 53518613c..4133ffd35 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php @@ -210,7 +210,7 @@ abstract class AbstractMappingDriverTest extends \Doctrine\Tests\OrmTestCase */ class User { - /** @Id @Column(type="int") @generatedValue(strategy="AUTO") */ + /** @Id @Column(type="integer") @generatedValue(strategy="AUTO") */ public $id; /** diff --git a/tests/Doctrine/Tests/ORM/Tools/AllTests.php b/tests/Doctrine/Tests/ORM/Tools/AllTests.php index acff3e8b3..30d50a7be 100644 --- a/tests/Doctrine/Tests/ORM/Tools/AllTests.php +++ b/tests/Doctrine/Tests/ORM/Tools/AllTests.php @@ -20,7 +20,10 @@ class AllTests { $suite = new \Doctrine\Tests\DoctrineTestSuite('Doctrine Orm Tools'); - $suite->addTestSuite('Doctrine\Tests\ORM\Tools\Export\ClassMetadataExporterTest'); + $suite->addTestSuite('Doctrine\Tests\ORM\Tools\Export\YamlClassMetadataExporterTest'); + $suite->addTestSuite('Doctrine\Tests\ORM\Tools\Export\XmlClassMetadataExporterTest'); + $suite->addTestSuite('Doctrine\Tests\ORM\Tools\Export\PhpClassMetadataExporterTest'); + $suite->addTestSuite('Doctrine\Tests\ORM\Tools\Export\AnnotationClassMetadataExporterTest'); $suite->addTestSuite('Doctrine\Tests\ORM\Tools\ConvertDoctrine1SchemaTest'); $suite->addTestSuite('Doctrine\Tests\ORM\Tools\SchemaToolTest'); diff --git a/tests/Doctrine/Tests/ORM/Tools/Export/AbstractClassMetadataExporterTest.php b/tests/Doctrine/Tests/ORM/Tools/Export/AbstractClassMetadataExporterTest.php new file mode 100644 index 000000000..67183dcf5 --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Tools/Export/AbstractClassMetadataExporterTest.php @@ -0,0 +1,281 @@ +. + */ + +namespace Doctrine\Tests\ORM\Tools\Export; + +use Doctrine\ORM\Tools\Export\ClassMetadataExporter, + Doctrine\ORM\Mapping\ClassMetadataInfo; + +require_once __DIR__ . '/../../../TestInit.php'; + +/** + * Test case for ClassMetadataExporter + * + * @author Jonathan H. Wage + * @author Roman Borschel _getType() == 'annotation') { + return 'Doctrine\Tests\ORM\Tools\Export\User2'; + } else { + return 'Doctrine\Tests\ORM\Tools\Export\User'; + } + } + + protected function _loadClassMetadataExporter() + { + $type = $this->_getType(); + + $cme = new ClassMetadataExporter(); + $cme->addMappingSource(__DIR__ . '/' . $type, $type); + + return $cme; + } + + public function testGetMetadatasForMappingSources() + { + $type = $this->_getType(); + $cme = $this->_loadClassMetadataExporter(); + $metadataInstances = $cme->getMetadatasForMappingSources(); + + $this->assertEquals('Doctrine\Tests\ORM\Tools\Export\User', $metadataInstances['Doctrine\Tests\ORM\Tools\Export\User']->name); + + return $cme; + } + + /** + * @depends testGetMetadatasForMappingSources + * @param ClassMetadataExporter $cme + */ + public function testExportDirectoryAndFilesAreCreated($cme) + { + $type = $this->_getType(); + $exporter = $cme->getExporter($type, __DIR__ . '/export/' . $type); + $this->_extension = $exporter->getExtension(); + $metadatas = $cme->getMetadatasForMappingSources(); + if ($type == 'annotation') { + $metadatas['Doctrine\Tests\ORM\Tools\Export\User']->name = $this->_getTestEntityName(); + } + + $exporter->setMetadatas($metadatas); + $exporter->export(); + + if ($type == 'annotation') { + $this->assertTrue(file_exists(__DIR__ . '/export/' . $type . '/'.str_replace('\\', '/', $this->_getTestEntityName()).$this->_extension)); + } else { + $this->assertTrue(file_exists(__DIR__ . '/export/' . $type . '/Doctrine.Tests.ORM.Tools.Export.User'.$this->_extension)); + } + } + + /** + * @depends testExportDirectoryAndFilesAreCreated + */ + public function testExportedMetadataCanBeReadBackIn() + { + $type = $this->_getType(); + $cme = new ClassMetadataExporter(); + $cme->addMappingSource(__DIR__ . '/export/' . $type, $type); + $metadataInstances = $cme->getMetadatasForMappingSources(); + $metadata = current($metadataInstances); + + $this->assertEquals($this->_getTestEntityName(), $metadata->name); + + return $metadata; + } + + /** + * @depends testExportedMetadataCanBeReadBackIn + * @param ClassMetadataInfo $metadata + */ + public function testTableIsExported($metadata) + { + $this->assertEquals('cms_users', $metadata->primaryTable['name']); + + return $metadata; + } + + /** + * @depends testTableIsExported + * @param ClassMetadataInfo $metadata + */ + public function testTypeIsExported($metadata) + { + $this->assertFalse($metadata->isMappedSuperclass); + + return $metadata; + } + + /** + * @depends testTypeIsExported + * @param ClassMetadataInfo $metadata + */ + public function testIdentifierIsExported($metadata) + { + $this->assertEquals(ClassMetadataInfo::GENERATOR_TYPE_AUTO, $metadata->generatorType); + $this->assertEquals(array('id'), $metadata->identifier); + $this->assertTrue(isset($metadata->fieldMappings['id']['id']) && $metadata->fieldMappings['id']['id'] === true); + + return $metadata; + } + + /** + * @depends testIdentifierIsExported + * @param ClassMetadataInfo $metadata + */ + public function testFieldsAreExpored($metadata) + { + $this->assertTrue(isset($metadata->fieldMappings['id']['id']) && $metadata->fieldMappings['id']['id'] === true); + $this->assertEquals('id', $metadata->fieldMappings['id']['fieldName']); + $this->assertEquals('integer', $metadata->fieldMappings['id']['type']); + $this->assertEquals('id', $metadata->fieldMappings['id']['columnName']); + + $this->assertEquals('name', $metadata->fieldMappings['name']['fieldName']); + $this->assertEquals('string', $metadata->fieldMappings['name']['type']); + $this->assertEquals(50, $metadata->fieldMappings['name']['length']); + $this->assertEquals('name', $metadata->fieldMappings['name']['columnName']); + + $this->assertEquals('email', $metadata->fieldMappings['email']['fieldName']); + $this->assertEquals('string', $metadata->fieldMappings['email']['type']); + $this->assertEquals('user_email', $metadata->fieldMappings['email']['columnName']); + $this->assertEquals('CHAR(32) NOT NULL', $metadata->fieldMappings['email']['columnDefinition']); + + return $metadata; + } + + /** + * @depends testFieldsAreExpored + * @param ClassMetadataInfo $metadata + */ + public function testOneToOneAssociationsAreExported($metadata) + { + $this->assertTrue(isset($metadata->associationMappings['address'])); + $this->assertTrue($metadata->associationMappings['address'] instanceof \Doctrine\ORM\Mapping\OneToOneMapping); + $this->assertEquals('Doctrine\Tests\ORM\Tools\Export\Address', $metadata->associationMappings['address']->targetEntityName); + $this->assertEquals('address_id', $metadata->associationMappings['address']->joinColumns[0]['name']); + $this->assertEquals('id', $metadata->associationMappings['address']->joinColumns[0]['referencedColumnName']); + $this->assertEquals('CASCADE', $metadata->associationMappings['address']->joinColumns[0]['onDelete']); + $this->assertEquals('CASCADE', $metadata->associationMappings['address']->joinColumns[0]['onUpdate']); + + $this->assertTrue($metadata->associationMappings['address']->isCascadeRemove); + $this->assertFalse($metadata->associationMappings['address']->isCascadePersist); + $this->assertFalse($metadata->associationMappings['address']->isCascadeRefresh); + $this->assertFalse($metadata->associationMappings['address']->isCascadeMerge); + $this->assertFalse($metadata->associationMappings['address']->isCascadeDetach); + + return $metadata; + } + + /** + * @depends testOneToOneAssociationsAreExported + * @param ClassMetadataInfo $metadata + */ + public function testOneToManyAssociationsAreExported($metadata) + { + $this->assertTrue(isset($metadata->associationMappings['phonenumbers'])); + $this->assertTrue($metadata->associationMappings['phonenumbers'] instanceof \Doctrine\ORM\Mapping\OneToManyMapping); + $this->assertEquals('Doctrine\Tests\ORM\Tools\Export\Phonenumber', $metadata->associationMappings['phonenumbers']->targetEntityName); + $this->assertEquals('user', $metadata->associationMappings['phonenumbers']->mappedBy); + $this->assertEquals(array('number' => 'ASC'), $metadata->associationMappings['phonenumbers']->orderBy); + + $this->assertFalse($metadata->associationMappings['phonenumbers']->isCascadeRemove); + $this->assertTrue($metadata->associationMappings['phonenumbers']->isCascadePersist); + $this->assertFalse($metadata->associationMappings['phonenumbers']->isCascadeRefresh); + $this->assertFalse($metadata->associationMappings['phonenumbers']->isCascadeMerge); + $this->assertFalse($metadata->associationMappings['phonenumbers']->isCascadeDetach); + + return $metadata; + } + + /** + * @depends testOneToManyAssociationsAreExported + * @param ClassMetadataInfo $metadata + */ + public function testManyToManyAssociationsAreExported($metadata) + { + $this->assertTrue(isset($metadata->associationMappings['groups'])); + $this->assertTrue($metadata->associationMappings['groups'] instanceof \Doctrine\ORM\Mapping\ManyToManyMapping); + $this->assertEquals('Doctrine\Tests\ORM\Tools\Export\Group', $metadata->associationMappings['groups']->targetEntityName); + $this->assertEquals('cms_users_groups', $metadata->associationMappings['groups']->joinTable['name']); + + $this->assertEquals('user_id', $metadata->associationMappings['groups']->joinTable['joinColumns'][0]['name']); + $this->assertEquals('id', $metadata->associationMappings['groups']->joinTable['joinColumns'][0]['referencedColumnName']); + + $this->assertEquals('group_id', $metadata->associationMappings['groups']->joinTable['inverseJoinColumns'][0]['name']); + $this->assertEquals('id', $metadata->associationMappings['groups']->joinTable['inverseJoinColumns'][0]['referencedColumnName']); + $this->assertEquals('INT NULL', $metadata->associationMappings['groups']->joinTable['inverseJoinColumns'][0]['columnDefinition']); + + $this->assertTrue($metadata->associationMappings['groups']->isCascadeRemove); + $this->assertTrue($metadata->associationMappings['groups']->isCascadePersist); + $this->assertTrue($metadata->associationMappings['groups']->isCascadeRefresh); + $this->assertTrue($metadata->associationMappings['groups']->isCascadeMerge); + $this->assertTrue($metadata->associationMappings['groups']->isCascadeDetach); + + return $metadata; + } + + /** + * @depends testManyToManyAssociationsAreExported + * @param ClassMetadataInfo $metadata + */ + public function testLifecycleCallbacksAreExported($metadata) + { + $this->assertTrue(isset($metadata->lifecycleCallbacks['prePersist'])); + $this->assertEquals(2, count($metadata->lifecycleCallbacks['prePersist'])); + $this->assertEquals('doStuffOnPrePersist', $metadata->lifecycleCallbacks['prePersist'][0]); + $this->assertEquals('doOtherStuffOnPrePersistToo', $metadata->lifecycleCallbacks['prePersist'][1]); + + $this->assertTrue(isset($metadata->lifecycleCallbacks['postPersist'])); + $this->assertEquals(1, count($metadata->lifecycleCallbacks['postPersist'])); + $this->assertEquals('doStuffOnPostPersist', $metadata->lifecycleCallbacks['postPersist'][0]); + + return $metadata; + } + + public function __destruct() + { + $type = $this->_getType(); + $this->_deleteDirectory(__DIR__ . '/export/'.$this->_getType()); + } + + protected function _deleteDirectory($path) + { + if (is_file($path)) { + return unlink($path); + } else if (is_dir($path)) { + $files = glob(rtrim($path,'/').'/*'); + foreach ($files as $file){ + $this->_deleteDirectory($file); + } + return rmdir($path); + } + } +} \ No newline at end of file diff --git a/tests/Doctrine/Tests/ORM/Tools/Export/AnnotationClassMetadataExporterTest.php b/tests/Doctrine/Tests/ORM/Tools/Export/AnnotationClassMetadataExporterTest.php new file mode 100644 index 000000000..c60281cf3 --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Tools/Export/AnnotationClassMetadataExporterTest.php @@ -0,0 +1,42 @@ +. + */ + +namespace Doctrine\Tests\ORM\Tools\Export; + +require_once __DIR__ . '/../../../TestInit.php'; + +/** + * Test case for AnnotationClassMetadataExporterTest + * + * @author Jonathan H. Wage + * @author Roman Borschel . - */ - -namespace Doctrine\Tests\ORM\Tools\Export; - -use Doctrine\ORM\Tools\Export\ClassMetadataExporter; - -require_once __DIR__ . '/../../../TestInit.php'; - -/** - * Test case for ClassMetadataExporter - * - * @author Jonathan H. Wage - * @author Roman Borschel getExporter('xml'); - $this->assertTrue($exporter instanceof \Doctrine\ORM\Tools\Export\Driver\XmlExporter); - - $exporter = $cme->getExporter('yml'); - $this->assertTrue($exporter instanceof \Doctrine\ORM\Tools\Export\Driver\YamlExporter); - - $exporter = $cme->getExporter('annotation'); - $this->assertTrue($exporter instanceof \Doctrine\ORM\Tools\Export\Driver\AnnotationExporter); - } - - /** - * Test that we can add mapping directories for the different types of - * mapping information. - */ - public function testAddMappingDirectory() - { - $cme = new ClassMetadataExporter(); - $cme->addMappingSource(__DIR__ . '/annotation', 'annotation'); - $cme->addMappingSource(__DIR__ . '/php', 'php'); - $cme->addMappingSource(__DIR__ . '/xml', 'xml'); - $cme->addMappingSource(__DIR__ . '/yml', 'yml'); - - $mappingSources = $cme->getMappingSources(); - $this->assertEquals(4, count($mappingSources)); - - $this->assertEquals($mappingSources[0][0], __DIR__.'/annotation'); - $this->assertTrue($mappingSources[0][1] instanceof \Doctrine\ORM\Mapping\Driver\AnnotationDriver); - - $this->assertEquals($mappingSources[1][0], __DIR__.'/php'); - $this->assertTrue($mappingSources[1][1] instanceof \Doctrine\ORM\Mapping\Driver\PhpDriver); - - $this->assertEquals($mappingSources[2][0], __DIR__.'/xml'); - $this->assertTrue($mappingSources[2][1] instanceof \Doctrine\ORM\Mapping\Driver\XmlDriver); - - $this->assertEquals($mappingSources[3][0], __DIR__.'/yml'); - $this->assertTrue($mappingSources[3][1] instanceof \Doctrine\ORM\Mapping\Driver\YamlDriver); - } - - /** - * Test that we can add mapping directories then retrieve all the defined - * ClassMetadata instances that are defined in the directories - */ - public function testGetMetadataInstances() - { - $cme = new ClassMetadataExporter(); - $cme->addMappingSource(__DIR__ . '/php', 'php'); - $cme->addMappingSource(__DIR__ . '/xml', 'xml'); - $cme->addMappingSource(__DIR__ . '/yml', 'yml'); - - $metadataInstances = $cme->getMetadatasForMappingSources(); - - $this->assertEquals(3, count($metadataInstances)); - $this->assertEquals('PhpTest', $metadataInstances['PhpTest']->name); - $this->assertEquals('XmlTest', $metadataInstances['XmlTest']->name); - $this->assertEquals('YmlTest', $metadataInstances['YmlTest']->name); - } - - /** - * Test that we can export mapping directories to another format and that - * the exported data can then be read back in properly. - */ - public function testExport() - { - $exportDir = __DIR__ . '/export'; - - if ( ! is_dir($exportDir)) { - mkdir($exportDir, 0777, true); - } - - $types = array('annotation', 'php', 'xml', 'yml'); - - $cme = new ClassMetadataExporter(); - $cme->addMappingSource(__DIR__ . '/php', 'php'); - $cme->addMappingSource(__DIR__ . '/xml', 'xml'); - $cme->addMappingSource(__DIR__ . '/yml', 'yml'); - - foreach ($types as $type) { - // Export the above mapping directories to the type - $exporter = $cme->getExporter($type, __DIR__ . '/export/' . $type); - $exporter->setMetadatas($cme->getMetadatasForMappingSources()); - $exporter->export(); - - // Make sure the files were written - $this->assertTrue(file_exists(__DIR__ . '/export/' . $type . '/PhpTest'.$exporter->getExtension())); - $this->assertTrue(file_exists(__DIR__ . '/export/' . $type . '/XmlTest'.$exporter->getExtension())); - $this->assertTrue(file_exists(__DIR__ . '/export/' . $type . '/YmlTest'.$exporter->getExtension())); - - // Try and read back in the exported mapping files to make sure they are valid - $cme2 = new ClassMetadataExporter(); - $cme2->addMappingSource(__DIR__ . '/export/' . $type, $type); - $metadataInstances = $cme2->getMetadatasForMappingSources(); - $this->assertEquals(3, count($metadataInstances)); - $this->assertEquals('PhpTest', $metadataInstances['PhpTest']->name); - $this->assertEquals('XmlTest', $metadataInstances['XmlTest']->name); - $this->assertEquals('YmlTest', $metadataInstances['YmlTest']->name); - - // Cleanup - unlink(__DIR__ . '/export/' . $type . '/PhpTest'.$exporter->getExtension()); - unlink(__DIR__ . '/export/' . $type . '/XmlTest'.$exporter->getExtension()); - unlink(__DIR__ . '/export/' . $type . '/YmlTest'.$exporter->getExtension()); - rmdir(__DIR__ . '/export/'.$type); - } - rmdir(__DIR__ . '/export'); - } -} \ No newline at end of file diff --git a/tests/Doctrine/Tests/ORM/Tools/Export/PhpClassMetadataExporterTEst.php b/tests/Doctrine/Tests/ORM/Tools/Export/PhpClassMetadataExporterTEst.php new file mode 100644 index 000000000..a9d443a9d --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Tools/Export/PhpClassMetadataExporterTEst.php @@ -0,0 +1,42 @@ +. + */ + +namespace Doctrine\Tests\ORM\Tools\Export; + +require_once __DIR__ . '/../../../TestInit.php'; + +/** + * Test case for PhpClassMetadataExporterTest + * + * @author Jonathan H. Wage + * @author Roman Borschel . + */ + +namespace Doctrine\Tests\ORM\Tools\Export; + +require_once __DIR__ . '/../../../TestInit.php'; + +/** + * Test case for XmlClassMetadataExporterTest + * + * @author Jonathan H. Wage + * @author Roman Borschel . + */ + +namespace Doctrine\Tests\ORM\Tools\Export; + +require_once __DIR__ . '/../../../TestInit.php'; + +/** + * Test case for YamlClassMetadataExporterTest + * + * @author Jonathan H. Wage + * @author Roman Borschel id = $value; - } - - /** - * Get id - */ - public function getId() - { - return $this->id; - } - - /** - * Set name - */ - public function setName($value) - { - $this->name = $value; - } - - /** - * Get name - */ - public function getName() - { - return $this->name; - } -} \ No newline at end of file diff --git a/tests/Doctrine/Tests/ORM/Tools/Export/annotation/Doctrine.Tests.ORM.Tools.Export.User.php b/tests/Doctrine/Tests/ORM/Tools/Export/annotation/Doctrine.Tests.ORM.Tools.Export.User.php new file mode 100644 index 000000000..25f6fdd7b --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Tools/Export/annotation/Doctrine.Tests.ORM.Tools.Export.User.php @@ -0,0 +1,68 @@ +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\\Tools\\Export\\Address', + 'cascade' => + array( + 0 => 'remove', + ), + 'mappedBy' => NULL, + 'joinColumns' => + array( + 0 => + array( + 'name' => 'address_id', + 'referencedColumnName' => 'id', + 'onDelete' => 'CASCADE', + 'onUpdate' => 'CASCADE' + ), + ), + 'orphanRemoval' => false, + )); +$metadata->mapOneToMany(array( + 'fieldName' => 'phonenumbers', + 'targetEntity' => 'Doctrine\\Tests\\ORM\\Tools\\Export\\Phonenumber', + 'cascade' => + array( + 1 => 'persist', + ), + 'mappedBy' => 'user', + 'orphanRemoval' => false, + 'orderBy' => + array( + 'number' => 'ASC', + ), + )); +$metadata->mapManyToMany(array( + 'fieldName' => 'groups', + 'targetEntity' => 'Doctrine\\Tests\\ORM\\Tools\\Export\\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, + )); \ No newline at end of file diff --git a/tests/Doctrine/Tests/ORM/Tools/Export/php/PhpTest.php b/tests/Doctrine/Tests/ORM/Tools/Export/php/PhpTest.php deleted file mode 100644 index be1b84309..000000000 --- a/tests/Doctrine/Tests/ORM/Tools/Export/php/PhpTest.php +++ /dev/null @@ -1,23 +0,0 @@ -setInheritanceType(ClassMetadataInfo::INHERITANCE_TYPE_NONE); -$metadata->setPrimaryTable(array( - 'name' => 'php_test', - )); -$metadata->setChangeTrackingPolicy(ClassMetadataInfo::CHANGETRACKING_DEFERRED_IMPLICIT); -$metadata->mapField(array( - 'id' => true, - 'fieldName' => 'id', - 'type' => 'integer', - 'columnName' => 'id', - )); -$metadata->mapField(array( - 'fieldName' => 'name', - 'type' => 'string', - 'length' => 50, - 'columnName' => 'name', - )); -$metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_AUTO); \ No newline at end of file diff --git a/tests/Doctrine/Tests/ORM/Tools/Export/xml/Doctrine.Tests.ORM.Tools.Export.User.dcm.xml b/tests/Doctrine/Tests/ORM/Tools/Export/xml/Doctrine.Tests.ORM.Tools.Export.User.dcm.xml new file mode 100644 index 000000000..e8444e738 --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Tools/Export/xml/Doctrine.Tests.ORM.Tools.Export.User.dcm.xml @@ -0,0 +1,53 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/Doctrine/Tests/ORM/Tools/Export/xml/XmlTest.dcm.xml b/tests/Doctrine/Tests/ORM/Tools/Export/xml/XmlTest.dcm.xml deleted file mode 100644 index 17be3e308..000000000 --- a/tests/Doctrine/Tests/ORM/Tools/Export/xml/XmlTest.dcm.xml +++ /dev/null @@ -1,2 +0,0 @@ - -DEFERRED_IMPLICIT diff --git a/tests/Doctrine/Tests/ORM/Tools/Export/yaml/Doctrine.Tests.ORM.Tools.Export.User.dcm.yml b/tests/Doctrine/Tests/ORM/Tools/Export/yaml/Doctrine.Tests.ORM.Tools.Export.User.dcm.yml new file mode 100644 index 000000000..d60f41026 --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Tools/Export/yaml/Doctrine.Tests.ORM.Tools.Export.User.dcm.yml @@ -0,0 +1,53 @@ +Doctrine\Tests\ORM\Tools\Export\User: + type: entity + table: cms_users + id: + id: + type: integer + generator: + strategy: AUTO + fields: + name: + type: string + length: 50 + nullable: true + unique: true + email: + type: string + column: user_email + columnDefinition: CHAR(32) NOT NULL + oneToOne: + address: + targetEntity: Doctrine\Tests\ORM\Tools\Export\Address + joinColumn: + name: address_id + referencedColumnName: id + onDelete: CASCADE + onUpdate: CASCADE + cascade: [ remove ] + oneToMany: + phonenumbers: + targetEntity: Doctrine\Tests\ORM\Tools\Export\Phonenumber + mappedBy: user + orderBy: + number: ASC + cascade: [ persist ] + manyToMany: + groups: + targetEntity: Doctrine\Tests\ORM\Tools\Export\Group + joinTable: + name: cms_users_groups + joinColumns: + user_id: + referencedColumnName: id + nullable: false + unique: false + inverseJoinColumns: + group_id: + referencedColumnName: id + columnDefinition: INT NULL + cascade: + - all + lifecycleCallbacks: + prePersist: [ doStuffOnPrePersist, doOtherStuffOnPrePersistToo ] + postPersist: [ doStuffOnPostPersist ] \ No newline at end of file diff --git a/tests/Doctrine/Tests/ORM/Tools/Export/yml/YmlTest.dcm.yml b/tests/Doctrine/Tests/ORM/Tools/Export/yml/YmlTest.dcm.yml deleted file mode 100644 index be69e80fa..000000000 --- a/tests/Doctrine/Tests/ORM/Tools/Export/yml/YmlTest.dcm.yml +++ /dev/null @@ -1,12 +0,0 @@ -YmlTest: - type: entity - table: yml_test - id: - id: - type: integer - generator: - strategy: AUTO - fields: - name: - type: string - length: 50 \ No newline at end of file