bug #17152 [DoctrineBridge] [PropertyInfo] Catch Doctrine\ORM\Mapping\MappingException (dunglas)

This PR was squashed before being merged into the 2.8 branch (closes #17152).

Discussion
----------

[DoctrineBridge] [PropertyInfo] Catch Doctrine\ORM\Mapping\MappingException

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | n/a
| License       | MIT
| Doc PR        | n/a

Sometimes the Doctrine ORM `ClassMetadataFactory` throws a `Doctrine\Common\Persistence\Mapping\MappingException` exception, sometime a `Doctrine\ORM\Mapping\MappingException`.
This PR catch both.

Port of dunglas/php-property-info#10.

Commits
-------

ceded10 [DoctrineBridge] [PropertyInfo] Catch Doctrine\ORM\Mapping\MappingException
This commit is contained in:
Fabien Potencier 2015-12-29 19:38:50 +01:00
commit ddc508bbd4
2 changed files with 15 additions and 0 deletions

View File

@ -14,6 +14,7 @@ namespace Symfony\Bridge\Doctrine\PropertyInfo;
use Doctrine\Common\Persistence\Mapping\ClassMetadataFactory;
use Doctrine\Common\Persistence\Mapping\MappingException;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Doctrine\ORM\Mapping\MappingException as OrmMappingException;
use Symfony\Component\PropertyInfo\PropertyListExtractorInterface;
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
use Symfony\Component\PropertyInfo\Type;
@ -44,6 +45,8 @@ class DoctrineExtractor implements PropertyListExtractorInterface, PropertyTypeE
$metadata = $this->classMetadataFactory->getMetadataFor($class);
} catch (MappingException $exception) {
return;
} catch (OrmMappingException $exception) {
return;
}
return array_merge($metadata->getFieldNames(), $metadata->getAssociationNames());
@ -58,6 +61,8 @@ class DoctrineExtractor implements PropertyListExtractorInterface, PropertyTypeE
$metadata = $this->classMetadataFactory->getMetadataFor($class);
} catch (MappingException $exception) {
return;
} catch (OrmMappingException $exception) {
return;
}
if ($metadata->hasAssociation($property)) {

View File

@ -81,4 +81,14 @@ class DoctrineExtractorTest extends \PHPUnit_Framework_TestCase
array('notMapped', null),
);
}
public function testGetPropertiesCatchException()
{
$this->assertNull($this->extractor->getProperties('Not\Exist'));
}
public function testGetTypesCatchException()
{
$this->assertNull($this->extractor->getTypes('Not\Exist', 'baz'));
}
}