Skip to content

Commit

Permalink
Improve logged in shop user provider
Browse files Browse the repository at this point in the history
  • Loading branch information
lchrusciel committed Jan 3, 2019
1 parent 65d9b44 commit 97507e6
Show file tree
Hide file tree
Showing 17 changed files with 119 additions and 91 deletions.
14 changes: 7 additions & 7 deletions spec/Handler/CompleteOrderHandlerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Sylius\Component\Resource\Factory\FactoryInterface;
use Sylius\ShopApiPlugin\Command\CompleteOrder;
use Sylius\ShopApiPlugin\Exception\WrongUserException;
use Sylius\ShopApiPlugin\Provider\LoggedInUserProviderInterface;
use Sylius\ShopApiPlugin\Provider\LoggedInShopUserProviderInterface;
use Symfony\Component\Security\Core\Exception\TokenNotFoundException;

final class CompleteOrderHandlerSpec extends ObjectBehavior
Expand All @@ -26,7 +26,7 @@ function let(
OrderRepositoryInterface $orderRepository,
CustomerRepositoryInterface $customerRepository,
FactoryInterface $customerFactory,
LoggedInUserProviderInterface $loggedInUserProvider,
LoggedInShopUserProviderInterface $loggedInUserProvider,
StateMachineFactoryInterface $stateMachineFactory
): void {
$this->beConstructedWith($orderRepository, $customerRepository, $customerFactory, $loggedInUserProvider, $stateMachineFactory);
Expand All @@ -35,7 +35,7 @@ function let(
function it_handles_order_completion_for_guest_checkout(
CustomerInterface $customer,
CustomerRepositoryInterface $customerRepository,
LoggedInUserProviderInterface $loggedInUserProvider,
LoggedInShopUserProviderInterface $loggedInUserProvider,
FactoryInterface $customerFactory,
OrderInterface $order,
OrderRepositoryInterface $orderRepository,
Expand All @@ -61,7 +61,7 @@ function it_handles_order_completion_for_guest_checkout(
function it_throws_an_exception_if_the_email_address_has_already_a_customer(
CustomerInterface $customer,
CustomerRepositoryInterface $customerRepository,
LoggedInUserProviderInterface $loggedInUserProvider,
LoggedInShopUserProviderInterface $loggedInUserProvider,
ShopUserInterface $shopUser,
OrderInterface $order,
OrderRepositoryInterface $orderRepository,
Expand All @@ -87,7 +87,7 @@ function it_throws_an_exception_if_the_email_address_has_already_a_customer(

function it_handles_order_completetion(
CustomerRepositoryInterface $customerRepository,
LoggedInUserProviderInterface $loggedInUserProvider,
LoggedInShopUserProviderInterface $loggedInUserProvider,
CustomerInterface $loggedInCustomer,
ShopUserInterface $shopUser,
OrderInterface $order,
Expand All @@ -114,7 +114,7 @@ function it_handles_order_completetion(
function it_handles_order_completion_with_notes(
CustomerInterface $customer,
CustomerRepositoryInterface $customerRepository,
LoggedInUserProviderInterface $loggedInUserProvider,
LoggedInShopUserProviderInterface $loggedInUserProvider,
ShopUserInterface $shopUser,
OrderInterface $order,
OrderRepositoryInterface $orderRepository,
Expand Down Expand Up @@ -150,7 +150,7 @@ function it_throws_an_exception_if_order_does_not_exist(
function it_throws_an_exception_if_the_user_is_logged_in_and_provides_email(
CustomerInterface $customer,
CustomerRepositoryInterface $customerRepository,
LoggedInUserProviderInterface $loggedInUserProvider,
LoggedInShopUserProviderInterface $loggedInUserProvider,
CustomerInterface $loggedInCustomer,
ShopUserInterface $shopUser,
OrderInterface $order,
Expand Down
65 changes: 65 additions & 0 deletions spec/Provider/LoggedInShopUserProviderSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

namespace spec\Sylius\ShopApiPlugin\Provider;

use PhpSpec\ObjectBehavior;
use Sylius\Component\Core\Model\ShopUserInterface;
use Sylius\Component\User\Model\UserInterface;
use Sylius\ShopApiPlugin\Provider\LoggedInShopUserProviderInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\TokenNotFoundException;

final class LoggedInShopUserProviderSpec extends ObjectBehavior
{
function let(TokenStorageInterface $tokenStorage): void
{
$this->beConstructedWith($tokenStorage);
}

function it_is_reviewer_subject_provider(): void
{
$this->shouldImplement(LoggedInShopUserProviderInterface::class);
}

function it_throws_an_error_if_there_is_no_shop_user_logged_in(
TokenStorageInterface $tokenStorage,
TokenInterface $token,
UserInterface $anotherUser
): void {
$tokenStorage->getToken()->willReturn(null, $token);
$token->getUser()->willReturn(null, $anotherUser);

$this->shouldThrow(TokenNotFoundException::class)->during('provide');
$this->shouldThrow(TokenNotFoundException::class)->during('provide');
$this->shouldThrow(TokenNotFoundException::class)->during('provide');
}

function it_returns_the_logged_in_user_if_there_is_one(
TokenStorageInterface $tokenStorage,
TokenInterface $token,
ShopUserInterface $shopUser
): void {
$token->getUser()->willReturn($shopUser);
$tokenStorage->getToken()->willReturn($token);

$this->provide()->shouldReturn($shopUser);
}

function it_checks_if_shop_user_is_logged_in(
TokenStorageInterface $tokenStorage,
TokenInterface $token,
ShopUserInterface $shopUser,
UserInterface $anotherUser
): void {
$tokenStorage->getToken()->willReturn(null, $token);
$token->getUser()->willReturn(null, $anotherUser, $shopUser);

$this->check()->shouldReturn(false);
$this->check()->shouldReturn(false);
$this->check()->shouldReturn(false);
$this->check()->shouldReturn(true);
}
}
46 changes: 0 additions & 46 deletions spec/Provider/LoggedInUserProviderSpec.php

This file was deleted.

8 changes: 4 additions & 4 deletions spec/Validator/Address/AddressExistsValidatorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Sylius\Component\Core\Model\CustomerInterface;
use Sylius\Component\Core\Model\ShopUserInterface;
use Sylius\Component\Core\Repository\AddressRepositoryInterface;
use Sylius\ShopApiPlugin\Provider\LoggedInUserProviderInterface;
use Sylius\ShopApiPlugin\Provider\LoggedInShopUserProviderInterface;
use Sylius\ShopApiPlugin\Validator\Constraints\AddressExists;
use Symfony\Component\Validator\Context\ExecutionContextInterface;

Expand All @@ -19,7 +19,7 @@ final class AddressExistsValidatorSpec extends ObjectBehavior
function let(
ExecutionContextInterface $executionContext,
AddressRepositoryInterface $addressRepository,
LoggedInUserProviderInterface $currentUserProvider
LoggedInShopUserProviderInterface $currentUserProvider
): void {
$this->beConstructedWith($addressRepository, $currentUserProvider);

Expand All @@ -30,7 +30,7 @@ function it_does_not_add_constraint_if_address_exists_and_its_owned_by_current_u
AddressInterface $address,
ShopUserInterface $shopUser,
CustomerInterface $customerOwner,
LoggedInUserProviderInterface $currentUserProvider,
LoggedInShopUserProviderInterface $currentUserProvider,
AddressRepositoryInterface $addressRepository,
ExecutionContextInterface $executionContext
): void {
Expand Down Expand Up @@ -61,7 +61,7 @@ function it_adds_constraint_if_address_does_not_exits_exists(
function it_adds_constraint_if_current_user_is_not_address_owner(
AddressInterface $address,
AddressRepositoryInterface $addressRepository,
LoggedInUserProviderInterface $currentUserProvider,
LoggedInShopUserProviderInterface $currentUserProvider,
ShopUserInterface $shopUser,
CustomerInterface $customerOwner,
ExecutionContextInterface $executionContext
Expand Down
6 changes: 3 additions & 3 deletions src/Controller/AddressBook/CreateAddressAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Sylius\ShopApiPlugin\Factory\AddressBookViewFactoryInterface;
use Sylius\ShopApiPlugin\Factory\ValidationErrorViewFactoryInterface;
use Sylius\ShopApiPlugin\Model\Address;
use Sylius\ShopApiPlugin\Provider\LoggedInUserProviderInterface;
use Sylius\ShopApiPlugin\Provider\LoggedInShopUserProviderInterface;
use Sylius\ShopApiPlugin\View\AddressBookView;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand All @@ -35,7 +35,7 @@ final class CreateAddressAction
/** @var ValidationErrorViewFactoryInterface */
private $validationErrorViewFactory;

/** @var LoggedInUserProviderInterface */
/** @var LoggedInShopUserProviderInterface */
private $loggedInUserProvider;

/** @var AddressBookViewFactoryInterface */
Expand All @@ -51,7 +51,7 @@ public function __construct(
ValidationErrorViewFactoryInterface $validationErrorViewFactory,
AddressBookViewFactoryInterface $addressViewFactory,
AddressRepositoryInterface $addressRepository,
LoggedInUserProviderInterface $loggedInUserProvider
LoggedInShopUserProviderInterface $loggedInUserProvider
) {
$this->viewHandler = $viewHandler;
$this->bus = $bus;
Expand Down
6 changes: 3 additions & 3 deletions src/Controller/AddressBook/RemoveAddressAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Sylius\Component\Core\Model\ShopUserInterface;
use Sylius\ShopApiPlugin\Command\RemoveAddress;
use Sylius\ShopApiPlugin\Factory\ValidationErrorViewFactory;
use Sylius\ShopApiPlugin\Provider\LoggedInUserProviderInterface;
use Sylius\ShopApiPlugin\Provider\LoggedInShopUserProviderInterface;
use Sylius\ShopApiPlugin\Request\RemoveAddressRequest;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand All @@ -31,15 +31,15 @@ final class RemoveAddressAction
/** @var CommandBus */
private $bus;

/** @var LoggedInUserProviderInterface */
/** @var LoggedInShopUserProviderInterface */
private $loggedInUserProvider;

public function __construct(
ViewHandlerInterface $viewHandler,
ValidatorInterface $validator,
ValidationErrorViewFactory $validationErrorViewFactory,
CommandBus $bus,
LoggedInUserProviderInterface $loggedInUserProvider
LoggedInShopUserProviderInterface $loggedInUserProvider
) {
$this->viewHandler = $viewHandler;
$this->validator = $validator;
Expand Down
6 changes: 3 additions & 3 deletions src/Controller/AddressBook/SetDefaultAddressAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Sylius\Component\Core\Model\ShopUserInterface;
use Sylius\ShopApiPlugin\Command\SetDefaultAddress;
use Sylius\ShopApiPlugin\Factory\ValidationErrorViewFactoryInterface;
use Sylius\ShopApiPlugin\Provider\LoggedInUserProviderInterface;
use Sylius\ShopApiPlugin\Provider\LoggedInShopUserProviderInterface;
use Sylius\ShopApiPlugin\Request\SetDefaultAddressRequest;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand All @@ -31,15 +31,15 @@ final class SetDefaultAddressAction
/** @var ValidationErrorViewFactoryInterface */
private $validationErrorViewFactory;

/** @var LoggedInUserProviderInterface */
/** @var LoggedInShopUserProviderInterface */
private $loggedInUserProvider;

public function __construct(
ViewHandlerInterface $viewHandler,
CommandBus $bus,
ValidatorInterface $validator,
ValidationErrorViewFactoryInterface $validationErrorViewFactory,
LoggedInUserProviderInterface $loggedInUserProvider
LoggedInShopUserProviderInterface $loggedInUserProvider
) {
$this->viewHandler = $viewHandler;
$this->bus = $bus;
Expand Down
6 changes: 3 additions & 3 deletions src/Controller/AddressBook/ShowAddressBookAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Sylius\Component\Core\Model\Customer;
use Sylius\Component\Core\Model\ShopUserInterface;
use Sylius\ShopApiPlugin\Factory\AddressBookViewFactoryInterface;
use Sylius\ShopApiPlugin\Provider\LoggedInUserProviderInterface;
use Sylius\ShopApiPlugin\Provider\LoggedInShopUserProviderInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Exception\TokenNotFoundException;

Expand All @@ -19,15 +19,15 @@ final class ShowAddressBookAction
/** @var ViewHandlerInterface */
private $viewHandler;

/** @var LoggedInUserProviderInterface */
/** @var LoggedInShopUserProviderInterface */
private $loggedInUserProvider;

/** @var AddressBookViewFactoryInterface */
private $addressBookViewFactory;

public function __construct(
ViewHandlerInterface $viewHandler,
LoggedInUserProviderInterface $loggedInUserProvider,
LoggedInShopUserProviderInterface $loggedInUserProvider,
AddressBookViewFactoryInterface $addressBookViewFactory
) {
$this->viewHandler = $viewHandler;
Expand Down
6 changes: 3 additions & 3 deletions src/Controller/AddressBook/UpdateAddressAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Sylius\ShopApiPlugin\Factory\AddressBookViewFactoryInterface;
use Sylius\ShopApiPlugin\Factory\ValidationErrorViewFactoryInterface;
use Sylius\ShopApiPlugin\Model\Address;
use Sylius\ShopApiPlugin\Provider\LoggedInUserProviderInterface;
use Sylius\ShopApiPlugin\Provider\LoggedInShopUserProviderInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Exception\TokenNotFoundException;
Expand All @@ -40,7 +40,7 @@ final class UpdateAddressAction
/** @var AddressRepositoryInterface */
private $addressRepository;

/** @var LoggedInUserProviderInterface */
/** @var LoggedInShopUserProviderInterface */
private $loggedInUserProvider;

public function __construct(
Expand All @@ -50,7 +50,7 @@ public function __construct(
ValidationErrorViewFactoryInterface $validationErrorViewFactory,
AddressBookViewFactoryInterface $addressViewFactory,
AddressRepositoryInterface $addressRepository,
LoggedInUserProviderInterface $loggedInUserProvider
LoggedInShopUserProviderInterface $loggedInUserProvider
) {
$this->viewHandler = $viewHandler;
$this->validator = $validator;
Expand Down
6 changes: 3 additions & 3 deletions src/Controller/Checkout/CompleteOrderAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use League\Tactician\CommandBus;
use Sylius\ShopApiPlugin\Command\CompleteOrder;
use Sylius\ShopApiPlugin\Exception\WrongUserException;
use Sylius\ShopApiPlugin\Provider\LoggedInUserProviderInterface;
use Sylius\ShopApiPlugin\Provider\LoggedInShopUserProviderInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Exception\TokenNotFoundException;
Expand All @@ -22,13 +22,13 @@ final class CompleteOrderAction
/** @var CommandBus */
private $bus;

/** @var LoggedInUserProviderInterface */
/** @var LoggedInShopUserProviderInterface */
private $loggedInUserProvider;

public function __construct(
ViewHandlerInterface $viewHandler,
CommandBus $bus,
LoggedInUserProviderInterface $loggedInUserProvider
LoggedInShopUserProviderInterface $loggedInUserProvider
) {
$this->viewHandler = $viewHandler;
$this->bus = $bus;
Expand Down
6 changes: 3 additions & 3 deletions src/Controller/Order/ShowOrderDetailsAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
use FOS\RestBundle\View\View;
use FOS\RestBundle\View\ViewHandlerInterface;
use Sylius\Component\Core\Model\ShopUserInterface;
use Sylius\ShopApiPlugin\Provider\LoggedInUserProviderInterface;
use Sylius\ShopApiPlugin\ViewRepository\Order\PlacedOrderViewRepositoryInterface;
use Sylius\ShopApiPlugin\Provider\LoggedInShopUserProviderInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
Expand All @@ -19,15 +19,15 @@ final class ShowOrderDetailsAction
/** @var ViewHandlerInterface */
private $viewHandler;

/** @var LoggedInUserProviderInterface */
/** @var LoggedInShopUserProviderInterface */
private $loggedInUserProvider;

/** @var PlacedOrderViewRepositoryInterface */
private $placedOrderQuery;

public function __construct(
ViewHandlerInterface $viewHandler,
LoggedInUserProviderInterface $loggedInUserProvider,
LoggedInShopUserProviderInterface $loggedInUserProvider,
PlacedOrderViewRepositoryInterface $placedOrderQuery
) {
$this->viewHandler = $viewHandler;
Expand Down
Loading

0 comments on commit 97507e6

Please sign in to comment.