Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions DependencyInjection/Compiler/SubscriberPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ private function initializeSubscriberEvents(ContainerBuilder $container)

$this->appendDoctrineEventsToList($container, $eventsList);
$this->appendSubscribedEventsToList($container, $eventsList);
$this->appendEmbeddedEventClasses($container, $eventsList);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of doing it manually, as this event is typed, try to solve it using typed event


$this->registerEventsToListener($eventsList, $container);
}
Expand Down Expand Up @@ -147,6 +148,26 @@ private function appendEventWithResolver(&$events, $items, $key)
}
}

/**
* @param ContainerBuilder $container
* @param array $events
*/
private function appendEmbeddedEventClasses(ContainerBuilder $container, array &$events = []): void
{
$taggedServices = $container->findTaggedServiceIds('easy_audit.embedded_event');

if (empty($taggedServices)) {
return;
}

foreach ($taggedServices as $id => $_) {
$class = $container->getDefinition($id)->getClass();
if ($class !== null) {
$events[] = $class;
}
}
}

/**
* @param $events
* @param ContainerBuilder $container
Expand Down
4 changes: 4 additions & 0 deletions DependencyInjection/XiideaEasyAuditExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Loader;
use Xiidea\EasyAuditBundle\Resolver\EmbeddedEventResolverInterface;

/**
* This is the class that loads and manages your bundle configuration.
Expand All @@ -38,6 +39,9 @@ public function load(array $configs, ContainerBuilder $container): void
$container->setParameter('xiidea.easy_audit.' . $key, $value);
}

$container->registerForAutoconfiguration(EmbeddedEventResolverInterface::class)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of defining in code, it is easy to define in configuration like:

    Xiidea\EasyAuditBundle\Resolver\EmbeddedEventResolverInterface:
        tags: ['easy_audit.embedded_event']

->addTag('easy_audit.embedded_event');

$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.yml');

Expand Down
90 changes: 79 additions & 11 deletions Tests/DependencyInjection/Compiler/SubscriberPassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Xiidea\EasyAuditBundle\DependencyInjection\Compiler\SubscriberPass;
use Xiidea\EasyAuditBundle\Tests\Fixtures\Common\EasySubscriberOne;
use Xiidea\EasyAuditBundle\Tests\Fixtures\Common\EasySubscriberTwo;
use Xiidea\EasyAuditBundle\Tests\Fixtures\Event\WithEmbeddedResolver;

class SubscriberPassTest extends TestCase
{
Expand Down Expand Up @@ -48,10 +49,12 @@ public function testDisableDoctrineEvents()
['xiidea.easy_audit.doctrine_objects', false],
]);

$containerBuilder->expects($this->once())
$containerBuilder->expects($this->exactly(2))
->method('findTaggedServiceIds')
->with($this->equalTo('easy_audit.event_subscriber'))
->will($this->returnValue([]));
->willReturnMap([
['easy_audit.event_subscriber', false, []],
['easy_audit.embedded_event', false, []],
]);

$this->processSubscriberPass($containerBuilder);
}
Expand Down Expand Up @@ -117,8 +120,10 @@ public function testSubscribedEventsWithDisableDoctrineEvents()

$containerBuilder
->method('findTaggedServiceIds')
->with($this->equalTo('easy_audit.event_subscriber'))
->will($this->returnValue($subscribers));
->willReturnMap([
['easy_audit.event_subscriber', false, $subscribers],
['easy_audit.embedded_event', false, []],
]);

$definitionMock = $this->getDefinitionMock();

Expand Down Expand Up @@ -214,8 +219,10 @@ public function testSubscribedEventsWithEnabledDoctrineEvents()

$containerBuilder
->method('findTaggedServiceIds')
->with($this->equalTo('easy_audit.event_subscriber'))
->will($this->returnValue($subscribers));
->willReturnMap([
['easy_audit.event_subscriber', false, $subscribers],
['easy_audit.embedded_event', false, []],
]);

$definitionMock = $this->getDefinitionMock();

Expand Down Expand Up @@ -293,6 +300,63 @@ public function testSubscribedEventsWithEnabledDoctrineEvents()
$this->processSubscriberPass($containerBuilder);
}

public function testEmbeddedEventClassesAreAutoRegistered()
{
$containerBuilder = $this->createMock('Symfony\Component\DependencyInjection\ContainerBuilder');

$containerBuilder->expects($this->any())
->method('hasDefinition')
->with($this->equalTo('xiidea.easy_audit.event_listener'))
->will($this->returnValue(true));

$containerBuilder->expects($this->any())
->method('getParameter')
->willReturnMap([
['xiidea.easy_audit.events', []],
['xiidea.easy_audit.doctrine_objects', false],
['xiidea.easy_audit.custom_resolvers', []],
]);

$embeddedServiceId = WithEmbeddedResolver::class;

$containerBuilder
->method('findTaggedServiceIds')
->willReturnMap([
['easy_audit.event_subscriber', false, []],
['easy_audit.embedded_event', false, [$embeddedServiceId => []]],
]);

$embeddedDefinition = $this->getMockBuilder('Symfony\Component\DependencyInjection\Definition')
->disableOriginalConstructor()
->getMock();
$embeddedDefinition->method('getClass')->willReturn($embeddedServiceId);

$listenerDefinition = $this->getDefinitionMock();

$containerBuilder
->method('getDefinition')
->willReturnMap([
['xiidea.easy_audit.event_listener', $listenerDefinition],
[$embeddedServiceId, $embeddedDefinition],
]);

$expectedEvents = [
['event' => $embeddedServiceId, 'method' => 'resolveEventHandler'],
];

$listenerDefinition
->expects($this->once())
->method('setTags')
->with($this->equalTo(['kernel.event_listener' => $expectedEvents]));

$containerBuilder
->expects($this->once())
->method('setParameter')
->with($this->equalTo('xiidea.easy_audit.custom_resolvers'), []);

$this->processSubscriberPass($containerBuilder);
}

/**
* @return \PHPUnit_Framework_MockObject_MockObject
*/
Expand Down Expand Up @@ -366,8 +430,10 @@ protected function containerExpectsForEnabledDoctrineEvents($containerBuilder, $

$containerBuilder
->method('findTaggedServiceIds')
->with($this->equalTo('easy_audit.event_subscriber'))
->will($this->returnValue($subscribedEvents));
->willReturnMap([
['easy_audit.event_subscriber', false, $subscribedEvents],
['easy_audit.embedded_event', false, []],
]);

$containerBuilder
->method('getDefinition')
Expand All @@ -383,8 +449,10 @@ protected function containerExpectsForDisabledDoctrineEvents($containerBuilder,
{
$containerBuilder
->method('findTaggedServiceIds')
->with($this->equalTo('easy_audit.event_subscriber'))
->will($this->returnValue($subscribedEvents));
->willReturnMap([
['easy_audit.event_subscriber', false, $subscribedEvents],
['easy_audit.embedded_event', false, []],
]);
}

/**
Expand Down
11 changes: 11 additions & 0 deletions Tests/DependencyInjection/XiideaEasyAuditExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Yaml\Parser;
use Xiidea\EasyAuditBundle\DependencyInjection\XiideaEasyAuditExtension;
use Xiidea\EasyAuditBundle\Resolver\EmbeddedEventResolverInterface;

class XiideaEasyAuditExtensionTest extends TestCase
{
Expand Down Expand Up @@ -62,6 +63,16 @@ public function testPrependEntityEventResolverValueOnlyIfDoctrineLoaded()
$this->assertHasDefinition('xiidea.easy_audit.default_doctrine_event_resolver');
}

public function testEmbeddedEventResolverIsAutoconfigured()
{
$loader = new XiideaEasyAuditExtension();
$loader->load([$this->getRequiredConfig()], $this->container);

$autoconfigured = $this->container->getAutoconfiguredInstanceof();
$this->assertArrayHasKey(EmbeddedEventResolverInterface::class, $autoconfigured);
$this->assertTrue($autoconfigured[EmbeddedEventResolverInterface::class]->hasTag('easy_audit.embedded_event'));
}

public function testEasyAuditLoadThrowsExceptionUnlessEntityClassSet()
{
$loader = new XiideaEasyAuditExtension();
Expand Down