Skip to content

Commit

Permalink
[Behat] Duplicate some steps from Sylius due to support for Sylius 1.8
Browse files Browse the repository at this point in the history
  • Loading branch information
GSadee committed Mar 24, 2021
1 parent 85b7959 commit 5092357
Show file tree
Hide file tree
Showing 3 changed files with 194 additions and 11 deletions.
173 changes: 173 additions & 0 deletions tests/Behat/Context/Setup/OrderShipmentContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
<?php

declare(strict_types=1);

namespace Tests\Sylius\RefundPlugin\Behat\Context\Setup;

use Behat\Behat\Context\Context;
use Doctrine\Persistence\ObjectManager;
use SM\Factory\FactoryInterface as StateMachineFactoryInterface;
use Sylius\Behat\Service\SharedStorageInterface;
use Sylius\Bundle\CoreBundle\Application\Kernel;
use Sylius\Component\Core\Model\AddressInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\ChannelPricingInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\OrderItemInterface;
use Sylius\Component\Core\Model\ProductInterface;
use Sylius\Component\Core\Model\ProductVariantInterface;
use Sylius\Component\Core\Model\ShipmentInterface;
use Sylius\Component\Core\Model\ShippingMethodInterface;
use Sylius\Component\Core\OrderCheckoutTransitions;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
use Sylius\Component\Order\Modifier\OrderItemQuantityModifierInterface;
use Sylius\Component\Payment\Repository\PaymentMethodRepositoryInterface;
use Sylius\Component\Product\Resolver\ProductVariantResolverInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Sylius\Component\Shipping\Repository\ShippingMethodRepositoryInterface;

if (Kernel::MAJOR_VERSION === '1' && Kernel::MINOR_VERSION === '8') {
final class OrderShipmentContext implements Context
{
/** @var SharedStorageInterface */
private $sharedStorage;

/** @var FactoryInterface */
private $orderItemFactory;

/** @var FactoryInterface */
private $shipmentFactory;

/** @var StateMachineFactoryInterface */
private $stateMachineFactory;

/** @var OrderRepositoryInterface */
private $orderRepository;

/** @var PaymentMethodRepositoryInterface */
private $paymentMethodRepository;

/** @var ShippingMethodRepositoryInterface */
private $shippingMethodRepository;

/** @var ProductVariantResolverInterface */
private $variantResolver;

/** @var OrderItemQuantityModifierInterface */
private $itemQuantityModifier;

/** @var ObjectManager */
private $objectManager;

public function __construct(
SharedStorageInterface $sharedStorage,
FactoryInterface $orderItemFactory,
FactoryInterface $shipmentFactory,
StateMachineFactoryInterface $stateMachineFactory,
OrderRepositoryInterface $orderRepository,
PaymentMethodRepositoryInterface $paymentMethodRepository,
ShippingMethodRepositoryInterface $shippingMethodRepository,
ProductVariantResolverInterface $variantResolver,
OrderItemQuantityModifierInterface $itemQuantityModifier,
ObjectManager $objectManager
) {
$this->sharedStorage = $sharedStorage;
$this->orderItemFactory = $orderItemFactory;
$this->shipmentFactory = $shipmentFactory;
$this->stateMachineFactory = $stateMachineFactory;
$this->orderRepository = $orderRepository;
$this->paymentMethodRepository = $paymentMethodRepository;
$this->shippingMethodRepository = $shippingMethodRepository;
$this->variantResolver = $variantResolver;
$this->itemQuantityModifier = $itemQuantityModifier;
$this->objectManager = $objectManager;
}

/**
* @Given the customer bought another :product with separate :shippingMethod shipment
*/
public function theCustomerBoughtAnotherProductWithSeparateShipment(
ProductInterface $product,
ShippingMethodInterface $shippingMethod
): void {
$this->addProductVariantToOrder($this->variantResolver->getVariant($product), 1);

/** @var OrderInterface $order */
$order = $this->sharedStorage->get('order');

/** @var ShipmentInterface $shipment */
$shipment = $this->shipmentFactory->createNew();
$shipment->setMethod($shippingMethod);
$shipment->setOrder($order);
$order->addShipment($shipment);

$this->objectManager->flush();
}

/**
* @Given /^the customer chose ("[^"]+" shipping method) (to "[^"]+")$/
*/
public function theCustomerChoseShippingTo(ShippingMethodInterface $shippingMethod, AddressInterface $address): void
{
/** @var OrderInterface $order */
$order = $this->sharedStorage->get('order');

$order->setShippingAddress($address);
$order->setBillingAddress(clone $address);

$this->applyTransitionOnOrderCheckout($order, OrderCheckoutTransitions::TRANSITION_ADDRESS);

foreach ($order->getShipments() as $shipment) {
$shipment->setMethod($shippingMethod);
}
$this->applyTransitionOnOrderCheckout($order, OrderCheckoutTransitions::TRANSITION_SELECT_SHIPPING);

$this->objectManager->flush();
}

private function addProductVariantToOrder(
ProductVariantInterface $productVariant,
int $quantity = 1,
?ChannelInterface $channel = null
): OrderInterface {
$order = $this->sharedStorage->get('order');

$this->addProductVariantsToOrderWithChannelPrice(
$order,
$channel ?? $this->sharedStorage->get('channel'),
$productVariant,
(int) $quantity
);

return $order;
}

private function addProductVariantsToOrderWithChannelPrice(
OrderInterface $order,
ChannelInterface $channel,
ProductVariantInterface $productVariant,
int $quantity = 1
) {
/** @var OrderItemInterface $item */
$item = $this->orderItemFactory->createNew();
$item->setVariant($productVariant);

/** @var ChannelPricingInterface $channelPricing */
$channelPricing = $productVariant->getChannelPricingForChannel($channel);
$item->setUnitPrice($channelPricing->getPrice());

$this->itemQuantityModifier->modify($item, $quantity);

$order->addItem($item);
}

private function applyTransitionOnOrderCheckout(OrderInterface $order, string $transition): void
{
$this->stateMachineFactory->get($order, OrderCheckoutTransitions::GRAPH)->apply($transition);
}
}
} else {
final class OrderShipmentContext implements Context
{
}
}
13 changes: 13 additions & 0 deletions tests/Behat/Resources/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,19 @@
<argument type="service" id="sylius.manager.order" />
</service>

