Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AssertProductTierPriceInCart.php
Go to the documentation of this file.
1 <?php
8 
10 use Magento\Catalog\Test\Page\Product\CatalogProductView;
11 use Magento\Checkout\Test\Page\CheckoutCart;
12 use Magento\Mtf\Client\BrowserInterface;
13 use Magento\Mtf\Constraint\AbstractConstraint;
14 use Magento\Mtf\Fixture\FixtureInterface;
15 
19 class AssertProductTierPriceInCart extends AbstractConstraint
20 {
26  private $formPrice;
27 
33  private $fixtureActualPrice;
34 
40  private $fixturePrice;
41 
51  public function processAssert(
52  CatalogProductView $catalogProductView,
53  FixtureInterface $product,
54  BrowserInterface $browser,
55  CheckoutCart $checkoutCart
56  ) {
57  $checkoutCart->open();
58  $checkoutCart->getCartBlock()->clearShoppingCart();
59  // Add product to cart
60  $browser->open($_ENV['app_frontend_url'] . $product->getUrlKey() . '.html');
61  $requiredQty = $product->getDataFieldConfig('tier_price')['source']->getData()[0]['price_qty'];
62  $catalogProductView->getViewBlock()->setQtyAndClickAddToCart($requiredQty);
63  $catalogProductView->getMessagesBlock()->waitSuccessMessage();
64 
65  // Check price
66  $this->countPrices($product, $checkoutCart);
67  \PHPUnit\Framework\Assert::assertEquals(
68  $this->fixtureActualPrice,
69  $this->formPrice,
70  'Product price in shopping cart is not correct.'
71  );
72  }
73 
81  private function countPrices(FixtureInterface $product, CheckoutCart $checkoutCart)
82  {
84  $this->fixturePrice = $product->getPrice();
85  $this->prepareFormPrice($product, $checkoutCart);
86  $this->countCheckoutCartItemPrice($product);
87  }
88 
96  private function prepareFormPrice(FixtureInterface $product, CheckoutCart $checkoutCart)
97  {
98  $checkoutCart->open();
99  $cartItem = $checkoutCart->getCartBlock()->getCartItem($product);
100  $this->formPrice = $cartItem->getPrice();
101  }
102 
109  private function countCheckoutCartItemPrice(FixtureInterface $product)
110  {
111  $tierPrice = $product->getDataFieldConfig('tier_price')['source']->getData()[0];
112 
113  if ($tierPrice['value_type'] === "Discount") {
114  $this->fixtureActualPrice = $this->fixturePrice * (1 - $tierPrice['percentage_value'] / 100);
115  } else {
116  $this->fixtureActualPrice = $tierPrice['price'];
117  }
118  }
119 
125  public function toString()
126  {
127  return 'Product is correctly displayed in cart.';
128  }
129 }
processAssert(CatalogProductView $catalogProductView, FixtureInterface $product, BrowserInterface $browser, CheckoutCart $checkoutCart)