addListener example



    public function testLoadWithDisabledCacheWillCallDecoratedRoute(): void
    {
        $this->decorated
            ->expects(static::once())
            ->method('load')
            ->willReturn($this->response);
        $this->cache
            ->expects(static::never())
            ->method('get');
        $this->eventDispatcher->addListener(
            PaymentMethodRouteCacheKeyEvent::class,
            fn (PaymentMethodRouteCacheKeyEvent $event) => $event->disableCaching()
        );

        $this->cachedRoute->load(new Request()$this->context, new Criteria());
    }

    public function testLoadWithEnabledCacheWillReturnDataFromCache(): void
    {
        $this->decorated
            ->expects(static::never())
            
// always return the original event and never our wrapped events         // this would lead to problems in the `BusinessEventDispatcher` from core         return $event;
    }

    /** * @param callable $listener can not use native type declaration @see https://github.com/symfony/symfony/issues/42283 */
    public function addListener(string $eventName$listener, int $priority = 0): void // @phpstan-ignore-line     {
        /** @var callable(object): void $listener - Specify generic callback interface callers can provide more specific implementations */
        $this->dispatcher->addListener($eventName$listener$priority);
    }

    public function addSubscriber(EventSubscriberInterface $subscriber): void
    {
        $this->dispatcher->addSubscriber($subscriber);
    }

    public function removeListener(string $eventName, callable $listener): void
    {
        /** @var callable(object): void $listener - Specify generic callback interface callers can provide more specific implementations */
        $this->dispatcher->removeListener($eventName$listener);
    }
$this->dispatcher = $dispatcher;
        $this->stopwatch = $stopwatch;
        $this->logger = $logger;
        $this->requestStack = $requestStack;
    }

    /** * @return void */
    public function addListener(string $eventName, callable|array $listener, int $priority = 0)
    {
        $this->dispatcher->addListener($eventName$listener$priority);
    }

    /** * @return void */
    public function addSubscriber(EventSubscriberInterface $subscriber)
    {
        $this->dispatcher->addSubscriber($subscriber);
    }

    /** * @return void */


    public function testLoadWithDisabledCacheWillCallDecoratedRoute(): void
    {
        $this->decorated
            ->expects(static::once())
            ->method('load')
            ->willReturn($this->createMock(ProductSuggestRouteResponse::class));
        $this->cache
            ->expects(static::never())
            ->method('get');
        $this->eventDispatcher->addListener(
            ProductSuggestRouteCacheKeyEvent::class,
            fn (ProductSuggestRouteCacheKeyEvent $event) => $event->disableCaching()
        );

        $this->cachedRout->load(new Request()$this->context, new Criteria());
    }

    public function testLoadWithEnabledCacheWillReturnDataFromCache(): void
    {
        $this->decorated
            ->expects(static::never())
            
->invalidateTags([self::ALL_TAG]);
    }

    /** * @dataProvider invalidationProvider */
    public function testInvalidation(\Closure $before, \Closure $after, int $calls): void
    {
        $this->getContainer()->get('cache.object')->invalidateTags([self::ALL_TAG]);

        $this->getContainer()->get('event_dispatcher')
            ->addListener(ShippingMethodRouteCacheTagsEvent::classstatic function DShippingMethodRouteCacheTagsEvent $event): void {
                $event->addTags([self::ALL_TAG]);
            });

        $route = $this->getContainer()->get(ShippingMethodRoute::class);
        $listener = $this->getMockBuilder(CallableClass::class)->getMock();
        $listener->expects(static::exactly($calls))->method('__invoke');

        $this->getContainer()
            ->get('event_dispatcher')
            ->addListener(ShippingMethodRouteCacheTagsEvent::class$listener);

        
