removeListener example

$this->importExportService->saveProgress($progress);

            $overallResults = $this->logResults($overallResults$result$this->repository->getDefinition()->getEntityName());

            ++$processed;
            if ($this->importLimit > 0 && $processed >= $this->importLimit) {
                break;
            }
        }
        $progress->setOffset($this->reader->getOffset());

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

        if (!empty($failedRecords)) {
            $invalidRecordsProgress = $this->exportInvalid($context$failedRecords);
            $progress->setInvalidRecordsLogId($invalidRecordsProgress->getLogId());
        }

        // importing the file is complete         if ($this->reader->getOffset() === $this->filesystem->fileSize($path)) {
            if ($this->logEntity->getInvalidRecordsLog() instanceof ImportExportLogEntity) {
                /** @var ImportExportLogEntity $invalidLog */
                $invalidLog = $this->logEntity->getInvalidRecordsLog();
                
$eventDispatcher = static::getContainer()->get(EventDispatcherInterface::class);
        $wrappedCallable = function DRequestEvent $event) use (&$callable) {
            $callable();
            $event->setResponse(new Response(''));
            $event->stopPropagation();
        };

        $eventDispatcher->addListener(KernelEvents::REQUEST, $wrappedCallable);
        try {
            $client->request('GET', '/'.uniqid('', true));
        } finally {
            $eventDispatcher->removeListener(KernelEvents::REQUEST, $wrappedCallable);
        }
    }
}
$this->dispatcher->addListener('pre.foo', $listener1, -10);
        $this->dispatcher->addListener('pre.foo', $listener2);
        $this->dispatcher->addListener('pre.foo', $listener3, 10);
        $this->dispatcher->dispatch(new Event(), self::preFoo);
        $this->assertEquals(['3', '2', '1']$invoked);
    }

    public function testRemoveListener()
    {
        $this->dispatcher->addListener('pre.bar', $this->listener);
        $this->assertTrue($this->dispatcher->hasListeners(self::preBar));
        $this->dispatcher->removeListener('pre.bar', $this->listener);
        $this->assertFalse($this->dispatcher->hasListeners(self::preBar));
        $this->dispatcher->removeListener('notExists', $this->listener);
    }

    public function testAddSubscriber()
    {
        $eventSubscriber = new TestEventSubscriber();
        $this->dispatcher->addSubscriber($eventSubscriber);
        $this->assertTrue($this->dispatcher->hasListeners(self::preFoo));
        $this->assertTrue($this->dispatcher->hasListeners(self::postFoo));
    }

    

    }

    /** * @return void */
    public function removeSubscriber(EventSubscriberInterface $subscriber)
    {
        foreach ($subscriber->getSubscribedEvents() as $eventName => $params) {
            if (\is_array($params) && \is_array($params[0])) {
                foreach ($params as $listener) {
                    $this->removeListener($eventName[$subscriber$listener[0]]);
                }
            } else {
                $this->removeListener($eventName[$subscriber, \is_string($params) ? $params : $params[0]]);
            }
        }
    }

    /** * Triggers the listeners of an event. * * This method can be overridden to add functionality that is executed * for each listener. * * @param callable[] $listeners The event listeners * @param string $eventName The name of the event to dispatch * @param object $event The event object to pass to the event handlers/listeners * * @return void */
$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);
    }

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

    /** * @return array<array-key, array<array-key, callable(object): void>|callable(object): void> */
    public function getListeners(?string $eventName = null): array
    {
\json_encode([
                    'orderId' => $this->orderId,
                    'paymentMethodId' => $newPaymentMethodId,
                ], \JSON_THROW_ON_ERROR) ?: ''
            );

        $response = json_decode((string) $this->browser->getResponse()->getContent(), true, 512, \JSON_THROW_ON_ERROR);

        static::assertArrayHasKey('success', $responseprint_r($response, true));
        static::assertTrue($response['success']print_r($response, true));

        $dispatcher->removeListener(MailSentEvent::class$listenerClosure);

        static::assertTrue($eventDidRun, 'The mail.sent Event did not run');
    }

    public function testSetSamePaymentMethodToOrder(): void
    {
        $dispatcher = $this->getContainer()->get('event_dispatcher');
        $phpunit = $this;
        $eventDidRun = false;
        $listenerClosure = function DMailSentEvent $event) use (&$eventDidRun$phpunit): void {
            $eventDidRun = true;
            
$this->salesChannelContext->setRuleIds([$priceRuleId]);
        $this->productRepository = $this->getContainer()->get('product.repository');
        $this->invoiceRenderer = $this->getContainer()->get(InvoiceRenderer::class);
        $this->cartService = $this->getContainer()->get(CartService::class);
        self::$deLanguageId = $this->getDeDeLanguageId();
    }

    protected function tearDown(): void
    {
        if (self::$callback instanceof \Closure) {
            $this->getContainer()->get('event_dispatcher')->removeListener(DocumentTemplateRendererParameterEvent::class, self::$callback);
        }
    }

    /** * @dataProvider invoiceDataProvider * * @param array<int|string, int> $possibleTaxes */
    public function testRender(array $possibleTaxes, ?\Closure $beforeRenderHook, \Closure $assertionCallback): void
    {
        $cart = $this->generateDemoCart($possibleTaxes);
        
$runWorker = function Dstring $transportName) use ($transports$bus$dispatcher): ?\Throwable {
            $throwable = null;
            $failedListener = function DWorkerMessageFailedEvent $event) use (&$throwable) {
                $throwable = $event->getThrowable();
            };
            $dispatcher->addListener(WorkerMessageFailedEvent::class$failedListener);

            $worker = new Worker([$transportName => $transports[$transportName]]$bus$dispatcher);

            $worker->run();

            $dispatcher->removeListener(WorkerMessageFailedEvent::class$failedListener);

            return $throwable;
        };

        // send the message         $envelope = new Envelope(new DummyMessage('API'));
        $bus->dispatch($envelope);

        // message has been sent         $this->assertCount(1, $transport1->getMessagesWaitingToBeReceived());
        $this->assertCount(1, $transport2->getMessagesWaitingToBeReceived());
        
