From 51881fed942e1e2d7b52a1213ed3b770d84e3b2f Mon Sep 17 00:00:00 2001 From: Guido Contreras Woda Date: Tue, 23 Dec 2014 10:16:07 -0300 Subject: [PATCH] Added tests and validation on joinColumns being set before checking if its a composite key. --- .../ORM/Mapping/ClassMetadataInfo.php | 4 + .../ORM/Mapping/ClassMetadataBuilderTest.php | 161 +++++++++++++++++- 2 files changed, 164 insertions(+), 1 deletion(-) diff --git a/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php b/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php index c3c8d8c83..a93f46a74 100644 --- a/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php +++ b/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php @@ -1461,6 +1461,10 @@ class ClassMetadataInfo implements ClassMetadata } if ( ! in_array($mapping['fieldName'], $this->identifier)) { + if (!isset($mapping['joinColumns'])) { + throw MappingException::illegalInverseIdentifierAssociation($this->name, $mapping['fieldName']); + } + if (count($mapping['joinColumns']) >= 2) { throw MappingException::cannotMapCompositePrimaryKeyEntitiesAsForeignId( $mapping['targetEntity'], $this->name, $mapping['fieldName'] diff --git a/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataBuilderTest.php b/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataBuilderTest.php index ad236b2d9..a3ce514bb 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataBuilderTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataBuilderTest.php @@ -332,6 +332,67 @@ class ClassMetadataBuilderTest extends \Doctrine\Tests\OrmTestCase ), $this->cm->associationMappings); } + public function testIdentityOnCreateManyToOne() + { + $this->assertIsFluent( + $this->builder->createManyToOne('groups', 'Doctrine\Tests\Models\CMS\CmsGroup') + ->addJoinColumn('group_id', 'id', true, false, 'CASCADE') + ->cascadeAll() + ->fetchExtraLazy() + ->isPrimaryKey() + ->build() + ); + + $this->assertEquals(array('groups' => array ( + 'fieldName' => 'groups', + 'targetEntity' => 'Doctrine\\Tests\\Models\\CMS\\CmsGroup', + 'cascade' => array ( + 0 => 'remove', + 1 => 'persist', + 2 => 'refresh', + 3 => 'merge', + 4 => 'detach', + ), + 'fetch' => 4, + 'joinColumns' => array ( + 0 => + array ( + 'name' => 'group_id', + 'referencedColumnName' => 'id', + 'nullable' => true, + 'unique' => false, + 'onDelete' => 'CASCADE', + 'columnDefinition' => NULL, + ), + ), + 'type' => 2, + 'mappedBy' => NULL, + 'inversedBy' => NULL, + 'isOwningSide' => true, + 'sourceEntity' => 'Doctrine\\Tests\\Models\\CMS\\CmsUser', + 'isCascadeRemove' => true, + 'isCascadePersist' => true, + 'isCascadeRefresh' => true, + 'isCascadeMerge' => true, + 'isCascadeDetach' => true, + 'sourceToTargetKeyColumns' => + array ( + 'group_id' => 'id', + ), + 'joinColumnFieldNames' => + array ( + 'group_id' => 'group_id', + ), + 'targetToSourceKeyColumns' => + array ( + 'id' => 'group_id', + ), + 'orphanRemoval' => false, + 'id' => true + ), + ), $this->cm->associationMappings); + } + public function testCreateOneToOne() { $this->assertIsFluent( @@ -386,11 +447,83 @@ class ClassMetadataBuilderTest extends \Doctrine\Tests\OrmTestCase array ( 'id' => 'group_id', ), - 'orphanRemoval' => false, + 'orphanRemoval' => false ), ), $this->cm->associationMappings); } + public function testCreateOneToOneWithIdentity() + { + $this->assertIsFluent( + $this->builder->createOneToOne('groups', 'Doctrine\Tests\Models\CMS\CmsGroup') + ->addJoinColumn('group_id', 'id', true, false, 'CASCADE') + ->cascadeAll() + ->fetchExtraLazy() + ->isPrimaryKey() + ->build() + ); + + $this->assertEquals(array('groups' => array ( + 'fieldName' => 'groups', + 'targetEntity' => 'Doctrine\\Tests\\Models\\CMS\\CmsGroup', + 'cascade' => array ( + 0 => 'remove', + 1 => 'persist', + 2 => 'refresh', + 3 => 'merge', + 4 => 'detach', + ), + 'fetch' => 4, + 'id' => true, + 'joinColumns' => array ( + 0 => + array ( + 'name' => 'group_id', + 'referencedColumnName' => 'id', + 'nullable' => true, + 'unique' => false, + 'onDelete' => 'CASCADE', + 'columnDefinition' => NULL, + ), + ), + 'type' => 1, + 'mappedBy' => NULL, + 'inversedBy' => NULL, + 'isOwningSide' => true, + 'sourceEntity' => 'Doctrine\\Tests\\Models\\CMS\\CmsUser', + 'isCascadeRemove' => true, + 'isCascadePersist' => true, + 'isCascadeRefresh' => true, + 'isCascadeMerge' => true, + 'isCascadeDetach' => true, + 'sourceToTargetKeyColumns' => + array ( + 'group_id' => 'id', + ), + 'joinColumnFieldNames' => + array ( + 'group_id' => 'group_id', + ), + 'targetToSourceKeyColumns' => + array ( + 'id' => 'group_id', + ), + 'orphanRemoval' => false + ), + ), $this->cm->associationMappings); + } + + public function testDisallowCreateOneToOneWithIdentityOnInverseSide() + { + $this->setExpectedException('Doctrine\ORM\Mapping\MappingException'); + + $this->builder->createOneToOne('groups', 'Doctrine\Tests\Models\CMS\CmsGroup') + ->mappedBy('test') + ->fetchExtraLazy() + ->isPrimaryKey() + ->build(); + } + public function testCreateManyToMany() { $this->assertIsFluent( @@ -474,6 +607,20 @@ class ClassMetadataBuilderTest extends \Doctrine\Tests\OrmTestCase ), $this->cm->associationMappings); } + public function testDisallowIdentityOnCreateManyToMany() + { + $this->setExpectedException('Doctrine\ORM\Mapping\MappingException'); + + $this->builder->createManyToMany('groups', 'Doctrine\Tests\Models\CMS\CmsGroup') + ->isPrimaryKey() + ->setJoinTable('groups_users') + ->addJoinColumn('group_id', 'id', true, false, 'CASCADE') + ->addInverseJoinColumn('user_id', 'id') + ->cascadeAll() + ->fetchExtraLazy() + ->build(); + } + public function testCreateOneToMany() { $this->assertIsFluent( @@ -513,6 +660,18 @@ class ClassMetadataBuilderTest extends \Doctrine\Tests\OrmTestCase ), $this->cm->associationMappings); } + public function testDisallowIdentityOnCreateOneToMany() + { + $this->setExpectedException('Doctrine\ORM\Mapping\MappingException'); + + $this->builder->createOneToMany('groups', 'Doctrine\Tests\Models\CMS\CmsGroup') + ->isPrimaryKey() + ->mappedBy('test') + ->setOrderBy(array('test')) + ->setIndexBy('test') + ->build(); + } + public function assertIsFluent($ret) { $this->assertSame($this->builder, $ret, "Return Value has to be same instance as used builder");