public function testFetchListeners() { $defMock = $this->getMockBuilder('Symfony\\Component\\DependencyInjection\\Definition')->disableOriginalConstructor()->getMock(); $defMock->expects($this->at(0))->method('getTags')->will($this->returnValue(array('test0.event_listener' => array('event' => 'test.event')))); $defMock->expects($this->at(1))->method('getTags')->will($this->returnValue(array('test1.event_listener' => array('event' => 'test.event')))); $defMock->expects($this->at(2))->method('getTags')->will($this->returnValue(array('test2.event_listener' => array('event' => 'test.event')))); $defMock->expects($this->exactly(3))->method('isPublic')->will($this->returnValue(true)); $defMock->expects($this->any())->method('getClass')->will($this->returnValue('Egulias\\ListenersDebugCommandBundle\\Tests\\Listener\\Listener')); $containerMock = $this->getMockBuilder('Symfony\\Component\\DependencyInjection\\ContainerBuilder')->disableOriginalConstructor()->getMock(); $containerMock->expects($this->once())->method('getDefinitions')->will($this->returnValue(array($defMock, $defMock, $defMock))); $containerMock->expects($this->exactly(3))->method('findTaggedServiceIds')->will($this->returnValue(array('test0.event_listener' => array(array('event' => 'test.event', 'method' => 'onTestEvent', 'priority' => 4)), 'test1.event_listener' => array(array('event' => 'test.event', 'method' => 'onTestEvent', 'priority' => 2)), 'test2.event_listener' => array(array())))); $containerMock->expects($this->any())->method('hasDefinition')->will($this->returnValue(true)); $containerMock->expects($this->exactly(3))->method('getDefinition')->will($this->returnValue($defMock)); $fetcher = new ListenerFetcher($containerMock); $listeners = $fetcher->fetchListeners(); $this->assertCount(3, $listeners); $this->assertEquals('test.event', $listeners[0][1]); $this->assertEquals('listener', $listeners[0][4]); $this->assertEquals('subscriber', $listeners[2][4]); foreach ($listeners as $listener) { $this->assertCount(6, $listener); $this->assertEquals('test.event', $listener[1]); $this->assertEquals('onTestEvent', $listener[2]); $this->assertNotEquals(0, $listener[3]); } }
/** * Renders detailed service information about one listener */ protected function outputListener(OutputInterface $output, $serviceId) { $fetcher = new ListenerFetcher($this->getContainerBuilder()); $definition = $fetcher->fetchListener($serviceId); $label = sprintf('Information for listener <info>%s</info>', $serviceId); $output->writeln($this->getHelper('formatter')->formatSection('container', $label)); $output->writeln(''); if ($definition instanceof Alias) { $output->writeln(sprintf('This service is an alias for the service <info>%s</info>', (string) $definition)); return; } $output->writeln(sprintf('<comment>Listener Id</comment> %s', $serviceId)); $output->writeln(sprintf('<comment>Class</comment> %s', $definition->getClass())); if ($definition instanceof Definition) { return; } $type = $fetcher->isSubscriber($definition) ? 'subscriber' : 'listener'; $output->writeln(sprintf('<comment>Type</comment> %s', $type)); $output->writeln(sprintf('<comment>Listens to</comment>', '')); $events = array(); $tags = $definition->getTags(); foreach ($tags as $tag => $details) { if (preg_match(self::SUBSCRIBER_PATTERN, $tag)) { $subscribed = $this->getEventSubscriberInformation($definition->getClass()); foreach ($subscribed as $name => $current) { //Exception when event only has the method name if (!is_array($current)) { $current = array($current); } elseif (is_array($current[0])) { $current = $current[0]; } $event['name'] = $name; $event['method'] = $current[0]; $event['priority'] = isset($current[1]) ? $current[1] : 0; } } elseif (preg_match(self::LISTENER_PATTERN, $tag)) { foreach ($details as $current) { $event['name'] = $current['event']; $event['method'] = isset($current['method']) ? $current['method'] : $current['event']; $event['priority'] = isset($current['priority']) ? $current['priority'] : 0; } } } foreach ($events as $event) { $output->writeln(sprintf('<comment> -Event</comment> %s', $event['name'])); $output->writeln(sprintf('<comment> -Method</comment> %s', $event['method'])); $output->writeln(sprintf('<comment> -Priority</comment> %s', $event['priority'])); $output->writeln(sprintf('<comment> -----------------------------------------</comment>')); } $tags = $tags ? implode(', ', array_keys($tags)) : '-'; $output->writeln(sprintf('<comment>Tags</comment> %s', $tags)); $public = $definition->isPublic() ? 'yes' : 'no'; $output->writeln(sprintf('<comment>Public</comment> %s', $public)); }