public function testAddSubscriberDisallowed()
    {
        $this->expectException(\BadMethodCallException::class);
        $subscriber = $this->createMock(EventSubscriberInterface::class);

        $this->dispatcher->addSubscriber($subscriber);
    }

    public function testRemoveListenerDisallowed()
    {
        $this->expectException(\BadMethodCallException::class);
        $this->dispatcher->removeListener('event', fn () => 'foo');
    }

    public function testRemoveSubscriberDisallowed()
    {
        $this->expectException(\BadMethodCallException::class);
        $subscriber = $this->createMock(EventSubscriberInterface::class);

        $this->dispatcher->removeSubscriber($subscriber);
    }
}
$this->appStateService->deactivateApp($appId$context);

        static::assertTrue($eventWasReceived);

        $criteria = new Criteria();
        $criteria->addFilter(new EqualsFilter('appId', $appId));
        $criteria->addFilter(new EqualsFilter('active', true));

        static::assertEquals(0, $this->templateRepo->search($criteria$context)->getTotal());

        $this->eventDispatcher->removeListener(AppDeactivatedEvent::class$onAppDeactivation);
    }

    public function testAppWithAThemeCanBeActivated(): void
    {
        $this->loadAppsFromDir(__DIR__ . '/../../Theme/fixtures/Apps/theme', false);
        $context = Context::createDefaultContext();

        $criteria = new Criteria();
        $criteria->addFilter(new EqualsFilter('name', 'SwagTheme'));
        /** @var string $appId */
        $appId = $this->appRepo->searchIds($criteria$context)->firstId();

        

        if (isset($this->wrappedListeners[$eventName])) {
            foreach ($this->wrappedListeners[$eventName] as $index => $wrappedListener) {
                if ($wrappedListener->getWrappedListener() === $listener || ($listener instanceof \Closure && $wrappedListener->getWrappedListener() == $listener)) {
                    $listener = $wrappedListener;
                    unset($this->wrappedListeners[$eventName][$index]);
                    break;
                }
            }
        }

        $this->dispatcher->removeListener($eventName$listener);
    }

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

    public function getListeners(string $eventName = null): array
    {
$event->addSuccess($successSpy(...));
            $event->addError($errorSpy(...));
        });

        $this->getContainer()->get('event_dispatcher')->addListener($eventClass$spy);

        $this->productRepository->delete($delete, Context::createDefaultContext());

        static::assertTrue($successSpy->called);
        static::assertFalse($errorSpy->called);

        $this->getContainer()->get('event_dispatcher')->removeListener($eventClass$spy);
    }

    /** * @dataProvider eventClasses */
    public function testMultipleCallbacksAreCalledOnTheSameEntityDeleteEvent(string $eventClass): void
    {
        $id = $this->ids->get('product');

        $delete = [['id' => $id]];

        
$phpunit = $this;
        $eventDidRun = false;
        $listenerClosure = function DMailSentEvent $event) use (&$eventDidRun$phpunit): void {
            $eventDidRun = true;
            $phpunit->assertStringContainsString('Shipping costs: €0.00', $event->getContents()['text/html']);
        };

        $this->addEventListener($dispatcher, MailSentEvent::class$listenerClosure);

        $cartService->order($cart$contextnew RequestDataBag());

        $dispatcher->removeListener(MailSentEvent::class$listenerClosure);

        static::assertTrue($eventDidRun, 'The mail.sent Event did not run');
    }

    public function testCartCreatedWithGivenToken(): void
    {
        $salesChannelContextFactory = $this->getContainer()->get(SalesChannelContextFactory::class);
        $context = $salesChannelContextFactory->create(Uuid::randomHex(), TestDefaults::SALES_CHANNEL);

        $token = Uuid::randomHex();
        $cartService = $this->getContainer()->get(CartService::class);
        
->request(
                'DELETE',
                '/store-api/customer/wishlist/delete/' . $productId
            );

        $response = json_decode((string) $this->browser->getResponse()->getContent(), true, 512, \JSON_THROW_ON_ERROR);

        static::assertSame(200, $this->browser->getResponse()->getStatusCode());
        static::assertTrue($response['success']);
        static::assertTrue($eventWasThrown);

        $dispatcher->removeListener(WishlistProductRemovedEvent::class$listener);
    }

    public function testDeleteProductShouldThrowCustomerWishlistNotActivatedException(): void
    {
        $productData = $this->createProduct($this->context);
        $this->systemConfigService->set('core.cart.wishlistEnabled', false);

        $this->browser
            ->request(
                'DELETE',
                '/store-api/customer/wishlist/delete/' . $productData[0]
            );
$route->load($idnew Request()$context);
        $route->load($idnew Request()$context);

        $after($ids$context$this->getContainer());

        $route->load($idnew Request()$context);
        $route->load($idnew Request()$context);

        $this->getContainer()
            ->get('event_dispatcher')
            ->removeListener(CategoryRouteCacheTagsEvent::class$listener);
    }

    public static function invalidationProvider(): \Generator
    {
        $ids = new IdsCollection();

        yield 'Test call multiple times without change' => [
            $ids,
            function DIdsCollection $ids, SalesChannelContext $context): void {
            },
            1,
        ];
Home | Imprint | This part of the site doesn't use cookies.