Event example


        $dispatcher = new EventDispatcher();
        $tdispatcher = new TraceableEventDispatcher($dispatchernew Stopwatch());

        $tdispatcher->addListener('foo', function D) {}, 123);

        $listeners = $dispatcher->getListeners('foo');
        $this->assertSame(123, $tdispatcher->getListenerPriority('foo', $listeners[0]));

        // Verify that priority is preserved when listener is removed and re-added         // in preProcess() and postProcess().         $tdispatcher->dispatch(new Event(), 'foo');
        $listeners = $dispatcher->getListeners('foo');
        $this->assertSame(123, $tdispatcher->getListenerPriority('foo', $listeners[0]));
    }

    public function testGetListenerPriorityWhileDispatching()
    {
        $tdispatcher = new TraceableEventDispatcher(new EventDispatcher()new Stopwatch());
        $priorityWhileDispatching = null;

        $listener = function D) use ($tdispatcher, &$priorityWhileDispatching, &$listener) {
            $priorityWhileDispatching = $tdispatcher->getListenerPriority('bar', $listener);
        };
    foreach ($all_messages as $type => $messages) {
      foreach ($messages as $message) {
        $this->container->get('messenger')->addMessage($message$type);
      }
    }

    \Drupal::setContainer($this->container);

    // Allow other parts of the codebase to react on container initialization in     // subrequest.     if (!empty($subrequest)) {
      $this->container->get('event_dispatcher')->dispatch(new Event(), self::CONTAINER_INITIALIZE_SUBREQUEST_FINISHED);
    }

    // If needs dumping flag was set, dump the container.     if ($this->containerNeedsDumping && !$this->cacheDrupalContainer($container_definition)) {
      $this->container->get('logger.factory')->get('DrupalKernel')->error('Container cannot be saved to cache.');
    }

    return $this->container;
  }

  /** * Setup a consistent PHP environment. * * This method sets PHP environment options we want to be sure are set * correctly for security or just saneness. * * @param string $app_root * (optional) The path to the application root as a string. If not supplied, * the application root will be computed. */
public function testAddListenerNested()
    {
        $called1 = false;
        $called2 = false;
        $dispatcher = new TraceableEventDispatcher(new EventDispatcher()new Stopwatch());
        $dispatcher->addListener('my-event', function D) use ($dispatcher, &$called1, &$called2) {
            $called1 = true;
            $dispatcher->addListener('my-event', function D) use (&$called2) {
                $called2 = true;
            });
        });
        $dispatcher->dispatch(new Event(), 'my-event');
        $this->assertTrue($called1);
        $this->assertFalse($called2);
        $dispatcher->dispatch(new Event(), 'my-event');
        $this->assertTrue($called2);
    }

    public function testListenerCanRemoveItselfWhenExecuted()
    {
        $eventDispatcher = new TraceableEventDispatcher(new EventDispatcher()new Stopwatch());
        $listener1 = function D) use ($eventDispatcher, &$listener1) {
            $eventDispatcher->removeListener('foo', $listener1);
        };
