Skip to content

Commit

Permalink
CartCest tests asserting prices in cart are now currency independent
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasLudvik authored and vitek-rostislav committed Oct 3, 2019
1 parent a7fc180 commit adb684f
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 10 deletions.
Expand Up @@ -81,3 +81,5 @@ services:
Shopsys\FrameworkBundle\Model\Administration\AdministrationFacade: ~

Shopsys\FrameworkBundle\Model\Pricing\PriceConverter: ~

Shopsys\FrameworkBundle\Model\Pricing\Rounding: ~
Expand Up @@ -59,7 +59,7 @@
</div>
</td>

<td class="table-cart__cell table-cart__cell--price">
<td class="table-cart__cell table-cart__cell--price js-cart-item-price">
{{ cartItemPrice.unitPrice.priceWithVat|price }}
</td>

Expand Down
26 changes: 22 additions & 4 deletions project-base/tests/ShopBundle/Acceptance/acceptance/CartCest.php
Expand Up @@ -141,14 +141,20 @@ public function testChangeCartItemAndRecalculatePrice(
AcceptanceTester $me
) {
$me->wantTo('change items in cart and recalculate price');

// 22-sencor-sle-22f46dm4-hello-kitty
$me->amOnLocalizedRoute('front_product_detail', ['id' => 1]);
$me->seeTranslationFrontend('Add to cart');
$productDetailPage->addProductIntoCart(3);
$me->clickByTranslationFrontend('Go to cart');

$cartPage->changeProductQuantity('22" Sencor SLE 22F46DM4 HELLO KITTY', 10);
$cartPage->assertTotalPriceWithVat('34990');

$products = [
'22" Sencor SLE 22F46DM4 HELLO KITTY' => 10,
];

$cartPage->assertTotalPriceWithVatByProducts($products);
}

/**
Expand Down Expand Up @@ -239,7 +245,14 @@ public function testPricingInCart(
$productDetailPage->addProductIntoCart(75);

$me->amOnLocalizedRoute('front_cart');
$cartPage->assertTotalPriceWithVat('17350');

$products = [
'Aquila Aquagym non-carbonated spring water' => 10,
'100 Czech crowns ticket' => 100,
'PremiumCord micro USB, A-B, 1m' => 75,
];

$cartPage->assertTotalPriceWithVatByProducts($products);
}

/**
Expand All @@ -263,14 +276,19 @@ public function testPromoCodeFlowInCart(

$me->amOnLocalizedRoute('front_cart');

$products = [
'Aquila Aquagym non-carbonated spring water' => 1,
'100 Czech crowns ticket' => 1,
];

$cartPage->applyPromoCode('test');

$cartPage->canSeePromoCodeRemoveButtonElement();
$cartPage->assertTotalPriceWithVat('122');
$cartPage->assertTotalPriceWithVatByProducts($products, 10);

$cartPage->removePromoCode();

$cartPage->canSeePromoCodeSubmitButtonElement();
$cartPage->assertTotalPriceWithVat('136');
$cartPage->assertTotalPriceWithVatByProducts($products);
}
}
Expand Up @@ -15,7 +15,8 @@ class CartBoxPage extends AbstractPage
*/
public function seeInCartBox(int $expectedCount, string $expectedPrice): void
{
$expectedFormattedPriceWithCurrency = $this->tester->getFormattedPriceWithCurrencySymbolOnFrontend(Money::create($expectedPrice));
$convertedPrice = Money::create($this->tester->getPriceWithVatConvertedToDomainDefaultCurrency($expectedPrice));
$expectedFormattedPriceWithCurrency = $this->tester->getFormattedPriceWithCurrencySymbolOnFrontend($convertedPrice);
$messageId = '{1} <strong class="cart__state">%itemsCount%</strong> item for <strong class="cart__state">%priceWithVat%</strong>|[2,Inf] <strong class="cart__state">%itemsCount%</strong> items for <strong class="cart__state">%priceWithVat%</strong>';
$translatedMessageWithTags = tc($messageId, $expectedCount, ['%itemsCount%' => $expectedCount, '%priceWithVat%' => $expectedFormattedPriceWithCurrency], 'messages', $this->tester->getFrontendLocale());

Expand Down
Expand Up @@ -27,8 +27,9 @@ public function assertProductQuantity($productName, $quantity)
*/
public function assertProductPrice($productName, $price)
{
$formattedPriceWithCurrency = $this->tester->getFormattedPriceWithCurrencySymbolOnFrontend(Money::create($price));
$productPriceCell = $this->getProductPriceCellByName($productName);
$convertedPrice = $this->tester->getPriceWithVatConvertedToDomainDefaultCurrency($price);
$formattedPriceWithCurrency = $this->tester->getFormattedPriceWithCurrencySymbolOnFrontend(Money::create($convertedPrice));
$productPriceCell = $this->getProductTotalPriceCellByName($productName);
$this->tester->seeInElement($formattedPriceWithCurrency, $productPriceCell);
}

Expand Down Expand Up @@ -123,13 +124,24 @@ private function findProductRowInCartByName($productName)
* @param string $productName
* @return \Facebook\WebDriver\WebDriverElement
*/
private function getProductPriceCellByName($productName)
private function getProductTotalPriceCellByName($productName)
{
$row = $this->findProductRowInCartByName($productName);

return $row->findElement(WebDriverBy::cssSelector('.js-cart-item-total-price'));
}

/**
* @param string $productName
* @return \Facebook\WebDriver\WebDriverElement
*/
private function getProductPriceCellByName($productName)
{
$row = $this->findProductRowInCartByName($productName);

return $row->findElement(WebDriverBy::cssSelector('.js-cart-item-price'));
}

/**
* @return \Facebook\WebDriver\WebDriverElement
*/
Expand Down Expand Up @@ -171,4 +183,38 @@ public function canSeePromoCodeRemoveButtonElement()
{
return $this->tester->canSeeElement(WebDriverBy::cssSelector('#js-promo-code-remove-button'));
}

/**
* @param array $products
* @param int $discount
*/
public function assertTotalPriceWithVatByProducts(array $products, int $discount = 0)
{
$totalPrice = Money::zero();

foreach ($products as $productName => $count) {
$totalPrice = $totalPrice->add(
Money::create($this->getProductTotalPriceByName($productName))
->divide(100, 6)
->multiply(100 - $discount)
->multiply($count)
);
}

$this->assertTotalPriceWithVat($totalPrice->getAmount());
}

/**
* @param string $productName
* @return string
*/
private function getProductTotalPriceByName(string $productName): string
{
$productName = t($productName, [], 'dataFixtures', $this->tester->getFrontendLocale());
$productPriceCell = $this->getProductPriceCellByName($productName);

$productPriceWithoutCurrencySymbol = preg_replace('/[^0-9.,]/', '', $productPriceCell->getText());

return $this->tester->getNumberFromLocalizedFormat($productPriceWithoutCurrencySymbol, $this->tester->getFrontendLocale());
}
}
Expand Up @@ -7,13 +7,16 @@
use Codeception\Module;
use Codeception\TestInterface;
use CommerceGuys\Intl\Currency\CurrencyRepositoryInterface;
use CommerceGuys\Intl\Formatter\NumberFormatter;
use CommerceGuys\Intl\NumberFormat\NumberFormatRepository;
use Shopsys\FrameworkBundle\Component\CurrencyFormatter\CurrencyFormatterFactory;
use Shopsys\FrameworkBundle\Component\Domain\Domain;
use Shopsys\FrameworkBundle\Component\Money\Money;
use Shopsys\FrameworkBundle\Component\Router\DomainRouterFactory;
use Shopsys\FrameworkBundle\Model\Localization\Localization;
use Shopsys\FrameworkBundle\Model\Pricing\Currency\CurrencyFacade;
use Shopsys\FrameworkBundle\Model\Pricing\PriceConverter;
use Shopsys\FrameworkBundle\Model\Pricing\Rounding;
use Shopsys\FrameworkBundle\Model\Product\Unit\UnitFacade;
use Shopsys\FrameworkBundle\Twig\NumberFormatterExtension;
use Tests\ShopBundle\Test\Codeception\Module\StrictWebDriver;
Expand Down Expand Up @@ -70,6 +73,16 @@ class LocalizationHelper extends Module
*/
private $priceConverter;

