GetPriceConfigurationObserverTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Tax\Test\Unit\Observer;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  8. /**
  9. * Class GetPriceConfigurationObserverTest
  10. *
  11. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  12. */
  13. class GetPriceConfigurationObserverTest extends \PHPUnit\Framework\TestCase
  14. {
  15. /**
  16. * @var \Magento\Tax\Observer\GetPriceConfigurationObserver
  17. */
  18. protected $model;
  19. /**
  20. * @var \Magento\Framework\Registry|\PHPUnit_Framework_MockObject_MockObject
  21. */
  22. protected $registry;
  23. /**
  24. * @var \Magento\Tax\Helper\Data|\PHPUnit_Framework_MockObject_MockObject
  25. */
  26. protected $taxData;
  27. /**
  28. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  29. */
  30. protected $objectManager;
  31. /**
  32. * test Execute
  33. * @dataProvider getPriceConfigurationProvider
  34. * @param array $testArray
  35. * @param array $expectedArray
  36. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  37. */
  38. public function testExecute($testArray, $expectedArray)
  39. {
  40. $configObj = new \Magento\Framework\DataObject(
  41. [
  42. 'config' => $testArray,
  43. ]
  44. );
  45. $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  46. $className = \Magento\Framework\Registry::class;
  47. $this->registry = $this->createMock($className);
  48. $className = \Magento\Tax\Helper\Data::class;
  49. $this->taxData = $this->createMock($className);
  50. $observerObject=$this->createMock(\Magento\Framework\Event\Observer::class);
  51. $observerObject->expects($this->any())
  52. ->method('getData')
  53. ->with('configObj')
  54. ->will($this->returnValue($configObj));
  55. $baseAmount = $this->createPartialMock(
  56. \Magento\Framework\Pricing\Amount\Base::class,
  57. ['getBaseAmount', 'getAdjustmentAmount', 'hasAdjustment']
  58. );
  59. $baseAmount->expects($this->any())
  60. ->method('hasAdjustment')
  61. ->will($this->returnValue(true));
  62. $baseAmount->expects($this->any())
  63. ->method('getBaseAmount')
  64. ->will($this->returnValue(33.5));
  65. $baseAmount->expects($this->any())
  66. ->method('getAdjustmentAmount')
  67. ->will($this->returnValue(1.5));
  68. $priceInfo = $this->createPartialMock(\Magento\Framework\Pricing\PriceInfo\Base::class, ['getPrice']);
  69. $basePrice = $this->createPartialMock(\Magento\Catalog\Pricing\Price\BasePrice::class, ['getAmount']);
  70. $basePrice->expects($this->any())
  71. ->method('getAmount')
  72. ->will($this->returnValue($baseAmount));
  73. $priceInfo->expects($this->any())
  74. ->method('getPrice')
  75. ->will($this->returnValue($basePrice));
  76. $prod1 = $this->createPartialMock(\Magento\Catalog\Model\Product::class, ['getId', 'getPriceInfo']);
  77. $prod2 = $this->createMock(\Magento\Catalog\Model\Product::class);
  78. $prod1->expects($this->any())
  79. ->method('getId')
  80. ->will($this->returnValue(1));
  81. $prod1->expects($this->any())
  82. ->method('getPriceInfo')
  83. ->will($this->returnValue($priceInfo));
  84. $optionCollection =
  85. $this->createPartialMock(\Magento\Bundle\Model\ResourceModel\Selection\Collection::class, ['getItems']);
  86. $optionCollection->expects($this->any())
  87. ->method('getItems')
  88. ->will($this->returnValue([$prod1, $prod2]));
  89. $productInstance =
  90. $this->createPartialMock(
  91. \Magento\Catalog\Model\Product\Type::class,
  92. ['setStoreFilter', 'getSelectionsCollection', 'getOptionsIds']
  93. );
  94. $product = $this->createPartialMock(
  95. \Magento\Bundle\Model\Product\Type::class,
  96. ['getTypeInstance', 'getTypeId', 'getStoreId', 'getSelectionsCollection']
  97. );
  98. $product->expects($this->any())
  99. ->method('getTypeInstance')
  100. ->will($this->returnValue($productInstance));
  101. $product->expects($this->any())
  102. ->method('getTypeId')
  103. ->will($this->returnValue('bundle'));
  104. $product->expects($this->any())
  105. ->method('getStoreId')
  106. ->will($this->returnValue(null));
  107. $productInstance->expects($this->any())
  108. ->method('getSelectionsCollection')
  109. ->will($this->returnValue($optionCollection));
  110. $productInstance->expects($this->any())
  111. ->method('getOptionsIds')
  112. ->will($this->returnValue(true));
  113. $this->registry->expects($this->any())
  114. ->method('registry')
  115. ->with('current_product')
  116. ->will($this->returnValue($product));
  117. $this->taxData->expects($this->any())
  118. ->method('displayPriceIncludingTax')
  119. ->will($this->returnValue(true));
  120. $objectManager = new ObjectManager($this);
  121. $this->model = $objectManager->getObject(
  122. \Magento\Tax\Observer\GetPriceConfigurationObserver::class,
  123. [
  124. 'taxData' => $this->taxData,
  125. 'registry' => $this->registry,
  126. ]
  127. );
  128. $this->model->execute($observerObject);
  129. $this->assertEquals($expectedArray, $configObj->getData('config'));
  130. }
  131. /**
  132. * @return array
  133. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  134. */
  135. public function getPriceConfigurationProvider()
  136. {
  137. return [
  138. "basic" => [
  139. 'testArray' => [
  140. [
  141. [
  142. 'optionId' => 1,
  143. 'prices' => [
  144. 'finalPrice' => ['amount' => 35.50],
  145. 'basePrice' => ['amount' => 30.50],
  146. ],
  147. ],
  148. [
  149. 'optionId' => 2,
  150. 'prices' => [
  151. 'finalPrice' =>['amount' => 333.50],
  152. 'basePrice' => ['amount' => 300.50],
  153. ],
  154. ],
  155. ],
  156. ],
  157. 'expectedArray' => [
  158. [
  159. [
  160. 'optionId' => 1,
  161. 'prices' => [
  162. 'finalPrice' => ['amount' => 35.50],
  163. 'basePrice' => ['amount' => 35],
  164. 'oldPrice' => ['amount' => 35],
  165. ],
  166. ],
  167. [
  168. 'optionId' => 2,
  169. 'prices' => [
  170. 'finalPrice' =>['amount' => 333.50],
  171. 'basePrice' => ['amount' => 300.50],
  172. ],
  173. ],
  174. ],
  175. ],
  176. ],
  177. ];
  178. }
  179. }