From 5b18718b925c74c80ca13e72341c08ddcfe6fb0c Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Sun, 1 Apr 2012 11:01:41 +0200 Subject: [PATCH] [DDC-1746] Throw exception on invalid cascade option. --- lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php | 6 ++++++ lib/Doctrine/ORM/Mapping/MappingException.php | 9 +++++++++ .../Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php | 12 ++++++++++++ 3 files changed, 27 insertions(+) diff --git a/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php b/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php index fa1198061..b36b7121e 100644 --- a/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php +++ b/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php @@ -1210,6 +1210,12 @@ class ClassMetadataInfo implements ClassMetadata $cascades = array('remove', 'persist', 'refresh', 'merge', 'detach'); } + if (count($cascades) !== count(array_intersect($cascades, array('remove', 'persist', 'refresh', 'merge', 'detach')))) { + throw MappingException::invalidCascadeOption( + array_diff($cascades, array_intersect($cascades, array('remove', 'persist', 'refresh', 'merge', 'detach'))) + ); + } + $mapping['cascade'] = $cascades; $mapping['isCascadeRemove'] = in_array('remove', $cascades); $mapping['isCascadePersist'] = in_array('persist', $cascades); diff --git a/lib/Doctrine/ORM/Mapping/MappingException.php b/lib/Doctrine/ORM/Mapping/MappingException.php index 60cbc5506..3e8022280 100644 --- a/lib/Doctrine/ORM/Mapping/MappingException.php +++ b/lib/Doctrine/ORM/Mapping/MappingException.php @@ -354,4 +354,13 @@ class MappingException extends \Doctrine\ORM\ORMException { return new self("The target-entity " . $targetEntity . " cannot be found in '" . $sourceEntity."#".$associationName."'."); } + + public static function invalidCascadeOption(array $cascades) + { + $cascades = implode(", ", array_map(function ($e) { return "'" . $e . "'"; }, $cascades)); + return new self( + "Invalid cascade option(s) specified: " . $cascades . ". " . + "Only 'remove', 'persist', 'refresh', 'merge' and 'detach' are allowed." + ); + } } diff --git a/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php b/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php index 8f079e789..4d7ec352f 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php @@ -637,6 +637,18 @@ class ClassMetadataTest extends \Doctrine\Tests\OrmTestCase $this->assertEquals('cms_cmsaddress_cms_cmsuser', $addressMetadata->associationMappings['user']['joinTable']['name']); $this->assertEquals('doctrineglobal_article_cms_cmsuser', $articleMetadata->associationMappings['author']['joinTable']['name']); } + + /** + * @group DDC-1746 + */ + public function testInvalidCascade() + { + $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser'); + $cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService); + + $this->setExpectedException("Doctrine\ORM\Mapping\MappingException", "Invalid cascade option(s) specified: 'invalid'. Only 'remove', 'persist', 'refresh', 'merge' and 'detach' are allowed."); + $cm->mapManyToOne(array('fieldName' => 'address', 'targetEntity' => 'UnknownClass', 'cascade' => array('invalid'))); + } } class MyNamespacedNamingStrategy extends \Doctrine\ORM\Mapping\DefaultNamingStrategy