8000 Implement reorder items feature in account order show by Klehm · Pull Request #1 · Klehm/Axon21-Test · GitHub
[go: up one dir, main page]

Skip to content
Open
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
1 change: 1 addition & 0 deletions behat.yml.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
imports:
- vendor/sylius/sylius/src/Sylius/Behat/Resources/config/suites.yml
- config/suites.yaml

default:
formatters:
Expand Down
19 changes: 19 additions & 0 deletions config/packages/sylius_twig_hooks.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
sylius_twig_hooks:
hooks:
"sylius_shop.shared.order.show.summary.table.header":
reorder:
template: "@SyliusShop/shared/order/show/summary/table/header/item.html.twig"
configuration:
label: "actions"
class: "text-end"
priority: -100

"sylius_shop.shared.order.show.summary.table.body":
reorder:
component: 'sylius_shop:product:reorder'
props:
variant: '@=_context.item.getVariant()'
product: '@=_context.item.getProduct()'
quantity: '@=_context.item.getQuantity()'
template: 'shop/order/show/summary/table/body/reorder.html.twig'
priority: -100
22 changes: 22 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,25 @@ services:
App\Controller\:
resource: '../src/Controller'
tags: ['controller.service_arguments']

App\Twig\Components\ProductReorderFormComponent:
class: App\Twig\Components\ProductReorderFormComponent
arguments:
- '@form.factory'
- '@doctrine.orm.entity_manager'
- '@router'
- '@request_stack'
- '@event_dispatcher'
- '@sylius.context.cart'
- '@sylius.factory.add_to_cart_command'
- '@sylius.factory.order_item'
- 'Sylius\Bundle\ShopBundle\Form\Type\AddToCartType'
- '@sylius.repository.product'
- '@sylius.repository.product_variant'
tags:
- { name: 'sylius.live_component.shop_account', key: 'sylius_shop:product:reorder' }
calls:
- method: setLiveResponder
arguments:
- '@ux.live_component.live_responder'

10 changes: 10 additions & 0 deletions config/services_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,13 @@ imports:

sylius_api:
enabled: true

services:
_defaults:
autowire: true
autoconfigure: true
bind:
$minkParameters: '@behat.mink.parameters'

App\Tests\Behat\:
resource: '../tests/Behat/*'
2 changes: 2 additions & 0 deletions config/suites.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
imports:
- suites/ui/account/reordering_order_items.yaml
63 changes: 63 additions & 0 deletions config/suites/ui/account/reordering_order_items.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
default:
suites:
ui_reordering_order_items:
contexts:
- sylius.behat.context.hook.doctrine_orm
- sylius.behat.context.hook.session

- sylius.behat.context.transform.address
- sylius.behat.context.transform.channel
- sylius.behat.context.transform.country
- sylius.behat.context.transform.customer
- sylius.behat.context.transform.lexical
- sylius.behat.context.transform 10BC8 .order
- sylius.behat.context.transform.payment
- sylius.behat.context.transform.product
- sylius.behat.context.transform.shared_storage
- sylius.behat.context.transform.shipping_method
- sylius.behat.context.transform.user
- sylius.behat.context.transform.zone

- sylius.behat.context.setup.admin_security
- sylius.behat.context.setup.admin_user
- sylius.behat.context.setup.calendar
- sylius.behat.context.setup.cart
- sylius.behat.context.setup.channel
- sylius.behat.context.setup.checkout.address
- sylius.behat.context.setup.checkout.shipping
- sylius.behat.context.setup.currency
- sylius.behat.context.setup.customer
- sylius.behat.context.setup.geographical
- sylius.behat.context.setup.locale
- sylius.behat.context.setup.order
- sylius.behat.context.setup.payment
- sylius.behat.context.setup.product
- sylius.behat.context.setup.product_taxon
- sylius.behat.context.setup.promotion
- sylius.behat.context.setup.shipping
- sylius.behat.context.setup.shop_security
- sylius.behat.context.setup.taxation
- sylius.behat.context.setup.taxonomy
- sylius.behat.context.setup.user
- sylius.behat.context.setup.zone

