Commit 50088b09 authored by Daniel Król's avatar Daniel Król

Test ListenerPass

parent 48ceffd5
......@@ -33,6 +33,7 @@
}
},
"require-dev": {
"phpunit/phpunit": "^5.6"
"phpunit/phpunit": "^5.6",
"symfony/expression-language": "^3.1"
}
}
......@@ -20,24 +20,32 @@ class ListenerPass implements \Symfony\Component\DependencyInjection\Compiler\Co
$services = $container->findTaggedServiceIds('narrator.listener');
foreach ($services as $serviceId => $tags) {
foreach ($tags as $tag) {
if (!isset($tag['event'])) {
throw new \RuntimeException('The narrator.listener must have event attribute');
}
$eventName = $tag['event'];
$emitterName = isset($tag['emitter']) ? $tag['emitter'] : 'default';
$methodName = isset($tag['method']) ? $tag['method'] : null;
$this->registerListener($container, $eventName, $emitterName, $serviceId, $methodName);
$this->processServiceTag($container, $tag, $serviceId);
}
}
}
/**
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
* @param array $tag
* @param string $serviceId
*/
private function processServiceTag(\Symfony\Component\DependencyInjection\ContainerBuilder $container, $tag, $serviceId)
{
if (!isset($tag['event'])) {
throw new \RuntimeException('The narrator.listener must have event attribute');
}
$eventName = $tag['event'];
$emitterName = isset($tag['emitter']) ? $tag['emitter'] : 'default';
$methodName = isset($tag['method']) ? $tag['method'] : null;
$this->registerListener($container, $eventName, $emitterName, $serviceId, $methodName);
}
private function registerListener(\Symfony\Component\DependencyInjection\ContainerBuilder $container, $eventName, $emitterName, $listenerServiceId, $methodName)
{
$emitterDefinition = $container->getDefinition('narrator.emitter.' . $emitterName);
$listeners = $emitterDefinition->getArgument(1);
$eventListeners = isset($listeners[$eventName]) ? $listeners[$eventName] : [];
$eventListeners[] = ['serviceId' => $listenerServiceId, 'methodName' => $methodName];
$listeners[$eventName] = $eventListeners;
$listeners[$eventName][] = ['serviceId' => $listenerServiceId, 'methodName' => $methodName];
$emitterDefinition->replaceArgument(1, $listeners);
}
}
<?php
/**
* @package narrator-bundle
*/
namespace Mleko\Narrator\Bundle\Tests\DependencyInjection\Compiler\Listener;
class ListenerPassTest extends \PHPUnit_Framework_TestCase
{
/** @var \Symfony\Component\DependencyInjection\ContainerBuilder | \PHPUnit_Framework_MockObject_MockObject */
private $container;
/** @var \Mleko\Narrator\Bundle\DependencyInjection\Compiler\ListenerPass */
private $compilerPass;
/** @var \Symfony\Component\DependencyInjection\Definition */
private $defaultEmitterDefinition;
public function setUp()
{
$this->container = $this->getMockBuilder(\Symfony\Component\DependencyInjection\ContainerBuilder::class)->getMock();
$this->defaultEmitterDefinition = new \Symfony\Component\DependencyInjection\Definition(null, [null, []]);
$this->container->method('getDefinition')->with('narrator.emitter.default')->willReturn($this->defaultEmitterDefinition);
$this->compilerPass = new \Mleko\Narrator\Bundle\DependencyInjection\Compiler\ListenerPass();
}
public function testProcess()
{
$this->container->expects($this->once())->method('findTaggedServiceIds')
->willReturn([
'service1' => [
[
'event' => 'stdClass'
]
]
]);
$this->compilerPass->process($this->container);
$listeners = $this->defaultEmitterDefinition->getArgument(1);
$this->assertCount(1, $listeners);
}
public function testFailOnMissingEventName()
{
$this->container->expects($this->once())->method('findTaggedServiceIds')
->willReturn([
'service1' => [
[]
]
]);
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('The narrator.listener must have event attribute');
$this->compilerPass->process($this->container);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment