SalesChannelContextCreatedEvent example


#[Package('buyers-experience')] class SalesChannelContextCreatedEventTest extends TestCase
{
    public function testEventReturnsAllNeededData(): void
    {
        $token = 'foo';
        $context = Context::createDefaultContext();
        $salesChannelContext = $this->createMock(SalesChannelContext::class);
        $salesChannelContext->method('getContext')->willReturn($context);

        $event = new SalesChannelContextCreatedEvent($salesChannelContext$token);
        static::assertSame($token$event->getUsedToken());
        static::assertSame($context$event->getContext());
        static::assertSame($salesChannelContext$event->getSalesChannelContext());
    }
}
$persister->method('load')->willReturn($session);

        $factory = $this->createMock(SalesChannelContextFactory::class);
        $factory->expects(static::once())
            ->method('create')
            ->with($token, TestDefaults::SALES_CHANNEL, $session)
            ->willReturn($context);

        $dispatcher = $this->createMock(EventDispatcherInterface::class);
        $dispatcher->expects(static::once())
            ->method('dispatch')
            ->with(new SalesChannelContextCreatedEvent($context$token));

        $service = new SalesChannelContextService(
            $factory,
            $this->createMock(CartRuleLoader::class),
            $persister,
            $this->createMock(CartService::class),
            $dispatcher,
        );

        $service->get(new SalesChannelContextServiceParameters(TestDefaults::SALES_CHANNEL, $token));
    }
}


            if ($parameters->getOriginalContext() !== null) {
                $session[self::ORIGINAL_CONTEXT] = $parameters->getOriginalContext();
            }

            if ($parameters->getCustomerId() !== null) {
                $session[self::CUSTOMER_ID] = $parameters->getCustomerId();
            }

            $context = $this->factory->create($token$parameters->getSalesChannelId()$session);
            $this->eventDispatcher->dispatch(new SalesChannelContextCreatedEvent($context$token));

            $result = $this->ruleLoader->loadByToken($context$token);

            $this->cartService->setCart($result->getCart());

            return $context;
        });
    }
}
Home | Imprint | This part of the site doesn't use cookies.