Skip to content

Commit

Permalink
[Catalog Promotion] process checkout with always proper price
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamKasp committed Sep 15, 2021
1 parent 62a0e7e commit a285b53
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 25 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
@checkout
Feature: Seeing current prices of products after catalog promotion becomes ineligible
In order to buy products in its correct prices
As a Customer
I want to have products with its current prices in the cart

Background:
Given the store operates on a single channel in "United States"
And the store has a product "T-Shirt"
And this product has "PHP T-Shirt" variant priced at "$20.00" in "United States" channel
And the store ships everywhere for free
And the store allows paying offline
And there is a catalog promotion "Winter sale" available in "United States" channel that reduces price by "25%" and applies on "PHP T-Shirt" variant
And I am a logged in customer

@ui @api
Scenario: Processing order with valid prices after catalog promotion becomes ineligibly
Given I have "PHP T-Shirt" variant of this product in the cart
When And the "Winter sale" catalog promotion is no longer available
And I specified the billing address as "Ankh Morpork", "Frost Alley", "90210", "United States" for "Jon Snow"
And I proceed through checkout process
Then I should be on the checkout summary step
And I should see product "T-Shirt" with unit price "$20.00"
25 changes: 25 additions & 0 deletions src/Sylius/Behat/Context/Api/Shop/CheckoutContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@

use ApiPlatform\Core\Api\IriConverterInterface;
use Behat\Behat\Context\Context;
use Doctrine\Persistence\ObjectManager;
use Sylius\Behat\Client\ApiClientInterface;
use Sylius\Behat\Client\Request;
use Sylius\Behat\Client\ResponseCheckerInterface;
use Sylius\Behat\Service\SharedStorageInterface;
use Sylius\Component\Addressing\Model\ProvinceInterface;
use Sylius\Component\Core\Formatter\StringInflector;
use Sylius\Component\Core\Model\AddressInterface;
use Sylius\Component\Core\Model\CatalogPromotionInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\CustomerInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\PaymentMethod;
Expand Down Expand Up @@ -1118,6 +1121,14 @@ public function iShouldBeNotifiedThatThisProductDoesNotHaveSufficientStock(Produ
));
}

/**
* @When /^I should see (product "[^"]+") with unit price ("[^"]+")$/
*/
public function iShouldSeeWithUnitPrice(ProductInterface $product, int $unitPrice): void
{
Assert::true($this->hasProductWithUnitPrice($product->getName(), $unitPrice));
}

private function assertProvinceMessage(string $addressType): void
{
$response = $this->ordersClient->getLastResponse();
Expand Down Expand Up @@ -1269,6 +1280,20 @@ private function hasProductWithNameAndQuantityInCart(string $productName, int $q
return false;
}

private function hasProductWithUnitPrice(string $productName, int $unitPrice): bool
{
/** @var array $items */
$items = $this->responseChecker->getValue($this->ordersClient->getLastResponse(), 'items');

foreach ($items as $item) {
if ($item['productName'] === $productName && $item['unitPrice'] === $unitPrice) {
return true;
}
}

return false;
}

private function fillAddress(string $addressType, AddressInterface $address): void
{
$this->content[$addressType]['city'] = $address->getCity() ?? '';
Expand Down
18 changes: 18 additions & 0 deletions src/Sylius/Behat/Context/Setup/CatalogPromotionContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Doctrine\ORM\EntityManagerInterface;
use Sylius\Behat\Service\SharedStorageInterface;
use Sylius\Bundle\CoreBundle\Fixture\Factory\ExampleFactoryInterface;
use Sylius\Component\Channel\Repository\ChannelRepositoryInterface;
use Sylius\Component\Core\Formatter\StringInflector;
use Sylius\Component\Core\Model\CatalogPromotionInterface;
use Sylius\Component\Core\Model\CatalogPromotionRuleInterface;
Expand All @@ -35,19 +36,23 @@ final class CatalogPromotionContext implements Context

private EntityManagerInterface $entityManager;

private ChannelRepositoryInterface $channelRepository;

private SharedStorageInterface $sharedStorage;

public function __construct(
ExampleFactoryInterface $catalogPromotionExampleFactory,
FactoryInterface $catalogPromotionRuleFactory,
FactoryInterface $catalogPromotionActionFactory,
EntityManagerInterface $entityManager,
ChannelRepositoryInterface $channelRepository,
SharedStorageInterface $sharedStorage
) {
$this->catalogPromotionExampleFactory = $catalogPromotionExampleFactory;
$this->catalogPromotionRuleFactory = $catalogPromotionRuleFactory;
$this->catalogPromotionActionFactory = $catalogPromotionActionFactory;
$this->entityManager = $entityManager;
$this->channelRepository = $channelRepository;
$this->sharedStorage = $sharedStorage;
}

Expand Down Expand Up @@ -172,6 +177,19 @@ public function thereIsACatalogPromotionAvailableInChannelThatReducesPriceByAndA
$this->entityManager->flush();
}

/**
* @When And the :catalogPromotion catalog promotion is no longer available
*/
public function theAdministratorMakesThisCatalogPromotionUnavailableInTheChannel(
CatalogPromotionInterface $catalogPromotion
): void {
foreach ($this->channelRepository->findAll() as $channel) {
$catalogPromotion->removeChannel($channel);
}
$this->entityManager->persist($catalogPromotion);
$this->entityManager->flush();
}

private function createCatalogPromotion(
string $name,
?string $code = null,
Expand Down
3 changes: 3 additions & 0 deletions src/Sylius/Behat/Context/Ui/Shop/CartContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@

use Behat\Behat\Context\Context;
use Behat\Mink\Exception\ElementNotFoundException;
use Doctrine\Persistence\ObjectManager;
use Sylius\Behat\NotificationType;
use Sylius\Behat\Page\Shop\Cart\SummaryPageInterface;
use Sylius\Behat\Page\Shop\Product\ShowPageInterface;
use Sylius\Behat\Service\NotificationCheckerInterface;
use Sylius\Behat\Service\SharedStorageInterface;
use Sylius\Component\Core\Model\CatalogPromotionInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Product\Model\ProductInterface;
use Sylius\Component\Product\Model\ProductOptionInterface;
use Webmozart\Assert\Assert;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@

use Behat\Behat\Context\Context;
use Behat\Mink\Exception\ElementNotFoundException;
use Doctrine\Persistence\ObjectManager;
use FriendsOfBehat\PageObjectExtension\Page\UnexpectedPageException;
use Sylius\Behat\NotificationType;
use Sylius\Behat\Page\Shop\Checkout\CompletePageInterface;
use Sylius\Behat\Service\NotificationCheckerInterface;
use Sylius\Behat\Service\SharedStorageInterface;
use Sylius\Component\Core\Formatter\StringInflector;
use Sylius\Component\Core\Model\CatalogPromotionInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\PaymentMethodInterface;
use Sylius\Component\Core\Model\ProductInterface;
use Sylius\Component\Core\Model\ProductVariantInterface;
Expand All @@ -40,6 +43,7 @@ final class CheckoutCompleteContext implements Context
/** @var NotificationCheckerInterface */
private $notificationChecker;


public function __construct(
SharedStorageInterface $sharedStorage,
CompletePageInterface $completePage,
Expand Down Expand Up @@ -380,4 +384,12 @@ public function iShouldNotBeAbleToProceedCheckoutCompleteStep(): void

throw new UnexpectedPageException('It should not be possible to complete checkout complete step.');
}

/**
* @When /^I should see (product "[^"]+") with unit price ("[^"]+")$/
*/
public function iShouldSeeWithUnitPrice(ProductInterface $product, int $unitPrice): void
{
Assert::same($this->completePage->getProductUnitPrice($product), $unitPrice);
}
}
5 changes: 5 additions & 0 deletions src/Sylius/Behat/Page/Shop/Checkout/CompletePage.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ public function hasPaymentMethod(): bool
return $this->hasElement('payment_method');
}

