From 33640a118ae7410ef965885f8fb3c0a0bcea4acf Mon Sep 17 00:00:00 2001 From: mamazu <14860264+mamazu@users.noreply.github.com> Date: Mon, 29 Jun 2020 19:00:46 +0200 Subject: [PATCH 1/2] Fixes #657 --- src/EventListener/CartBlamerListener.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) 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; } From 3ea83efc03627ccb8a6edc7c509a693736b1e972 Mon Sep 17 00:00:00 2001 From: mamazu Date: Mon, 29 Jun 2020 22:56:09 +0200 Subject: [PATCH 2/2] Adding tests for cart blamer --- spec/EventListener/CartBlamerListenerSpec.php | 113 ++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 spec/EventListener/CartBlamerListenerSpec.php 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); + } +}