Skip to content

Commit

Permalink
[2.0] Improving test coverage for mapping exporters as well as adding…
Browse files Browse the repository at this point in the history
… missing functionality
  • Loading branch information
jwage committed Mar 17, 2010
1 parent 1d60c65 commit 770d00a
Show file tree
Hide file tree
Showing 22 changed files with 901 additions and 265 deletions.
2 changes: 1 addition & 1 deletion lib/Doctrine/ORM/Mapping/Driver/PhpDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,6 @@ public function loadMetadataForClass($className, ClassMetadataInfo $metadata)
protected function _loadMappingFile($file)
{
$metadata = $this->_metadata;
require_once $file;
include $file;
}
}
2 changes: 1 addition & 1 deletion lib/Doctrine/ORM/Tools/Export/Driver/AbstractExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function setOutputDir($dir)
public function export()
{
if ( ! is_dir($this->_outputDir)) {
mkdir($this->_outputDir, 0777);
mkdir($this->_outputDir, 0777, true);
}

foreach ($this->_metadatas as $metadata) {
Expand Down
73 changes: 68 additions & 5 deletions lib/Doctrine/ORM/Tools/Export/Driver/AnnotationExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,17 +232,40 @@ private function _getNamespace($metadata)

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 . '")';
}

if (isset($metadata->lifecycleCallbacks) && $metadata->lifecycleCallbacks) {
$lines[] = ' * @HasLifecycleCallbacks';
}

return $str;
$lines[] = ' */';
$lines[] = '';

return implode("\n", $lines);
}

private function _getTableAnnotation($metadata)
Expand Down Expand Up @@ -350,6 +373,23 @@ private function _addMethod($type, $fieldName, $metadata, array &$methods)
$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();
Expand Down Expand Up @@ -380,6 +420,14 @@ private function _getMethods($metadata)
}
}

if (isset($metadata->lifecycleCallbacks) && $metadata->lifecycleCallbacks) {
foreach ($metadata->lifecycleCallbacks as $name => $callbacks) {
foreach ($callbacks as $callback) {
$this->_addLifecycleCallbackMethod($name, $callback, $metadata, $methods);
}
}
}

return $methods;
}

Expand All @@ -404,6 +452,9 @@ private function _getJoinColumnAnnotation(array $joinColumn)
if (isset($joinColumn['onUpdate'])) {
$joinColumnAnnot[] = 'onUpdate=' . ($joinColumn['onUpdate'] ? 'true' : 'false');
}
if (isset($joinColumn['columnDefinition'])) {
$joinColumnAnnot[] = 'columnDefinition="' . $joinColumn['columnDefinition'] . '"';
}
return '@JoinColumn(' . implode(', ', $joinColumnAnnot) . ')';
}

Expand Down Expand Up @@ -472,6 +523,15 @@ private function _getAssociationMappingAnnotation(AssociationMapping $associatio
$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);
Expand Down Expand Up @@ -501,6 +561,9 @@ private function _getFieldMappingAnnotation(array $fieldMapping, ClassMetadataIn
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) {
Expand Down
70 changes: 69 additions & 1 deletion lib/Doctrine/ORM/Tools/Export/Driver/XmlExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ public function exportClassMetadata(ClassMetadataInfo $metadata)
if (isset($field['version'])) {
$fieldXml->addAttribute('version', $field['version']);
}
if (isset($field['columnDefinition'])) {
$fieldXml->addAttribute('column-definition', $field['columnDefinition']);
}
}
}

Expand Down Expand Up @@ -204,9 +207,63 @@ public function exportClassMetadata(ClassMetadataInfo $metadata)
$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';
Expand All @@ -231,6 +288,17 @@ public function exportClassMetadata(ClassMetadataInfo $metadata)
}
}

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);
}

Expand Down
35 changes: 28 additions & 7 deletions lib/Doctrine/ORM/Tools/Export/Driver/YamlExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,22 +133,38 @@ public function exportClassMetadata(ClassMetadataInfo $metadata)

$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) {
$joinColumns = $associationMapping->joinColumns;
$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,
Expand All @@ -162,6 +178,7 @@ public function exportClassMetadata(ClassMetadataInfo $metadata)
$oneToManyMappingArray = array(
'mappedBy' => $associationMapping->mappedBy,
'orphanRemoval' => $associationMapping->orphanRemoval,
'orderBy' => $associationMapping->orderBy
);

$associationMappingArray = array_merge($associationMappingArray, $oneToManyMappingArray);
Expand All @@ -170,12 +187,16 @@ public function exportClassMetadata(ClassMetadataInfo $metadata)
$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);
}
Expand Down
10 changes: 2 additions & 8 deletions lib/Doctrine/ORM/Tools/Export/Driver/annotation.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,8 @@
use <?php echo $this->_getClassToExtendNamespace() ?>;
<?php endif; ?>

/**
* <?php echo $this->_getEntityAnnotation($metadata)."\n"; ?>
* <?php echo $this->_getTableAnnotation($metadata)."\n" ?>
* <?php echo $this->_getInheritanceAnnotation($metadata)."\n" ?>
* <?php echo $this->_getDiscriminatorColumnAnnotation($metadata)."\n" ?>
* <?php echo $this->_getDiscriminatorMapAnnotation($metadata)."\n" ?>
*/
class <?php echo $this->_getClassName($metadata); ?><?php if ($this->_extendsClass()): ?> extends <?php echo $this->_getClassToExtendName() ?><?php endif; ?>
<?php echo $this->_getEntityAnnotation($metadata) ?>
class <?php echo $this->_getClassName($metadata); ?><?php if ($this->_extendsClass()): ?> extends <?php echo $this->_getClassToExtendName() ?><?php endif; ?>
{
<?php include('annotation_body.tpl.php') ?>
}
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public function testJoinColumnOnDeleteAndOnUpdate($class)
*/
class User
{
/** @Id @Column(type="int") @generatedValue(strategy="AUTO") */
/** @Id @Column(type="integer") @generatedValue(strategy="AUTO") */
public $id;

/**
Expand Down
5 changes: 4 additions & 1 deletion tests/Doctrine/Tests/ORM/Tools/AllTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ public static function suite()
{
$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');

Expand Down
Loading

0 comments on commit 770d00a

Please sign in to comment.