- sylius.behat.context.ui.browser
- sylius.behat.context.ui.channel
- sylius.behat.context.ui.email
- sylius.behat.context.ui.save
- sylius.behat.context.ui.shop.account
- sylius.behat.context.ui.shop.cart
- sylius.behat.context.ui.shop.checkout
- sylius.behat.context.ui.shop.checkout.addressing
- sylius.behat.context.ui.shop.checkout.complete
- sylius.behat.context.ui.shop.checkout.order_details
- sylius.behat.context.ui.shop.checkout.payment
- sylius.behat.context.ui.shop.checkout.shipping
- sylius.behat.context.ui.shop.currency
- sylius.behat.context.ui.shop.homepage
- sylius.behat.context.ui.user

- App\Tests\Behat\Context\Ui\Shop\ReorderContext

filters:
tags: "@reordering_order_items && @ui && @javascript"
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
@reordering_order_items
Feature: Viewing items on the order show page
As a Customer
I want to be able to reorder items individually from my order history

Background:
Given the store operates on a single channel in "United States"
And the store has a product "Angel T-Shirt" priced at "$39.00"
And "Angel T-Shirt" product is tracked by the inventory
And there are 1 units of product "Angel T-Shirt" available in the inventory
And the store has a product "Angel Mug" priced at "$19.00"
And "Angel Mug" product is tracked by the inventory
And there are 8 units of product "Angel Mug" available in the inventory
But the store has "DHL" shipping method with "$8.60" fee
And the store allows paying with "Cash on Delivery"
And I am a logged in customer
And I placed an order "#00000666"
And the customer bought a single "Angel T-Shirt"
And the customer bought 2 "Angel Mug" products
And I addressed it to "Lucifer Morningstar", "Seaside Fwy", "90802" "Los Angeles" in the "United States" with identical billing address
And I chose "DHL" shipping method with "Cash on Delivery" payment

@no-api @ui @javascript
Scenario: Reordering an item in stock from my order
When I view the summary of my order "#00000666"
And I reorder the "Angel Mug" item
Then I should be notified that "Angel Mug" item has been added to my cart
And I should see cart total quantity is 2

@no-api @ui @javascript
Scenario: Reordering an item out of stock from my order
When I view the summary of my order "#00000666"
And I reorder the "Angel T-Shirt" item
Then I should be notified that "Angel T-Shirt" item does not have sufficient stock
And I should see cart total quantity is 0
99 changes: 99 additions & 0 deletions src/Twig/Components/ProductReorderFormComponent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

declare(strict_types=1);

namespace App\Twig\Components;

use Doctrine\Persistence\ObjectManager;
use Sylius\Bundle\OrderBundle\Factory\AddToCartCommandFactoryInterface;
use Sylius\Bundle\ShopBundle\Twig\Component\Cart\FormComponent as CartFormComponent;
use Sylius\Bundle\ShopBundle\Twig\Component\Product\Trait\ProductLivePropTrait;
use Sylius\Bundle\ShopBundle\Twig\Component\Product\Trait\ProductVariantLivePropTrait;
use Sylius\Bundle\UiBundle\Twig\Component\TemplatePropTrait;
use Sylius\Component\Core\Factory\CartItemFactoryInterface;
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
use Sylius\Component\Core\Repository\ProductVariantRepositoryInterface;
use Sylius\Component\Inventory\Checker\AvailabilityCheckerInterface;
use Sylius\Component\Order\Context\CartContextInterface;
use Sylius\Component\Order\Modifier\OrderItemQuantityModifierInterface;
use Sylius\Component\Order\SyliusCartEvents;
use Sylius\TwigHooks\LiveComponent\HookableLiveComponentTrait;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\RouterInterface;
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\LiveComponent\Attribute\LiveAction;
use Symfony\UX\LiveComponent\Attribute\LiveProp;
use Symfony\UX\LiveComponent\ComponentToolsTrait;
use Symfony\UX\LiveComponent\ComponentWithFormTrait;
use Symfony\UX\LiveComponent\DefaultActionTrait;

