From 3aa8d3fdac760ca23491eef1157b76b970f52522 Mon Sep 17 00:00:00 2001 From: "Fabio B. Silva" Date: Sat, 11 Aug 2012 21:52:03 -0300 Subject: [PATCH] test constructor exceptions --- .../ORM/Internal/Hydration/ObjectHydrator.php | 22 +++++------ .../Tests/ORM/Functional/NewOperatorTest.php | 39 +++++++++++++++++++ 2 files changed, 50 insertions(+), 11 deletions(-) diff --git a/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php b/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php index 00fa42f32..edce92f80 100644 --- a/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php +++ b/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php @@ -115,7 +115,7 @@ class ObjectHydrator extends AbstractHydrator } $sourceClassName = $this->_rsm->aliasMap[$this->_rsm->parentAliasMap[$dqlAlias]]; - $sourceClass = $this->_getClassMetadata($sourceClassName); + $sourceClass = $this->getClassMetadata($sourceClassName); $assoc = $sourceClass->associationMappings[$this->_rsm->relationMap[$dqlAlias]]; $this->_hints['fetched'][$this->_rsm->parentAliasMap[$dqlAlias]][$assoc['fieldName']] = true; @@ -192,7 +192,7 @@ class ObjectHydrator extends AbstractHydrator * @param string $fieldName The name of the field on the entity that holds the collection. * @param string $parentDqlAlias Alias of the parent fetch joining this collection. */ - private function _initRelatedCollection($entity, $class, $fieldName, $parentDqlAlias) + private function initRelatedCollection($entity, $class, $fieldName, $parentDqlAlias) { $oid = spl_object_hash($entity); $relation = $class->associationMappings[$fieldName]; @@ -238,7 +238,7 @@ class ObjectHydrator extends AbstractHydrator * @param string $dqlAlias The DQL alias of the entity's class. * @return object The entity. */ - private function _getEntity(array $data, $dqlAlias) + private function getEntity(array $data, $dqlAlias) { $className = $this->_rsm->aliasMap[$dqlAlias]; @@ -277,7 +277,7 @@ class ObjectHydrator extends AbstractHydrator * @param array $data * @return mixed */ - private function _getEntityFromIdentityMap($className, array $data) + private function getEntityFromIdentityMap($className, array $data) { // TODO: Abstract this code and UnitOfWork::createEntity() equivalent? $class = $this->ce[$className]; @@ -308,7 +308,7 @@ class ObjectHydrator extends AbstractHydrator * @param string $className The name of the class. * @return ClassMetadata */ - private function _getClassMetadata($className) + private function getClassMetadata($className) { if ( ! isset($this->ce[$className])) { $this->ce[$className] = $this->_em->getClassMetadata($className); @@ -412,7 +412,7 @@ class ObjectHydrator extends AbstractHydrator if (isset($this->initializedCollections[$collKey])) { $reflFieldValue = $this->initializedCollections[$collKey]; } else if ( ! isset($this->existingCollections[$collKey])) { - $reflFieldValue = $this->_initRelatedCollection($parentObject, $parentClass, $relationField, $parentAlias); + $reflFieldValue = $this->initRelatedCollection($parentObject, $parentClass, $relationField, $parentAlias); } $indexExists = isset($this->identifierMap[$path][$id[$parentAlias]][$id[$dqlAlias]]); @@ -422,13 +422,13 @@ class ObjectHydrator extends AbstractHydrator if ( ! $indexExists || ! $indexIsValid) { if (isset($this->existingCollections[$collKey])) { // Collection exists, only look for the element in the identity map. - if ($element = $this->_getEntityFromIdentityMap($entityName, $data)) { + if ($element = $this->getEntityFromIdentityMap($entityName, $data)) { $this->resultPointers[$dqlAlias] = $element; } else { unset($this->resultPointers[$dqlAlias]); } } else { - $element = $this->_getEntity($data, $dqlAlias); + $element = $this->getEntity($data, $dqlAlias); if (isset($this->_rsm->indexByMap[$dqlAlias])) { $indexValue = $row[$this->_rsm->indexByMap[$dqlAlias]]; @@ -447,7 +447,7 @@ class ObjectHydrator extends AbstractHydrator $this->resultPointers[$dqlAlias] = $reflFieldValue[$index]; } } else if ( ! $reflFieldValue) { - $reflFieldValue = $this->_initRelatedCollection($parentObject, $parentClass, $relationField, $parentAlias); + $reflFieldValue = $this->initRelatedCollection($parentObject, $parentClass, $relationField, $parentAlias); } else if ($reflFieldValue instanceof PersistentCollection && $reflFieldValue->isInitialized() === false) { $reflFieldValue->setInitialized(true); } @@ -459,7 +459,7 @@ class ObjectHydrator extends AbstractHydrator // we only need to take action if this value is null, // we refresh the entity or its an unitialized proxy. if (isset($nonemptyComponents[$dqlAlias])) { - $element = $this->_getEntity($data, $dqlAlias); + $element = $this->getEntity($data, $dqlAlias); $reflField->setValue($parentObject, $element); $this->_uow->setOriginalEntityProperty($oid, $relationField, $element); $targetClass = $this->ce[$relation['targetEntity']]; @@ -513,7 +513,7 @@ class ObjectHydrator extends AbstractHydrator // check for existing result from the iterations before if ( ! isset($this->identifierMap[$dqlAlias][$id[$dqlAlias]])) { - $element = $this->_getEntity($rowData[$dqlAlias], $dqlAlias); + $element = $this->getEntity($rowData[$dqlAlias], $dqlAlias); if ($this->_rsm->isMixed) { $element = array($entityKey => $element); } diff --git a/tests/Doctrine/Tests/ORM/Functional/NewOperatorTest.php b/tests/Doctrine/Tests/ORM/Functional/NewOperatorTest.php index e83130e28..2109482fa 100644 --- a/tests/Doctrine/Tests/ORM/Functional/NewOperatorTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/NewOperatorTest.php @@ -450,4 +450,43 @@ class NewOperatorTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->assertEquals($this->fixtures[1]->address->country, $result[1][1]->country); $this->assertEquals($this->fixtures[2]->address->country, $result[2][1]->country); } + + /** + * @expectedException Doctrine\ORM\Query\QueryException + * @expectedExceptionMessage [Semantical Error] line 0, col 11 near '\InvalidClass(u.name)': Error: Class "\InvalidClass" is not defined. + */ + public function testInvalidClassException() + { + $dql = "SELECT new \InvalidClass(u.name) FROM Doctrine\Tests\Models\CMS\CmsUser u"; + $this->_em->createQuery($dql)->getResult(); + } + + /** + * @expectedException Doctrine\ORM\Query\QueryException + * @expectedExceptionMessage [Semantical Error] line 0, col 11 near '\stdClass(u.name)': Error: Class "\stdClass" has not a valid contructor. + */ + public function testInvalidClassConstructorException() + { + $dql = "SELECT new \stdClass(u.name) FROM Doctrine\Tests\Models\CMS\CmsUser u"; + $this->_em->createQuery($dql)->getResult(); + } + + /** + * @expectedException Doctrine\ORM\Query\QueryException + * @expectedExceptionMessage [Semantical Error] line 0, col 68 near ') FROM Doctrine\Tests\Models\CMS\CmsUser': Error: Number of arguments does not match. + */ + public function testInvalidClassWithoutConstructorException() + { + $dql = "SELECT new Doctrine\Tests\ORM\Functional\ClassWithTooMuchArgs(u.name) FROM Doctrine\Tests\Models\CMS\CmsUser u"; + $this->_em->createQuery($dql)->getResult(); + } +} + +class ClassWithTooMuchArgs +{ + public function __construct($foo, $bar) + { + $this->foo = $foo; + $this->bor = $bar; + } } \ No newline at end of file