Skip to content

Commit

Permalink
Allow Annotations 2, add attribute support to testing fixtures and te…
Browse files Browse the repository at this point in the history
…st with attributes when able, allow the Annotations package to be an optional dependency (#1598)
  • Loading branch information
mbabker authored Dec 28, 2022
1 parent 79fb012 commit 0421ebc
Show file tree
Hide file tree
Showing 16 changed files with 183 additions and 23 deletions.
5 changes: 5 additions & 0 deletions DependencyInjection/DoctrineExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\ServiceRepositoryCompilerPass;
use Doctrine\Bundle\DoctrineBundle\EventSubscriber\EventSubscriberInterface;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepositoryInterface;
use Doctrine\Common\Annotations\Annotation;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Connections\PrimaryReadReplicaConnection;
use Doctrine\DBAL\Driver\Middleware as MiddlewareInterface;
Expand Down Expand Up @@ -376,6 +377,10 @@ protected function ormLoad(array $config, ContainerBuilder $container)
$container->getDefinition('form.type.entity')->addTag('kernel.reset', ['method' => 'reset']);
}

if (! class_exists(Annotation::class)) {
$container->removeAlias('doctrine.orm.metadata.annotation_reader');
}

// available in Symfony 5.1 and up to Symfony 5.4 (deprecated)
if (! class_exists(PdoCacheAdapterDoctrineSchemaSubscriber::class)) {
$container->removeDefinition('doctrine.orm.listeners.pdo_cache_adapter_doctrine_schema_subscriber');
Expand Down
7 changes: 7 additions & 0 deletions Tests/Command/ImportMappingDoctrineCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Doctrine\Bundle\DoctrineBundle\Tests\Command;

use Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection\Fixtures\TestKernel;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Tools\Export\ClassMetadataExporter;
use InvalidArgumentException;
Expand All @@ -17,6 +18,8 @@
use function interface_exists;
use function sys_get_temp_dir;

use const PHP_VERSION_ID;

/** @group legacy */
class ImportMappingDoctrineCommandTest extends TestCase
{
Expand All @@ -25,6 +28,10 @@ class ImportMappingDoctrineCommandTest extends TestCase

public static function setUpBeforeClass(): void
{
if (PHP_VERSION_ID < 80000 && ! class_exists(AnnotationReader::class)) {
self::markTestSkipped('This test requires Annotations when run on PHP 7');
}

if (interface_exists(EntityManagerInterface::class) && class_exists(ClassMetadataExporter::class)) {
return;
}
Expand Down
20 changes: 20 additions & 0 deletions Tests/Command/Proxy/InfoDoctrineCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,31 @@

use Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection\Fixtures\TestKernel;
use Doctrine\Bundle\DoctrineBundle\Tests\TestCase;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;

use function class_exists;
use function interface_exists;

use const PHP_VERSION_ID;

class InfoDoctrineCommandTest extends TestCase
{
public static function setUpBeforeClass(): void
{
if (PHP_VERSION_ID < 80000 && ! class_exists(AnnotationReader::class)) {
self::markTestSkipped('This test requires Annotations when run on PHP 7');
}

if (interface_exists(EntityManagerInterface::class)) {
return;
}

self::markTestSkipped('This test requires ORM');
}

public function testExecute(): void
{
$kernel = new TestKernel();
Expand Down
6 changes: 5 additions & 1 deletion Tests/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,15 @@ public function testContainer(): void

$container = $this->createXmlBundleTestContainer();

/** @psalm-suppress UndefinedClass */
if (interface_exists(Reader::class)) {
$this->assertInstanceOf(Reader::class, $container->get('doctrine.orm.metadata.annotation_reader'));
}

$this->assertInstanceOf(DoctrineDataCollector::class, $container->get('data_collector.doctrine'));
$this->assertInstanceOf(DBALConfiguration::class, $container->get('doctrine.dbal.default_connection.configuration'));
$this->assertInstanceOf(EventManager::class, $container->get('doctrine.dbal.default_connection.event_manager'));
$this->assertInstanceOf(Connection::class, $container->get('doctrine.dbal.default_connection'));
$this->assertInstanceOf(Reader::class, $container->get('doctrine.orm.metadata.annotation_reader'));
$this->assertInstanceOf(Configuration::class, $container->get('doctrine.orm.default_configuration'));
$this->assertInstanceOf(MappingDriverChain::class, $container->get('doctrine.orm.default_metadata_driver'));
$this->assertInstanceOf(PhpArrayAdapter::class, $container->get('doctrine.orm.default_metadata_cache'));
Expand Down
19 changes: 19 additions & 0 deletions Tests/DependencyInjection/Compiler/CacheCompatibilityPassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,39 @@

use Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection\Fixtures\TestKernel;
use Doctrine\Bundle\DoctrineBundle\Tests\TestCase;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Cache\Psr6\DoctrineProvider;
use Doctrine\ORM\Cache\Region;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;

use function class_exists;
use function get_class;
use function interface_exists;

use const PHP_VERSION_ID;

class CacheCompatibilityPassTest extends TestCase
{
use ExpectDeprecationTrait;

public static function setUpBeforeClass(): void
{
if (PHP_VERSION_ID < 80000 && ! class_exists(AnnotationReader::class)) {
self::markTestSkipped('This test requires Annotations when run on PHP 7');
}

if (interface_exists(EntityManagerInterface::class)) {
return;
}

self::markTestSkipped('This test requires ORM');
}

public function testCacheConfigUsingServiceDefinedByApplication(): void
{
$customRegionClass = get_class($this->createMock(Region::class));
Expand Down
61 changes: 47 additions & 14 deletions Tests/DependencyInjection/Compiler/IdGeneratorPassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,30 @@
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\ORM\EntityManagerInterface;
use Fixtures\Bundles\AnnotationsBundle\AnnotationsBundle;
use Fixtures\Bundles\AnnotationsBundle\Entity\TestCustomIdGeneratorEntity;
use Fixtures\Bundles\AnnotationsBundle\Entity\TestCustomIdGeneratorEntity as AnnotationCustomIdGeneratorEntity;
use Fixtures\Bundles\AttributesBundle\AttributesBundle;
use Fixtures\Bundles\AttributesBundle\Entity\TestCustomIdGeneratorEntity as AttributeCustomIdGeneratorEntity;
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\FrameworkExtension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;

use function assert;
use function class_exists;
use function interface_exists;
use function sys_get_temp_dir;
use function uniqid;

use const PHP_VERSION_ID;

class IdGeneratorPassTest extends TestCase
{
public static function setUpBeforeClass(): void
{
if (PHP_VERSION_ID < 80000 && ! class_exists(AnnotationReader::class)) {
self::markTestSkipped('This test requires Annotations when run on PHP 7');
}

if (interface_exists(EntityManagerInterface::class)) {
return;
}
Expand All @@ -33,9 +42,31 @@ public static function setUpBeforeClass(): void

public function testRepositoryServiceWiring(): void
{
if (PHP_VERSION_ID >= 80000) {
$bundles = ['AttributesBundle' => AttributesBundle::class];
$entity = AttributeCustomIdGeneratorEntity::class;
$mappings = [
'AttributesBundle' => [
'type' => 'attribute',
'dir' => __DIR__ . '/../Fixtures/Bundles/AttributesBundle/Entity',
'prefix' => 'Fixtures\Bundles\AttributesBundle\Entity',
],
];
} else {
$bundles = ['AnnotationsBundle' => AnnotationsBundle::class];
$entity = AnnotationCustomIdGeneratorEntity::class;
$mappings = [
'AnnotationsBundle' => [
'type' => 'annotation',
'dir' => __DIR__ . '/../Fixtures/Bundles/AnnotationsBundle/Entity',
'prefix' => 'Fixtures\Bundles\AnnotationsBundle\Entity',
],
];
}

$container = new ContainerBuilder(new ParameterBag([
'kernel.debug' => false,
'kernel.bundles' => ['AnnotationsBundle' => AnnotationsBundle::class],
'kernel.bundles' => $bundles,
'kernel.cache_dir' => sys_get_temp_dir(),
'kernel.environment' => 'test',
'kernel.runtime_environment' => '%%env(default:kernel.environment:APP_RUNTIME_ENV)%%',
Expand All @@ -50,11 +81,21 @@ public function testRepositoryServiceWiring(): void
'env(base64:default::SYMFONY_DECRYPTION_SECRET)' => 'foo',
'debug.file_link_format' => null,
]));
$container->set('annotation_reader', new AnnotationReader());

if (class_exists(AnnotationReader::class)) {
$container->set('annotation_reader', new AnnotationReader());
}

$extension = new FrameworkExtension();
$container->registerExtension($extension);
$extension->load(['framework' => ['http_method_override' => false]], $container);
$extension->load([
'framework' => [
'http_method_override' => false,
'annotations' => [
'enabled' => class_exists(AnnotationReader::class),
],
],
], $container);

$extension = new DoctrineExtension();
$container->registerExtension($extension);
Expand All @@ -64,15 +105,7 @@ public function testRepositoryServiceWiring(): void
'driver' => 'pdo_sqlite',
'charset' => 'UTF8',
],
'orm' => [
'mappings' => [
'AnnotationsBundle' => [
'type' => 'annotation',
'dir' => __DIR__ . '/../Fixtures/Bundles/AnnotationsBundle/Entity',
'prefix' => 'Fixtures\Bundles\AnnotationsBundle\Entity',
],
],
],
'orm' => ['mappings' => $mappings],
],
], $container);

Expand All @@ -87,7 +120,7 @@ public function testRepositoryServiceWiring(): void
$em = $container->get('doctrine.orm.default_entity_manager');
assert($em instanceof EntityManagerInterface);

$metadata = $em->getClassMetadata(TestCustomIdGeneratorEntity::class);
$metadata = $em->getClassMetadata($entity);
$this->assertInstanceOf(CustomIdGenerator::class, $metadata->idGenerator);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Fixtures\Bundles\AttributesBundle\Entity;

use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
class TestCustomIdGeneratorEntity
{
#[ORM\Id, ORM\GeneratedValue(strategy: 'CUSTOM'), ORM\CustomIdGenerator('my_id_generator'), ORM\Column(type: Types::INTEGER)]
public ?int $id = null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@

namespace Fixtures\Bundles\RepositoryServiceBundle\Entity;

use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Fixtures\Bundles\RepositoryServiceBundle\Repository\TestCustomClassRepoRepository;

/** @ORM\Entity(repositoryClass="Fixtures\Bundles\RepositoryServiceBundle\Repository\TestCustomClassRepoRepository") */
#[ORM\Entity(repositoryClass: TestCustomClassRepoRepository::class)]
class TestCustomClassRepoEntity
{
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
#[ORM\Id, ORM\GeneratedValue(strategy: 'AUTO'), ORM\Column(type: Types::INTEGER)]
private ?int $id = null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@

namespace Fixtures\Bundles\RepositoryServiceBundle\Entity;

use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Fixtures\Bundles\RepositoryServiceBundle\Repository\TestCustomServiceRepoRepository;

/** @ORM\Entity(repositoryClass="Fixtures\Bundles\RepositoryServiceBundle\Repository\TestCustomServiceRepoRepository") */
#[ORM\Entity(repositoryClass: TestCustomServiceRepoRepository::class)]
class TestCustomServiceRepoEntity
{
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
#[ORM\Id, ORM\GeneratedValue(strategy: 'AUTO'), ORM\Column(type: Types::INTEGER)]
private ?int $id = null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@

namespace Fixtures\Bundles\RepositoryServiceBundle\Entity;

use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;

/** @ORM\Entity */
#[ORM\Entity]
class TestDefaultRepoEntity
{
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
#[ORM\Id, ORM\GeneratedValue(strategy: 'AUTO'), ORM\Column(type: Types::INTEGER)]
private ?int $id = null;
}
10 changes: 9 additions & 1 deletion Tests/DependencyInjection/Fixtures/DbalTestKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
use Doctrine\Bundle\DoctrineBundle\Tests\TestCaseAllPublicCompilerPass;
use Doctrine\Common\Annotations\Annotation;
use Psr\Log\NullLogger;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\HttpKernel\Kernel;

use function class_exists;
use function md5;
use function mt_rand;
use function sys_get_temp_dir;
Expand Down Expand Up @@ -42,7 +44,13 @@ public function registerBundles(): iterable
public function registerContainerConfiguration(LoaderInterface $loader): void
{
$loader->load(function (ContainerBuilder $container): void {
$container->loadFromExtension('framework', ['secret' => 'F00', 'http_method_override' => false]);
$container->loadFromExtension('framework', [
'secret' => 'F00',
'http_method_override' => false,
'annotations' => [
'enabled' => class_exists(Annotation::class),
],
]);

$container->loadFromExtension('doctrine', [
'dbal' => $this->dbalConfig,
Expand Down
Loading

0 comments on commit 0421ebc

Please sign in to comment.