Commit 59d00343 authored by Daniel Król's avatar Daniel Król

Test and fix for resolver service

parent e0395752
......@@ -14,6 +14,7 @@ namespace Mleko\Narrator\Bundle\DependencyInjection\Configuration;
use Mleko\Narrator\BasicEventBus;
use Mleko\Narrator\ListenerResolver\InstanceOfResolver;
use Mleko\Narrator\ListenerResolver\NameBasedResolver;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
......@@ -29,11 +30,13 @@ class EventBusConfiguration
foreach ($buses as $busName => $busConfig) {
$resolverServiceId = "narrator.event_bus.$busName.resolver";
$resolverConfig = isset($busConfig['resolver']) ? $busConfig['resolver'] : [];
$container->setDefinition(
$resolverServiceId,
$this->buildResolver($resolverConfig)
);
$resolver = $this->buildResolver($resolverConfig);
if ($resolver instanceof Definition) {
$container->setDefinition($resolverServiceId, $resolver);
} elseif ($resolver instanceof Alias) {
$container->setAlias($resolverServiceId, $resolver);
}
$container->setDefinition(
"narrator.event_bus.$busName",
new Definition(
......@@ -47,6 +50,10 @@ class EventBusConfiguration
}
}
/**
* @param array $resolverConfig
* @return Alias|Definition
*/
private function buildResolver($resolverConfig)
{
$resolverType = isset($resolverConfig['type']) ? $resolverConfig['type'] : 'name';
......@@ -54,7 +61,7 @@ class EventBusConfiguration
case 'instanceof':
return new Definition(InstanceOfResolver::class);
case 'service':
return new Reference($resolverType['service_id']);
return new Alias($resolverConfig['service_id'], false);
default:
$nameExtractorId = isset($resolverConfig['name_extractor']) ? $resolverConfig['name_extractor'] : "narrator.name_extractor.class_name";
return new Definition(
......
......@@ -11,7 +11,9 @@
namespace Mleko\Narrator\Bundle\Tests\Integration;
use Mleko\Narrator\EventBus;
use Mleko\Narrator\EventEmitter;
use Mleko\Narrator\Listener\EventTrap;
use org\bovigo\vfs\vfsStream;
/**
......@@ -80,4 +82,32 @@ EOF
/** @var EventEmitter $eventEmitter */
$this->assertNotNull($container->get("narrator.event_bus.inheritance"));
}
public function testServiceResolver()
{
$servicesConfig = vfsStream::newFile("config.yml");
$servicesConfig->setContent(<<<'EOF'
services:
custom_resolver:
class: Mleko\Narrator\ListenerResolver\InstanceOfResolver
narrator:
event_bus:
default:
resolver:
type: service
service_id: custom_resolver
EOF
);
$this->root->addChild($servicesConfig);
$this->kernel->setConfigPath($servicesConfig->url());
$this->kernel->boot();
$container = $this->kernel->getContainer();
$this->assertTrue($container->has("narrator.event_bus.default"));
/** @var EventBus $bus */
$bus = $container->get("narrator.event_bus.default");
$bus->subscribe(\Exception::class, $trap = new EventTrap());
$bus->emit($e = new \RuntimeException());
$this->assertContains($e, $trap->getTrappedEvents());
}
}
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