Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AssertProductInCart.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 AssertProductInCart extends AbstractConstraint
20 {
26  protected $formPrice;
27 
34 
40  protected $fixturePrice;
41 
51  public function processAssert(
52  CatalogProductView $catalogProductView,
53  FixtureInterface $product,
54  BrowserInterface $browser,
55  CheckoutCart $checkoutCart
56  ) {
57  // Add product to cart
58  $browser->open($_ENV['app_frontend_url'] . $product->getUrlKey() . '.html');
59  $catalogProductView->getViewBlock()->fillOptions($product);
60  $catalogProductView->getViewBlock()->clickAddToCart();
61  $catalogProductView->getMessagesBlock()->waitSuccessMessage();
62 
63  // Check price
64  $this->countPrices($product, $checkoutCart);
65  \PHPUnit\Framework\Assert::assertEquals(
66  $this->fixtureActualPrice,
67  $this->formPrice,
68  'Product price in shopping cart is not correct.'
69  );
70  }
71 
79  protected function countPrices(FixtureInterface $product, CheckoutCart $checkoutCart)
80  {
82  $this->fixturePrice = $product->getPrice();
83  $this->prepareFormPrice($product, $checkoutCart);
84  $this->countSpecialPrice($product);
85  $this->countCheckoutCartItemPrice($product);
86  $this->countCustomOptionsPrice($product);
87  }
88 
95  protected function countSpecialPrice(FixtureInterface $product)
96  {
98  $specialPrice = $product->getSpecialPrice();
99  if ($specialPrice) {
100  $this->fixturePrice = $product->getSpecialPrice();
101  }
102  }
103 
111  protected function prepareFormPrice(FixtureInterface $product, CheckoutCart $checkoutCart)
112  {
113  $checkoutCart->open();
114  $cartItem = $checkoutCart->getCartBlock()->getCartItem($product);
115  $this->formPrice = $cartItem->getPrice();
116  }
117 
124  protected function countCheckoutCartItemPrice(FixtureInterface $product)
125  {
127  $checkoutData = $product->getCheckoutData();
128  $checkoutCartItem = isset($checkoutData['cartItem']) ? $checkoutData['cartItem'] : [];
129  if (isset($checkoutCartItem['price'])) {
130  $this->fixturePrice = $checkoutCartItem['price'];
131  }
132  $this->fixtureActualPrice = $this->fixturePrice;
133  }
134 
141  protected function countCustomOptionsPrice(FixtureInterface $product)
142  {
144  $customOptions = $product->getCustomOptions();
145  $checkoutData = $product->getCheckoutData();
146  $checkoutCustomOptions = isset($checkoutData['options']['custom_options'])
147  ? $checkoutData['options']['custom_options']
148  : [];
149  foreach ($checkoutCustomOptions as $checkoutOption) {
150  $attributeKey = str_replace('attribute_key_', '', $checkoutOption['title']);
151  $optionKey = str_replace('option_key_', '', $checkoutOption['value']);
152  $option = $customOptions[$attributeKey]['options'][$optionKey];
153 
154  if ('Fixed' == $option['price_type']) {
155  $this->fixtureActualPrice += $option['price'];
156  } else {
157  $this->fixtureActualPrice += ($this->fixturePrice / 100) * $option['price'];
158  }
159  }
160  }
161 
167  public function toString()
168  {
169  return 'Product is correctly displayed in cart.';
170  }
171 }
processAssert(CatalogProductView $catalogProductView, FixtureInterface $product, BrowserInterface $browser, CheckoutCart $checkoutCart)
prepareFormPrice(FixtureInterface $product, CheckoutCart $checkoutCart)