Skip to content

Commit

Permalink
Check requirements for metadata drivers (#9459)
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabus authored Feb 1, 2022
1 parent b3d849d commit 5f882b1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/Doctrine/ORM/Tools/DoctrineSetup.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
use Doctrine\ORM\Mapping\Driver\XmlDriver;
use Doctrine\ORM\Mapping\Driver\YamlDriver;
use LogicException;
use Memcached;
use Psr\Cache\CacheItemPoolInterface;
use Redis;
Expand All @@ -24,6 +25,7 @@
use function class_exists;
use function extension_loaded;
use function md5;
use function sprintf;
use function sys_get_temp_dir;

final class DoctrineSetup
Expand Down Expand Up @@ -54,6 +56,14 @@ public static function createDefaultAnnotationDriver(
array $paths = [],
?CacheItemPoolInterface $cache = null
): AnnotationDriver {
if (! class_exists(AnnotationReader::class)) {
throw new LogicException(sprintf(
'The annotation metadata driver cannot be enabled because the "doctrine/annotations" library'
. ' is not installed. Please run "composer require doctrine/annotations" or choose a different'
. ' metadata driver.'
));
}

$reader = new AnnotationReader();

if ($cache === null && class_exists(ArrayAdapter::class)) {
Expand Down
14 changes: 14 additions & 0 deletions tests/Doctrine/Tests/ORM/Tools/DoctrineSetupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Doctrine\ORM\Mapping\Driver\XmlDriver;
use Doctrine\ORM\Mapping\Driver\YamlDriver;
use Doctrine\ORM\Tools\DoctrineSetup;
use LogicException;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use ReflectionProperty;
Expand Down Expand Up @@ -62,6 +63,19 @@ public function testAttributeConfiguration(): void
self::assertInstanceOf(AttributeDriver::class, $config->getMetadataDriverImpl());
}

/**
* @requires PHP < 8
*/
public function testAttributeConfigurationFailsOnPHP7(): void
{
self::expectException(LogicException::class);
self::expectExceptionMessage(
'The attribute metadata driver cannot be enabled on PHP 7. Please upgrade to PHP 8 or choose a different metadata driver.'
);

DoctrineSetup::createAttributeMetadataConfiguration([], true);
}

public function testXMLConfiguration(): void
{
$config = DoctrineSetup::createXMLMetadataConfiguration([], true);
Expand Down

0 comments on commit 5f882b1

Please sign in to comment.