Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix adding item to wishlist without redirection to wishlist route #134

Merged
merged 3 commits into from
Aug 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions features/adding_product_to_wishlist.feature
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ Feature: Adding a product to wishlist
Given the store has a product "Jack Daniels Gentleman" priced at "$10.00"
And all store products appear under a main taxonomy
When I add this product to wishlist
Then I should be on my wishlist page
And I should be notified that the product has been successfully added to my wishlist
Then I should be notified that the product has been successfully added to my wishlist
And I should have one item in my wishlist

@ui
Expand All @@ -22,8 +21,7 @@ Feature: Adding a product to wishlist
And all store products appear under a main taxonomy
When I view product "Some other whiskey"
And I add this product to wishlist
Then I should be on my wishlist page
And I should be notified that the product has been successfully added to my wishlist
Then I should be notified that the product has been successfully added to my wishlist
And I should have one item in my wishlist

@ui
Expand Down
20 changes: 11 additions & 9 deletions spec/Controller/Action/AddProductToWishlistActionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\ProductInterface;
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

final class AddProductToWishlistActionSpec extends ObjectBehavior
Expand All @@ -35,7 +35,6 @@ public function let(
WishlistProductFactoryInterface $wishlistProductFactory,
FlashBagInterface $flashBag,
TranslatorInterface $translator,
UrlGeneratorInterface $urlGenerator,
WishlistsResolverInterface $wishlistsResolver,
ObjectManager $wishlistManager,
ChannelContextInterface $channelContext
Expand All @@ -45,7 +44,6 @@ public function let(
$wishlistProductFactory,
$flashBag,
$translator,
$urlGenerator,
$wishlistsResolver,
$wishlistManager,
$channelContext
Expand Down Expand Up @@ -76,10 +74,10 @@ public function it_handles_the_request_and_persist_new_wishlist_for_logged_shop_
WishlistInterface $wishlist2,
TranslatorInterface $translator,
FlashBagInterface $flashBag,
UrlGeneratorInterface $urlGenerator,
ObjectManager $wishlistManager,
ChannelContextInterface $channelContext,
ChannelInterface $channel
ChannelInterface $channel,
ParameterBag $headers
): void {
$request->get('productId')->willReturn(1);

Expand All @@ -93,7 +91,6 @@ 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);
Expand All @@ -102,6 +99,9 @@ public function it_handles_the_request_and_persist_new_wishlist_for_logged_shop_
$wishlistManager->flush()->shouldBeCalledOnce();
$flashBag->add('success', 'Product has been added to your wishlist.')->shouldBeCalled();

$request->headers = $headers;
$headers->get('referer')->willReturn('value');

$this->__invoke($request)->shouldHaveType(RedirectResponse::class);
}

Expand All @@ -116,10 +116,10 @@ public function it_handles_the_request_and_persist_new_wishlist_for_anonymous_us
WishlistProductInterface $wishlistProduct,
TranslatorInterface $translator,
FlashBagInterface $flashBag,
UrlGeneratorInterface $urlGenerator,
ObjectManager $wishlistManager,
ChannelContextInterface $channelContext,
ChannelInterface $channel
ChannelInterface $channel,
ParameterBag $headers
): void {
$request->get('productId')->willReturn(1);
$productRepository->find(1)->willReturn($product);
Expand All @@ -132,7 +132,6 @@ 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);
Expand All @@ -141,6 +140,9 @@ public function it_handles_the_request_and_persist_new_wishlist_for_anonymous_us
$wishlistManager->flush()->shouldBeCalledOnce();
$flashBag->add('success', 'Product has been added to your wishlist.')->shouldBeCalled();

$request->headers = $headers;
$headers->get('referer')->willReturn('value');

$this->__invoke($request)->shouldHaveType(RedirectResponse::class);
}
}
2 changes: 1 addition & 1 deletion src/CommandHandler/Wishlist/AddProductsToCartHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ private function addProductToWishlist(WishlistItem $wishlistProduct): void
$this->orderModifier->addToOrder($cart, $cartItem);
$this->orderRepository->add($cart);

if (false === $this->flashBag->has('success')){
if (false === $this->flashBag->has('success')) {
$this->flashBag->add('success', $this->translator->trans('bitbag_sylius_wishlist_plugin.ui.added_to_cart'));
}
}
Expand Down
10 changes: 4 additions & 6 deletions src/Controller/Action/AddProductToWishlistAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

final class AddProductToWishlistAction
Expand All @@ -38,8 +37,6 @@ final class AddProductToWishlistAction

private TranslatorInterface $translator;

private UrlGeneratorInterface $urlGenerator;

private WishlistsResolverInterface $wishlistsResolver;

private ObjectManager $wishlistManager;
Expand All @@ -51,14 +48,12 @@ public function __construct(
WishlistProductFactoryInterface $wishlistProductFactory,
FlashBagInterface $flashBag,
TranslatorInterface $translator,
UrlGeneratorInterface $urlGenerator,
WishlistsResolverInterface $wishlistsResolver,
ObjectManager $wishlistManager,
ChannelContextInterface $channelContext
) {
$this->productRepository = $productRepository;
$this->wishlistProductFactory = $wishlistProductFactory;
$this->urlGenerator = $urlGenerator;
$this->flashBag = $flashBag;
$this->translator = $translator;
$this->wishlistsResolver = $wishlistsResolver;
Expand Down Expand Up @@ -107,6 +102,9 @@ public function __invoke(Request $request): Response

$this->flashBag->add('success', $this->translator->trans('bitbag_sylius_wishlist_plugin.ui.added_wishlist_item'));

return new RedirectResponse($this->urlGenerator->generate('bitbag_sylius_wishlist_plugin_shop_wishlist_list_products'));
$referer = $request->headers->get('referer');
$refererPathInfo = Request::create($referer)->getPathInfo();

return new RedirectResponse($refererPathInfo);
}
}
1 change: 0 additions & 1 deletion src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ services:
- "@bitbag_sylius_wishlist_plugin.factory.wishlist_product"
- "@session.flash_bag"
- "@translator"
- "@router"
- "@bitbag_sylius_wishlist_plugin.resolver.wishlists_resolver"
- "@bitbag_sylius_wishlist_plugin.manager.wishlist"
- "@sylius.context.channel"
Expand Down
4 changes: 2 additions & 2 deletions src/Resources/views/WishlistDetails/_variantPrice.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
{% set prices %}
[
{%- for price in pricing -%}
{
{%- for option, value in price -%}
"{{- option -}}":
{%- if option == 'value' or option == 'original-price' -%}
"{{ money.convertAndFormat(value) -}}"
{%- else -%}
"{{ value|replace({'\"': '\''}) -}}"
{%- endif -%}
{{- not loop.last ? ', '-}}
{%- endfor -%}
}
{{- not loop.last ? ', '-}}
{%- endfor -%}
]
Expand Down