Skip to content

Commit

Permalink
Feature/multi channels (BitBagCommerce#126)
Browse files Browse the repository at this point in the history
* add channel to wishlist entity

* edit nessesary services to use channel

* edit config

* edit twig templates

* fix tests

* add new method to twig extension

* run ecs

* CR fixes

* fix

* add some behat tests

* try to fix behat tests in github actions

* revert github actions confing

* ECS fix

* add message and translations to exceptions

* change translation key
  • Loading branch information
BartoszWojdalowicz authored Apr 1, 2022
1 parent 5c402c3 commit ee7da58
Show file tree
Hide file tree
Showing 58 changed files with 674 additions and 81 deletions.
67 changes: 67 additions & 0 deletions features/adding_product_to_wishlist_on_multiple_channels.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
@wishlist
Feature: Adding a product to wishlist
In order to compare or buy products later
As a Visitor
I want to be able to add products to my wishlist on 2 different channels

Background:
Given the store operates on a channel named "Web-US" in "USD" currency
And the store also operates on another channel named "Web-EU" in "EUR" currency
And the store has a product "Leprechaun's Gold" priced at "$100.00" available in channel "Web-US" and channel "Web-EU"
And the store has a product "Leprechaun's Silver" priced at "€10.00" in "Web-EU" channel
And all store products appear under a main taxonomy

@ui
Scenario: Adding product to wishlist in the first channel checking wishlist on the second channel.
Given I change my current channel to "Web-EU"
When I add "Leprechaun's Silver" product to my wishlist
And I should be notified that the product has been successfully added to my wishlist
Then I change my current channel to "Web-US"
And I go to the wishlist page
And I should have 0 products in my wishlist

@ui
Scenario: Adding product to wishlisht on both channels
Given I change my current channel to "Web-EU"
When I add "Leprechaun's Silver" product to my wishlist
And I should be notified that the product has been successfully added to my wishlist
And I should have one item in my wishlist
Then I change my current channel to "Web-US"
And I add "Leprechaun's Gold" product to my wishlist
And I should be notified that the product has been successfully added to my wishlist
And I should have one item in my wishlist

@ui
Scenario: Adding product to wishlist on both channels and removing from one channel.
Given I change my current channel to "Web-EU"
When I add "Leprechaun's Silver" product to my wishlist
And I should be notified that the product has been successfully added to my wishlist
Then I change my current channel to "Web-US"
And I add "Leprechaun's Gold" product to my wishlist
And I should be notified that the product has been successfully added to my wishlist
And I should have one item in my wishlist
And I go to the wishlist page
Then I check "Leprechaun's Gold"
And I remove selected products from wishlist
And I should have 0 products in my wishlist
And I change my current channel to "Web-EU"
And I go to the wishlist page
And I should have one item in my wishlist

@ui
Scenario: Adding multiple channels product to wishlist and removing one.
Given I change my current channel to "Web-EU"
When I add "Leprechaun's Gold" product to my wishlist
And I should be notified that the product has been successfully added to my wishlist
Then I change my current channel to "Web-US"
And I add "Leprechaun's Gold" product to my wishlist
And I should be notified that the product has been successfully added to my wishlist
And I should have one item in my wishlist
And I go to the wishlist page
Then I check "Leprechaun's Gold"
And I remove selected products from wishlist
And I should have 0 products in my wishlist
And I change my current channel to "Web-EU"
And I go to the wishlist page
And I should have one item in my wishlist

Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
@api_wishlist
Feature: Adding a product to wishlist
In order to compare or buy products later
As a Visitor
I want to be able to add products to my wishlist on 2 different channels

Background:
Given the store operates on a channel named "Web-US" in "USD" currency
And the store also operates on another channel named "Web-EU" in "EUR" currency
And the store has a product "Leprechaun's Gold" priced at "$100.00" available in channel "Web-US" and channel "Web-EU"
And the store has a product "Leprechaun's Silver" priced at "€10.00" in "Web-EU" channel
And all store products appear under a main taxonomy

@api
Scenario: Adding product to wishlist in the first channel checking wishlist on the second channel.
Given I am browsing channel "Web-EU"
And user has a wishlist
When user adds product "Leprechaun's Silver" to the wishlist in "Web-EU"
Then user should have product "Leprechaun's Silver" in the wishlist
Then I am browsing channel "Web-US"
And user has a wishlist in "Web-US"
Then user should have an empty wishlist in "Web-US"


@api
Scenario: Adding product to wishlisht on both channels
Given I change my current channel to "Web-EU"
And user has a wishlist in "Web-EU"
When user adds product "Leprechaun's Silver" to the wishlist in "Web-EU"
Then user should have product "Leprechaun's Silver" in the wishlist
Then I change my current channel to "Web-US"
And user has a wishlist in "Web-US"
When user adds product "Leprechaun's Gold" to the wishlist in "Web-US"
Then user should have product "Leprechaun's Gold" in the wishlist

@api
Scenario: Adding product to wishlist on both channels and removing from one channel.
Given I change my current channel to "Web-EU"
And user has a wishlist in "Web-EU"
When user adds product "Leprechaun's Silver" to the wishlist in "Web-EU"
Then user should have product "Leprechaun's Silver" in the wishlist
Then I change my current channel to "Web-US"
And user has a wishlist in "Web-US"
When user adds product "Leprechaun's Gold" to the wishlist in "Web-US"
Then user should have product "Leprechaun's Gold" in the wishlist
Then user removes product "Leprechaun's Gold" from the wishlist
Then user should have an empty wishlist
And I change my current channel to "Web-EU"
Then user should have product "Leprechaun's Silver" in the wishlist on "Web-EU"
26 changes: 19 additions & 7 deletions spec/Context/WishlistContextSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
use BitBag\SyliusWishlistPlugin\Factory\WishlistFactoryInterface;
use BitBag\SyliusWishlistPlugin\Repository\WishlistRepositoryInterface;
use PhpSpec\ObjectBehavior;
use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\ShopUserInterface;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Request;
Expand All @@ -27,13 +29,15 @@ final class WishlistContextSpec extends ObjectBehavior
function let(
TokenStorageInterface $tokenStorage,
WishlistRepositoryInterface $wishlistRepository,
WishlistFactoryInterface $wishlistFactory
WishlistFactoryInterface $wishlistFactory,
ChannelContextInterface $channelContext
) {
$this->beConstructedWith(
$tokenStorage,
$wishlistRepository,
$wishlistFactory,
'bitbag_sylius_wishlist'
'bitbag_sylius_wishlist',
$channelContext,
);
}

Expand Down Expand Up @@ -107,13 +111,17 @@ function it_returns_user_wishlist_if_found_and_user_logged_in(
TokenInterface $token,
ShopUserInterface $shopUser,
WishlistRepositoryInterface $wishlistRepository,
WishlistInterface $wishlist
WishlistInterface $wishlist,
ChannelContextInterface $channelContext,
ChannelInterface $channel
): void {
$request->cookies = $parameterBag;
$parameterBag->get('bitbag_sylius_wishlist')->willReturn(null);
$tokenStorage->getToken()->willReturn($token);
$token->getUser()->willReturn($shopUser);
$wishlistRepository->findOneByShopUser($shopUser)->willReturn($wishlist);
$channelContext->getChannel()->willReturn($channel);
$wishlistRepository->findOneByShopUserAndChannel($shopUser,$channel)->willReturn($wishlist);


$this->getWishlist($request)->shouldReturn($wishlist);
}
Expand All @@ -126,15 +134,19 @@ function it_returns_new_wishlist_if_not_found_and_user_logged_in(
ShopUserInterface $shopUser,
WishlistRepositoryInterface $wishlistRepository,
WishlistFactoryInterface $wishlistFactory,
WishlistInterface $wishlist
WishlistInterface $wishlist,
ChannelContextInterface $channelContext,
ChannelInterface $channel
): void {
$request->cookies = $parameterBag;
$parameterBag->get('bitbag_sylius_wishlist')->willReturn(null);
$wishlistFactory->createNew()->willReturn($wishlist);
$tokenStorage->getToken()->willReturn($token);
$token->getUser()->willReturn($shopUser);
$wishlistRepository->findOneByShopUser($shopUser)->willReturn(null);
$wishlistFactory->createForUser($shopUser)->willReturn($wishlist);
$channelContext->getChannel()->willReturn($channel);
$wishlistRepository->findOneByShopUserAndChannel($shopUser,$channel)->willReturn(null);
$wishlistFactory->createForUserAndChannel($shopUser,$channel)->willReturn($wishlist);


$this->getWishlist($request)->shouldReturn($wishlist);
}
Expand Down
22 changes: 18 additions & 4 deletions spec/Controller/Action/AddProductToWishlistActionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use BitBag\SyliusWishlistPlugin\Resolver\WishlistsResolverInterface;
use Doctrine\Persistence\ObjectManager;
use PhpSpec\ObjectBehavior;
use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\ProductInterface;
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
Expand All @@ -35,7 +37,8 @@ public function let(
TranslatorInterface $translator,
UrlGeneratorInterface $urlGenerator,
WishlistsResolverInterface $wishlistsResolver,
ObjectManager $wishlistManager
ObjectManager $wishlistManager,
ChannelContextInterface $channelContext
): void {
$this->beConstructedWith(
$productRepository,
Expand All @@ -44,7 +47,8 @@ public function let(
$translator,
$urlGenerator,
$wishlistsResolver,
$wishlistManager
$wishlistManager,
$channelContext
);
}

Expand Down Expand Up @@ -73,7 +77,9 @@ public function it_handles_the_request_and_persist_new_wishlist_for_logged_shop_
TranslatorInterface $translator,
FlashBagInterface $flashBag,
UrlGeneratorInterface $urlGenerator,
ObjectManager $wishlistManager
ObjectManager $wishlistManager,
ChannelContextInterface $channelContext,
ChannelInterface $channel
): void {
$request->get('productId')->willReturn(1);

Expand All @@ -88,6 +94,9 @@ public function it_handles_the_request_and_persist_new_wishlist_for_logged_shop_
$wishlistProductFactory->createForWishlistAndProduct($wishlist1, $product)->willReturn($wishlistProduct);
$translator->trans('bitbag_sylius_wishlist_plugin.ui.added_wishlist_item')->willReturn('Product has been added to your wishlist.');
$urlGenerator->generate('bitbag_sylius_wishlist_plugin_shop_wishlist_list_products')->willReturn('/wishlist');
$channelContext->getChannel()->willReturn($channel);
$channel->getId()->willReturn(1);
$wishlist1->getChannel()->willReturn($channel);

$wishlist1->addWishlistProduct($wishlistProduct)->shouldBeCalled();
$wishlistManager->flush()->shouldBeCalledOnce();
Expand All @@ -108,7 +117,9 @@ public function it_handles_the_request_and_persist_new_wishlist_for_anonymous_us
TranslatorInterface $translator,
FlashBagInterface $flashBag,
UrlGeneratorInterface $urlGenerator,
ObjectManager $wishlistManager
ObjectManager $wishlistManager,
ChannelContextInterface $channelContext,
ChannelInterface $channel
): void {
$request->get('productId')->willReturn(1);
$productRepository->find(1)->willReturn($product);
Expand All @@ -122,6 +133,9 @@ public function it_handles_the_request_and_persist_new_wishlist_for_anonymous_us
$wishlistProductFactory->createForWishlistAndProduct($wishlist1, $product)->willReturn($wishlistProduct);
$translator->trans('bitbag_sylius_wishlist_plugin.ui.added_wishlist_item')->willReturn('Product has been added to your wishlist.');
$urlGenerator->generate('bitbag_sylius_wishlist_plugin_shop_wishlist_list_products')->willReturn('/wishlist');
$channelContext->getChannel()->willReturn($channel);
$channel->getId()->willReturn(1);
$wishlist1->getChannel()->willReturn($channel);

$wishlist1->addWishlistProduct($wishlistProduct)->shouldBeCalled();
$wishlistManager->flush()->shouldBeCalledOnce();
Expand Down
2 changes: 1 addition & 1 deletion src/BitBagSyliusWishlistPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function getContainerExtension(): ?ExtensionInterface
{
$this->containerExtension = $this->createContainerExtension() ?? false;

return $this->containerExtension !== false ? $this->containerExtension : null;
return false !== $this->containerExtension ? $this->containerExtension : null;
}

public function build(ContainerBuilder $container): void
Expand Down
3 changes: 2 additions & 1 deletion src/Command/Wishlist/AddProductToSelectedWishlist.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
final class AddProductToSelectedWishlist
{
private WishlistInterface $wishlist;

private ProductInterface $product;

public function __construct(WishlistInterface $wishlist, ProductInterface $product)
Expand All @@ -33,4 +34,4 @@ public function getProduct(): ProductInterface
{
return $this->product;
}
}
}
12 changes: 10 additions & 2 deletions src/Command/Wishlist/CreateNewWishlist.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,23 @@

final class CreateNewWishlist
{
public string $name = "Wishlist";
public string $name = 'Wishlist';

public function __construct(string $name)
public ?string $channelCode = null;

public function __construct(string $name, ?string $channelCode)
{
$this->name = $name;
$this->channelCode = $channelCode;
}

public function getName(): string
{
return $this->name;
}

public function getChannelCode(): ?string
{
return $this->channelCode;
}
}
10 changes: 9 additions & 1 deletion src/Command/Wishlist/CreateWishlist.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,16 @@ final class CreateWishlist
{
public ?string $tokenValue;

public function __construct(?string $tokenValue)
public ?string $channelCode;

public function __construct(?string $tokenValue, ?string $channelCode)
{
$this->tokenValue = $tokenValue;
$this->channelCode = $channelCode;
}

public function getChannelCode(): ?string
{
return $this->channelCode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@

namespace BitBag\SyliusWishlistPlugin\Command\Wishlist;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Symfony\Component\HttpFoundation\Request;

final class ExportSelectedProductsFromWishlistToPdf implements ExportSelectedProductsFromWishlistToPdfInterface
{
Expand Down
8 changes: 7 additions & 1 deletion src/Command/Wishlist/WishlistItemInterface.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
<?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 BitBag\SyliusWishlistPlugin\Command\Wishlist;
Expand All @@ -20,4 +26,4 @@ public function setSelected(?bool $selected): void;
public function getCartItem(): ?AddToCartCommandInterface;

public function setCartItem(?AddToCartCommandInterface $cartItem): void;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@

namespace BitBag\SyliusWishlistPlugin\CommandHandler\Wishlist;

use BitBag\SyliusWishlistPlugin\Repository\WishlistRepositoryInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use BitBag\SyliusWishlistPlugin\Command\Wishlist\AddProductToSelectedWishlist;
use BitBag\SyliusWishlistPlugin\Entity\WishlistProductInterface;
use BitBag\SyliusWishlistPlugin\Factory\WishlistProductFactoryInterface;
use BitBag\SyliusWishlistPlugin\Repository\WishlistRepositoryInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;


final class AddProductToSelectedWishlistHandler implements MessageHandlerInterface
{
private WishlistProductFactoryInterface $wishlistProductFactory;
Expand Down
9 changes: 8 additions & 1 deletion src/CommandHandler/Wishlist/AddProductToWishlistHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use BitBag\SyliusWishlistPlugin\Command\Wishlist\AddProductToWishlist;
use BitBag\SyliusWishlistPlugin\Entity\WishlistInterface;
use BitBag\SyliusWishlistPlugin\Exception\ProductNotFoundException;
use BitBag\SyliusWishlistPlugin\Exception\WishlistNotFoundException;
use BitBag\SyliusWishlistPlugin\Factory\WishlistProductFactoryInterface;
use Doctrine\Persistence\ObjectManager;
use Sylius\Component\Core\Model\ProductInterface;
Expand Down Expand Up @@ -51,11 +52,17 @@ public function __invoke(AddProductToWishlist $addProductToWishlist): WishlistIn
);
}

if (null === $wishlist) {
throw new WishlistNotFoundException(
'bitbag_sylius_wishlist_plugin.ui.wishlist_for_channel_not_found'
);
}

$wishlistProduct = $this->wishlistProductFactory->createForWishlistAndProduct($wishlist, $product);

$wishlist->addWishlistProduct($wishlistProduct);

$this->wishlistManager->persist($wishlist);
$this->wishlistManager->persist($wishlistProduct);
$this->wishlistManager->flush();

return $wishlist;
Expand Down
Loading

0 comments on commit ee7da58

Please sign in to comment.