#[AsLiveComponent]
class ProductReorderFormComponent
{
use ComponentToolsTrait;
use ComponentWithFormTrait;
use DefaultActionTrait;
use HookableLiveComponentTrait;
use ProductLivePropTrait;
use ProductVariantLivePropTrait;
use TemplatePropTrait;

#[LiveProp]
public int $quantity = 1;

#[LiveProp]
public bool $submitted = false;

public function __construct(
protected readonly FormFactoryInterface $formFactory,
protected readonly ObjectManager $manager,
protected readonly RouterInterface $router,
protected readonly RequestStack $requestStack,
protected readonly EventDispatcherInterface $eventDispatcher,
protected readonly CartContextInterface $cartContext,
protected readonly AddToCartCommandFactoryInterface $addToCartCommandFactory,
protected readonly CartItemFactoryInterface $cartItemFactory,
protected readonly string $formClass,
ProductRepositoryInterface $productRepository,
ProductVariantRepositoryInterface $productVariantRepository,
protected readonly OrderItemQuantityModifierInterface $quantityModifier,
protected readonly AvailabilityCheckerInterface $orderItemAvailabilityChecker,
) {
$this->initializeProduct($productRepository);
$this->initializeProductVariant($productVariantRepository);
}

#[LiveAction]
public function addToCart(): void
{
$this->submitForm();

$addToCartCommand = $this->getForm()->getData();
$this->eventDispatcher->dispatch(new GenericEvent($addToCartCommand), SyliusCartEvents::CART_ITEM_ADD);
$this->manager->persist($addToCartCommand->getCart());
$this->manager->flush();

$this->submitted = true;

// Reload the cart summary to reflect the changes
$this->emit(CartFormComponent::SYLIUS_SHOP_CART_CHANGED, ['cartId' => $this->cartContext->getCart()->getId()]);
}

protected function instantiateForm(): FormInterface
{
$cartItem = $this->cartItemFactory->createForProduct($this->product);
$cartItem->setVariant($this->variant);
$this->quantityModifier->modify($cartItem, $this->quantity);

$addToCartCommand = $this->addToCartCommandFactory->createWithCartAndCartItem(
$this->cartContext->getCart(),
$cartItem,
);

return $this->formFactory->create($this->formClass, $addToCartCommand, ['product' => $this->product]);
}
}
32 changes: 32 additions & 0 deletions templates/shop/order/show/summary/table/body/reorder.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{% form_theme form '@SyliusShop/form/theme.html.twig' %}
{% import '@SyliusShop/shared/buttons.html.twig' as buttons %}

{% set enabled = form.vars.valid and variant is not null and variant.enabled and not submitted %}

<td class="text-end position-relative" {{ attributes }}>
<div data-loading>
<div class="sylius-shop-loader">
<img src="{{ asset('build/shop/images/loader.gif', 'shop') }}" alt="{{ 'sylius.ui.loading'|trans }}" />
</div>
</div>

<div>
{{ form_start(form, {'attr': {'data-action': 'live#action:prevent live#$render', 'data-live-action-param': 'addToCart'}}) }}
{{ form_errors(form) }}

{{ buttons.submit('sylius.ui.reorder'|trans, 'reorder-button', null, 'btn-primary', not enabled) }}

{% if not form.vars.valid %}
<div class="invalid-feedback d-block" {{ sylius_test_html_attribute('product-reorder-error') }}>
{{ 'sylius.ui.product_not_reordered'|trans }}
</div>
{% endif %}

{{ form_row(form._token) }}
{{ form_end(form, {'render_rest': false}) }}
</div>

{% if submitted %}
<div class="valid-feedback d-block" {{ sylius_test_html_attribute('product-reorder-success') }}>{{ 'sylius.ui.product_reordered'|trans }}</div>
{% endif %}
</td>
36 changes: 36 additions & 0 deletions tests/Behat/Context/Ui/Shop/ReorderContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace App\Tests\Behat\Context\Ui\Shop;

use App\Tests\Behat\Page\Shop\Account\Order\ShowPage;
use Behat\Behat\Context\Context;
use Behat\Step\Then;
use Behat\Step\When;

final class ReorderContext implements Context
{
public function __construct(
private ShowPage $orderShowPage,
) {
}

#[When('I reorder the :arg1 item')]
public function iReorderTheItem($arg1): void
{
$this->orderShowPage->reorder($arg1);
}

#[Then('I should be notified that :arg1 item has been added to my cart')]
public function iShouldBeNotifiedThatItemHasBeenAddedToMyCart($arg1): void
{
$this->orderShowPage->hasReorderFeedbackSuccess($arg1);
}

#[Then('I should be notified that :arg1 item does not have sufficient stock')]
public function iShouldBeNotifiedThatItemDoesNotHaveSufficientStock($arg1): void
{
$this->orderShowPage->hasReorderFeedbackError($arg1);
}
}
Loading
0