public function it_should_get_3rd_party_editor_link(Article $article, TraceableEventDispatcher $dispatcher, GenericEvent $event) { $dispatcher->dispatch('newscoop_admin.editor', Argument::type('Newscoop\\EventDispatcher\\Events\\GenericEvent'))->shouldBeCalled()->willReturn($event); $link = '/admin/some/3rd/party/editor/link/10/1'; $event->hasArgument('link')->willReturn(true); $event->getArgument('link')->willReturn($link); $this->getLink($article)->shouldReturn($link); }
public function testListenerCanRemoveItselfWhenExecuted() { $eventDispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch()); $listener1 = function () use($eventDispatcher, &$listener1) { $eventDispatcher->removeListener('foo', $listener1); }; $eventDispatcher->addListener('foo', $listener1); $eventDispatcher->addListener('foo', function () { }); $eventDispatcher->dispatch('foo'); $this->assertCount(1, $eventDispatcher->getListeners('foo'), 'expected listener1 to be removed'); }
public function getChoices($key, $section = "default") { $event = new \Symbb\Core\SystemBundle\Event\ConfigChoicesEvent($key, $section, $this->container); $this->dispatcher->dispatch('symbb.config.choices', $event); $options = $event->getChoices(); return $options; }
public function testAddListenerNested() { $called1 = false; $called2 = false; $dispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch()); $dispatcher->addListener('my-event', function () use($dispatcher, &$called1, &$called2) { $called1 = true; $dispatcher->addListener('my-event', function () use(&$called2) { $called2 = true; }); }); $dispatcher->dispatch('my-event'); $this->assertTrue($called1); $this->assertFalse($called2); $dispatcher->dispatch('my-event'); $this->assertTrue($called2); }
public function testDispatchNested() { $dispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch()); $loop = 1; $dispatcher->addListener('foo', $listener1 = function () use($dispatcher, &$loop) { ++$loop; if (2 == $loop) { $dispatcher->dispatch('foo'); } }); $dispatcher->dispatch('foo'); }
public function register(Application $app) { $app['dispatcher'] = $app->share($app->extend('dispatcher', function ($dispatcher, $app) { $dispatcher = new TraceableEventDispatcher($dispatcher, $app['stopwatch'], $app['logger']); $dispatcher->setProfiler($app['profiler']); return $dispatcher; })); $app['data_collector.templates'] = array(array('config', '@WebProfiler/Collector/config.html.twig'), array('request', '@WebProfiler/Collector/request.html.twig'), array('exception', '@WebProfiler/Collector/exception.html.twig'), array('events', '@WebProfiler/Collector/events.html.twig'), array('logger', '@WebProfiler/Collector/logger.html.twig'), array('time', '@WebProfiler/Collector/time.html.twig'), array('router', '@WebProfiler/Collector/router.html.twig'), array('memory', '@WebProfiler/Collector/memory.html.twig')); $app['data_collectors'] = array('config' => $app->share(function ($app) { return new ConfigDataCollector(); }), 'request' => $app->share(function ($app) { return new RequestDataCollector($app); }), 'exception' => $app->share(function ($app) { return new ExceptionDataCollector(); }), 'events' => $app->share(function ($app) { return new EventDataCollector(); }), 'logger' => $app->share(function ($app) { return new LoggerDataCollector($app['logger']); }), 'time' => $app->share(function ($app) { return new TimeDataCollector(); }), 'router' => $app->share(function ($app) { return new RouterDataCollector(); }), 'memory' => $app->share(function ($app) { return new MemoryDataCollector(); })); $app['web_profiler.controller.profiler'] = $app->share(function ($app) { return new ProfilerController($app['url_generator'], $app['profiler'], $app['twig'], $app['data_collector.templates'], $app['web_profiler.debug_toolbar.position']); }); $app['web_profiler.controller.router'] = $app->share(function ($app) { return new RouterController($app['profiler'], $app['twig'], isset($app['url_matcher']) ? $app['url_matcher'] : null, $app['routes']); }); $app['web_profiler.controller.exception'] = $app->share(function ($app) { return new ExceptionController($app['profiler'], $app['twig'], $app['debug']); }); $app['web_profiler.toolbar.listener'] = $app->share(function ($app) { return new WebDebugToolbarListener($app['twig']); }); $app['web_profiler.debug_toolbar.position'] = 'bottom'; $app['profiler'] = $app->share(function ($app) { $profiler = new Profiler($app['profiler.storage'], $app['logger']); foreach ($app['data_collectors'] as $collector) { $profiler->add($collector($app)); } return $profiler; }); $app['profiler.storage'] = $app->share(function ($app) { return new FileProfilerStorage('file:' . $app['profiler.cache_dir']); }); $app['profiler.request_matcher'] = null; $app['profiler.only_exceptions'] = false; $app['profiler.only_master_requests'] = false; $app['profiler.listener'] = $app->share(function ($app) { return new ProfilerListener($app['profiler'], $app['profiler.request_matcher'], $app['profiler.only_exceptions'], $app['profiler.only_master_requests']); }); $app['stopwatch'] = $app->share(function () { return new Stopwatch(); }); $app['code.file_link_format'] = null; $app['twig'] = $app->share($app->extend('twig', function ($twig, $app) { $twig->addExtension(new CodeExtension($app['code.file_link_format'], '', $app['charset'])); return $twig; })); $app['twig.loader.filesystem'] = $app->share($app->extend('twig.loader.filesystem', function ($loader, $app) { $loader->addPath($app['profiler.templates_path'], 'WebProfiler'); return $loader; })); $app['profiler.templates_path'] = function () { $r = new \ReflectionClass('Symfony\\Bundle\\WebProfilerBundle\\EventListener\\WebDebugToolbarListener'); return dirname(dirname($r->getFileName())) . '/Resources/views'; }; }
public function testDispatchReusedEventNested() { $nestedCall = false; $dispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch()); $dispatcher->addListener('foo', function (Event $e) use($dispatcher) { $dispatcher->dispatch('bar', $e); }); $dispatcher->addListener('bar', function (Event $e) use(&$nestedCall) { $nestedCall = true; }); $this->assertFalse($nestedCall); $dispatcher->dispatch('foo'); $this->assertTrue($nestedCall); }
public function testDispatchCallListeners() { $called = array(); $dispatcher = new EventDispatcher(); $tdispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch()); $tdispatcher->addListener('foo', $listener1 = function () use(&$called) { $called[] = 'foo1'; }); $tdispatcher->addListener('foo', $listener2 = function () use(&$called) { $called[] = 'foo2'; }); $tdispatcher->dispatch('foo'); $this->assertEquals(array('foo1', 'foo2'), $called); }