Skip to content

Commit

Permalink
Merge pull request #220 from BitBagCommerce/OP-205/fix_symfony_5_comp…
Browse files Browse the repository at this point in the history
…atibility

[OP-205]Fix compatibility for Symfony 5.x
  • Loading branch information
senghe authored Sep 9, 2023
2 parents c040b4c + faa8e4f commit c34dd8b
Show file tree
Hide file tree
Showing 14 changed files with 182 additions and 26 deletions.
31 changes: 20 additions & 11 deletions spec/Context/WishlistContextSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use BitBag\SyliusWishlistPlugin\Entity\WishlistInterface;
use BitBag\SyliusWishlistPlugin\Factory\WishlistFactoryInterface;
use BitBag\SyliusWishlistPlugin\Repository\WishlistRepositoryInterface;
use BitBag\SyliusWishlistPlugin\Resolver\TokenUserResolverInterface;
use PhpSpec\ObjectBehavior;
use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\Component\Core\Model\ChannelInterface;
Expand All @@ -30,14 +31,16 @@ function let(
TokenStorageInterface $tokenStorage,
WishlistRepositoryInterface $wishlistRepository,
WishlistFactoryInterface $wishlistFactory,
ChannelContextInterface $channelContext
ChannelContextInterface $channelContext,
TokenUserResolverInterface $tokenUserResolver,
) {
$this->beConstructedWith(
$tokenStorage,
$wishlistRepository,
$wishlistFactory,
'bitbag_sylius_wishlist',
$channelContext,
$tokenUserResolver,
);
}

