Skip to content

Commit

Permalink
Custom em in reader (#78)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
pawelmicnas authored and DamienHarper committed Sep 3, 2019
1 parent 1b1111b commit 6e093dd
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 25 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ dh_doctrine_audit:
timezone: 'Europe/London'
```


### Creating audit tables

Open a command console, enter your project directory and execute the
Expand Down
24 changes: 22 additions & 2 deletions src/DoctrineAuditBundle/AuditConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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'];
Expand Down Expand Up @@ -324,4 +336,12 @@ public function getFirewallMap(): FirewallMap
{
return $this->firewallMap;
}

/**
* @return EntityManagerInterface|null
*/
public function getCustomStorageEntityManager(): ?EntityManagerInterface
{
return $this->customStorageEntityManager;
}
}
17 changes: 6 additions & 11 deletions src/DoctrineAuditBundle/AuditManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use DH\DoctrineAuditBundle\Helper\AuditHelper;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;

class AuditManager
{
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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;
}
}
17 changes: 14 additions & 3 deletions src/DoctrineAuditBundle/Reader/AuditReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}
7 changes: 7 additions & 0 deletions tests/DoctrineAuditBundle/AuditConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
29 changes: 26 additions & 3 deletions tests/DoctrineAuditBundle/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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;
Expand Down Expand Up @@ -164,7 +165,6 @@ protected function getEntityManager(): EntityManager
Gedmo\DoctrineExtensions::registerAnnotations();

$connection = $this->getSharedConnection();
// $connection = $this->getConnection();

$this->setAuditConfiguration($this->createAuditConfiguration());
$configuration = $this->getAuditConfiguration();
Expand Down Expand Up @@ -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());
Expand Down
6 changes: 4 additions & 2 deletions tests/DoctrineAuditBundle/CoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -257,7 +258,8 @@ protected function createAuditConfiguration(array $options = []): AuditConfigura
], $options),
new TokenStorageUserProvider($security),
$requestStack,
new FirewallMap($container, [])
new FirewallMap($container, []),
$entityManager
);

return $auditConfiguration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
Expand Down

0 comments on commit 6e093dd

Please sign in to comment.