From f7b95c1aa5ea3436aff1e2281db39beb85914ce7 Mon Sep 17 00:00:00 2001 From: Frank Date: Wed, 9 Apr 2014 01:55:49 +0200 Subject: [PATCH 1/5] Add: invalidDiscriminatorValue method --- .../ORM/Internal/Hydration/HydrationException.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/Doctrine/ORM/Internal/Hydration/HydrationException.php b/lib/Doctrine/ORM/Internal/Hydration/HydrationException.php index c1c13c1f7..b35012c99 100644 --- a/lib/Doctrine/ORM/Internal/Hydration/HydrationException.php +++ b/lib/Doctrine/ORM/Internal/Hydration/HydrationException.php @@ -88,4 +88,18 @@ class HydrationException extends \Doctrine\ORM\ORMException $discrColumnName, $entityName, $dqlAlias )); } + + /** + * @param string $discrValue + * @param array $discrMap + * + * @return HydrationException + */ + public static function invalidDiscriminatorValue($discrValue, $discrMap) + { + return new self(sprintf( + 'The discriminator value "%s" is invalid. It must be one of "%s".', + $discrValue, implode('", "', $discrMap) + )); + } } From d1c722c1d0f537aa832ba21637e8a7494a1aa491 Mon Sep 17 00:00:00 2001 From: Frank Date: Wed, 9 Apr 2014 01:55:54 +0200 Subject: [PATCH 2/5] Fix: handle invalid discriminator value --- .../ORM/Internal/Hydration/SimpleObjectHydrator.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/Doctrine/ORM/Internal/Hydration/SimpleObjectHydrator.php b/lib/Doctrine/ORM/Internal/Hydration/SimpleObjectHydrator.php index eb0982c7d..504d82c51 100644 --- a/lib/Doctrine/ORM/Internal/Hydration/SimpleObjectHydrator.php +++ b/lib/Doctrine/ORM/Internal/Hydration/SimpleObjectHydrator.php @@ -98,7 +98,13 @@ class SimpleObjectHydrator extends AbstractHydrator throw HydrationException::emptyDiscriminatorValue(key($this->_rsm->aliasMap)); } - $entityName = $this->class->discriminatorMap[$sqlResult[$discrColumnName]]; + $discrMap = $this->class->discriminatorMap; + + if ( ! isset($discrMap[$sqlResult[$discrColumnName]])) { + throw HydrationException::invalidDiscriminatorValue($sqlResult[$discrColumnName], array_keys($discrMap)); + } + + $entityName = $discrMap[$sqlResult[$discrColumnName]]; unset($sqlResult[$discrColumnName]); } From a054b595509b6a01fdd74da086148849f022314b Mon Sep 17 00:00:00 2001 From: Frank Liepert Date: Wed, 9 Apr 2014 10:19:48 +0200 Subject: [PATCH 3/5] [DDC-3076] Add test --- .../Hydration/SimpleObjectHydratorTest.php | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tests/Doctrine/Tests/ORM/Hydration/SimpleObjectHydratorTest.php b/tests/Doctrine/Tests/ORM/Hydration/SimpleObjectHydratorTest.php index bd8a8d00d..44823fa51 100644 --- a/tests/Doctrine/Tests/ORM/Hydration/SimpleObjectHydratorTest.php +++ b/tests/Doctrine/Tests/ORM/Hydration/SimpleObjectHydratorTest.php @@ -56,4 +56,31 @@ class SimpleObjectHydratorTest extends HydrationTestCase $result = $hydrator->hydrateAll($stmt, $rsm); $this->assertEquals($result[0], $expectedEntity); } + + /** + * @group DDC-3076 + * + * @expectedException \Doctrine\ORM\Internal\Hydration\HydrationException + * @expectedExceptionMessage The discriminator value "subworker" is invalid. It must be one of "person", "manager", "employee". + */ + public function testInvalidDiscriminatorValueException() + { + $rsm = new ResultSetMapping; + $rsm->addEntityResult('Doctrine\Tests\Models\Company\CompanyPerson', 'p'); + $rsm->addFieldResult('p', 'p__id', 'id'); + $rsm->addFieldResult('p', 'p__name', 'name'); + $rsm->addMetaResult('p ', 'discr', 'discr'); + $rsm->setDiscriminatorColumn('p', 'discr'); + $resultSet = array( + array( + 'u__id' => '1', + 'u__name' => 'Fabio B. Silva', + 'discr' => 'subworker' + ), + ); + + $stmt = new HydratorMockStatement($resultSet); + $hydrator = new \Doctrine\ORM\Internal\Hydration\SimpleObjectHydrator($this->_em); + $hydrator->hydrateAll($stmt, $rsm); + } } From df020e08a0f55edc3a13a38f749b3e2be84ca5a6 Mon Sep 17 00:00:00 2001 From: Frank Liepert Date: Wed, 9 Apr 2014 11:30:11 +0200 Subject: [PATCH 4/5] [DDC-3076] Fix ObjectHydrator --- lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php b/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php index 859bd0b00..823f301b2 100644 --- a/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php +++ b/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php @@ -266,7 +266,13 @@ class ObjectHydrator extends AbstractHydrator throw HydrationException::emptyDiscriminatorValue($dqlAlias); } - $className = $this->ce[$className]->discriminatorMap[$data[$discrColumn]]; + $discrMap = $this->ce[$className]->discriminatorMap; + + if ( ! isset($discrMap[$data[$discrColumn]])) { + throw HydrationException::invalidDiscriminatorValue($data[$discrColumn], array_keys($discrMap)); + } + + $className = $discrMap[$data[$discrColumn]]; unset($data[$discrColumn]); } From 8740d54210329a139399d2b99462d5788952050e Mon Sep 17 00:00:00 2001 From: Frank Liepert Date: Wed, 9 Apr 2014 11:31:16 +0200 Subject: [PATCH 5/5] [DDC-3076] Add/Improve tests --- .../ORM/Hydration/ObjectHydratorTest.php | 30 +++++++++++++++++++ .../Hydration/SimpleObjectHydratorTest.php | 9 ++++-- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/tests/Doctrine/Tests/ORM/Hydration/ObjectHydratorTest.php b/tests/Doctrine/Tests/ORM/Hydration/ObjectHydratorTest.php index 91fffcb6d..8d2b9b226 100644 --- a/tests/Doctrine/Tests/ORM/Hydration/ObjectHydratorTest.php +++ b/tests/Doctrine/Tests/ORM/Hydration/ObjectHydratorTest.php @@ -1926,4 +1926,34 @@ class ObjectHydratorTest extends HydrationTestCase $hydrator = new \Doctrine\ORM\Internal\Hydration\ObjectHydrator($this->_em); $hydrator->hydrateAll($stmt, $rsm); } + + /** + * @group DDC-3076 + * + * @expectedException \Doctrine\ORM\Internal\Hydration\HydrationException + * @expectedExceptionMessage The discriminator value "subworker" is invalid. It must be one of "person", "manager", "employee". + */ + public function testInvalidDiscriminatorValueException() + { + $rsm = new ResultSetMapping; + + $rsm->addEntityResult('Doctrine\Tests\Models\Company\CompanyPerson', 'p'); + + $rsm->addFieldResult('p', 'p__id', 'id'); + $rsm->addFieldResult('p', 'p__name', 'name'); + $rsm->addMetaResult('p', 'discr', 'discr'); + $rsm->setDiscriminatorColumn('p', 'discr'); + + $resultSet = array( + array( + 'p__id' => '1', + 'p__name' => 'Fabio B. Silva', + 'discr' => 'subworker' + ), + ); + + $stmt = new HydratorMockStatement($resultSet); + $hydrator = new \Doctrine\ORM\Internal\Hydration\ObjectHydrator($this->_em); + $hydrator->hydrateAll($stmt, $rsm); + } } diff --git a/tests/Doctrine/Tests/ORM/Hydration/SimpleObjectHydratorTest.php b/tests/Doctrine/Tests/ORM/Hydration/SimpleObjectHydratorTest.php index 44823fa51..459ce9ba1 100644 --- a/tests/Doctrine/Tests/ORM/Hydration/SimpleObjectHydratorTest.php +++ b/tests/Doctrine/Tests/ORM/Hydration/SimpleObjectHydratorTest.php @@ -66,15 +66,18 @@ class SimpleObjectHydratorTest extends HydrationTestCase public function testInvalidDiscriminatorValueException() { $rsm = new ResultSetMapping; + $rsm->addEntityResult('Doctrine\Tests\Models\Company\CompanyPerson', 'p'); + $rsm->addFieldResult('p', 'p__id', 'id'); $rsm->addFieldResult('p', 'p__name', 'name'); - $rsm->addMetaResult('p ', 'discr', 'discr'); + $rsm->addMetaResult('p', 'discr', 'discr'); $rsm->setDiscriminatorColumn('p', 'discr'); + $resultSet = array( array( - 'u__id' => '1', - 'u__name' => 'Fabio B. Silva', + 'p__id' => '1', + 'p__name' => 'Fabio B. Silva', 'discr' => 'subworker' ), );