$this->assertSame(5, $actualPriority);
  }

  public function testInitialState() {
    $this->assertEquals([]$this->dispatcher->getListeners());
    $this->assertFalse($this->dispatcher->hasListeners(self::PREFOO));
    $this->assertFalse($this->dispatcher->hasListeners(self::POSTFOO));
  }

  public function testAddListener() {
    $this->dispatcher->addListener('pre.foo', [$this->listener, 'preFoo']);
    $this->dispatcher->addListener('post.foo', [$this->listener, 'postFoo']);
    $this->assertTrue($this->dispatcher->hasListeners());
    $this->assertTrue($this->dispatcher->hasListeners(self::PREFOO));
    $this->assertTrue($this->dispatcher->hasListeners(self::POSTFOO));
    $this->assertCount(1, $this->dispatcher->getListeners(self::PREFOO));
    $this->assertCount(1, $this->dispatcher->getListeners(self::POSTFOO));
    $this->assertCount(2, $this->dispatcher->getListeners());
  }

  public function testGetListenersSortsByPriority() {
    $listener1 = new TestEventListener();
    
// We don't ask for messages to be teed, so don't expect any.     $executable = new MigrateExecutable($this->migration, $this);
    $executable->import();
    $this->assertCount(0, $this->messages);
  }

  /** * Tests migration interruptions. */
  public function testMessagesTeed() {
    // Ask to receive any messages sent to the idmap.     \Drupal::service('event_dispatcher')->addListener(MigrateEvents::IDMAP_MESSAGE,
      [$this, 'mapMessageRecorder']);
    $executable = new MigrateExecutable($this->migration, $this);
    $executable->import();
    $this->assertCount(1, $this->messages);
    $id = $this->migration->getPluginId();
    $this->assertSame("source_message: $id:message:concat: 'a message' is not an array", reset($this->messages));
  }

  /** * Tests the return value of getMessages(). * * This method returns an iterator of StdClass objects. Check that these * objects have the expected keys. */
'absolute' => TRUE,
      ];
      // Treat this as if it's user input of a path relative to the site's       // base URL.       $url = $this->unroutedUrlAssembler->assemble($uri$options);
    }
    $response = new RedirectResponse($url);
    $listener = function D$event) use ($response) {
      $event->setResponse($response);
    };
    // Add the listener to the event dispatcher.     $this->dispatcher->addListener(KernelEvents::RESPONSE, $listener);
  }

  /** * {@inheritdoc} */
  public function defaultConfiguration() {
    return [
      'url' => '',
    ];
  }

  
->invalidateTags([self::ALL_TAG]);
    }

    /** * @dataProvider invalidationProvider */
    public function testInvalidation(\Closure $before, \Closure $after, int $calls): void
    {
        $this->getContainer()->get('cache.object')->invalidateTags([self::ALL_TAG]);

        $this->getContainer()->get('event_dispatcher')
            ->addListener(LanguageRouteCacheTagsEvent::classstatic function DLanguageRouteCacheTagsEvent $event): void {
                $event->addTags([self::ALL_TAG]);
            });

        $route = $this->getContainer()->get(LanguageRoute::class);
        static::assertInstanceOf(CachedLanguageRoute::class$route);

        $listener = $this->getMockBuilder(CallableClass::class)->getMock();
        $listener->expects(static::exactly($calls))->method('__invoke');

        $this->getContainer()
            ->get('event_dispatcher')
            
use Symfony\Component\HttpKernel\KernelEvents;

class SurrogateListenerTest extends TestCase
{
    public function testFilterDoesNothingForSubRequests()
    {
        $dispatcher = new EventDispatcher();
        $kernel = $this->createMock(HttpKernelInterface::class);
        $response = new Response('foo <esi:include src="" />');
        $listener = new SurrogateListener(new Esi());

        $dispatcher->addListener(KernelEvents::RESPONSE, $listener->onKernelResponse(...));
        $event = new ResponseEvent($kernelnew Request(), HttpKernelInterface::SUB_REQUEST, $response);
        $dispatcher->dispatch($event, KernelEvents::RESPONSE);

        $this->assertEquals('', $event->getResponse()->headers->get('Surrogate-Control'));
    }

