Skip to content

Commit

Permalink
DI: implement ResolveTargetEntityListener [#102, closes #52]
Browse files Browse the repository at this point in the history
  • Loading branch information
juniwalk authored Oct 25, 2023
1 parent 9e2cd91 commit 1bd93ee
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/DI/OrmExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManager as DoctrineEntityManager;
use Doctrine\ORM\Mapping\UnderscoreNamingStrategy;
use Doctrine\ORM\Tools\ResolveTargetEntityListener;
use Doctrine\Persistence\Mapping\Driver\MappingDriverChain;
use Nette\DI\Definitions\Statement;
use Nette\DI\Helpers;
Expand Down Expand Up @@ -43,6 +44,7 @@ public function getConfigSchema(): Schema
'proxyNamespace' => Expect::string('Nettrine\Proxy')->nullable(),
'metadataDriverImpl' => Expect::string(),
'entityNamespaces' => Expect::array(),
'resolveTargetEntities' => Expect::array(),
'customStringFunctions' => Expect::array(),
'customNumericFunctions' => Expect::array(),
'customDatetimeFunctions' => Expect::array(),
Expand Down Expand Up @@ -191,6 +193,15 @@ public function loadEntityManagerConfiguration(): void
}
}

if ($config->configuration->resolveTargetEntities !== []) {
$resolver = $builder->addDefinition($this->prefix('targetEntityResolver'))
->setType(ResolveTargetEntityListener::class);

foreach ($config->configuration->resolveTargetEntities as $name => $implementation) {
$resolver->addSetup('addResolveTargetEntity', [$name, $implementation, []]);
}
}

// Manager Registry
$builder->addDefinition($this->prefix('managerRegistry'))
->setFactory(ManagerRegistry::class, [
Expand Down
5 changes: 3 additions & 2 deletions tests/Cases/DI/OrmAttributesExtension.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use Nette\DI\InvalidConfigurationException;
use Nettrine\ORM\DI\OrmAttributesExtension;
use Contributte\Tester\Toolkit;
use Tester\Assert;
use Tests\Fixtures\Dummy\DummyEntity;
use Tests\Toolkit\Container;
use Tests\Toolkit\Helpers;

Expand All @@ -25,7 +26,7 @@ Toolkit::test(function (): void {
$compiler->addConfig(Helpers::neon('
nettrine.orm.attributes:
mapping:
App\Model\Entity: %appDir%
Tests\Fixtures\Dummy: %appDir%
'));
})
->build();
Expand All @@ -37,7 +38,7 @@ Toolkit::test(function (): void {

/** @var AttributeDriver $attributeDriver */
$attributeDriver = current($driver->getDrivers());
Assert::equal([], $attributeDriver->getAllClassNames());
Assert::equal([DummyEntity::class], $attributeDriver->getAllClassNames());
});

// Error (missing mapping)
Expand Down
35 changes: 35 additions & 0 deletions tests/Cases/DI/OrmExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
use Contributte\Tester\Toolkit;
use Doctrine\ORM\Decorator\EntityManagerDecorator;
use Nette\DI\Compiler;
use Nettrine\ORM\DI\OrmAttributesExtension;
use Nettrine\ORM\Exception\Logical\InvalidArgumentException;
use Tester\Assert;
use Tests\Fixtures\Dummy\DummyConfiguration;
use Tests\Fixtures\Dummy\DummyEntity;
use Tests\Fixtures\Dummy\DummyEntityManagerDecorator;
use Tests\Fixtures\Dummy\DummyFilter;
use Tests\Fixtures\Dummy\DummyIdentity;
use Tests\Toolkit\Container;
use Tests\Toolkit\Helpers;

require_once __DIR__ . '/../../bootstrap.php';

Expand Down Expand Up @@ -72,6 +76,37 @@
Assert::equal(false, $filters->isEnabled('autoDisabledFilter'));
});

// ResolveTargetEntityListener
Toolkit::test(function (): void {
$container = Container::of()
->withDefaults()
->withCompiler(static function (Compiler $compiler): void {
$compiler->addExtension('nettrine.orm.attributes', new OrmAttributesExtension());
$compiler->addConfig(Helpers::neon('
nettrine.orm.attributes:
mapping:
Tests\Fixtures\Dummy: %appDir%
'));

$compiler->addConfig([
'nettrine.orm' => [
'configuration' => [
'resolveTargetEntities' => [
DummyIdentity::class => DummyEntity::class,
],
],
],
]);
})
->build();

/** @var EntityManagerDecorator $em */
$em = $container->getService('nettrine.orm.entityManagerDecorator');
$cm = $em->getClassMetadata(DummyIdentity::class);

Assert::equal($cm->name, DummyEntity::class);
});

// Error (configuration subclass)
Toolkit::test(function (): void {
Assert::exception(function (): void {
Expand Down
24 changes: 24 additions & 0 deletions tests/Fixtures/Dummy/DummyEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php declare(strict_types = 1);

namespace Tests\Fixtures\Dummy;

use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Id;

#[Entity]
class DummyEntity implements DummyIdentity
{

#[Column(type: 'integer', unique: true, nullable: false)]
#[GeneratedValue(strategy: 'IDENTITY')]
#[Id]
private int $id;

public function getId(): int
{
return $this->id;
}

}
8 changes: 8 additions & 0 deletions tests/Fixtures/Dummy/DummyIdentity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types = 1);

namespace Tests\Fixtures\Dummy;

interface DummyIdentity
{

}

0 comments on commit 1bd93ee

Please sign in to comment.