BundleOptionsTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Bundle\Test\Unit\Pricing\Price;
  8. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  9. use PHPUnit_Framework_MockObject_MockObject as MockObject;
  10. use Magento\Framework\Pricing\Amount\AmountFactory;
  11. use Magento\Framework\Pricing\Adjustment\Calculator as AdjustmentCalculator;
  12. use Magento\Framework\Pricing\PriceInfo\Base as BasePriceInfo;
  13. use Magento\Framework\Pricing\PriceCurrencyInterface;
  14. use Magento\Framework\Pricing\Amount\AmountInterface;
  15. use Magento\Framework\Pricing\Amount\Base as BaseAmount;
  16. use Magento\Bundle\Pricing\Price\BundleOptions;
  17. use Magento\Bundle\Pricing\Price\BundleSelectionPrice;
  18. use Magento\Bundle\Pricing\Price\BundleSelectionFactory;
  19. use Magento\Bundle\Pricing\Adjustment\Calculator as BundleAdjustmentCalculator;
  20. use Magento\Bundle\Model\Option as BundleOption;
  21. use Magento\Bundle\Model\Product\Type as BundleProductType;
  22. use Magento\Bundle\Model\ResourceModel\Option\Collection as BundleOptionCollection;
  23. use Magento\Catalog\Model\Product;
  24. use Magento\Tax\Helper\Data as TaxHelperData;
  25. /**
  26. * Test for Magento\Bundle\Pricing\Price\BundleOptions
  27. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  28. */
  29. class BundleOptionsTest extends \PHPUnit\Framework\TestCase
  30. {
  31. /**
  32. * @var BundleOptions
  33. */
  34. private $bundleOptions;
  35. /**
  36. * @var AdjustmentCalculator|MockObject
  37. */
  38. private $baseCalculator;
  39. /**
  40. * @var ObjectManagerHelper
  41. */
  42. private $objectManagerHelper;
  43. /**
  44. * @var Product|MockObject
  45. */
  46. private $saleableItemMock;
  47. /**
  48. * @var BundleAdjustmentCalculator|MockObject
  49. */
  50. private $bundleCalculatorMock;
  51. /**
  52. * @var BundleSelectionFactory|MockObject
  53. */
  54. private $selectionFactoryMock;
  55. /**
  56. * @var AmountFactory|MockObject
  57. */
  58. private $amountFactory;
  59. /**
  60. * @var BasePriceInfo|MockObject
  61. */
  62. private $priceInfoMock;
  63. protected function setUp()
  64. {
  65. $this->priceInfoMock = $this->getMockBuilder(BasePriceInfo::class)
  66. ->disableOriginalConstructor()
  67. ->getMock();
  68. $this->saleableItemMock = $this->getMockBuilder(Product::class)
  69. ->disableOriginalConstructor()
  70. ->getMock();
  71. $priceCurrency = $this->getMockBuilder(PriceCurrencyInterface::class)->getMock();
  72. $priceCurrency->expects($this->any())->method('round')->willReturnArgument(0);
  73. $this->selectionFactoryMock = $this->getMockBuilder(BundleSelectionFactory::class)
  74. ->disableOriginalConstructor()
  75. ->getMock();
  76. $this->amountFactory = $this->getMockBuilder(AmountFactory::class)
  77. ->disableOriginalConstructor()
  78. ->getMock();
  79. $factoryCallback = $this->returnCallback(
  80. function ($fullAmount, $adjustments) {
  81. return $this->createAmountMock(['amount' => $fullAmount, 'adjustmentAmounts' => $adjustments]);
  82. }
  83. );
  84. $this->amountFactory->expects($this->any())->method('create')->will($factoryCallback);
  85. $this->baseCalculator = $this->getMockBuilder(AdjustmentCalculator::class)
  86. ->disableOriginalConstructor()
  87. ->getMock();
  88. $taxData = $this->getMockBuilder(TaxHelperData::class)
  89. ->disableOriginalConstructor()
  90. ->getMock();
  91. $this->bundleCalculatorMock = $this->getMockBuilder(BundleAdjustmentCalculator::class)
  92. ->setConstructorArgs(
  93. [$this->baseCalculator, $this->amountFactory, $this->selectionFactoryMock, $taxData, $priceCurrency]
  94. )
  95. ->setMethods(['getOptionsAmount'])
  96. ->getMock();
  97. $this->objectManagerHelper = new ObjectManagerHelper($this);
  98. $this->bundleOptions = $this->objectManagerHelper->getObject(
  99. BundleOptions::class,
  100. [
  101. 'calculator' => $this->bundleCalculatorMock,
  102. 'bundleSelectionFactory' => $this->selectionFactoryMock,
  103. ]
  104. );
  105. }
  106. /**
  107. * @dataProvider getOptionsDataProvider
  108. * @param array $selectionCollection
  109. *
  110. * @return void
  111. */
  112. public function testGetOptions(array $selectionCollection)
  113. {
  114. $this->prepareOptionMocks($selectionCollection);
  115. $this->bundleOptions->getOptions($this->saleableItemMock);
  116. $this->assertSame($selectionCollection, $this->bundleOptions->getOptions($this->saleableItemMock));
  117. }
  118. /**
  119. * @param array $selectionCollection
  120. *
  121. * @return void
  122. */
  123. private function prepareOptionMocks(array $selectionCollection)
  124. {
  125. $this->saleableItemMock->expects($this->atLeastOnce())
  126. ->method('getStoreId')
  127. ->willReturn(1);
  128. $priceTypeMock = $this->getMockBuilder(BundleProductType::class)
  129. ->disableOriginalConstructor()
  130. ->getMock();
  131. $priceTypeMock->expects($this->atLeastOnce())
  132. ->method('setStoreFilter')
  133. ->with(1, $this->saleableItemMock)
  134. ->willReturnSelf();
  135. $optionIds = ['41', '55'];
  136. $priceTypeMock->expects($this->atLeastOnce())
  137. ->method('getOptionsIds')
  138. ->with($this->saleableItemMock)
  139. ->willReturn($optionIds);
  140. $priceTypeMock->expects($this->atLeastOnce())
  141. ->method('getSelectionsCollection')
  142. ->with($optionIds, $this->saleableItemMock)
  143. ->willReturn($selectionCollection);
  144. $collection = $this->getMockBuilder(BundleOptionCollection::class)
  145. ->disableOriginalConstructor()
  146. ->getMock();
  147. $collection->expects($this->atLeastOnce())
  148. ->method('appendSelections')
  149. ->with($selectionCollection, true, false)
  150. ->willReturn($selectionCollection);
  151. $priceTypeMock->expects($this->atLeastOnce())
  152. ->method('getOptionsCollection')
  153. ->with($this->saleableItemMock)
  154. ->willReturn($collection);
  155. $this->saleableItemMock->expects($this->atLeastOnce())
  156. ->method('getTypeInstance')
  157. ->willReturn($priceTypeMock);
  158. }
  159. /**
  160. * @return array
  161. */
  162. public function getOptionsDataProvider() : array
  163. {
  164. return [
  165. [
  166. ['1', '2'],
  167. ],
  168. ];
  169. }
  170. /**
  171. * @dataProvider selectionAmountDataProvider
  172. *
  173. * @param float $selectionQty
  174. * @param float|bool $selectionAmount
  175. * @param bool $useRegularPrice
  176. *
  177. * @return void
  178. */
  179. public function testGetOptionSelectionAmount(float $selectionQty, $selectionAmount, bool $useRegularPrice)
  180. {
  181. $selection = $this->createPartialMock(Product::class, ['getSelectionQty', '__wakeup']);
  182. $amountInterfaceMock = $this->getMockBuilder(AmountInterface::class)
  183. ->getMockForAbstractClass();
  184. $amountInterfaceMock->expects($this->once())
  185. ->method('getValue')
  186. ->willReturn($selectionAmount);
  187. $selection->expects($this->once())
  188. ->method('getSelectionQty')
  189. ->willReturn($selectionQty);
  190. $priceMock = $this->getMockBuilder(BundleSelectionPrice::class)
  191. ->disableOriginalConstructor()
  192. ->getMock();
  193. $priceMock->expects($this->once())
  194. ->method('getAmount')
  195. ->willReturn($amountInterfaceMock);
  196. $this->selectionFactoryMock->expects($this->once())
  197. ->method('create')
  198. ->with($this->saleableItemMock, $selection, $selectionQty)
  199. ->willReturn($priceMock);
  200. $optionSelectionAmount = $this->bundleOptions->getOptionSelectionAmount(
  201. $this->saleableItemMock,
  202. $selection,
  203. $useRegularPrice
  204. );
  205. $this->assertSame($selectionAmount, $optionSelectionAmount->getValue());
  206. }
  207. /**
  208. * @return array
  209. */
  210. public function selectionAmountDataProvider(): array
  211. {
  212. return [
  213. [1., 50.5, false],
  214. [2.2, false, true],
  215. ];
  216. }
  217. /**
  218. * Create amount mock.
  219. *
  220. * @param array $amountData
  221. * @return BaseAmount|MockObject
  222. */
  223. private function createAmountMock(array $amountData)
  224. {
  225. /** @var BaseAmount|MockObject $amount */
  226. $amount = $this->getMockBuilder(BaseAmount::class)
  227. ->disableOriginalConstructor()
  228. ->getMock();
  229. $amount->expects($this->any())->method('getAdjustmentAmounts')
  230. ->willReturn($amountData['adjustmentAmounts'] ?? []);
  231. $amount->expects($this->any())->method('getValue')->willReturn($amountData['amount']);
  232. return $amount;
  233. }
  234. /**
  235. * Create option mock.
  236. *
  237. * @param array $optionData
  238. * @return BundleOption|MockObject
  239. */
  240. private function createOptionMock(array $optionData)
  241. {
  242. /** @var BundleOption|MockObject $option */
  243. $option = $this->createPartialMock(BundleOption::class, ['isMultiSelection', '__wakeup']);
  244. $option->expects($this->any())->method('isMultiSelection')
  245. ->willReturn($optionData['isMultiSelection']);
  246. $selections = [];
  247. foreach ($optionData['selections'] as $selectionData) {
  248. $selections[] = $this->createSelectionMock($selectionData);
  249. }
  250. foreach ($optionData['data'] as $key => $value) {
  251. $option->setData($key, $value);
  252. }
  253. $option->setData('selections', $selections);
  254. return $option;
  255. }
  256. /**
  257. * Create selection product mock.
  258. *
  259. * @param array $selectionData
  260. * @return Product|MockObject
  261. */
  262. private function createSelectionMock(array $selectionData)
  263. {
  264. $selection = $this->getMockBuilder(Product::class)
  265. ->setMethods(['isSalable', 'getAmount', 'getQuantity', 'getProduct', '__wakeup'])
  266. ->disableOriginalConstructor()
  267. ->getMock();
  268. // All items are saleable
  269. $selection->expects($this->any())->method('isSalable')->willReturn(true);
  270. foreach ($selectionData['data'] as $key => $value) {
  271. $selection->setData($key, $value);
  272. }
  273. $amountMock = $this->createAmountMock($selectionData['amount']);
  274. $selection->expects($this->any())->method('getAmount')->willReturn($amountMock);
  275. $selection->expects($this->any())->method('getQuantity')->willReturn(1);
  276. $innerProduct = $this->getMockBuilder(Product::class)
  277. ->setMethods(['getSelectionCanChangeQty', '__wakeup'])
  278. ->disableOriginalConstructor()
  279. ->getMock();
  280. $innerProduct->expects($this->any())->method('getSelectionCanChangeQty')->willReturn(true);
  281. $selection->expects($this->any())->method('getProduct')->willReturn($innerProduct);
  282. return $selection;
  283. }
  284. /**
  285. * @dataProvider getTestDataForCalculation
  286. * @param array $optionList
  287. * @param array $expected
  288. *
  289. * @return void
  290. */
  291. public function testCalculation(array $optionList, array $expected)
  292. {
  293. $storeId = 1;
  294. $this->saleableItemMock->expects($this->any())->method('getStoreId')->willReturn($storeId);
  295. $this->selectionFactoryMock->expects($this->any())->method('create')->willReturnArgument(1);
  296. $this->baseCalculator->expects($this->atLeastOnce())->method('getAmount')
  297. ->willReturn($this->createAmountMock(['amount' => 0.]));
  298. $options = [];
  299. foreach ($optionList as $optionData) {
  300. $options[] = $this->createOptionMock($optionData);
  301. }
  302. /** @var BundleOptionCollection|MockObject $optionsCollection */
  303. $optionsCollection = $this->getMockBuilder(BundleOptionCollection::class)
  304. ->disableOriginalConstructor()
  305. ->getMock();
  306. $optionsCollection->expects($this->atLeastOnce())->method('appendSelections')->willReturn($options);
  307. /** @var \Magento\Catalog\Model\Product\Type\AbstractType|MockObject $typeMock */
  308. $typeMock = $this->getMockBuilder(BundleProductType::class)
  309. ->disableOriginalConstructor()
  310. ->getMock();
  311. $typeMock->expects($this->any())->method('setStoreFilter')
  312. ->with($storeId, $this->saleableItemMock);
  313. $typeMock->expects($this->any())->method('getOptionsCollection')
  314. ->with($this->saleableItemMock)
  315. ->willReturn($optionsCollection);
  316. $this->saleableItemMock->expects($this->any())->method('getTypeInstance')->willReturn($typeMock);
  317. $this->assertEquals($expected['min'], $this->bundleOptions->calculateOptions($this->saleableItemMock));
  318. $this->assertEquals($expected['max'], $this->bundleOptions->calculateOptions($this->saleableItemMock, false));
  319. }
  320. /**
  321. * @return array
  322. */
  323. public function getTestDataForCalculation(): array
  324. {
  325. return [
  326. 'first case' => [
  327. 'optionList' => [
  328. // first option with single choice of product
  329. [
  330. 'isMultiSelection' => false,
  331. 'data' => [
  332. 'title' => 'test option 1',
  333. 'default_title' => 'test option 1',
  334. 'type' => 'select',
  335. 'option_id' => '1',
  336. 'position' => '0',
  337. 'required' => '1',
  338. ],
  339. 'selections' => [
  340. [
  341. 'data' => ['price' => 70.],
  342. 'amount' => ['amount' => 70],
  343. ],
  344. [
  345. 'data' => ['price' => 80.],
  346. 'amount' => ['amount' => 80],
  347. ],
  348. [
  349. 'data' => ['price' => 50.],
  350. 'amount' => ['amount' => 50],
  351. ],
  352. ],
  353. ],
  354. // second not required option
  355. [
  356. 'isMultiSelection' => false,
  357. 'data' => [
  358. 'title' => 'test option 2',
  359. 'default_title' => 'test option 2',
  360. 'type' => 'select',
  361. 'option_id' => '2',
  362. 'position' => '1',
  363. 'required' => '0',
  364. ],
  365. 'selections' => [
  366. [
  367. 'data' => ['value' => 20.],
  368. 'amount' => ['amount' => 20],
  369. ],
  370. ],
  371. ],
  372. // third with multi-selection
  373. [
  374. 'isMultiSelection' => true,
  375. 'data' => [
  376. 'title' => 'test option 3',
  377. 'default_title' => 'test option 3',
  378. 'type' => 'select',
  379. 'option_id' => '3',
  380. 'position' => '2',
  381. 'required' => '1',
  382. ],
  383. 'selections' => [
  384. [
  385. 'data' => ['price' => 40.],
  386. 'amount' => ['amount' => 40],
  387. ],
  388. [
  389. 'data' => ['price' => 20.],
  390. 'amount' => ['amount' => 20],
  391. ],
  392. [
  393. 'data' => ['price' => 60.],
  394. 'amount' => ['amount' => 60],
  395. ],
  396. ],
  397. ],
  398. // fourth without selections
  399. [
  400. 'isMultiSelection' => true,
  401. 'data' => [
  402. 'title' => 'test option 3',
  403. 'default_title' => 'test option 3',
  404. 'type' => 'select',
  405. 'option_id' => '4',
  406. 'position' => '3',
  407. 'required' => '1',
  408. ],
  409. 'selections' => [],
  410. ],
  411. ],
  412. 'expected' => ['min' => 70, 'max' => 220],
  413. ],
  414. ];
  415. }
  416. }