diff --git a/spec/EventListener/CartBlamerListenerSpec.php b/spec/EventListener/CartBlamerListenerSpec.php new file mode 100644 index 000000000..abf26556f --- /dev/null +++ b/spec/EventListener/CartBlamerListenerSpec.php @@ -0,0 +1,113 @@ +beConstructedWith($orderRepository, $bus, $requestStack); + } + + function it_should_be_initializable(): void + { + $this->beAnInstanceOf(CartBlamerListener::class); + } + + function it_assigns_the_cart_to_the_logged_in_user( + OrderRepositoryInterface $orderRepository, + OrderInterface $cart, + MessageBusInterface $bus, + RequestStack $requestStack, + ShopUserInterface $shopUser, + CustomerInterface $customer, + JWTCreatedEvent $jwtCreated + ): void { + $request = new Request([], ['token' => 'CART_TOKEN']); + $requestStack->getCurrentRequest()->willReturn($request); + + $jwtCreated->getUser()->willReturn($shopUser); + $shopUser->getCustomer()->willReturn($customer); + $customer->getEmail()->willReturn('test@sylius.com'); + + $orderRepository->findOneBy(['tokenValue' => 'CART_TOKEN'])->willReturn($cart); + + $assignCartCommand = new AssignCustomerToCart('CART_TOKEN', 'test@sylius.com'); + $bus->dispatch($assignCartCommand)->willReturn(new Envelope($assignCartCommand))->shouldBeCalled(); + + $this->onJwtLogin($jwtCreated); + } + + function it_does_not_assign_the_cart_to_admin_user( + OrderRepositoryInterface $orderRepository, + MessageBusInterface $bus, + RequestStack $requestStack, + AdminUserInterface $adminUser, + JWTCreatedEvent $jwtCreated + ): void { + $request = new Request([], ['token' => 'CART_TOKEN']); + $requestStack->getCurrentRequest()->willReturn($request); + + $jwtCreated->getUser()->willReturn($adminUser); + + $orderRepository->findOneBy(Argument::any())->shouldNotBeCalled(); + + $bus->dispatch(Argument::any())->shouldNotBeCalled(); + + $this->onJwtLogin($jwtCreated); + } + + function it_does_not_assign_a_cart_if_there_is_no_cart_with_this_token( + OrderRepositoryInterface $orderRepository, + MessageBusInterface $bus, + RequestStack $requestStack, + ShopUserInterface $shopUser, + CustomerInterface $customer, + JWTCreatedEvent $jwtCreated + ): void { + $request = new Request([], ['token' => 'CART_TOKEN']); + $requestStack->getCurrentRequest()->willReturn($request); + + $jwtCreated->getUser()->willReturn($shopUser); + $shopUser->getCustomer()->willReturn($customer); + $customer->getEmail()->willReturn('test@sylius.com'); + + $orderRepository->findOneBy(['tokenValue' => 'CART_TOKEN'])->willReturn(null); + + $bus->dispatch(Argument::any())->shouldNotBeCalled(); + + $this->onJwtLogin($jwtCreated); + } + + function it_does_not_assign_a_cart_if_the_token_was_created_on_the_console( + OrderRepositoryInterface $orderRepository, + MessageBusInterface $bus, + RequestStack $requestStack, + JWTCreatedEvent $jwtCreated + ): void { + $requestStack->getCurrentRequest()->willReturn(null); + + $orderRepository->findOneBy(Argument::any())->shouldNotBeCalled(); + + $bus->dispatch(Argument::any())->shouldNotBeCalled(); + + $this->onJwtLogin($jwtCreated); + } +} diff --git a/src/EventListener/CartBlamerListener.php b/src/EventListener/CartBlamerListener.php index 560f704d4..3be980e2c 100644 --- a/src/EventListener/CartBlamerListener.php +++ b/src/EventListener/CartBlamerListener.php @@ -10,7 +10,6 @@ use Sylius\ShopApiPlugin\Command\Cart\AssignCustomerToCart; use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\Messenger\MessageBusInterface; -use Webmozart\Assert\Assert; final class CartBlamerListener { @@ -35,11 +34,13 @@ public function __construct( public function onJwtLogin(JWTCreatedEvent $interactiveLoginEvent): void { - $user = $interactiveLoginEvent->getUser(); $request = $this->requestStack->getCurrentRequest(); + // If there is no request then it was a console login, where there is no user to be assigned a cart + if ($request === null) { + return; + } - Assert::notNull($request); - + $user = $interactiveLoginEvent->getUser(); if (!$user instanceof ShopUserInterface) { return; }