1
0
mirror of synced 2025-01-18 14:31:40 +03:00

[2.0] DDC-268 - Exchanged DoctrineException for MappingException and added missing exception method (thanks to Christian Heinrich for the patch)

This commit is contained in:
beberlei 2010-02-02 21:17:00 +00:00
parent ee9aa005b2
commit 703ba989be
12 changed files with 125 additions and 22 deletions

View File

@ -21,8 +21,6 @@
namespace Doctrine\ORM\Mapping;
use Doctrine\Common\DoctrineException;
/**
* A <tt>ClassMetadata</tt> instance holds all the object-relational mapping metadata
* of an entity and it's associations.

View File

@ -21,8 +21,6 @@
namespace Doctrine\ORM\Mapping;
use Doctrine\Common\DoctrineException;
/**
* A <tt>ClassMetadata</tt> 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']);
}
}
}

View File

@ -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;

View File

@ -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,

View File

@ -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,

View File

@ -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 <entity...> 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);
}
}

View File

@ -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'];

View File

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

View File

@ -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');

View File

@ -0,0 +1,49 @@
<?php
namespace Doctrine\Tests\ORM\Mapping;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Events;
require_once __DIR__ . '/../../TestInit.php';
class AnnotationDriverTest extends \Doctrine\Tests\OrmTestCase
{
/**
* @group DDC-268
*/
public function testLoadMetadataForNonEntityThrowsException()
{
$cm = new ClassMetadata('stdClass');
$reader = new \Doctrine\Common\Annotations\AnnotationReader(new \Doctrine\Common\Cache\ArrayCache());
$annotationDriver = new \Doctrine\ORM\Mapping\Driver\AnnotationDriver($reader);
$this->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;
}

View File

@ -15,7 +15,7 @@ class BasicInheritanceMappingTest extends \Doctrine\Tests\OrmTestCase
}
/**
* @expectedException Doctrine\Common\DoctrineException
* @expectedException Doctrine\ORM\Mapping\MappingException
*/
public function testGetMetadataForTransientClassThrowsException()
{

View File

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