From 10bc51fdcdccf1bdd1868afc9e66fb03ebcd662c Mon Sep 17 00:00:00 2001 From: romanb Date: Fri, 6 Nov 2009 10:28:37 +0000 Subject: [PATCH] [2.0][DDC-116] Fixed. --- lib/Doctrine/ORM/Id/Assigned.php | 17 +++-- tests/Doctrine/Tests/ORM/Id/AllTests.php | 1 + .../Doctrine/Tests/ORM/Id/AssignedIdTest.php | 67 +++++++++++++++++++ 3 files changed, 76 insertions(+), 9 deletions(-) create mode 100644 tests/Doctrine/Tests/ORM/Id/AssignedIdTest.php diff --git a/lib/Doctrine/ORM/Id/Assigned.php b/lib/Doctrine/ORM/Id/Assigned.php index 3eb64c34e..dfd3cf0d4 100644 --- a/lib/Doctrine/ORM/Id/Assigned.php +++ b/lib/Doctrine/ORM/Id/Assigned.php @@ -29,6 +29,7 @@ use Doctrine\ORM\ORMException; * * @since 2.0 * @author Roman Borschel + * @todo Rename: AssignedGenerator? */ class Assigned extends AbstractIdGenerator { @@ -42,28 +43,26 @@ class Assigned extends AbstractIdGenerator public function generate(EntityManager $em, $entity) { $class = $em->getClassMetadata(get_class($entity)); - $identifier = null; - if ($class->isIdentifierComposite()) { - $identifier = array(); + $identifier = array(); + if ($class->isIdentifierComposite) { $idFields = $class->getIdentifierFieldNames(); foreach ($idFields as $idField) { - $identifier[] = $value = $class->getReflectionProperty($idField)->getValue($entity); if (isset($value)) { $identifier[] = $value; + } else { + throw ORMException::entityMissingAssignedId($entity); } } } else { $value = $class->getReflectionProperty($class->getSingleIdentifierFieldName()) ->getValue($entity); if (isset($value)) { - $identifier = array($value); + $identifier[] = $value; + } else { + throw ORMException::entityMissingAssignedId($entity); } } - - if ( ! $identifier) { - throw ORMException::entityMissingAssignedId($entity); - } return $identifier; } diff --git a/tests/Doctrine/Tests/ORM/Id/AllTests.php b/tests/Doctrine/Tests/ORM/Id/AllTests.php index a9826ff12..9a7c7dfb1 100644 --- a/tests/Doctrine/Tests/ORM/Id/AllTests.php +++ b/tests/Doctrine/Tests/ORM/Id/AllTests.php @@ -20,6 +20,7 @@ class AllTests $suite = new \Doctrine\Tests\DoctrineTestSuite('Doctrine Orm Id'); $suite->addTestSuite('Doctrine\Tests\ORM\Id\SequenceGeneratorTest'); + $suite->addTestSuite('Doctrine\Tests\ORM\Id\AssignedIdTest'); return $suite; } diff --git a/tests/Doctrine/Tests/ORM/Id/AssignedIdTest.php b/tests/Doctrine/Tests/ORM/Id/AssignedIdTest.php new file mode 100644 index 000000000..2c09bb36e --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Id/AssignedIdTest.php @@ -0,0 +1,67 @@ +_em = $this->_getTestEntityManager(); + $this->_assignedGen = new Assigned; + } + + public function testThrowsExceptionIfIdNotAssigned() + { + try { + $entity = new AssignedSingleIdEntity; + $this->_assignedGen->generate($this->_em, $entity); + $this->fail('Assigned generator did not throw exception even though ID was missing.'); + } catch (\Doctrine\ORM\ORMException $expected) {} + + try { + $entity = new AssignedCompositeIdEntity; + $this->_assignedGen->generate($this->_em, $entity); + $this->fail('Assigned generator did not throw exception even though ID was missing.'); + } catch (\Doctrine\ORM\ORMException $expected) {} + } + + public function testCorrectIdGeneration() + { + $entity = new AssignedSingleIdEntity; + $entity->myId = 1; + $id = $this->_assignedGen->generate($this->_em, $entity); + $this->assertEquals(array(1), $id); + + $entity = new AssignedCompositeIdEntity; + $entity->myId2 = 2; + $entity->myId1 = 4; + $id = $this->_assignedGen->generate($this->_em, $entity); + $this->assertEquals(array(4, 2), $id); + } +} + +/** @Entity */ +class AssignedSingleIdEntity { + /** @Id @Column(type="integer") */ + public $myId; +} + +/** @Entity */ +class AssignedCompositeIdEntity { + /** @Id @Column(type="integer") */ + public $myId1; + /** @Id @Column(type="integer") */ + public $myId2; +}