Skip to content

Commit

Permalink
Improve code to throw exception getting parents class instead of php …
Browse files Browse the repository at this point in the history
…warning
  • Loading branch information
eb-eoliveira committed May 9, 2013
1 parent 7eea788 commit 660900a
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/Doctrine/Common/Persistence/Mapping/MappingException.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,14 @@ public static function invalidMappingFile($entityName, $fileName)
{
return new self("Invalid mapping file '$fileName' for class '$entityName'.");
}

/**
* @param string $className
*
* @return \Doctrine\Common\Persistence\Mapping\MappingException
*/
public static function nonExistingClass($className)
{
return new self("Class '$className' does not exists");
}
}
3 changes: 3 additions & 0 deletions lib/Doctrine/Common/Persistence/Mapping/ReflectionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ interface ReflectionService
* Return an array of the parent classes (not interfaces) for the given class.
*
* @param string $class
*
* @throws \Doctrine\Common\Persistence\Mapping\MappingException
*
* @return array
*/
function getParentClasses($class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use ReflectionClass;
use ReflectionProperty;
use Doctrine\Common\Reflection\RuntimePublicReflectionProperty;
use Doctrine\Common\Persistence\Mapping\MappingException;

/**
* PHP Runtime Reflection Service
Expand All @@ -35,6 +36,10 @@ class RuntimeReflectionService implements ReflectionService
*/
public function getParentClasses($class)
{
if ( ! class_exists($class)) {
throw MappingException::nonExistingClass($class);
}

return class_parents($class);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ public function testGetMetadataFor()
$this->assertTrue($this->cmf->hasMetadataFor('stdClass'));
}

public function testGetMetadataForAbsentClass()
{
$this->setExpectedException('Doctrine\Common\Persistence\Mapping\MappingException');
$this->cmf->getMetadataFor(__NAMESPACE__ . '\AbsentClass');
}

public function testGetParentMetadata()
{
$metadata = $this->cmf->getMetadataFor(__NAMESPACE__ . '\ChildEntity');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ public function testGetParentClasses()
$this->assertTrue(count($classes) >= 1, "The test class ".__CLASS__." should have at least one parent.");
}

public function testGetParentClassesForAbsentClass()
{
$this->setExpectedException('Doctrine\Common\Persistence\Mapping\MappingException');
$this->reflectionService->getParentClasses(__NAMESPACE__ . '\AbsentClass');
}

public function testGetReflectionClass()
{
$class = $this->reflectionService->getClass(__CLASS__);
Expand Down

0 comments on commit 660900a

Please sign in to comment.