public function getProductUnitPrice(ProductInterface $product): int
{
return $this->getPriceFromString($this->getElement('product_unit_price', ['%name%' => $product->getName()])->getText());
}

public function hasProductDiscountedUnitPriceBy(ProductInterface $product, int $amount): bool
{
$priceWithoutDiscount = $this->getPriceFromString($this->getElement('product_old_price', ['%name%' => $product->getName()])->getText());
Expand Down
2 changes: 2 additions & 0 deletions src/Sylius/Behat/Page/Shop/Checkout/CompletePageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public function getPaymentMethodName(): string;

public function hasPaymentMethod(): bool;

public function getProductUnitPrice(ProductInterface $product): int;

public function hasProductDiscountedUnitPriceBy(ProductInterface $product, int $amount): bool;

public function hasOrderTotal(int $total): bool;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@
<argument type="service" id="sylius.factory.catalog_promotion_rule" />
<argument type="service" id="sylius.factory.catalog_promotion_action" />
<argument type="service" id="sylius.manager.catalog_promotion" />
<argument type="service" id="sylius.repository.channel" />
<argument type="service" id="sylius.behat.shared_storage" />
</service>
</services>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ default:
- sylius.behat.context.transform.tax_category
- sylius.behat.context.transform.user
- sylius.behat.context.transform.zone
- Sylius\Behat\Context\Transform\CatalogPromotionContext

- sylius.behat.context.setup.address
- sylius.behat.context.setup.admin_user
Expand All @@ -44,6 +45,7 @@ default:
- sylius.behat.context.setup.taxation
- sylius.behat.context.setup.user
- sylius.behat.context.setup.zone
- Sylius\Behat\Context\Setup\CatalogPromotionContext

- sylius.behat.context.api.shop.cart
- sylius.behat.context.api.shop.channel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ default:
- sylius.behat.context.transform.payment
- sylius.behat.context.transform.product
- sylius.behat.context.transform.product_option
- sylius.behat.context.transform.product_variant
- sylius.behat.context.transform.province
- sylius.behat.context.transform.shared_storage
- sylius.behat.context.transform.shipping_category
- sylius.behat.context.transform.shipping_method
- sylius.behat.context.transform.tax_category
- sylius.behat.context.transform.user
- sylius.behat.context.transform.zone
- Sylius\Behat\Context\Transform\CatalogPromotionContext

- sylius.behat.context.setup.address
- sylius.behat.context.setup.admin_user
Expand All @@ -44,6 +46,7 @@ default:
- sylius.behat.context.setup.taxation
- sylius.behat.context.setup.user
- sylius.behat.context.setup.zone
- Sylius\Behat\Context\Setup\CatalogPromotionContext

- sylius.behat.context.ui.admin.managing_orders
- sylius.behat.context.ui.channel
Expand Down

0 comments on commit a285b53

Please sign in to comment.