From 703ba989be8a63261fa44445af0adbeabdf5c163 Mon Sep 17 00:00:00 2001 From: beberlei Date: Tue, 2 Feb 2010 21:17:00 +0000 Subject: [PATCH] [2.0] DDC-268 - Exchanged DoctrineException for MappingException and added missing exception method (thanks to Christian Heinrich for the patch) --- lib/Doctrine/ORM/Mapping/ClassMetadata.php | 2 - .../ORM/Mapping/ClassMetadataInfo.php | 6 +-- .../ORM/Mapping/Driver/AnnotationDriver.php | 9 ++-- .../ORM/Mapping/Driver/DatabaseDriver.php | 3 +- lib/Doctrine/ORM/Mapping/Driver/PhpDriver.php | 3 +- lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php | 4 +- .../ORM/Mapping/Driver/YamlDriver.php | 9 ++-- lib/Doctrine/ORM/Mapping/MappingException.php | 35 ++++++++++++- tests/Doctrine/Tests/ORM/Mapping/AllTests.php | 1 + .../ORM/Mapping/AnnotationDriverTest.php | 49 +++++++++++++++++++ .../Mapping/BasicInheritanceMappingTest.php | 2 +- .../Tests/ORM/Mapping/ClassMetadataTest.php | 24 +++++++++ 12 files changed, 125 insertions(+), 22 deletions(-) create mode 100644 tests/Doctrine/Tests/ORM/Mapping/AnnotationDriverTest.php diff --git a/lib/Doctrine/ORM/Mapping/ClassMetadata.php b/lib/Doctrine/ORM/Mapping/ClassMetadata.php index 943987b95..7b3336524 100644 --- a/lib/Doctrine/ORM/Mapping/ClassMetadata.php +++ b/lib/Doctrine/ORM/Mapping/ClassMetadata.php @@ -21,8 +21,6 @@ namespace Doctrine\ORM\Mapping; -use Doctrine\Common\DoctrineException; - /** * A ClassMetadata instance holds all the object-relational mapping metadata * of an entity and it's associations. diff --git a/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php b/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php index c82c4cc25..2b981f384 100644 --- a/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php +++ b/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php @@ -21,8 +21,6 @@ namespace Doctrine\ORM\Mapping; -use Doctrine\Common\DoctrineException; - /** * A ClassMetadata instance holds all the object-relational mapping metadata * of an entity and it's associations. @@ -740,7 +738,7 @@ class ClassMetadataInfo public function getSingleIdentifierFieldName() { if ($this->isIdentifierComposite) { - throw DoctrineException::singleIdNotAllowedOnCompositePrimaryKey($this->name); + throw MappingException::singleIdNotAllowedOnCompositePrimaryKey($this->name); } return $this->identifier[0]; } @@ -1623,7 +1621,7 @@ class ClassMetadataInfo } else if ($mapping['type'] == 'datetime') { $mapping['default'] = 'CURRENT_TIMESTAMP'; } else { - throw DoctrineException::unsupportedOptimisticLockingType($this->name, $mapping['fieldName'], $mapping['type']); + throw MappingException::unsupportedOptimisticLockingType($this->name, $mapping['fieldName'], $mapping['type']); } } } diff --git a/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php b/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php index 7d123be02..d8d14b546 100644 --- a/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php +++ b/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php @@ -21,8 +21,7 @@ namespace Doctrine\ORM\Mapping\Driver; -use Doctrine\Common\DoctrineException, - Doctrine\Common\Cache\ArrayCache, +use Doctrine\Common\Cache\ArrayCache, Doctrine\Common\Annotations\AnnotationReader, Doctrine\ORM\Mapping\ClassMetadataInfo, Doctrine\ORM\Mapping\MappingException; @@ -137,7 +136,7 @@ class AnnotationDriver implements Driver } else if (isset($classAnnotations['Doctrine\ORM\Mapping\MappedSuperclass'])) { $metadata->isMappedSuperclass = true; } else { - throw DoctrineException::classIsNotAValidEntityOrMappedSuperClass($className); + throw MappingException::classIsNotAValidEntityOrMappedSuperClass($className); } // Evaluate DoctrineTable annotation @@ -239,7 +238,7 @@ class AnnotationDriver implements Driver // @Column, @OneToOne, @OneToMany, @ManyToOne, @ManyToMany if ($columnAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\Column')) { if ($columnAnnot->type == null) { - throw DoctrineException::propertyTypeIsRequired($property->getName()); + throw MappingException::propertyTypeIsRequired($className, $property->getName()); } $mapping['type'] = $columnAnnot->type; @@ -282,7 +281,7 @@ class AnnotationDriver implements Driver 'initialValue' => $seqGeneratorAnnot->initialValue )); } else if ($tblGeneratorAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\TableGenerator')) { - throw DoctrineException::tableIdGeneratorNotImplemented(); + throw MappingException::tableIdGeneratorNotImplemented($className); } } else if ($oneToOneAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\OneToOne')) { $mapping['targetEntity'] = $oneToOneAnnot->targetEntity; diff --git a/lib/Doctrine/ORM/Mapping/Driver/DatabaseDriver.php b/lib/Doctrine/ORM/Mapping/Driver/DatabaseDriver.php index 5b32482f7..464b14638 100644 --- a/lib/Doctrine/ORM/Mapping/Driver/DatabaseDriver.php +++ b/lib/Doctrine/ORM/Mapping/Driver/DatabaseDriver.php @@ -21,8 +21,7 @@ namespace Doctrine\ORM\Mapping\Driver; -use Doctrine\Common\DoctrineException, - Doctrine\Common\Cache\ArrayCache, +use Doctrine\Common\Cache\ArrayCache, Doctrine\Common\Annotations\AnnotationReader, Doctrine\DBAL\Schema\AbstractSchemaManager, Doctrine\ORM\Mapping\ClassMetadataInfo, diff --git a/lib/Doctrine/ORM/Mapping/Driver/PhpDriver.php b/lib/Doctrine/ORM/Mapping/Driver/PhpDriver.php index 8de3cbad3..8be5c031f 100644 --- a/lib/Doctrine/ORM/Mapping/Driver/PhpDriver.php +++ b/lib/Doctrine/ORM/Mapping/Driver/PhpDriver.php @@ -21,8 +21,7 @@ namespace Doctrine\ORM\Mapping\Driver; -use Doctrine\Common\DoctrineException, - Doctrine\Common\Cache\ArrayCache, +use Doctrine\Common\Cache\ArrayCache, Doctrine\Common\Annotations\AnnotationReader, Doctrine\DBAL\Schema\AbstractSchemaManager, Doctrine\ORM\Mapping\ClassMetadataInfo, diff --git a/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php b/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php index 5c2aa07ba..16bb8d74c 100644 --- a/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php +++ b/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php @@ -57,7 +57,7 @@ class XmlDriver extends AbstractFileDriver } else if ($xmlRoot->getName() == 'mapped-superclass') { $metadata->isMappedSuperclass = true; } else { - throw DoctrineException::classIsNotAValidEntityOrMapperSuperClass($className); + throw MappingException::classIsNotAValidEntityOrMapperSuperClass($className); } // Evaluate attributes @@ -200,7 +200,7 @@ class XmlDriver extends AbstractFileDriver 'initialValue' => $seqGeneratorAnnot->{'initial-value'} )); } else if (isset($idElement->{'table-generator'})) { - throw DoctrineException::tableIdGeneratorNotImplemented(); + throw MappingException::tableIdGeneratorNotImplemented($className); } } diff --git a/lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php b/lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php index 4f1c0261b..f8b67f793 100644 --- a/lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php +++ b/lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php @@ -22,7 +22,6 @@ namespace Doctrine\ORM\Mapping\Driver; use Doctrine\ORM\Mapping\ClassMetadataInfo, - Doctrine\Common\DoctrineException, Doctrine\ORM\Mapping\MappingException; if ( ! class_exists('sfYaml', false)) { @@ -65,7 +64,7 @@ class YamlDriver extends AbstractFileDriver } else if ($element['type'] == 'mappedSuperclass') { $metadata->isMappedSuperclass = true; } else { - throw DoctrineException::classIsNotAValidEntityOrMapperSuperClass($className); + throw MappingException::classIsNotAValidEntityOrMapperSuperClass($className); } // Evaluate root level properties @@ -165,6 +164,10 @@ class YamlDriver extends AbstractFileDriver // Evaluate fields if (isset($element['fields'])) { foreach ($element['fields'] as $name => $fieldMapping) { + if (!isset($fieldMapping['type'])) { + throw MappingException::propertyTypeIsRequired($className, $name); + } + $e = explode('(', $fieldMapping['type']); $fieldMapping['type'] = $e[0]; if (isset($e[1])) { @@ -185,7 +188,7 @@ class YamlDriver extends AbstractFileDriver if (isset($fieldMapping['sequenceGenerator'])) { $metadata->setSequenceGeneratorDefinition($fieldMapping['sequenceGenerator']); } else if (isset($fieldMapping['tableGenerator'])) { - throw DoctrineException::tableIdGeneratorNotImplemented(); + throw MappingException::tableIdGeneratorNotImplemented($className); } if (isset($fieldMapping['column'])) { $mapping['columnName'] = $fieldMapping['column']; diff --git a/lib/Doctrine/ORM/Mapping/MappingException.php b/lib/Doctrine/ORM/Mapping/MappingException.php index b3524b7bf..fbd834ae8 100644 --- a/lib/Doctrine/ORM/Mapping/MappingException.php +++ b/lib/Doctrine/ORM/Mapping/MappingException.php @@ -127,10 +127,43 @@ class MappingException extends \Doctrine\ORM\ORMException return new self('The column ' . $joinColumn . ' must be mapped to a field in class ' . $className . ' since it is referenced by a join column of another class.'); } + + public static function classIsNotAValidEntityOrMappedSuperClass($className) + { + return new self('Class '.$className.' is not a valid entity or mapped super class.'); + } + + public static function propertyTypeIsRequired($className, $propertyName) + { + return new self("The attribute 'type' is required for the column description of property ".$className."::\$".$propertyName."."); + } + + public static function tableIdGeneratorNotImplemented($className) + { + return new self("TableIdGenerator is not yet implemented for use with class ".$className); + } + + /** + * + * @param string $entity The entity's name + * @param string $fieldName The name of the field that was already declared + */ + public static function duplicateFieldMapping($entity, $fieldName) { + return new self('Property "'.$fieldName.'" in "'.$entity.'" was already declared, but it must be declared only once'); + } + + public static function singleIdNotAllowedOnCompositePrimaryKey($entity) { + return new self('Single id is not allowed on composite primary key in entity '.$entity); + } + + public static function unsupportedOptimisticLockingType($entity, $fieldName, $unsupportedType) { + return new self('Locking type "'.$unsupportedType.'" (specified in "'.$entity.'", field "'.$fieldName.'") ' + .'is not supported by Doctrine.' + ); + } public static function annotationDriverRequiresConfiguredDirectoryPath() { return new self('The annotation driver needs to have a directory path'); } - } \ No newline at end of file diff --git a/tests/Doctrine/Tests/ORM/Mapping/AllTests.php b/tests/Doctrine/Tests/ORM/Mapping/AllTests.php index 78c63b096..b667656eb 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/AllTests.php +++ b/tests/Doctrine/Tests/ORM/Mapping/AllTests.php @@ -22,6 +22,7 @@ class AllTests $suite->addTestSuite('Doctrine\Tests\ORM\Mapping\ClassMetadataTest'); $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\ClassMetadataFactoryTest'); $suite->addTestSuite('Doctrine\Tests\ORM\Mapping\ClassMetadataLoadEventTest'); $suite->addTestSuite('Doctrine\Tests\ORM\Mapping\BasicInheritanceMappingTest'); diff --git a/tests/Doctrine/Tests/ORM/Mapping/AnnotationDriverTest.php b/tests/Doctrine/Tests/ORM/Mapping/AnnotationDriverTest.php new file mode 100644 index 000000000..436b2e824 --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Mapping/AnnotationDriverTest.php @@ -0,0 +1,49 @@ +setExpectedException('Doctrine\ORM\Mapping\MappingException'); + $annotationDriver->loadMetadataForClass('stdClass', $cm); + } + + /** + * @group DDC-268 + */ + public function testColumnWithMissingTypeThrowsException() + { + $cm = new ClassMetadata('Doctrine\Tests\ORM\Mapping\InvalidColumn'); + $reader = new \Doctrine\Common\Annotations\AnnotationReader(new \Doctrine\Common\Cache\ArrayCache()); + $reader->setDefaultAnnotationNamespace('Doctrine\ORM\Mapping\\'); + $annotationDriver = new \Doctrine\ORM\Mapping\Driver\AnnotationDriver($reader); + + $this->setExpectedException('Doctrine\ORM\Mapping\MappingException', + "The attribute 'type' is required for the column description of property Doctrine\\Tests\\ORM\\Mapping\\InvalidColumn::\$id"); + $annotationDriver->loadMetadataForClass('Doctrine\Tests\ORM\Mapping\InvalidColumn', $cm); + } +} + +/** + * @Entity + */ +class InvalidColumn +{ + /** @Id @Column */ + public $id; +} + diff --git a/tests/Doctrine/Tests/ORM/Mapping/BasicInheritanceMappingTest.php b/tests/Doctrine/Tests/ORM/Mapping/BasicInheritanceMappingTest.php index 0b54cdca5..c123a556d 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/BasicInheritanceMappingTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/BasicInheritanceMappingTest.php @@ -15,7 +15,7 @@ class BasicInheritanceMappingTest extends \Doctrine\Tests\OrmTestCase } /** - * @expectedException Doctrine\Common\DoctrineException + * @expectedException Doctrine\ORM\Mapping\MappingException */ public function testGetMetadataForTransientClassThrowsException() { diff --git a/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php b/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php index c86233354..ad4723425 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php @@ -113,4 +113,28 @@ class ClassMetadataTest extends \Doctrine\Tests\OrmTestCase $this->assertEquals("DoctrineGlobal_Article", $cm->subClasses[0]); } + + /** + * @group DDC-268 + */ + public function testSetInvalidVersionMapping_ThrowsException() + { + $field = array(); + $field['fieldName'] = 'foo'; + $field['type'] = 'string'; + + $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser'); + + $this->setExpectedException('Doctrine\ORM\Mapping\MappingException'); + $cm->setVersionMapping($field); + } + + public function testGetSingleIdentifierFieldName_MultipleIdentifierEntity_ThrowsException() + { + $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser'); + $cm->isIdentifierComposite = true; + + $this->setExpectedException('Doctrine\ORM\Mapping\MappingException'); + $cm->getSingleIdentifierFieldName(); + } } \ No newline at end of file