Skip to content

Commit

Permalink
bug #658 Fixes #657 (mamazu)
Browse files Browse the repository at this point in the history
This PR was merged into the 1.0-dev branch.

Discussion
----------

If the login was from the console we don't want to assign a cart to the user object.

Commits
-------

33640a1 Fixes #657
3ea83ef Adding tests for cart blamer
  • Loading branch information
mamazu authored Jul 1, 2020
2 parents b6818fa + 3ea83ef commit 3ace127
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 4 deletions.
113 changes: 113 additions & 0 deletions spec/EventListener/CartBlamerListenerSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

declare(strict_types=1);

namespace spec\Sylius\ShopApiPlugin\EventListener;

use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTCreatedEvent;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Sylius\Component\Core\Model\AdminUserInterface;
use Sylius\Component\Core\Model\CustomerInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\ShopUserInterface;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
use Sylius\ShopApiPlugin\Command\Cart\AssignCustomerToCart;
use Sylius\ShopApiPlugin\EventListener\CartBlamerListener;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\MessageBusInterface;

final class CartBlamerListenerSpec extends ObjectBehavior
{
function let(OrderRepositoryInterface $orderRepository, MessageBusInterface $bus, RequestStack $requestStack): void
{
$this->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('[email protected]');

$orderRepository->findOneBy(['tokenValue' => 'CART_TOKEN'])->willReturn($cart);

$assignCartCommand = new AssignCustomerToCart('CART_TOKEN', '[email protected]');
$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('[email protected]');

$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);
}
}
9 changes: 5 additions & 4 deletions src/EventListener/CartBlamerListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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;
}
Expand Down

0 comments on commit 3ace127

Please sign in to comment.