/**
* @var \CommerceGuys\Intl\Formatter\NumberFormatter
*/
private $numberFormatter;

/**
* @var \Shopsys\FrameworkBundle\Model\Pricing\Rounding
*/
private $rounding;

/**
* @param \Codeception\TestInterface $test
*/
Expand All @@ -89,6 +102,8 @@ public function _before(TestInterface $test): void
$this->unitFacade = $symfonyHelper->grabServiceFromContainer(UnitFacade::class);
$this->numberFormatterExtension = $symfonyHelper->grabServiceFromContainer(NumberFormatterExtension::class);
$this->priceConverter = $symfonyHelper->grabServiceFromContainer(PriceConverter::class);
$this->numberFormatter = new NumberFormatter(new NumberFormatRepository());
$this->rounding = $symfonyHelper->grabServiceFromContainer(Rounding::class);
}

/**
Expand Down Expand Up @@ -221,7 +236,31 @@ public function getFormattedPriceWithCurrencySymbolOnFrontend(Money $price): str

$intlCurrency = $this->intlCurrencyRepository->get($firstDomainDefaultCurrency->getCode(), $firstDomainLocale);

$formattedPriceWithCurrencySymbol = $currencyFormatter->format($price->getAmount(), $intlCurrency->getCurrencyCode());
$formattedPriceWithCurrencySymbol = $currencyFormatter->format(
$this->rounding->roundPriceWithVat($price)->getAmount(),
$intlCurrency->getCurrencyCode()
);

return $this->normalizeSpaces($formattedPriceWithCurrencySymbol);
}

/**
* Inspired by formatCurrency() method, {@see \Shopsys\FrameworkBundle\Twig\PriceExtension}
* @param \Shopsys\FrameworkBundle\Component\Money\Money $price
* @return string
*/
public function getFormattedPriceOnFrontend(Money $price): string
{
$firstDomainDefaultCurrency = $this->currencyFacade->getDomainDefaultCurrencyByDomainId(1);
$firstDomainLocale = $this->getFrontendLocale();
$currencyFormatter = $this->currencyFormatterFactory->create($firstDomainLocale);

$intlCurrency = $this->intlCurrencyRepository->get($firstDomainDefaultCurrency->getCode(), $firstDomainLocale);

$formattedPriceWithCurrencySymbol = $currencyFormatter->format(
$this->rounding->roundPriceWithVat($price)->getAmount(),
$intlCurrency->getCurrencyCode()
);

return $this->normalizeSpaces($formattedPriceWithCurrencySymbol);
}
Expand Down Expand Up @@ -269,4 +308,14 @@ private function normalizeSpaces(string $text): string
{
return preg_replace('~\x{00a0}~siu', ' ', $text);
}

/**
* @param string $number
* @param string $locale
* @return string
*/
public function getNumberFromLocalizedFormat(string $number, string $locale): string
{
return $this->numberFormatter->parse($number, ['locale' => $locale]);
}
}

0 comments on commit adb684f

Please sign in to comment.