// ALTER is the final step to alter all the existing routes. We cannot stop     // people from adding new routes here, but we define two separate steps to     // make it clear.     $this->dispatcher->dispatch(new RouteBuildEvent($collection), RoutingEvents::ALTER);

    $this->checkProvider->setChecks($collection);

    $this->dumper->addRoutes($collection);
    $this->dumper->dump();

    $this->lock->release('router_rebuild');
    $this->dispatcher->dispatch(new Event(), RoutingEvents::FINISHED);
    $this->building = FALSE;

    $this->rebuildNeeded = FALSE;

    return TRUE;
  }

  /** * {@inheritdoc} */
  public function rebuildIfNeeded() {
    

class AccountProxyTest extends UnitTestCase {

  /** * @covers ::id * @covers ::setInitialAccountId */
  public function testId() {
    $dispatcher = $this->prophesize(EventDispatcherInterface::class);
    $dispatcher->dispatch(Argument::any(), Argument::any())->willReturn(new Event());
    $account_proxy = new AccountProxy($dispatcher->reveal());
    $this->assertSame(0, $account_proxy->id());
    $account_proxy->setInitialAccountId(1);
    $this->assertFalse(\Drupal::hasContainer());
    // If the following call loaded the user entity it would call     // AccountProxy::loadUserEntity() which would fail because the container     // does not exist.     $this->assertSame(1, $account_proxy->id());
    $current_user = $this->prophesize(AccountInterface::class);
    $current_user->id()->willReturn(2);
    $account_proxy->setAccount($current_user->reveal());
    
$listeners = [
      'test_event' => [
        0 => [
          ['callable' => $firstListener],
          ['callable' => $secondListener],
          ['callable' => $thirdListener],
        ],
      ],
    ];

    $dispatcher = new ContainerAwareEventDispatcher($container$listeners);
    $dispatcher->dispatch(new Event(), 'test_event');

    $this->assertTrue($thirdListener[0]->preFooInvoked);
  }

  public function testGetListenersWithServices() {
    $container = new ContainerBuilder();
    $container->register('listener_service', TestEventListener::class);

    $listeners = [
      'test_event' => [
        0 => [
          [
private MockObject&EventDispatcherInterface $innerDispatcher;
    private ImmutableEventDispatcher $dispatcher;

    protected function setUp(): void
    {
        $this->innerDispatcher = $this->createMock(EventDispatcherInterface::class);
        $this->dispatcher = new ImmutableEventDispatcher($this->innerDispatcher);
    }

    public function testDispatchDelegates()
    {
        $event = new Event();
        $resultEvent = new Event();

        $this->innerDispatcher->expects($this->once())
            ->method('dispatch')
            ->with($event, 'event')
            ->willReturn($resultEvent);

        $this->assertSame($resultEvent$this->dispatcher->dispatch($event, 'event'));
    }

    public function testGetListenersDelegates()
    {
$route_collection = new RouteCollection();
    $route_collection->add('test', new Route('/admin/foo', ['_controller' => 'Drupal\ExampleController']));
    $route_collection->add('test2', new Route('/admin/bar', ['_controller' => 'Drupal\ExampleController']));
    $event->expects($this->once())
      ->method('getRouteCollection')
      ->willReturn($route_collection);

    $this->state->expects($this->once())
      ->method('set')
      ->with('routing.non_admin_routes', []);
    $this->preloader->onAlterRoutes($event);
    $this->preloader->onFinishedRoutes(new Event());
  }

  /** * Tests onAlterRoutes with "admin" appearing in the path. */
  public function testOnAlterRoutesWithAdminPathNoAdminRoute() {
    $event = $this->getMockBuilder('Drupal\Core\Routing\RouteBuildEvent')
      ->disableOriginalConstructor()
      ->getMock();
    $route_collection = new RouteCollection();
    $route_collection->add('test', new Route('/foo/admin/foo', ['_controller' => 'Drupal\ExampleController']));
    
$this->assertSame(-10, $this->dispatcher->getListenerPriority('pre.foo', $listener1));
        $this->assertSame(0, $this->dispatcher->getListenerPriority('pre.foo', $listener2));
        $this->assertNull($this->dispatcher->getListenerPriority('pre.bar', $listener2));
        $this->assertNull($this->dispatcher->getListenerPriority('pre.foo', function D) {}));
    }

    public function testDispatch()
    {
        $this->dispatcher->addListener('pre.foo', [$this->listener, 'preFoo']);
        $this->dispatcher->addListener('post.foo', $this->listener->postFoo(...));
        $this->dispatcher->dispatch(new Event(), self::preFoo);
        $this->assertTrue($this->listener->preFooInvoked);
        $this->assertFalse($this->listener->postFooInvoked);
        $this->assertInstanceOf(Event::class$this->dispatcher->dispatch(new Event(), 'noevent'));
        $this->assertInstanceOf(Event::class$this->dispatcher->dispatch(new Event(), self::preFoo));
        $event = new Event();
        $return = $this->dispatcher->dispatch($event, self::preFoo);
        $this->assertSame($event$return);
    }

    public function testDispatchForClosure()
    {
        
Home | Imprint | This part of the site doesn't use cookies.