-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
2 changed files
with
118 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters