/dev/tests/functional/tests/app/Mage/Catalog/Test/Constraint/AssertProductInCart.php

https://gitlab.com/LisovyiEvhenii/ismextensions · PHP · 144 lines · 70 code · 12 blank · 62 comment · 7 complexity · 0435be1f657083aad2272b31b6e5d8d1 MD5 · raw file

  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magento.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magento.com for more information.
  20. *
  21. * @category Tests
  22. * @package Tests_Functional
  23. * @copyright Copyright (c) 2006-2016 X.commerce, Inc. and affiliates (http://www.magento.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. namespace Mage\Catalog\Test\Constraint;
  27. use Magento\Mtf\Client\Browser;
  28. use Magento\Mtf\Fixture\InjectableFixture;
  29. use Magento\Mtf\Constraint\AbstractConstraint;
  30. use Mage\Checkout\Test\Page\CheckoutCart;
  31. use Mage\Catalog\Test\Page\Product\CatalogProductView;
  32. use Mage\Catalog\Test\Fixture\CatalogProductSimple;
  33. /**
  34. * Assertion that the product is correctly displayed in cart.
  35. */
  36. class AssertProductInCart extends AbstractConstraint
  37. {
  38. /**
  39. * Constraint severeness.
  40. *
  41. * @var string
  42. */
  43. protected $severeness = 'low';
  44. /**
  45. * Assertion that the product is correctly displayed in cart.
  46. *
  47. * @param CatalogProductView $catalogProductView
  48. * @param InjectableFixture $product
  49. * @param Browser $browser
  50. * @param CheckoutCart $checkoutCart
  51. * @return void
  52. */
  53. public function processAssert(
  54. CatalogProductView $catalogProductView,
  55. InjectableFixture $product,
  56. Browser $browser,
  57. CheckoutCart $checkoutCart
  58. ) {
  59. // Add product to cart
  60. $browser->open($_ENV['app_frontend_url'] . $product->getUrlKey() . '.html');
  61. $catalogProductView->getViewBlock()->addToCart($product);
  62. // Check price
  63. $this->assertOnShoppingCart($product, $checkoutCart);
  64. }
  65. /**
  66. * Assert prices on the shopping cart.
  67. *
  68. * @param InjectableFixture $product
  69. * @param CheckoutCart $checkoutCart
  70. * @return void
  71. */
  72. protected function assertOnShoppingCart(InjectableFixture $product, CheckoutCart $checkoutCart)
  73. {
  74. $cartItem = $checkoutCart->getCartBlock()->getCartItem($product);
  75. $formPrice = $cartItem->getCartItemTypePrice('price');
  76. $fixturePrice = number_format($this->prepareFixturePrice($product), 2);
  77. \PHPUnit_Framework_Assert::assertEquals(
  78. $fixturePrice,
  79. $formPrice,
  80. 'Product price in shopping cart is not correct.'
  81. );
  82. }
  83. /**
  84. * Prepare product price from fixture.
  85. *
  86. * @param InjectableFixture $product
  87. * @return float
  88. */
  89. protected function prepareFixturePrice(InjectableFixture $product)
  90. {
  91. /** @var CatalogProductSimple $product */
  92. $customOptions = $product->getCustomOptions();
  93. $checkoutData = $product->getCheckoutData();
  94. $checkoutCustomOptions = isset($checkoutData['options']['custom_options'])
  95. ? $checkoutData['options']['custom_options']
  96. : [];
  97. if (isset($checkoutData['cartItem'])) {
  98. $fixturePrice = $checkoutData['cartItem']['price'];
  99. } else {
  100. $fixturePrice = $product->getPrice();
  101. $groupPrice = $product->getGroupPrice();
  102. $specialPrice = $product->getSpecialPrice();
  103. if ($groupPrice) {
  104. $groupPrice = reset($groupPrice);
  105. $fixturePrice = $groupPrice['price'];
  106. }
  107. if ($specialPrice) {
  108. $fixturePrice = $specialPrice;
  109. }
  110. foreach ($checkoutCustomOptions as $checkoutOption) {
  111. $attributeKey = str_replace('attribute_key_', '', $checkoutOption['title']);
  112. $optionKey = str_replace('option_key_', '', $checkoutOption['value']);
  113. $option = $customOptions[$attributeKey]['options'][$optionKey];
  114. if ('Fixed' == $option['price_type']) {
  115. $fixturePrice += $option['price'];
  116. } else {
  117. $fixturePrice += ($fixturePrice / 100) * $option['price'];
  118. }
  119. }
  120. }
  121. return $fixturePrice;
  122. }
  123. /**
  124. * Returns a string representation of the object.
  125. *
  126. * @return string
  127. */
  128. public function toString()
  129. {
  130. return 'Product is correctly displayed in cart.';
  131. }
  132. }