From 6e093dd689e45e39def08f92605315d182f55389 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82?= <35198902+pawello92@users.noreply.github.com> Date: Wed, 4 Sep 2019 00:55:20 +0200 Subject: [PATCH] Custom em in reader (#78) * Add custom storage entity manager * Refactor jobs & add new unit test * Add information to README.md * Change readme * Changes due to failed test in code climate * Add warning at the beginning of the section about custom em * Add second em to reader class * Move custom storage em to AuditConfiguration * Renamed test * Add test regarding custom entity manager * Replaced EntityManager typehint with EntityManagerInterface --- README.md | 1 - .../AuditConfiguration.php | 24 +++++++++++++-- src/DoctrineAuditBundle/AuditManager.php | 17 ++++------- .../Reader/AuditReader.php | 17 +++++++++-- .../AuditConfigurationTest.php | 7 +++++ tests/DoctrineAuditBundle/BaseTest.php | 29 +++++++++++++++++-- tests/DoctrineAuditBundle/CoreTest.php | 6 ++-- .../EventSubscriber/AuditSubscriberTest.php | 12 ++++++-- 8 files changed, 88 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 41f947db..c8a9874f 100644 --- a/README.md +++ b/README.md @@ -155,7 +155,6 @@ dh_doctrine_audit: timezone: 'Europe/London' ``` - ### Creating audit tables Open a command console, enter your project directory and execute the diff --git a/src/DoctrineAuditBundle/AuditConfiguration.php b/src/DoctrineAuditBundle/AuditConfiguration.php index ad4d55d4..b266d400 100644 --- a/src/DoctrineAuditBundle/AuditConfiguration.php +++ b/src/DoctrineAuditBundle/AuditConfiguration.php @@ -4,6 +4,7 @@ use DH\DoctrineAuditBundle\Helper\DoctrineHelper; use DH\DoctrineAuditBundle\User\UserProviderInterface; +use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\SecurityBundle\Security\FirewallMap; use Symfony\Component\HttpFoundation\RequestStack; @@ -54,11 +55,22 @@ class AuditConfiguration */ private $firewallMap; - public function __construct(array $config, UserProviderInterface $userProvider, RequestStack $requestStack, FirewallMap $firewallMap) - { + /** + * @var EntityManagerInterface|null + */ + private $customStorageEntityManager; + + public function __construct( + array $config, + UserProviderInterface $userProvider, + RequestStack $requestStack, + FirewallMap $firewallMap, + ?EntityManagerInterface $customStorageEntityManager = null + ) { $this->userProvider = $userProvider; $this->requestStack = $requestStack; $this->firewallMap = $firewallMap; + $this->customStorageEntityManager = $customStorageEntityManager; $this->enabled = $config['enabled']; $this->tablePrefix = $config['table_prefix']; @@ -324,4 +336,12 @@ public function getFirewallMap(): FirewallMap { return $this->firewallMap; } + + /** + * @return EntityManagerInterface|null + */ + public function getCustomStorageEntityManager(): ?EntityManagerInterface + { + return $this->customStorageEntityManager; + } } diff --git a/src/DoctrineAuditBundle/AuditManager.php b/src/DoctrineAuditBundle/AuditManager.php index 48fc6173..cad4bcff 100644 --- a/src/DoctrineAuditBundle/AuditManager.php +++ b/src/DoctrineAuditBundle/AuditManager.php @@ -4,6 +4,7 @@ use DH\DoctrineAuditBundle\Helper\AuditHelper; use Doctrine\ORM\EntityManager; +use Doctrine\ORM\EntityManagerInterface; class AuditManager { @@ -23,16 +24,10 @@ class AuditManager */ private $helper; - /** - * @var EntityManager|null - */ - private $customStorageEntityManager; - - public function __construct(AuditConfiguration $configuration, AuditHelper $helper, ?EntityManager $customStorageEntityManager = null) + public function __construct(AuditConfiguration $configuration, AuditHelper $helper) { $this->configuration = $configuration; $this->helper = $helper; - $this->customStorageEntityManager = $customStorageEntityManager; } /** @@ -459,11 +454,11 @@ public function reset(): void } /** - * @param EntityManager $em - * @return EntityManager + * @param EntityManagerInterface $em + * @return EntityManagerInterface */ - private function selectStorageSpace(EntityManager $em): EntityManager + private function selectStorageSpace(EntityManagerInterface $em): EntityManagerInterface { - return $this->customStorageEntityManager ?? $em; + return $this->configuration->getCustomStorageEntityManager() ?? $em; } } diff --git a/src/DoctrineAuditBundle/Reader/AuditReader.php b/src/DoctrineAuditBundle/Reader/AuditReader.php index e6d80b4b..4c47ad5c 100644 --- a/src/DoctrineAuditBundle/Reader/AuditReader.php +++ b/src/DoctrineAuditBundle/Reader/AuditReader.php @@ -41,8 +41,10 @@ class AuditReader * @param AuditConfiguration $configuration * @param EntityManagerInterface $entityManager */ - public function __construct(AuditConfiguration $configuration, EntityManagerInterface $entityManager) - { + public function __construct( + AuditConfiguration $configuration, + EntityManagerInterface $entityManager + ) { $this->configuration = $configuration; $this->entityManager = $entityManager; } @@ -199,7 +201,8 @@ private function getAuditsQueryBuilder($entity, $id = null, ?int $page = null, ? throw new \InvalidArgumentException('$pageSize must be greater or equal than 1.'); } - $connection = $this->entityManager->getConnection(); + $storage = $this->selectStorage(); + $connection = $storage->getConnection(); $queryBuilder = $connection->createQueryBuilder(); $queryBuilder @@ -298,4 +301,12 @@ public function getEntityAuditTableName($entity): string return sprintf('%s%s%s%s', $schema, $this->configuration->getTablePrefix(), $this->getEntityTableName($entityName), $this->configuration->getTableSuffix()); } + + /** + * @return EntityManagerInterface + */ + private function selectStorage(): EntityManagerInterface + { + return $this->configuration->getCustomStorageEntityManager() ?? $this->entityManager; + } } diff --git a/tests/DoctrineAuditBundle/AuditConfigurationTest.php b/tests/DoctrineAuditBundle/AuditConfigurationTest.php index 19a7150d..6c20d131 100644 --- a/tests/DoctrineAuditBundle/AuditConfigurationTest.php +++ b/tests/DoctrineAuditBundle/AuditConfigurationTest.php @@ -378,6 +378,13 @@ public function testCustomTimezone(): void $this->assertSame('Europe/London', $configuration->getTimezone(), 'custom timezone is "Europe/London".'); } + public function testCustomStorageEntityManagerIsNullByDefault(): void + { + $configuration = $this->getAuditConfiguration(); + + $this->assertNull($configuration->getCustomStorageEntityManager(), 'custom storage entity manager is null by default'); + } + protected function getAuditConfiguration(array $options = []): AuditConfiguration { $container = new ContainerBuilder(); diff --git a/tests/DoctrineAuditBundle/BaseTest.php b/tests/DoctrineAuditBundle/BaseTest.php index e45609df..61687a11 100644 --- a/tests/DoctrineAuditBundle/BaseTest.php +++ b/tests/DoctrineAuditBundle/BaseTest.php @@ -117,7 +117,7 @@ protected function setAuditConfiguration(AuditConfiguration $configuration): voi $this->auditConfiguration = $configuration; } - protected function createAuditConfiguration(array $options = []): AuditConfiguration + protected function createAuditConfiguration(array $options = [], ?EntityManager $entityManager = null): AuditConfiguration { $container = new ContainerBuilder(); @@ -132,7 +132,8 @@ protected function createAuditConfiguration(array $options = []): AuditConfigura ], $options), new TokenStorageUserProvider(new Security($container)), new RequestStack(), - new FirewallMap($container, []) + new FirewallMap($container, []), + $entityManager ); return $auditConfiguration; @@ -164,7 +165,6 @@ protected function getEntityManager(): EntityManager Gedmo\DoctrineExtensions::registerAnnotations(); $connection = $this->getSharedConnection(); -// $connection = $this->getConnection(); $this->setAuditConfiguration($this->createAuditConfiguration()); $configuration = $this->getAuditConfiguration(); @@ -275,6 +275,29 @@ protected function getConnectionParameters(): array return $params; } + protected function getSecondaryEntityManager(): EntityManager + { + $connection = $this->getSecondaryConnection(); + + $config = new Configuration(); + $config->setMetadataCacheImpl(new ArrayCache()); + $config->setQueryCacheImpl(new ArrayCache()); + $config->setProxyDir(__DIR__.'/Proxies'); + $config->setAutoGenerateProxyClasses(ProxyFactory::AUTOGENERATE_EVAL); + $config->setProxyNamespace('DH\DoctrineAuditBundle\Tests\Proxies'); + $config->setMetadataDriverImpl($config->newDefaultAnnotationDriver([__DIR__.'/Fixtures'], false)); + + return EntityManager::create($connection, $config); + } + + protected function getSecondaryConnection(): Connection + { + return DriverManager::getConnection([ + 'driver' => 'pdo_sqlite', + 'memory' => true, + ]); + } + protected function getReader(AuditConfiguration $configuration = null): AuditReader { return new AuditReader($configuration ?? $this->createAuditConfiguration(), $this->getEntityManager()); diff --git a/tests/DoctrineAuditBundle/CoreTest.php b/tests/DoctrineAuditBundle/CoreTest.php index 07ccabd8..a91cafed 100644 --- a/tests/DoctrineAuditBundle/CoreTest.php +++ b/tests/DoctrineAuditBundle/CoreTest.php @@ -10,6 +10,7 @@ use DH\DoctrineAuditBundle\Tests\Fixtures\Core\Tag; use DH\DoctrineAuditBundle\Tests\Fixtures\Core\User; use DH\DoctrineAuditBundle\User\TokenStorageUserProvider; +use Doctrine\ORM\EntityManager; use Symfony\Bundle\SecurityBundle\Security\FirewallMap; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpFoundation\Request; @@ -222,7 +223,7 @@ protected function setupEntities(): void $em->flush(); } - protected function createAuditConfiguration(array $options = []): AuditConfiguration + protected function createAuditConfiguration(array $options = [], ?EntityManager $entityManager = null): AuditConfiguration { $container = new ContainerBuilder(); $security = new Security($container); @@ -257,7 +258,8 @@ protected function createAuditConfiguration(array $options = []): AuditConfigura ], $options), new TokenStorageUserProvider($security), $requestStack, - new FirewallMap($container, []) + new FirewallMap($container, []), + $entityManager ); return $auditConfiguration; diff --git a/tests/DoctrineAuditBundle/EventSubscriber/AuditSubscriberTest.php b/tests/DoctrineAuditBundle/EventSubscriber/AuditSubscriberTest.php index 49a6fa5c..2f43e6e0 100644 --- a/tests/DoctrineAuditBundle/EventSubscriber/AuditSubscriberTest.php +++ b/tests/DoctrineAuditBundle/EventSubscriber/AuditSubscriberTest.php @@ -6,12 +6,9 @@ use DH\DoctrineAuditBundle\Reader\AuditReader; use DH\DoctrineAuditBundle\Tests\CoreTest; use DH\DoctrineAuditBundle\Tests\Fixtures\Core\Author; -use DH\DoctrineAuditBundle\Tests\Fixtures\Core\Bike; -use DH\DoctrineAuditBundle\Tests\Fixtures\Core\Car; use DH\DoctrineAuditBundle\Tests\Fixtures\Core\DummyEntity; use DH\DoctrineAuditBundle\Tests\Fixtures\Core\Post; use DH\DoctrineAuditBundle\Tests\Fixtures\Core\Tag; -use DH\DoctrineAuditBundle\Tests\Fixtures\Core\Vehicle; /** * @covers \DH\DoctrineAuditBundle\AuditConfiguration @@ -29,6 +26,15 @@ */ class AuditSubscriberTest extends CoreTest { + public function testCustomStorageEntityManager(): void + { + $configuration = $this->createAuditConfiguration([], $this->getSecondaryEntityManager()); + $defaultEM = $this->getEntityManager(); + + $this->assertNotNull($configuration->getCustomStorageEntityManager(), 'custom storage entity manager is not null'); + $this->assertNotSame($defaultEM, $configuration->getCustomStorageEntityManager(), 'custom storage entity manager is not default one'); + } + public function testInsertWithoutRelation(): void { $em = $this->getEntityManager();