/dev/tests/functional/tests/app/Magento/Bundle/Test/Constraint/AssertBundlePriceType.php

https://gitlab.com/yousafsyed/easternglamor · PHP · 130 lines · 75 code · 12 blank · 43 comment · 2 complexity · 4f1945e5b35e9311535e44be86acc826 MD5 · raw file

  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Bundle\Test\Constraint;
  7. use Magento\Bundle\Test\Fixture\BundleProduct;
  8. use Magento\Catalog\Test\Page\Product\CatalogProductView;
  9. use Magento\Checkout\Test\Page\CheckoutCart;
  10. use Magento\Mtf\Client\BrowserInterface;
  11. use Magento\Mtf\Constraint\AbstractConstraint;
  12. /**
  13. * Class AssertBundlePriceType
  14. */
  15. class AssertBundlePriceType extends AbstractConstraint
  16. {
  17. /**
  18. * Product price type
  19. *
  20. * @var string
  21. */
  22. protected $productPriceType = 'Dynamic';
  23. /**
  24. * Assert that displayed price for bundle items on shopping cart page equals to passed from fixture.
  25. * Price for bundle items has two options:
  26. * 1. Fixed (price of bundle product)
  27. * 2. Dynamic (price of bundle item)
  28. *
  29. * @param CatalogProductView $catalogProductView
  30. * @param BundleProduct $product
  31. * @param CheckoutCart $checkoutCartView
  32. * @param BrowserInterface $browser
  33. * @param BundleProduct $originalProduct [optional]
  34. * @return void
  35. */
  36. public function processAssert(
  37. CatalogProductView $catalogProductView,
  38. BundleProduct $product,
  39. CheckoutCart $checkoutCartView,
  40. BrowserInterface $browser,
  41. BundleProduct $originalProduct = null
  42. ) {
  43. $checkoutCartView->open()->getCartBlock()->clearShoppingCart();
  44. //Open product view page
  45. $browser->open($_ENV['app_frontend_url'] . $product->getUrlKey() . '.html');
  46. //Process assertions
  47. $this->assertPrice($product, $catalogProductView, $checkoutCartView, $originalProduct);
  48. }
  49. /**
  50. * Assert prices on the product view page and shopping cart page.
  51. *
  52. * @param BundleProduct $product
  53. * @param CatalogProductView $catalogProductView
  54. * @param CheckoutCart $checkoutCartView
  55. * @param BundleProduct $originalProduct [optional]
  56. * @return void
  57. *
  58. * @SuppressWarnings(PHPMD.NPathComplexity)
  59. */
  60. protected function assertPrice(
  61. BundleProduct $product,
  62. CatalogProductView $catalogProductView,
  63. CheckoutCart $checkoutCartView,
  64. BundleProduct $originalProduct = null
  65. ) {
  66. $bundleData = $product->getData();
  67. $this->productPriceType = $originalProduct !== null
  68. ? $originalProduct->getPriceType()
  69. : $product->getPriceType();
  70. $catalogProductView->getViewBlock()->addToCart($product);
  71. $catalogProductView->getMessagesBlock()->waitSuccessMessage();
  72. $checkoutCartView->open();
  73. $cartItem = $checkoutCartView->getCartBlock()->getCartItem($product);
  74. $specialPrice = 0;
  75. $optionPrice = [];
  76. $fillData = $product->getCheckoutData();
  77. foreach ($fillData['options']['bundle_options'] as $key => $data) {
  78. $subProductPrice = 0;
  79. foreach ($bundleData['bundle_selections']['products'][$key] as $productKey => $itemProduct) {
  80. if (strpos($itemProduct->getName(), $data['value']['name']) !== false) {
  81. $data['value']['key'] = $productKey;
  82. $subProductPrice = $itemProduct->getPrice();
  83. }
  84. }
  85. $optionPrice[$key]['price'] = $this->productPriceType == 'Fixed'
  86. ? number_format(
  87. $bundleData['bundle_selections']['bundle_options'][$key]['assigned_products'][$data['value']['key']]
  88. ['data']['selection_price_value'],
  89. 2
  90. )
  91. : number_format($subProductPrice, 2);
  92. }
  93. foreach ($optionPrice as $index => $item) {
  94. $item['price'] -= $item['price'] * $specialPrice;
  95. \PHPUnit_Framework_Assert::assertEquals(
  96. number_format($item['price'], 2),
  97. $cartItem->getPriceBundleOptions($index + 1),
  98. 'Bundle item ' . ($index + 1) . ' options on frontend don\'t equal to fixture.'
  99. );
  100. }
  101. $sumOptionsPrice = $product->getDataFieldConfig('price')['source']->getPriceData()['cart_price'];
  102. $subTotal = number_format($cartItem->getPrice(), 2);
  103. \PHPUnit_Framework_Assert::assertEquals(
  104. $sumOptionsPrice,
  105. $subTotal,
  106. 'Bundle unit price on frontend doesn\'t equal to fixture.'
  107. );
  108. }
  109. /**
  110. * Returns a string representation of the object
  111. *
  112. * @return string
  113. */
  114. public function toString()
  115. {
  116. return 'Bundle price on shopping cart page is not correct.';
  117. }
  118. }