GetPriceConfigurationObserverTest.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Weee\Test\Unit\Observer;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  8. class GetPriceConfigurationObserverTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * Tests the methods that rely on the ScopeConfigInterface object to provide their return values
  12. * @dataProvider getPriceConfigurationProvider
  13. * @param bool $hasWeeeAttributes
  14. * @param array $testArray
  15. * @param array $expectedArray
  16. */
  17. public function testGetPriceConfiguration($hasWeeeAttributes, $testArray, $expectedArray)
  18. {
  19. $configObj = new \Magento\Framework\DataObject(
  20. [
  21. 'config' => $testArray,
  22. ]
  23. );
  24. $weeeObject1 = new \Magento\Framework\DataObject(
  25. [
  26. 'code' => 'fpt1',
  27. 'amount' => '15.0000',
  28. ]
  29. );
  30. $weeeObject2 = new \Magento\Framework\DataObject(
  31. [
  32. 'code' => 'fpt2',
  33. 'amount' => '16.0000',
  34. ]
  35. );
  36. $weeeHelper=$this->createMock(\Magento\Weee\Helper\Data::class);
  37. $weeeHelper->expects($this->any())
  38. ->method('isEnabled')
  39. ->will($this->returnValue(true));
  40. $observerObject=$this->createMock(\Magento\Framework\Event\Observer::class);
  41. $observerObject->expects($this->any())
  42. ->method('getData')
  43. ->with('configObj')
  44. ->will($this->returnValue($configObj));
  45. $productInstance=$this->createMock(\Magento\Catalog\Model\Product\Type\Simple::class);
  46. $product = $this->createPartialMock(
  47. \Magento\Bundle\Model\Product\Type::class,
  48. ['getTypeInstance', 'getTypeId', 'getStoreId']
  49. );
  50. $product->expects($this->any())
  51. ->method('getTypeInstance')
  52. ->will($this->returnValue($productInstance));
  53. $product->expects($this->any())
  54. ->method('getTypeId')
  55. ->will($this->returnValue('simple'));
  56. $product->expects($this->any())
  57. ->method('getStoreId')
  58. ->will($this->returnValue(null));
  59. $registry=$this->createMock(\Magento\Framework\Registry::class);
  60. $registry->expects($this->any())
  61. ->method('registry')
  62. ->with('current_product')
  63. ->will($this->returnValue($product));
  64. if ($hasWeeeAttributes) {
  65. $weeeHelper->expects($this->any())
  66. ->method('getWeeeAttributesForBundle')
  67. ->will($this->returnValue([
  68. 1 => ['fpt1' => $weeeObject1],
  69. 2 => [
  70. 'fpt1' => $weeeObject1,
  71. 'fpt2' => $weeeObject2
  72. ]
  73. ]));
  74. } else {
  75. $weeeHelper->expects($this->any())
  76. ->method('getWeeeAttributesForBundle')
  77. ->will($this->returnValue(null));
  78. }
  79. $objectManager = new ObjectManager($this);
  80. /** @var \Magento\Weee\Observer\GetPriceConfigurationObserver $weeeObserverObject */
  81. $weeeObserverObject = $objectManager->getObject(
  82. \Magento\Weee\Observer\GetPriceConfigurationObserver::class,
  83. [
  84. 'weeeData' => $weeeHelper,
  85. 'registry' => $registry,
  86. ]
  87. );
  88. $weeeObserverObject->execute($observerObject);
  89. $this->assertEquals($expectedArray, $configObj->getData('config'));
  90. }
  91. /**
  92. * @return array
  93. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  94. */
  95. public function getPriceConfigurationProvider()
  96. {
  97. return [
  98. "basic" => [
  99. 'hasWeeeAttributes' => true,
  100. 'testArray' => [
  101. [
  102. [
  103. 'optionId' => 1,
  104. 'prices' => [
  105. 'finalPrice' => ['amount' => 31.50],
  106. 'basePrice' => ['amount' => 33.50],
  107. ],
  108. ],
  109. [
  110. 'optionId' => 2,
  111. 'prices' => [
  112. 'finalPrice' =>['amount' => 331.50],
  113. 'basePrice' => ['amount' => 333.50],
  114. ],
  115. ],
  116. ],
  117. ],
  118. 'expectedArray' => [
  119. [
  120. [
  121. 'optionId' => 1,
  122. 'prices' => [
  123. 'finalPrice' => ['amount' => 31.50],
  124. 'basePrice' => ['amount' => 33.50],
  125. 'weeePrice' => ['amount' => 46.5],
  126. 'weeePricefpt1' => ['amount' => 15],
  127. ],
  128. ],
  129. [
  130. 'optionId' => 2,
  131. 'prices' => [
  132. 'finalPrice' =>['amount' => 331.50],
  133. 'basePrice' => ['amount' => 333.50],
  134. 'weeePrice' => ['amount' => 362.5],
  135. 'weeePricefpt1' => ['amount' => 15],
  136. 'weeePricefpt2' => ['amount' => 16],
  137. ],
  138. ],
  139. ],
  140. ],
  141. ],
  142. "layered, with extra keys" => [
  143. 'hasWeeeAttributes' => true,
  144. 'testArray' => [
  145. [
  146. [
  147. 'prices' => [
  148. 'finalPrice' => ['amount' => 31.50],
  149. ],
  150. 'somekey' => 0,
  151. ],
  152. [
  153. [
  154. [
  155. 'prices' => [
  156. 'finalPrice' =>['amount' => 321.50],
  157. ],
  158. ],
  159. 'otherkey' => [ 1, 2 , 3],
  160. ]
  161. ],
  162. ],
  163. ],
  164. 'expectedArray' => [
  165. [
  166. [
  167. 'prices' => [
  168. 'finalPrice' => ['amount' => 31.50],
  169. 'weeePrice' => ['amount' => 31.50],
  170. ],
  171. 'somekey' => 0,
  172. ],
  173. [
  174. [
  175. [
  176. 'prices' => [
  177. 'finalPrice' =>['amount' => 321.50],
  178. 'weeePrice' => ['amount' => 321.50],
  179. ],
  180. ],
  181. 'otherkey' => [ 1, 2 , 3],
  182. ]
  183. ],
  184. ],
  185. ],
  186. ],
  187. "no Weee attributes, expect WeeePrice to be same as FinalPrice" => [
  188. 'hasWeeeAttributes' => false,
  189. 'testArray' => [
  190. [
  191. [
  192. 'optionId' => 1,
  193. 'prices' => [
  194. 'basePrice' => ['amount' => 10],
  195. 'finalPrice' => ['amount' => 11],
  196. ],
  197. ],
  198. ],
  199. ],
  200. 'expectedArray' => [
  201. [
  202. [
  203. 'optionId' => 1,
  204. 'prices' => [
  205. 'basePrice' => ['amount' => 10],
  206. 'finalPrice' => ['amount' => 11],
  207. 'weeePrice' => ['amount' => 11],
  208. ],
  209. ],
  210. ],
  211. ],
  212. ],
  213. ];
  214. }
  215. }