Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AssertBundlePriceType.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 
18 class AssertBundlePriceType extends AbstractConstraint
19 {
25  protected $productPriceType = 'Yes';
26 
40  public function processAssert(
41  CatalogProductView $catalogProductView,
42  BundleProduct $product,
43  CheckoutCart $checkoutCartView,
44  BrowserInterface $browser,
45  BundleProduct $originalProduct = null
46  ) {
47  $checkoutCartView->open()->getCartBlock()->clearShoppingCart();
48  //Open product view page
49  $browser->open($_ENV['app_frontend_url'] . $product->getUrlKey() . '.html');
50 
51  //Process assertions
52  $this->assertPrice($product, $catalogProductView, $checkoutCartView, $originalProduct);
53  }
54 
66  protected function assertPrice(
67  BundleProduct $product,
68  CatalogProductView $catalogProductView,
69  CheckoutCart $checkoutCartView,
70  BundleProduct $originalProduct = null
71  ) {
72  $bundleData = $product->getData();
73  $this->productPriceType = $originalProduct !== null
74  ? $originalProduct->getPriceType()
75  : $product->getPriceType();
76  $catalogProductView->getViewBlock()->addToCart($product);
77  $catalogProductView->getMessagesBlock()->waitSuccessMessage();
78  $checkoutCartView->open();
79  $cartItem = $checkoutCartView->getCartBlock()->getCartItem($product);
80  $specialPrice = 0;
81 
82  $optionPrice = [];
83  $fillData = $product->getCheckoutData();
84  foreach ($fillData['options']['bundle_options'] as $key => $data) {
85  $subProductPrice = 0;
86  foreach ($bundleData['bundle_selections']['products'][$key] as $productKey => $itemProduct) {
87  if (strpos($itemProduct->getName(), $data['value']['name']) !== false) {
88  $data['value']['key'] = $productKey;
89  $subProductPrice = $itemProduct->getPrice();
90  }
91  }
92 
93  $optionPrice[$key]['price'] = $this->productPriceType == 'No'
94  ? number_format(
95  $bundleData['bundle_selections']['bundle_options'][$key]['assigned_products'][$data['value']['key']]
96  ['data']['selection_price_value'],
97  2
98  )
99  : number_format($subProductPrice, 2);
100  }
101 
102  foreach ($optionPrice as $index => $item) {
103  $item['price'] -= $item['price'] * $specialPrice;
104  \PHPUnit\Framework\Assert::assertEquals(
105  number_format($item['price'], 2),
106  $cartItem->getPriceBundleOptions($index + 1),
107  'Bundle item ' . ($index + 1) . ' options on frontend don\'t equal to fixture.'
108  );
109  }
110  $sumOptionsPrice = $product->getDataFieldConfig('price')['source']->getPriceData()['cart_price'];
111 
112  $subTotal = number_format($cartItem->getPrice(), 2);
113  \PHPUnit\Framework\Assert::assertEquals(
114  $sumOptionsPrice,
115  $subTotal,
116  'Bundle unit price on frontend doesn\'t equal to fixture.'
117  );
118  }
119 
125  public function toString()
126  {
127  return 'Bundle price on shopping cart page is not correct.';
128  }
129 }
processAssert(CatalogProductView $catalogProductView, BundleProduct $product, CheckoutCart $checkoutCartView, BrowserInterface $browser, BundleProduct $originalProduct=null)
assertPrice(BundleProduct $product, CatalogProductView $catalogProductView, CheckoutCart $checkoutCartView, BundleProduct $originalProduct=null)
$index
Definition: list.phtml:44