Expand All @@ -57,12 +60,14 @@ function it_creates_new_wishlist_if_no_cookie_and_user(
TokenStorageInterface $tokenStorage,
TokenInterface $token,
WishlistFactoryInterface $wishlistFactory,
WishlistInterface $wishlist
WishlistInterface $wishlist,
TokenUserResolverInterface $tokenUserResolver,
): void {
$request->cookies = $parameterBag;
$parameterBag->get('bitbag_sylius_wishlist')->willReturn(null);
$tokenStorage->getToken()->willReturn($token);
$token->getUser()->willReturn(null);
$tokenUserResolver->resolve($token)->willReturn(null);

$wishlistFactory->createNew()->willReturn($wishlist);

$this->getWishlist($request)->shouldReturn($wishlist);
Expand All @@ -74,12 +79,13 @@ function it_returns_cookie_wishlist_if_cookie_and_no_user(
TokenStorageInterface $tokenStorage,
TokenInterface $token,
WishlistRepositoryInterface $wishlistRepository,
WishlistInterface $wishlist
WishlistInterface $wishlist,
TokenUserResolverInterface $tokenUserResolver,
): void {
$request->cookies = $parameterBag;
$parameterBag->get('bitbag_sylius_wishlist')->willReturn('Fq8N4W6mk12i9J2HX0U60POGG5UEzSgGW37OWd6sv2dd8FlBId');
$tokenStorage->getToken()->willReturn($token);
$token->getUser()->willReturn(null);
$tokenUserResolver->resolve($token)->willReturn(null);
$wishlistRepository->findByToken('Fq8N4W6mk12i9J2HX0U60POGG5UEzSgGW37OWd6sv2dd8FlBId')->willReturn($wishlist);

$this->getWishlist($request)->shouldReturn($wishlist);
Expand All @@ -92,12 +98,13 @@ function it_returns_new_wishlist_if_cookie_not_found_and_no_user(
TokenInterface $token,
WishlistRepositoryInterface $wishlistRepository,
WishlistFactoryInterface $wishlistFactory,
WishlistInterface $wishlist
WishlistInterface $wishlist,
TokenUserResolverInterface $tokenUserResolver,
): void {
$request->cookies = $parameterBag;
$parameterBag->get('bitbag_sylius_wishlist')->willReturn('Fq8N4W6mk12i9J2HX0U60POGG5UEzSgGW37OWd6sv2dd8FlBId');
$tokenStorage->getToken()->willReturn($token);
$token->getUser()->willReturn(null);
$tokenUserResolver->resolve($token)->willReturn(null);
$wishlistRepository->findByToken('Fq8N4W6mk12i9J2HX0U60POGG5UEzSgGW37OWd6sv2dd8FlBId')->willReturn(null);
$wishlistFactory->createNew()->willReturn($wishlist);

Expand All @@ -113,12 +120,13 @@ function it_returns_user_wishlist_if_found_and_user_logged_in(
WishlistRepositoryInterface $wishlistRepository,
WishlistInterface $wishlist,
ChannelContextInterface $channelContext,
ChannelInterface $channel
ChannelInterface $channel,
TokenUserResolverInterface $tokenUserResolver,
): void {
$request->cookies = $parameterBag;
$parameterBag->get('bitbag_sylius_wishlist')->willReturn(null);
$tokenStorage->getToken()->willReturn($token);
$token->getUser()->willReturn($shopUser);
$tokenUserResolver->resolve($token)->willReturn($shopUser);
$channelContext->getChannel()->willReturn($channel);
$wishlistRepository->findOneByShopUserAndChannel($shopUser,$channel)->willReturn($wishlist);

Expand All @@ -136,13 +144,14 @@ function it_returns_new_wishlist_if_not_found_and_user_logged_in(
WishlistFactoryInterface $wishlistFactory,
WishlistInterface $wishlist,
ChannelContextInterface $channelContext,
ChannelInterface $channel
ChannelInterface $channel,
TokenUserResolverInterface $tokenUserResolver,
): void {
$request->cookies = $parameterBag;
$parameterBag->get('bitbag_sylius_wishlist')->willReturn(null);
$wishlistFactory->createNew()->willReturn($wishlist);
$tokenStorage->getToken()->willReturn($token);
$token->getUser()->willReturn($shopUser);
$tokenUserResolver->resolve($token)->willReturn($shopUser);
$channelContext->getChannel()->willReturn($channel);
$wishlistRepository->findOneByShopUserAndChannel($shopUser,$channel)->willReturn(null);
$wishlistFactory->createForUserAndChannel($shopUser,$channel)->willReturn($wishlist);
Expand Down
43 changes: 43 additions & 0 deletions spec/Resolver/TokenUserResolverSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/*
* This file was created by developers working at BitBag
* Do you need more information about us and what we do? Visit our https://bitbag.io website!
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
*/

declare(strict_types=1);

namespace spec\BitBag\SyliusWishlistPlugin\Resolver;

use BitBag\SyliusWishlistPlugin\Resolver\TokenUserResolver;
use BitBag\SyliusWishlistPlugin\Resolver\TokenUserResolverInterface;
use PhpSpec\ObjectBehavior;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\User\UserInterface;

/** We cannot spec the scenario, when user is 'anon.' because of BC in Symfony 6 */
final class TokenUserResolverSpec extends ObjectBehavior
{
public function it_is_initializable(): void
{
$this->shouldHaveType(TokenUserResolver::class);
$this->shouldImplement(TokenUserResolverInterface::class);
}

public function it_returns_null_for_null_token(): void
{
$this->resolve(null)
->shouldReturn(null);
}

public function it_returns_user_for_non_anonymous_token(
TokenInterface $token,
UserInterface $user,
): void {
$token->getUser()->willReturn($user);

$this->resolve($token)
->shouldReturn($user);
}
}
11 changes: 9 additions & 2 deletions src/CommandHandler/Wishlist/CreateNewWishlistHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use BitBag\SyliusWishlistPlugin\Exception\WishlistNameIsTakenException;
use BitBag\SyliusWishlistPlugin\Factory\WishlistFactoryInterface;
use BitBag\SyliusWishlistPlugin\Repository\WishlistRepositoryInterface;
use BitBag\SyliusWishlistPlugin\Resolver\TokenUserResolverInterface;
use BitBag\SyliusWishlistPlugin\Resolver\WishlistCookieTokenResolverInterface;
use Sylius\Component\Channel\Repository\ChannelRepositoryInterface;
use Sylius\Component\Core\Model\ShopUserInterface;
Expand All @@ -35,25 +36,31 @@ final class CreateNewWishlistHandler implements MessageHandlerInterface

private WishlistNameCheckerInterface $wishlistNameChecker;

private TokenUserResolverInterface $tokenUserResolver;

public function __construct(
WishlistRepositoryInterface $wishlistRepository,
TokenStorageInterface $tokenStorage,
WishlistFactoryInterface $wishlistFactory,
WishlistCookieTokenResolverInterface $wishlistCookieTokenResolver,
ChannelRepositoryInterface $channelRepository,
WishlistNameCheckerInterface $wishlistNameChecker
WishlistNameCheckerInterface $wishlistNameChecker,
TokenUserResolverInterface $tokenUserResolver,
) {
$this->wishlistRepository = $wishlistRepository;
$this->tokenStorage = $tokenStorage;
$this->wishlistFactory = $wishlistFactory;
$this->wishlistCookieTokenResolver = $wishlistCookieTokenResolver;
$this->channelRepository = $channelRepository;
$this->wishlistNameChecker = $wishlistNameChecker;
$this->tokenUserResolver = $tokenUserResolver;
}

public function __invoke(CreateNewWishlist $createNewWishlist): void
{
$user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
$token = $this->tokenStorage->getToken();
$user = $this->tokenUserResolver->resolve($token);

$wishlistCookieToken = $this->wishlistCookieTokenResolver->resolve();

if ($user instanceof ShopUserInterface) {
Expand Down
10 changes: 7 additions & 3 deletions src/CommandHandler/Wishlist/CreateWishlistHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use BitBag\SyliusWishlistPlugin\Entity\WishlistInterface;
use BitBag\SyliusWishlistPlugin\Factory\WishlistFactoryInterface;
use BitBag\SyliusWishlistPlugin\Resolver\ShopUserWishlistResolverInterface;
use BitBag\SyliusWishlistPlugin\Resolver\TokenUserResolverInterface;
use Doctrine\Persistence\ObjectManager;
use Sylius\Component\Channel\Repository\ChannelRepositoryInterface;
use Sylius\Component\Core\Model\ShopUserInterface;
Expand All @@ -33,26 +34,29 @@ final class CreateWishlistHandler implements MessageHandlerInterface

private ChannelRepositoryInterface $channelRepository;

private TokenUserResolverInterface $tokenUserResolver;

public function __construct(
TokenStorageInterface $tokenStorage,
WishlistFactoryInterface $wishlistFactory,
ShopUserWishlistResolverInterface $shopUserWishlistResolver,
ObjectManager $wishlistManager,
ChannelRepositoryInterface $channelRepository
ChannelRepositoryInterface $channelRepository,
TokenUserResolverInterface $tokenUserResolver,
) {
$this->tokenStorage = $tokenStorage;
$this->wishlistFactory = $wishlistFactory;
$this->shopUserWishlistResolver = $shopUserWishlistResolver;
$this->wishlistManager = $wishlistManager;
$this->channelRepository = $channelRepository;
$this->tokenUserResolver = $tokenUserResolver;
}

public function __invoke(CreateWishlist $createWishlist): WishlistInterface
{
/** @var ?TokenInterface $token */
$token = $this->tokenStorage->getToken();

$user = null !== $token ? $token->getUser() : null;
$user = $this->tokenUserResolver->resolve($token);

/** @var WishlistInterface $wishlist */
$wishlist = $this->wishlistFactory->createNew();
Expand Down
9 changes: 7 additions & 2 deletions src/Context/WishlistContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use BitBag\SyliusWishlistPlugin\Entity\WishlistInterface;
use BitBag\SyliusWishlistPlugin\Factory\WishlistFactoryInterface;
use BitBag\SyliusWishlistPlugin\Repository\WishlistRepositoryInterface;
use BitBag\SyliusWishlistPlugin\Resolver\TokenUserResolverInterface;
use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\Component\Channel\Context\ChannelNotFoundException;
use Sylius\Component\Core\Model\ShopUserInterface;
Expand All @@ -32,18 +33,22 @@ final class WishlistContext implements WishlistContextInterface

private ChannelContextInterface $channelContext;

private TokenUserResolverInterface $tokenUserResolver;

public function __construct(
TokenStorageInterface $tokenStorage,
WishlistRepositoryInterface $wishlistRepository,
WishlistFactoryInterface $wishlistFactory,
string $wishlistCookieToken,
ChannelContextInterface $channelContext
ChannelContextInterface $channelContext,
TokenUserResolverInterface $tokenUserResolver,
) {
$this->tokenStorage = $tokenStorage;
$this->wishlistRepository = $wishlistRepository;
$this->wishlistFactory = $wishlistFactory;
$this->wishlistCookieToken = $wishlistCookieToken;
$this->channelContext = $channelContext;
$this->tokenUserResolver = $tokenUserResolver;
}

public function getWishlist(Request $request): WishlistInterface
Expand All @@ -57,7 +62,7 @@ public function getWishlist(Request $request): WishlistInterface
/** @var WishlistInterface $wishlist */
$wishlist = $this->wishlistFactory->createNew();

$user = null !== $token ? $token->getUser() : null;
$user = $this->tokenUserResolver->resolve($token);

if (null === $cookieWishlistToken && null === $user) {
return $wishlist;
Expand Down
12 changes: 10 additions & 2 deletions src/Controller/Action/AddWishlistToUserAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use BitBag\SyliusWishlistPlugin\Command\Wishlist\AddWishlistToUser;
use BitBag\SyliusWishlistPlugin\Repository\WishlistRepositoryInterface;
use BitBag\SyliusWishlistPlugin\Resolver\TokenUserResolverInterface;
use Sylius\Component\Core\Model\ShopUserInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
Expand All @@ -38,26 +39,33 @@ final class AddWishlistToUserAction

private TokenStorageInterface $tokenStorage;

private TokenUserResolverInterface $tokenUserResolver;

public function __construct(
MessageBusInterface $commandBus,
RequestStack $requestStack,
TranslatorInterface $translator,
WishlistRepositoryInterface $wishlistRepository,
UrlGeneratorInterface $urlGenerator,
TokenStorageInterface $tokenStorage
TokenStorageInterface $tokenStorage,
TokenUserResolverInterface $tokenUserResolver,
) {
$this->commandBus = $commandBus;
$this->requestStack = $requestStack;
$this->translator = $translator;
$this->wishlistRepository = $wishlistRepository;
$this->urlGenerator = $urlGenerator;
$this->tokenStorage = $tokenStorage;
$this->tokenUserResolver = $tokenUserResolver;
}

public function __invoke(Request $request): Response
{
$token = $this->tokenStorage->getToken();

/** @var ShopUserInterface $shopUser */
$shopUser = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
$shopUser = $this->tokenUserResolver->resolve($token);

$wishlistId = $request->attributes->getInt('id');
$wishlist = $this->wishlistRepository->find($wishlistId);

Expand Down
12 changes: 10 additions & 2 deletions src/Controller/Action/ShowChosenWishlistAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use BitBag\SyliusWishlistPlugin\Form\Type\WishlistCollectionType;
use BitBag\SyliusWishlistPlugin\Processor\WishlistCommandProcessorInterface;
use BitBag\SyliusWishlistPlugin\Repository\WishlistRepositoryInterface;
use BitBag\SyliusWishlistPlugin\Resolver\TokenUserResolverInterface;
use BitBag\SyliusWishlistPlugin\Resolver\WishlistCookieTokenResolverInterface;
use Sylius\Component\Core\Model\ShopUserInterface;
use Sylius\Component\Order\Context\CartContextInterface;
Expand Down Expand Up @@ -44,6 +45,8 @@ final class ShowChosenWishlistAction

private TokenStorageInterface $tokenStorage;

private TokenUserResolverInterface $tokenUserResolver;

public function __construct(
WishlistRepositoryInterface $wishlistRepository,
CartContextInterface $cartContext,
Expand All @@ -52,7 +55,8 @@ public function __construct(
WishlistCommandProcessorInterface $wishlistCommandProcessor,
UrlGeneratorInterface $urlGenerator,
WishlistCookieTokenResolverInterface $wishlistCookieTokenResolver,
TokenStorageInterface $tokenStorage
TokenStorageInterface $tokenStorage,
TokenUserResolverInterface $tokenUserResolver,
) {
$this->wishlistRepository = $wishlistRepository;
$this->cartContext = $cartContext;
Expand All @@ -62,14 +66,18 @@ public function __construct(
$this->urlGenerator = $urlGenerator;
$this->wishlistCookieTokenResolver = $wishlistCookieTokenResolver;
$this->tokenStorage = $tokenStorage;
$this->tokenUserResolver = $tokenUserResolver;
}

public function __invoke(string $wishlistId, Request $request): Response
{
$token = $this->tokenStorage->getToken();

/** @var WishlistInterface $wishlist */
$wishlist = $this->wishlistRepository->find((int)$wishlistId);
$wishlistCookieToken = $this->wishlistCookieTokenResolver->resolve();
$user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;

$user = $this->tokenUserResolver->resolve($token);

if ($wishlist instanceof WishlistInterface && $user instanceof ShopUserInterface
|| $wishlist instanceof WishlistInterface && $wishlist->getToken() === $wishlistCookieToken
Expand Down
Loading

0 comments on commit c34dd8b

Please sign in to comment.