Skip to content

Commit

Permalink
Remove unused api cart blamer method add missing spec
Browse files Browse the repository at this point in the history
  • Loading branch information
arti0090 committed Apr 13, 2021
1 parent ccf0808 commit 1a7f337
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

use Sylius\Bundle\ApiBundle\SectionResolver\ShopApiOrdersSubSection;
use Sylius\Bundle\CoreBundle\SectionResolver\SectionProviderInterface;
use Sylius\Bundle\UserBundle\Event\UserEvent;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\ShopUserInterface;
use Sylius\Component\Order\Context\CartContextInterface;
Expand All @@ -39,20 +38,6 @@ public function __construct(
$this->uriBasedSectionContext = $uriBasedSectionContext;
}

public function onImplicitLogin(UserEvent $userEvent): void
{
if (!$this->uriBasedSectionContext->getSection() instanceof ShopApiOrdersSubSection) {
return;
}

$user = $userEvent->getUser();
if (!$user instanceof ShopUserInterface) {
return;
}

$this->blame($user);
}

public function onInteractiveLogin(InteractiveLoginEvent $interactiveLoginEvent): void
{
if (!$this->uriBasedSectionContext->getSection() instanceof ShopApiOrdersSubSection) {
Expand Down
1 change: 0 additions & 1 deletion src/Sylius/Bundle/ApiBundle/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,6 @@
<service id="sylius.listener.api_cart_blamer" class="Sylius\Bundle\ApiBundle\EventListener\ApiCartBlamerListener">
<argument type="service" id="sylius.context.cart" />
<argument type="service" id="sylius.section_resolver.uri_based_section_resolver" />
<tag name="kernel.event_listener" event="sylius.user.security.implicit_login" method="onImplicitLogin" />
<tag name="kernel.event_listener" event="security.interactive_login" method="onInteractiveLogin" />
</service>
</services>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace spec\Sylius\Bundle\ApiBundle\EventListener;

use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Sylius\Bundle\ApiBundle\SectionResolver\AdminApiSection;
use Sylius\Bundle\ApiBundle\SectionResolver\ShopApiOrdersSubSection;
use Sylius\Bundle\CoreBundle\SectionResolver\SectionInterface;
use Sylius\Bundle\CoreBundle\SectionResolver\SectionProviderInterface;
use Sylius\Bundle\ShopBundle\SectionResolver\ShopSection;
use Sylius\Component\Core\Model\CustomerInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\ShopUserInterface;
use Sylius\Component\Order\Context\CartContextInterface;
use Sylius\Component\Order\Context\CartNotFoundException;
use Sylius\Component\Order\Model\OrderInterface as BaseOrderInterface;
use Sylius\Component\Resource\Exception\UnexpectedTypeException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;

final class ApiCartBlamerListenerSpec extends ObjectBehavior
{
function let(
CartContextInterface $cartContext,
SectionProviderInterface $sectionResolver
): void {
$this->beConstructedWith($cartContext, $sectionResolver);
}

function it_throws_an_exception_when_cart_does_not_implement_core_order_interface_on_interactive_login(
BaseOrderInterface $order,
CartContextInterface $cartContext,
SectionProviderInterface $sectionResolver,
ShopUserInterface $user,
Request $request,
TokenInterface $token,
ShopApiOrdersSubSection $shopApiOrdersSubSectionSection
): void {
$sectionResolver->getSection()->willReturn($shopApiOrdersSubSectionSection);
$cartContext->getCart()->willReturn($order);
$token->getUser()->willReturn($user);

$this
->shouldThrow(UnexpectedTypeException::class)
->during('onInteractiveLogin', [new InteractiveLoginEvent($request->getWrappedObject(), $token->getWrappedObject())])
;
}

function it_blames_cart_on_user_on_interactive_login(
CartContextInterface $cartContext,
SectionProviderInterface $sectionResolver,
OrderInterface $cart,
Request $request,
TokenInterface $token,
ShopUserInterface $user,
CustomerInterface $customer,
ShopApiOrdersSubSection $shopApiOrdersSubSectionSection
): void {
$sectionResolver->getSection()->willReturn($shopApiOrdersSubSectionSection);
$cartContext->getCart()->willReturn($cart);
$cart->getCustomer()->willReturn(null);
$token->getUser()->willReturn($user);
$user->getCustomer()->willReturn($customer);

$cart->setCustomer($customer)->shouldBeCalled();

$this->onInteractiveLogin(new InteractiveLoginEvent($request->getWrappedObject(), $token->getWrappedObject()));
}

function it_does_nothing_if_given_cart_has_been_blamed_in_past(
CartContextInterface $cartContext,
SectionProviderInterface $sectionResolver,
OrderInterface $cart,
Request $request,
TokenInterface $token,
CustomerInterface $customer,
ShopSection $shopSection
): void {
$sectionResolver->getSection()->willReturn($shopSection);
$cartContext->getCart()->willReturn($cart);
$cart->getCustomer()->willReturn($customer);

$cart->setCustomer(Argument::any())->shouldNotBeCalled();

$this->onInteractiveLogin(new InteractiveLoginEvent($request->getWrappedObject(), $token->getWrappedObject()));
}

function it_does_nothing_if_given_user_is_invalid_on_interactive_login(
CartContextInterface $cartContext,
SectionProviderInterface $sectionResolver,
OrderInterface $cart,
Request $request,
TokenInterface $token,
ShopSection $shopSection
): void {
$sectionResolver->getSection()->willReturn($shopSection);
$cartContext->getCart()->willReturn($cart);
$token->getUser()->willReturn('anon.');

$cart->setCustomer(Argument::any())->shouldNotBeCalled();

$this->onInteractiveLogin(new InteractiveLoginEvent($request->getWrappedObject(), $token->getWrappedObject()));
}

function it_does_nothing_if_there_is_no_existing_cart_on_interactive_login(
CartContextInterface $cartContext,
SectionProviderInterface $sectionResolver,
Request $request,
TokenInterface $token,
ShopUserInterface $user,
ShopSection $shopSection
): void {
$sectionResolver->getSection()->willReturn($shopSection);
$cartContext->getCart()->willThrow(CartNotFoundException::class);
$token->getUser()->willReturn($user);

$this->onInteractiveLogin(new InteractiveLoginEvent($request->getWrappedObject(), $token->getWrappedObject()));
}

function it_does_nothing_if_the_current_section_is_not_shop_on_interactive_login(
CartContextInterface $cartContext,
SectionProviderInterface $sectionResolver,
Request $request,
TokenInterface $token,
SectionInterface $section
): void {
$sectionResolver->getSection()->willReturn($section);

$token->getUser()->shouldNotBeCalled();
$cartContext->getCart()->shouldNotBeCalled();

$this->onInteractiveLogin(new InteractiveLoginEvent($request->getWrappedObject(), $token->getWrappedObject()));
}

function it_does_nothing_if_the_current_section_is_not_orders_subsection(
CartContextInterface $cartContext,
SectionProviderInterface $sectionResolver,
Request $request,
TokenInterface $token,
AdminApiSection $section
): void {
$sectionResolver->getSection()->willReturn($section);

$token->getUser()->shouldNotBeCalled();
$cartContext->getCart()->shouldNotBeCalled();

$this->onInteractiveLogin(new InteractiveLoginEvent($request->getWrappedObject(), $token->getWrappedObject()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

namespace spec\Sylius\Bundle\ShopBundle\EventListener;

use Doctrine\Persistence\ObjectManager;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Sylius\Bundle\CoreBundle\SectionResolver\SectionInterface;
Expand Down

0 comments on commit 1a7f337

Please sign in to comment.