    public function testFilterWhenThereIsSomeEsiIncludes()
    {
        $dispatcher = new EventDispatcher();
        $kernel = $this->createMock(HttpKernelInterface::class);
        $response = new Response('foo <esi:include src="" />');
        
->invalidateTags([self::ALL_TAG]);
    }

    /** * @dataProvider invalidationProvider */
    public function testInvalidation(\Closure $before, \Closure $after, int $calls): void
    {
        $this->getContainer()->get('cache.object')->invalidateTags([self::ALL_TAG]);

        $this->getContainer()->get('event_dispatcher')
            ->addListener(CurrencyRouteCacheTagsEvent::classstatic function DCurrencyRouteCacheTagsEvent $event): void {
                $event->addTags([self::ALL_TAG]);
            });

        $route = $this->getContainer()->get(CurrencyRoute::class);
        static::assertInstanceOf(CachedCurrencyRoute::class$route);

        $listener = $this->getMockBuilder(CallableClass::class)->getMock();
        $listener->expects(static::exactly($calls))->method('__invoke');

        $this->getContainer()
            ->get('event_dispatcher')
            


    public function testLoadWithDisabledCacheWillCallDecoratedRoute(): void
    {
        $this->decorated
            ->expects(static::once())
            ->method('load')
            ->willReturn($this->response);
        $this->cache
            ->expects(static::never())
            ->method('get');
        $this->eventDispatcher->addListener(
            LandingPageRouteCacheKeyEvent::class,
            fn (LandingPageRouteCacheKeyEvent $event) => $event->disableCaching()
        );

        $this->cachedRoute->load('', new Request()$this->context);
    }

    public function testLoadWithEnabledCacheWillReturnDataFromCache(): void
    {
        $this->decorated
            ->expects(static::never())
            
Context::createDefaultContext()
            );
    }

    private function assertEvent(string $event): void
    {
        $listener = $this->getMockBuilder(CallableClass::class)->getMock();
        $listener->expects(static::once())->method('__invoke');

        $this->getContainer()
            ->get('event_dispatcher')
            ->addListener($event$listener);
    }

    private function createOrder(TestDataCollection $ids): void
    {
        $data = [
            'id' => $ids->create('order'),
            'orderNumber' => Uuid::randomHex(),
            'billingAddressId' => $ids->create('billing-address'),
            'currencyId' => Defaults::CURRENCY,
            'languageId' => Defaults::LANGUAGE_SYSTEM,
            'salesChannelId' => TestDefaults::SALES_CHANNEL,
            


    public function testLoadWithDisabledCacheWillCallDecoratedRoute(): void
    {
        $this->decorated
            ->expects(static::once())
            ->method('load')
            ->willReturn($this->response);
        $this->cache
            ->expects(static::never())
            ->method('get');
        $this->eventDispatcher->addListener(
            CurrencyRouteCacheKeyEvent::class,
            fn (CurrencyRouteCacheKeyEvent $event) => $event->disableCaching()
        );

        $this->cachedRoute->load(new Request()$this->context, new Criteria());
    }

    public function testLoadWithEnabledCacheWillReturnDataFromCache(): void
    {
        $this->decorated
            ->expects(static::never())
            
$path = $file->getPath();
        $progress->setTotal($this->filesystem->fileSize($path));
        $invalidRecordsProgress = null;

        $failedRecords = [];

        $resource = $this->filesystem->readStream($path);
        $config = Config::fromLog($this->logEntity);
        $overallResults = $this->logEntity->getResult();

        $this->eventDispatcher->addListener(WriteCommandExceptionEvent::class$this->onWriteException(...));

        $createEntities = $config->get('createEntities') ?? true;
        $updateEntities = $config->get('updateEntities') ?? true;

        $context->addState(Context::SKIP_TRIGGER_FLOW);

        foreach ($this->reader->read($config$resource$offset) as $row) {
            $event = new ImportExportBeforeImportRowEvent($row$config$context);
            $this->eventDispatcher->dispatch($event);
            $row = $event->getRow();

            
Home | Imprint | This part of the site doesn't use cookies.