<service id="Tests\Sylius\RefundPlugin\Behat\Context\Setup\OrderShipmentContext">
<argument type="service" id="sylius.behat.shared_storage" />
<argument type="service" id="sylius.factory.order_item" />
<argument type="service" id="sylius.factory.shipment" />
<argument type="service" id="sm.factory" />
<argument type="service" id="sylius.repository.order" />
<argument type="service" id="sylius.repository.payment_method" />
<argument type="service" id="sylius.repository.shipping_method" />
<argument type="service" id="sylius.product_variant_resolver.default" />
<argument type="service" id="sylius.order_item_quantity_modifier" />
<argument type="service" id="doctrine.orm.entity_manager" />
</service>

<service id="Tests\Sylius\RefundPlugin\Behat\Context\Transform\OrderContext">
<argument type="service" id="sylius.repository.order" />
</service>
Expand Down
19 changes: 8 additions & 11 deletions tests/Behat/Resources/suites.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ default:
- sylius.behat.context.transform.tax_category
- sylius.behat.context.transform.tax_rate
- sylius.behat.context.transform.zone

- Tests\Sylius\RefundPlugin\Behat\Context\Transform\OrderContext

- sylius.behat.context.setup.admin_security
Expand All @@ -33,21 +32,22 @@ default:
- sylius.behat.context.setup.shipping
- sylius.behat.context.setup.taxation
- sylius.behat.context.setup.zone

- Tests\Sylius\RefundPlugin\Behat\Context\Setup\ChannelContext
- Tests\Sylius\RefundPlugin\Behat\Context\Setup\OrderContext
- Tests\Sylius\RefundPlugin\Behat\Context\Setup\OrderShipmentContext
- Tests\Sylius\RefundPlugin\Behat\Context\Setup\ProductContext
- Tests\Sylius\RefundPlugin\Behat\Context\Setup\RefundingContext

- sylius.behat.context.ui.admin.managing_orders

- Tests\Sylius\RefundPlugin\Behat\Context\Application\EmailsContext
- Tests\Sylius\RefundPlugin\Behat\Context\Setup\ChannelContext
- Tests\Sylius\RefundPlugin\Behat\Context\Ui\CreditMemoContext
- Tests\Sylius\RefundPlugin\Behat\Context\Ui\ManagingOrdersContext
- Tests\Sylius\RefundPlugin\Behat\Context\Ui\RefundingContext

filters:
tags: "@refunds && @ui"

application_refunds:
contexts:
- sylius.behat.context.hook.doctrine_orm
Expand Down Expand Up @@ -77,18 +77,17 @@ default:
- sylius.behat.context.setup.shipping
- sylius.behat.context.setup.taxation
- sylius.behat.context.setup.zone

- sylius.behat.context.transform.payment

- Tests\Sylius\RefundPlugin\Behat\Context\Setup\ChannelContext
- Tests\Sylius\RefundPlugin\Behat\Context\Setup\OrderShipmentContext
- Tests\Sylius\RefundPlugin\Behat\Context\Setup\RefundingContext

- Tests\Sylius\RefundPlugin\Behat\Context\Application\CreditMemoContext
- Tests\Sylius\RefundPlugin\Behat\Context\Application\EmailsContext
- Tests\Sylius\RefundPlugin\Behat\Context\Application\RefundingContext
- Tests\Sylius\RefundPlugin\Behat\Context\Setup\ChannelContext

filters:
tags: "@refunds && @application"

customer_credit_memos:
contexts:
- sylius.behat.context.hook.doctrine_orm
Expand All @@ -114,14 +113,12 @@ default:
- sylius.behat.context.setup.shipping
- sylius.behat.context.setup.shop_security
- sylius.behat.context.setup.user
- Tests\Sylius\RefundPlugin\Behat\Context\Setup\ChannelContext
- Tests\Sylius\RefundPlugin\Behat\Context\Setup\RefundingContext

- sylius.behat.context.ui.shop.account

- sylius.behat.context.transform.payment

- Tests\Sylius\RefundPlugin\Behat\Context\Application\EmailsContext
- Tests\Sylius\RefundPlugin\Behat\Context\Setup\ChannelContext
- Tests\Sylius\RefundPlugin\Behat\Context\Setup\RefundingContext
- Tests\Sylius\RefundPlugin\Behat\Context\Ui\Shop\Customer\CreditMemoContext

filters:
Expand Down

0 comments on commit 5092357

Please sign in to comment.