LinkManagementTest.php 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Bundle\Test\Unit\Model;
  8. use Magento\Bundle\Model\LinkManagement;
  9. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  10. /**
  11. * Class LinkManagementTest
  12. *
  13. * @SuppressWarnings(PHPMD.TooManyFields)
  14. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  15. */
  16. class LinkManagementTest extends \PHPUnit\Framework\TestCase
  17. {
  18. /**
  19. * @var \Magento\Bundle\Model\LinkManagement
  20. */
  21. protected $model;
  22. /**
  23. * @var \Magento\Catalog\Model\ProductRepository|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $productRepository;
  26. /**
  27. * @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $product;
  30. /**
  31. * @var \PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $linkFactory;
  34. /**
  35. * @var \Magento\Catalog\Model\Product\Type\Interceptor|\PHPUnit_Framework_MockObject_MockObject
  36. */
  37. protected $productType;
  38. /**
  39. * @var \Magento\Bundle\Model\ResourceModel\Option\Collection|\PHPUnit_Framework_MockObject_MockObject
  40. */
  41. protected $optionCollection;
  42. /**
  43. * @var \Magento\Bundle\Model\ResourceModel\Selection\Collection|\PHPUnit_Framework_MockObject_MockObject
  44. */
  45. protected $selectionCollection;
  46. /**
  47. * @var \Magento\Bundle\Model\Option|\PHPUnit_Framework_MockObject_MockObject
  48. */
  49. protected $option;
  50. /**
  51. * @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Bundle\Model\SelectionFactory
  52. */
  53. protected $bundleSelectionMock;
  54. /**
  55. * @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Bundle\Model\ResourceModel\BundleFactory
  56. */
  57. protected $bundleFactoryMock;
  58. /**
  59. * @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Bundle\Model\ResourceModel\Option\CollectionFactory
  60. */
  61. protected $optionCollectionFactoryMock;
  62. /**
  63. * @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Store\Model\StoreManagerInterface
  64. */
  65. protected $storeManagerMock;
  66. /**
  67. * @var \PHPUnit_Framework_MockObject_MockObject
  68. */
  69. protected $link;
  70. /**
  71. * @var int
  72. */
  73. protected $storeId = 2;
  74. /**
  75. * @var array
  76. */
  77. protected $optionIds = [1, 2, 3];
  78. /**
  79. * @var \PHPUnit_Framework_MockObject_MockObject
  80. */
  81. protected $dataObjectHelperMock;
  82. /**
  83. * @var \Magento\Framework\EntityManager\MetadataPool|\PHPUnit_Framework_MockObject_MockObject
  84. */
  85. protected $metadataPoolMock;
  86. /**
  87. * @var \Magento\Framework\EntityManager\EntityMetadata|\PHPUnit_Framework_MockObject_MockObject
  88. */
  89. protected $metadataMock;
  90. /**
  91. * @var string
  92. */
  93. protected $linkField = 'product_id';
  94. protected function setUp()
  95. {
  96. $helper = new ObjectManager($this);
  97. $this->productRepository = $this->getMockBuilder(\Magento\Catalog\Model\ProductRepository::class)
  98. ->setMethods(['get'])
  99. ->disableOriginalConstructor()
  100. ->getMock();
  101. $this->productType = $this->getMockBuilder(\Magento\Bundle\Model\Product\Type\Interceptor::class)
  102. ->setMethods(['getOptionsCollection', 'setStoreFilter', 'getSelectionsCollection', 'getOptionsIds'])
  103. ->disableOriginalConstructor()
  104. ->getMock();
  105. $this->option = $this->getMockBuilder(\Magento\Bundle\Model\Option::class)
  106. ->setMethods(['getSelections', 'getOptionId', '__wakeup'])
  107. ->disableOriginalConstructor()
  108. ->getMock();
  109. $this->optionCollection = $this->getMockBuilder(\Magento\Bundle\Model\ResourceModel\Option\Collection::class)
  110. ->setMethods(['appendSelections'])
  111. ->disableOriginalConstructor()
  112. ->getMock();
  113. $this->selectionCollection = $this->getMockBuilder(
  114. \Magento\Bundle\Model\ResourceModel\Selection\Collection::class
  115. )->disableOriginalConstructor()->getMock();
  116. $this->product = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
  117. ->setMethods(['getTypeInstance', 'getStoreId', 'getTypeId', '__wakeup', 'getId', 'getData'])
  118. ->disableOriginalConstructor()
  119. ->getMock();
  120. $this->link = $this->getMockBuilder(\Magento\Bundle\Api\Data\LinkInterface::class)
  121. ->disableOriginalConstructor()
  122. ->getMock();
  123. $this->linkFactory = $this->getMockBuilder(\Magento\Bundle\Api\Data\LinkInterfaceFactory::class)
  124. ->setMethods(['create'])
  125. ->disableOriginalConstructor()
  126. ->getMock();
  127. $this->bundleSelectionMock = $this->createPartialMock(
  128. \Magento\Bundle\Model\SelectionFactory::class,
  129. ['create']
  130. );
  131. $this->bundleFactoryMock = $this->createPartialMock(
  132. \Magento\Bundle\Model\ResourceModel\BundleFactory::class,
  133. ['create']
  134. );
  135. $this->optionCollectionFactoryMock = $this->createPartialMock(
  136. \Magento\Bundle\Model\ResourceModel\Option\CollectionFactory::class,
  137. ['create']
  138. );
  139. $this->storeManagerMock = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
  140. $this->metadataPoolMock = $this->getMockBuilder(\Magento\Framework\EntityManager\MetadataPool::class)
  141. ->disableOriginalConstructor()
  142. ->getMock();
  143. $this->metadataMock = $this->getMockBuilder(\Magento\Framework\EntityManager\EntityMetadata::class)
  144. ->disableOriginalConstructor()
  145. ->getMock();
  146. $this->metadataPoolMock->expects($this->any())->method('getMetadata')
  147. ->with(\Magento\Catalog\Api\Data\ProductInterface::class)
  148. ->willReturn($this->metadataMock);
  149. $this->dataObjectHelperMock = $this->getMockBuilder(\Magento\Framework\Api\DataObjectHelper::class)
  150. ->disableOriginalConstructor()
  151. ->getMock();
  152. $this->model = $helper->getObject(
  153. LinkManagement::class,
  154. [
  155. 'productRepository' => $this->productRepository,
  156. 'linkFactory' => $this->linkFactory,
  157. 'bundleFactory' => $this->bundleFactoryMock,
  158. 'bundleSelection' => $this->bundleSelectionMock,
  159. 'optionCollection' => $this->optionCollectionFactoryMock,
  160. 'storeManager' => $this->storeManagerMock,
  161. 'dataObjectHelper' => $this->dataObjectHelperMock,
  162. ]
  163. );
  164. $refClass = new \ReflectionClass(LinkManagement::class);
  165. $refProperty = $refClass->getProperty('metadataPool');
  166. $refProperty->setAccessible(true);
  167. $refProperty->setValue($this->model, $this->metadataPoolMock);
  168. }
  169. public function testGetChildren()
  170. {
  171. $productSku = 'productSku';
  172. $this->getOptions();
  173. $this->productRepository->expects($this->any())->method('get')->with($this->equalTo($productSku))
  174. ->will($this->returnValue($this->product));
  175. $this->product->expects($this->once())->method('getTypeId')->will($this->returnValue('bundle'));
  176. $this->productType->expects($this->once())->method('setStoreFilter')->with(
  177. $this->equalTo($this->storeId),
  178. $this->product
  179. );
  180. $this->productType->expects($this->once())->method('getSelectionsCollection')
  181. ->with($this->equalTo($this->optionIds), $this->equalTo($this->product))
  182. ->will($this->returnValue($this->selectionCollection));
  183. $this->productType->expects($this->once())->method('getOptionsIds')->with($this->equalTo($this->product))
  184. ->will($this->returnValue($this->optionIds));
  185. $this->optionCollection->expects($this->once())->method('appendSelections')
  186. ->with($this->equalTo($this->selectionCollection))
  187. ->will($this->returnValue([$this->option]));
  188. $this->option->expects($this->any())->method('getSelections')->willReturn([$this->product]);
  189. $this->product->expects($this->any())->method('getData')->willReturn([]);
  190. $this->dataObjectHelperMock->expects($this->once())
  191. ->method('populateWithArray')
  192. ->with($this->link, $this->anything(), \Magento\Bundle\Api\Data\LinkInterface::class)
  193. ->willReturnSelf();
  194. $this->link->expects($this->once())->method('setIsDefault')->willReturnSelf();
  195. $this->link->expects($this->once())->method('setQty')->willReturnSelf();
  196. $this->link->expects($this->once())->method('setCanChangeQuantity')->willReturnSelf();
  197. $this->link->expects($this->once())->method('setPrice')->willReturnSelf();
  198. $this->link->expects($this->once())->method('setPriceType')->willReturnSelf();
  199. $this->link->expects($this->once())->method('setId')->willReturnSelf();
  200. $this->linkFactory->expects($this->once())->method('create')->willReturn($this->link);
  201. $this->assertEquals([$this->link], $this->model->getChildren($productSku));
  202. }
  203. public function testGetChildrenWithOptionId()
  204. {
  205. $productSku = 'productSku';
  206. $this->getOptions();
  207. $this->productRepository->expects($this->any())->method('get')->with($this->equalTo($productSku))
  208. ->will($this->returnValue($this->product));
  209. $this->product->expects($this->once())->method('getTypeId')->will($this->returnValue('bundle'));
  210. $this->productType->expects($this->once())->method('setStoreFilter')->with(
  211. $this->equalTo($this->storeId),
  212. $this->product
  213. );
  214. $this->productType->expects($this->once())->method('getSelectionsCollection')
  215. ->with($this->equalTo($this->optionIds), $this->equalTo($this->product))
  216. ->will($this->returnValue($this->selectionCollection));
  217. $this->productType->expects($this->once())->method('getOptionsIds')->with($this->equalTo($this->product))
  218. ->will($this->returnValue($this->optionIds));
  219. $this->optionCollection->expects($this->once())->method('appendSelections')
  220. ->with($this->equalTo($this->selectionCollection))
  221. ->will($this->returnValue([$this->option]));
  222. $this->option->expects($this->any())->method('getOptionId')->will($this->returnValue(10));
  223. $this->option->expects($this->once())->method('getSelections')->willReturn([1, 2]);
  224. $this->dataObjectHelperMock->expects($this->never())->method('populateWithArray');
  225. $this->assertEquals([], $this->model->getChildren($productSku, 1));
  226. }
  227. /**
  228. * @expectedException \Magento\Framework\Exception\InputException
  229. */
  230. public function testGetChildrenException()
  231. {
  232. $productSku = 'productSku';
  233. $this->productRepository->expects($this->once())->method('get')->with($this->equalTo($productSku))
  234. ->will($this->returnValue($this->product));
  235. $this->product->expects($this->once())->method('getTypeId')->will($this->returnValue('simple'));
  236. $this->assertEquals([$this->link], $this->model->getChildren($productSku));
  237. }
  238. /**
  239. * @expectedException \Magento\Framework\Exception\InputException
  240. */
  241. public function testAddChildToNotBundleProduct()
  242. {
  243. $productLink = $this->createMock(\Magento\Bundle\Api\Data\LinkInterface::class);
  244. $productLink->expects($this->any())->method('getOptionId')->will($this->returnValue(1));
  245. $productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
  246. $productMock->expects($this->once())->method('getTypeId')->will($this->returnValue(
  247. \Magento\Catalog\Model\Product\Type::TYPE_SIMPLE
  248. ));
  249. $this->model->addChild($productMock, 1, $productLink);
  250. }
  251. /**
  252. * @expectedException \Magento\Framework\Exception\InputException
  253. */
  254. public function testAddChildNonExistingOption()
  255. {
  256. $productLink = $this->createMock(\Magento\Bundle\Api\Data\LinkInterface::class);
  257. $productLink->expects($this->any())->method('getOptionId')->will($this->returnValue(1));
  258. $productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
  259. $productMock->expects($this->once())->method('getTypeId')->will($this->returnValue(
  260. \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE
  261. ));
  262. $store = $this->createMock(\Magento\Store\Model\Store::class);
  263. $this->storeManagerMock->expects($this->any())->method('getStore')->will($this->returnValue($store));
  264. $store->expects($this->any())->method('getId')->will($this->returnValue(0));
  265. $emptyOption = $this->getMockBuilder(\Magento\Bundle\Model\Option::class)->disableOriginalConstructor()
  266. ->setMethods(['getId', '__wakeup'])
  267. ->getMock();
  268. $emptyOption->expects($this->once())
  269. ->method('getId')
  270. ->will($this->returnValue(null));
  271. $optionsCollectionMock = $this->createMock(\Magento\Bundle\Model\ResourceModel\Option\Collection::class);
  272. $optionsCollectionMock->expects($this->once())
  273. ->method('setIdFilter')
  274. ->with($this->equalTo(1))
  275. ->will($this->returnSelf());
  276. $optionsCollectionMock->expects($this->once())
  277. ->method('getFirstItem')
  278. ->will($this->returnValue($emptyOption));
  279. $this->optionCollectionFactoryMock->expects($this->any())->method('create')->will(
  280. $this->returnValue($optionsCollectionMock)
  281. );
  282. $this->model->addChild($productMock, 1, $productLink);
  283. }
  284. /**
  285. * @expectedException \Magento\Framework\Exception\InputException
  286. * @expectedExceptionMessage The bundle product can't contain another composite product.
  287. */
  288. public function testAddChildLinkedProductIsComposite()
  289. {
  290. $productLink = $this->createMock(\Magento\Bundle\Api\Data\LinkInterface::class);
  291. $productLink->expects($this->any())->method('getSku')->will($this->returnValue('linked_product_sku'));
  292. $productLink->expects($this->any())->method('getOptionId')->will($this->returnValue(1));
  293. $this->metadataMock->expects($this->once())->method('getLinkField')->willReturn($this->linkField);
  294. $productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
  295. $productMock->expects($this->once())->method('getTypeId')->will($this->returnValue(
  296. \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE
  297. ));
  298. $productMock->expects($this->any())
  299. ->method('getData')
  300. ->with($this->linkField)
  301. ->willReturn($this->linkField);
  302. $linkedProductMock = $this->createMock(\Magento\Catalog\Model\Product::class);
  303. $linkedProductMock->expects($this->any())->method('getId')->will($this->returnValue(13));
  304. $linkedProductMock->expects($this->once())->method('isComposite')->will($this->returnValue(true));
  305. $this->productRepository
  306. ->expects($this->once())
  307. ->method('get')
  308. ->with('linked_product_sku')
  309. ->will($this->returnValue($linkedProductMock));
  310. $store = $this->createMock(\Magento\Store\Model\Store::class);
  311. $this->storeManagerMock->expects($this->any())->method('getStore')->will($this->returnValue($store));
  312. $store->expects($this->any())->method('getId')->will($this->returnValue(0));
  313. $option = $this->getMockBuilder(\Magento\Bundle\Model\Option::class)->disableOriginalConstructor()
  314. ->setMethods(['getId', '__wakeup'])
  315. ->getMock();
  316. $option->expects($this->once())->method('getId')->will($this->returnValue(1));
  317. $optionsCollectionMock = $this->createMock(\Magento\Bundle\Model\ResourceModel\Option\Collection::class);
  318. $optionsCollectionMock->expects($this->once())
  319. ->method('setIdFilter')
  320. ->with($this->equalTo('1'))
  321. ->will($this->returnSelf());
  322. $optionsCollectionMock->expects($this->once())
  323. ->method('getFirstItem')
  324. ->will($this->returnValue($option));
  325. $this->optionCollectionFactoryMock->expects($this->any())->method('create')->will(
  326. $this->returnValue($optionsCollectionMock)
  327. );
  328. $bundle = $this->createMock(\Magento\Bundle\Model\ResourceModel\Bundle::class);
  329. $bundle->expects($this->once())->method('getSelectionsData')->with($this->linkField)->willReturn([]);
  330. $this->bundleFactoryMock->expects($this->once())->method('create')->will($this->returnValue($bundle));
  331. $this->model->addChild($productMock, 1, $productLink);
  332. }
  333. /**
  334. * @expectedException \Magento\Framework\Exception\CouldNotSaveException
  335. */
  336. public function testAddChildProductAlreadyExistsInOption()
  337. {
  338. $productLink = $this->getMockBuilder(\Magento\Bundle\Api\Data\LinkInterface::class)
  339. ->setMethods(['getSku', 'getOptionId', 'getSelectionId'])
  340. ->disableOriginalConstructor()
  341. ->getMockForAbstractClass();
  342. $productLink->expects($this->any())->method('getSku')->will($this->returnValue('linked_product_sku'));
  343. $productLink->expects($this->any())->method('getOptionId')->will($this->returnValue(1));
  344. $productLink->expects($this->any())->method('getSelectionId')->will($this->returnValue(1));
  345. $this->metadataMock->expects($this->once())->method('getLinkField')->willReturn($this->linkField);
  346. $productMock = $this->createPartialMock(
  347. \Magento\Catalog\Model\Product::class,
  348. ['getTypeId', 'getCopyFromView', 'getData', 'getTypeInstance', 'getSku']
  349. );
  350. $productMock->expects($this->once())->method('getTypeId')->willReturn(
  351. \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE
  352. );
  353. $productMock->expects($this->any())
  354. ->method('getData')
  355. ->with($this->linkField)
  356. ->willReturn($this->linkField);
  357. $productMock->expects($this->any())->method('getCopyFromView')->will($this->returnValue(false));
  358. $linkedProductMock = $this->createMock(\Magento\Catalog\Model\Product::class);
  359. $linkedProductMock->expects($this->any())->method('getEntityId')->will($this->returnValue(13));
  360. $linkedProductMock->expects($this->once())->method('isComposite')->will($this->returnValue(false));
  361. $this->productRepository
  362. ->expects($this->once())
  363. ->method('get')
  364. ->with('linked_product_sku')
  365. ->will($this->returnValue($linkedProductMock));
  366. $store = $this->createMock(\Magento\Store\Model\Store::class);
  367. $this->storeManagerMock->expects($this->any())->method('getStore')->will($this->returnValue($store));
  368. $store->expects($this->any())->method('getId')->will($this->returnValue(0));
  369. $option = $this->getMockBuilder(\Magento\Bundle\Model\Option::class)->disableOriginalConstructor()
  370. ->setMethods(['getId', '__wakeup'])
  371. ->getMock();
  372. $option->expects($this->once())->method('getId')->will($this->returnValue(1));
  373. $optionsCollectionMock = $this->createMock(\Magento\Bundle\Model\ResourceModel\Option\Collection::class);
  374. $optionsCollectionMock->expects($this->once())
  375. ->method('setIdFilter')
  376. ->with($this->equalTo(1))
  377. ->will($this->returnSelf());
  378. $optionsCollectionMock->expects($this->once())
  379. ->method('getFirstItem')
  380. ->will($this->returnValue($option));
  381. $this->optionCollectionFactoryMock->expects($this->any())->method('create')->will(
  382. $this->returnValue($optionsCollectionMock)
  383. );
  384. $selections = [
  385. ['option_id' => 1, 'product_id' => 12, 'parent_product_id' => 'product_id'],
  386. ['option_id' => 1, 'product_id' => 13, 'parent_product_id' => 'product_id'],
  387. ];
  388. $bundle = $this->createMock(\Magento\Bundle\Model\ResourceModel\Bundle::class);
  389. $bundle->expects($this->once())->method('getSelectionsData')
  390. ->with($this->linkField)
  391. ->will($this->returnValue($selections));
  392. $this->bundleFactoryMock->expects($this->once())->method('create')->will($this->returnValue($bundle));
  393. $this->model->addChild($productMock, 1, $productLink);
  394. }
  395. /**
  396. * @expectedException \Magento\Framework\Exception\CouldNotSaveException
  397. */
  398. public function testAddChildCouldNotSave()
  399. {
  400. $productLink = $this->getMockBuilder(\Magento\Bundle\Api\Data\LinkInterface::class)
  401. ->setMethods(['getSku', 'getOptionId', 'getSelectionId'])
  402. ->disableOriginalConstructor()
  403. ->getMockForAbstractClass();
  404. $productLink->expects($this->any())->method('getSku')->will($this->returnValue('linked_product_sku'));
  405. $productLink->expects($this->any())->method('getOptionId')->will($this->returnValue(1));
  406. $productLink->expects($this->any())->method('getSelectionId')->will($this->returnValue(1));
  407. $this->metadataMock->expects($this->once())->method('getLinkField')->willReturn($this->linkField);
  408. $productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
  409. $productMock->expects($this->once())->method('getTypeId')->will($this->returnValue(
  410. \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE
  411. ));
  412. $productMock->expects($this->any())
  413. ->method('getData')
  414. ->with($this->linkField)
  415. ->willReturn($this->linkField);
  416. $linkedProductMock = $this->createMock(\Magento\Catalog\Model\Product::class);
  417. $linkedProductMock->expects($this->any())->method('getId')->will($this->returnValue(13));
  418. $linkedProductMock->expects($this->once())->method('isComposite')->will($this->returnValue(false));
  419. $this->productRepository
  420. ->expects($this->once())
  421. ->method('get')
  422. ->with('linked_product_sku')
  423. ->will($this->returnValue($linkedProductMock));
  424. $store = $this->createMock(\Magento\Store\Model\Store::class);
  425. $this->storeManagerMock->expects($this->any())->method('getStore')->will($this->returnValue($store));
  426. $store->expects($this->any())->method('getId')->will($this->returnValue(0));
  427. $option = $this->getMockBuilder(\Magento\Bundle\Model\Option::class)->disableOriginalConstructor()
  428. ->setMethods(['getId', '__wakeup'])
  429. ->getMock();
  430. $option->expects($this->once())->method('getId')->will($this->returnValue(1));
  431. $optionsCollectionMock = $this->createMock(\Magento\Bundle\Model\ResourceModel\Option\Collection::class);
  432. $optionsCollectionMock->expects($this->once())
  433. ->method('setIdFilter')
  434. ->with($this->equalTo(1))
  435. ->will($this->returnSelf());
  436. $optionsCollectionMock->expects($this->once())
  437. ->method('getFirstItem')
  438. ->will($this->returnValue($option));
  439. $this->optionCollectionFactoryMock->expects($this->any())->method('create')->will(
  440. $this->returnValue($optionsCollectionMock)
  441. );
  442. $selections = [
  443. ['option_id' => 1, 'product_id' => 11],
  444. ['option_id' => 1, 'product_id' => 12],
  445. ];
  446. $bundle = $this->createMock(\Magento\Bundle\Model\ResourceModel\Bundle::class);
  447. $bundle->expects($this->once())->method('getSelectionsData')
  448. ->with($this->linkField)
  449. ->will($this->returnValue($selections));
  450. $this->bundleFactoryMock->expects($this->once())->method('create')->will($this->returnValue($bundle));
  451. $selection = $this->createPartialMock(\Magento\Bundle\Model\Selection::class, ['save']);
  452. $selection->expects($this->once())->method('save')
  453. ->will(
  454. $this->returnCallback(
  455. function () {
  456. throw new \Exception('message');
  457. }
  458. )
  459. );
  460. $this->bundleSelectionMock->expects($this->once())->method('create')->will($this->returnValue($selection));
  461. $this->model->addChild($productMock, 1, $productLink);
  462. }
  463. public function testAddChild()
  464. {
  465. $productLink = $this->getMockBuilder(\Magento\Bundle\Api\Data\LinkInterface::class)
  466. ->setMethods(['getSku', 'getOptionId', 'getSelectionId'])
  467. ->disableOriginalConstructor()
  468. ->getMockForAbstractClass();
  469. $productLink->expects($this->any())->method('getSku')->will($this->returnValue('linked_product_sku'));
  470. $productLink->expects($this->any())->method('getOptionId')->will($this->returnValue(1));
  471. $productLink->expects($this->any())->method('getSelectionId')->will($this->returnValue(1));
  472. $this->metadataMock->expects($this->once())->method('getLinkField')->willReturn($this->linkField);
  473. $productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
  474. $productMock->expects($this->once())->method('getTypeId')->will($this->returnValue(
  475. \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE
  476. ));
  477. $productMock->expects($this->any())
  478. ->method('getData')
  479. ->with($this->linkField)
  480. ->willReturn($this->linkField);
  481. $linkedProductMock = $this->createMock(\Magento\Catalog\Model\Product::class);
  482. $linkedProductMock->expects($this->any())->method('getId')->will($this->returnValue(13));
  483. $linkedProductMock->expects($this->once())->method('isComposite')->will($this->returnValue(false));
  484. $this->productRepository
  485. ->expects($this->once())
  486. ->method('get')
  487. ->with('linked_product_sku')
  488. ->will($this->returnValue($linkedProductMock));
  489. $store = $this->createMock(\Magento\Store\Model\Store::class);
  490. $this->storeManagerMock->expects($this->any())->method('getStore')->will($this->returnValue($store));
  491. $store->expects($this->any())->method('getId')->will($this->returnValue(0));
  492. $option = $this->getMockBuilder(\Magento\Bundle\Model\Option::class)->disableOriginalConstructor()
  493. ->setMethods(['getId', '__wakeup'])
  494. ->getMock();
  495. $option->expects($this->once())->method('getId')->will($this->returnValue(1));
  496. $optionsCollectionMock = $this->createMock(\Magento\Bundle\Model\ResourceModel\Option\Collection::class);
  497. $optionsCollectionMock->expects($this->once())
  498. ->method('setIdFilter')
  499. ->with($this->equalTo(1))
  500. ->will($this->returnSelf());
  501. $optionsCollectionMock->expects($this->once())
  502. ->method('getFirstItem')
  503. ->will($this->returnValue($option));
  504. $this->optionCollectionFactoryMock->expects($this->any())->method('create')->will(
  505. $this->returnValue($optionsCollectionMock)
  506. );
  507. $selections = [
  508. ['option_id' => 1, 'product_id' => 11],
  509. ['option_id' => 1, 'product_id' => 12],
  510. ];
  511. $bundle = $this->createMock(\Magento\Bundle\Model\ResourceModel\Bundle::class);
  512. $bundle->expects($this->once())->method('getSelectionsData')
  513. ->with($this->linkField)
  514. ->will($this->returnValue($selections));
  515. $this->bundleFactoryMock->expects($this->once())->method('create')->will($this->returnValue($bundle));
  516. $selection = $this->createPartialMock(\Magento\Bundle\Model\Selection::class, ['save', 'getId']);
  517. $selection->expects($this->once())->method('save');
  518. $selection->expects($this->once())->method('getId')->will($this->returnValue(42));
  519. $this->bundleSelectionMock->expects($this->once())->method('create')->will($this->returnValue($selection));
  520. $result = $this->model->addChild($productMock, 1, $productLink);
  521. $this->assertEquals(42, $result);
  522. }
  523. public function testSaveChild()
  524. {
  525. $id = 12;
  526. $optionId = 1;
  527. $position = 3;
  528. $qty = 2;
  529. $priceType = 1;
  530. $price = 10.5;
  531. $canChangeQuantity = true;
  532. $isDefault = true;
  533. $linkProductId = 45;
  534. $parentProductId = 32;
  535. $bundleProductSku = 'bundleProductSku';
  536. $productLink = $this->getMockBuilder(\Magento\Bundle\Api\Data\LinkInterface::class)
  537. ->setMethods(['getSku', 'getOptionId', 'getSelectionId'])
  538. ->disableOriginalConstructor()
  539. ->getMockForAbstractClass();
  540. $productLink->expects($this->any())->method('getSku')->will($this->returnValue('linked_product_sku'));
  541. $productLink->expects($this->any())->method('getId')->will($this->returnValue($id));
  542. $productLink->expects($this->any())->method('getOptionId')->will($this->returnValue($optionId));
  543. $productLink->expects($this->any())->method('getPosition')->will($this->returnValue($position));
  544. $productLink->expects($this->any())->method('getQty')->will($this->returnValue($qty));
  545. $productLink->expects($this->any())->method('getPriceType')->will($this->returnValue($priceType));
  546. $productLink->expects($this->any())->method('getPrice')->will($this->returnValue($price));
  547. $productLink->expects($this->any())->method('getCanChangeQuantity')
  548. ->will($this->returnValue($canChangeQuantity));
  549. $productLink->expects($this->any())->method('getIsDefault')->will($this->returnValue($isDefault));
  550. $productLink->expects($this->any())->method('getSelectionId')->will($this->returnValue($optionId));
  551. $this->metadataMock->expects($this->once())->method('getLinkField')->willReturn($this->linkField);
  552. $productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
  553. $productMock->expects($this->once())->method('getTypeId')->will($this->returnValue(
  554. \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE
  555. ));
  556. $productMock->expects($this->any())
  557. ->method('getData')
  558. ->with($this->linkField)
  559. ->willReturn($parentProductId);
  560. $linkedProductMock = $this->createMock(\Magento\Catalog\Model\Product::class);
  561. $linkedProductMock->expects($this->any())->method('getId')->will($this->returnValue($linkProductId));
  562. $linkedProductMock->expects($this->once())->method('isComposite')->will($this->returnValue(false));
  563. $this->productRepository
  564. ->expects($this->at(0))
  565. ->method('get')
  566. ->with($bundleProductSku)
  567. ->will($this->returnValue($productMock));
  568. $this->productRepository
  569. ->expects($this->at(1))
  570. ->method('get')
  571. ->with('linked_product_sku')
  572. ->will($this->returnValue($linkedProductMock));
  573. $store = $this->createMock(\Magento\Store\Model\Store::class);
  574. $this->storeManagerMock->expects($this->any())->method('getStore')->will($this->returnValue($store));
  575. $store->expects($this->any())->method('getId')->will($this->returnValue(0));
  576. $selection = $this->createPartialMock(\Magento\Bundle\Model\Selection::class, [
  577. 'save',
  578. 'getId',
  579. 'load',
  580. 'setProductId',
  581. 'setParentProductId',
  582. 'setOptionId',
  583. 'setPosition',
  584. 'setSelectionQty',
  585. 'setSelectionPriceType',
  586. 'setSelectionPriceValue',
  587. 'setSelectionCanChangeQty',
  588. 'setIsDefault'
  589. ]);
  590. $selection->expects($this->once())->method('save');
  591. $selection->expects($this->once())->method('load')->with($id)->will($this->returnSelf());
  592. $selection->expects($this->any())->method('getId')->will($this->returnValue($id));
  593. $selection->expects($this->once())->method('setProductId')->with($linkProductId);
  594. $selection->expects($this->once())->method('setParentProductId')->with($parentProductId);
  595. $selection->expects($this->once())->method('setOptionId')->with($optionId);
  596. $selection->expects($this->once())->method('setPosition')->with($position);
  597. $selection->expects($this->once())->method('setSelectionQty')->with($qty);
  598. $selection->expects($this->once())->method('setSelectionPriceType')->with($priceType);
  599. $selection->expects($this->once())->method('setSelectionPriceValue')->with($price);
  600. $selection->expects($this->once())->method('setSelectionCanChangeQty')->with($canChangeQuantity);
  601. $selection->expects($this->once())->method('setIsDefault')->with($isDefault);
  602. $this->bundleSelectionMock->expects($this->once())->method('create')->will($this->returnValue($selection));
  603. $this->assertTrue($this->model->saveChild($bundleProductSku, $productLink));
  604. }
  605. /**
  606. * @expectedException \Magento\Framework\Exception\CouldNotSaveException
  607. */
  608. public function testSaveChildFailedToSave()
  609. {
  610. $id = 12;
  611. $linkProductId = 45;
  612. $parentProductId = 32;
  613. $productLink = $this->getMockBuilder(\Magento\Bundle\Api\Data\LinkInterface::class)
  614. ->setMethods(['getSku', 'getOptionId', 'getSelectionId'])
  615. ->disableOriginalConstructor()
  616. ->getMockForAbstractClass();
  617. $productLink->expects($this->any())->method('getSku')->will($this->returnValue('linked_product_sku'));
  618. $productLink->expects($this->any())->method('getId')->will($this->returnValue($id));
  619. $productLink->expects($this->any())->method('getSelectionId')->will($this->returnValue(1));
  620. $bundleProductSku = 'bundleProductSku';
  621. $productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
  622. $productMock->expects($this->once())->method('getTypeId')->will($this->returnValue(
  623. \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE
  624. ));
  625. $productMock->expects($this->any())->method('getId')->will($this->returnValue($parentProductId));
  626. $linkedProductMock = $this->createMock(\Magento\Catalog\Model\Product::class);
  627. $linkedProductMock->expects($this->any())->method('getId')->will($this->returnValue($linkProductId));
  628. $linkedProductMock->expects($this->once())->method('isComposite')->will($this->returnValue(false));
  629. $this->productRepository
  630. ->expects($this->at(0))
  631. ->method('get')
  632. ->with($bundleProductSku)
  633. ->will($this->returnValue($productMock));
  634. $this->productRepository
  635. ->expects($this->at(1))
  636. ->method('get')
  637. ->with('linked_product_sku')
  638. ->will($this->returnValue($linkedProductMock));
  639. $store = $this->createMock(\Magento\Store\Model\Store::class);
  640. $this->storeManagerMock->expects($this->any())->method('getStore')->will($this->returnValue($store));
  641. $store->expects($this->any())->method('getId')->will($this->returnValue(0));
  642. $selection = $this->createPartialMock(\Magento\Bundle\Model\Selection::class, [
  643. 'save',
  644. 'getId',
  645. 'load',
  646. 'setProductId',
  647. 'setParentProductId',
  648. 'setSelectionId',
  649. 'setOptionId',
  650. 'setPosition',
  651. 'setSelectionQty',
  652. 'setSelectionPriceType',
  653. 'setSelectionPriceValue',
  654. 'setSelectionCanChangeQty',
  655. 'setIsDefault'
  656. ]);
  657. $mockException = $this->createMock(\Exception::class);
  658. $selection->expects($this->once())->method('save')->will($this->throwException($mockException));
  659. $selection->expects($this->once())->method('load')->with($id)->will($this->returnSelf());
  660. $selection->expects($this->any())->method('getId')->will($this->returnValue($id));
  661. $selection->expects($this->once())->method('setProductId')->with($linkProductId);
  662. $this->bundleSelectionMock->expects($this->once())->method('create')->will($this->returnValue($selection));
  663. $this->model->saveChild($bundleProductSku, $productLink);
  664. }
  665. /**
  666. * @expectedException \Magento\Framework\Exception\InputException
  667. */
  668. public function testSaveChildWithoutId()
  669. {
  670. $bundleProductSku = "bundleSku";
  671. $linkedProductSku = 'simple';
  672. $productLink = $this->createMock(\Magento\Bundle\Api\Data\LinkInterface::class);
  673. $productLink->expects($this->any())->method('getId')->will($this->returnValue(null));
  674. $productLink->expects($this->any())->method('getSku')->will($this->returnValue($linkedProductSku));
  675. $productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
  676. $productMock->expects($this->once())->method('getTypeId')->will($this->returnValue(
  677. \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE
  678. ));
  679. $linkedProductMock = $this->createMock(\Magento\Catalog\Model\Product::class);
  680. $linkedProductMock->expects($this->once())->method('isComposite')->will($this->returnValue(false));
  681. $this->productRepository
  682. ->expects($this->at(0))
  683. ->method('get')
  684. ->with($bundleProductSku)
  685. ->will($this->returnValue($productMock));
  686. $this->productRepository
  687. ->expects($this->at(1))
  688. ->method('get')
  689. ->with($linkedProductSku)
  690. ->will($this->returnValue($linkedProductMock));
  691. $this->model->saveChild($bundleProductSku, $productLink);
  692. }
  693. /**
  694. * @expectedException \Magento\Framework\Exception\InputException
  695. * @expectedExceptionMessage The product link with the "12345" ID field wasn't found. Verify the ID and try again.
  696. */
  697. public function testSaveChildWithInvalidId()
  698. {
  699. $id = 12345;
  700. $linkedProductSku = 'simple';
  701. $bundleProductSku = "bundleProductSku";
  702. $productLink = $this->createMock(\Magento\Bundle\Api\Data\LinkInterface::class);
  703. $productLink->expects($this->any())->method('getId')->will($this->returnValue($id));
  704. $productLink->expects($this->any())->method('getSku')->will($this->returnValue($linkedProductSku));
  705. $productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
  706. $productMock->expects($this->once())->method('getTypeId')->will($this->returnValue(
  707. \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE
  708. ));
  709. $linkedProductMock = $this->createMock(\Magento\Catalog\Model\Product::class);
  710. $linkedProductMock->expects($this->once())->method('isComposite')->will($this->returnValue(false));
  711. $this->productRepository
  712. ->expects($this->at(0))
  713. ->method('get')
  714. ->with($bundleProductSku)
  715. ->will($this->returnValue($productMock));
  716. $this->productRepository
  717. ->expects($this->at(1))
  718. ->method('get')
  719. ->with($linkedProductSku)
  720. ->will($this->returnValue($linkedProductMock));
  721. $selection = $this->createPartialMock(\Magento\Bundle\Model\Selection::class, [
  722. 'getId',
  723. 'load',
  724. ]);
  725. $selection->expects($this->once())->method('load')->with($id)->will($this->returnSelf());
  726. $selection->expects($this->any())->method('getId')->will($this->returnValue(null));
  727. $this->bundleSelectionMock->expects($this->once())->method('create')->will($this->returnValue($selection));
  728. $this->model->saveChild($bundleProductSku, $productLink);
  729. }
  730. /**
  731. * @expectedException \Magento\Framework\Exception\InputException
  732. */
  733. public function testSaveChildWithCompositeProductLink()
  734. {
  735. $bundleProductSku = "bundleProductSku";
  736. $id = 12;
  737. $linkedProductSku = 'simple';
  738. $productLink = $this->createMock(\Magento\Bundle\Api\Data\LinkInterface::class);
  739. $productLink->expects($this->any())->method('getId')->will($this->returnValue($id));
  740. $productLink->expects($this->any())->method('getSku')->will($this->returnValue($linkedProductSku));
  741. $productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
  742. $productMock->expects($this->once())->method('getTypeId')->will($this->returnValue(
  743. \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE
  744. ));
  745. $linkedProductMock = $this->createMock(\Magento\Catalog\Model\Product::class);
  746. $linkedProductMock->expects($this->once())->method('isComposite')->will($this->returnValue(true));
  747. $this->productRepository
  748. ->expects($this->at(0))
  749. ->method('get')
  750. ->with($bundleProductSku)
  751. ->will($this->returnValue($productMock));
  752. $this->productRepository
  753. ->expects($this->at(1))
  754. ->method('get')
  755. ->with($linkedProductSku)
  756. ->will($this->returnValue($linkedProductMock));
  757. $this->model->saveChild($bundleProductSku, $productLink);
  758. }
  759. /**
  760. * @expectedException \Magento\Framework\Exception\InputException
  761. */
  762. public function testSaveChildWithSimpleProduct()
  763. {
  764. $id = 12;
  765. $linkedProductSku = 'simple';
  766. $bundleProductSku = "bundleProductSku";
  767. $productLink = $this->createMock(\Magento\Bundle\Api\Data\LinkInterface::class);
  768. $productLink->expects($this->any())->method('getId')->will($this->returnValue($id));
  769. $productLink->expects($this->any())->method('getSku')->will($this->returnValue($linkedProductSku));
  770. $productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
  771. $productMock->expects($this->once())->method('getTypeId')->will($this->returnValue(
  772. \Magento\Catalog\Model\Product\Type::TYPE_SIMPLE
  773. ));
  774. $this->productRepository->expects($this->once())->method('get')->with($bundleProductSku)
  775. ->willReturn($productMock);
  776. $this->model->saveChild($bundleProductSku, $productLink);
  777. }
  778. public function testRemoveChild()
  779. {
  780. $this->productRepository->expects($this->any())->method('get')->will($this->returnValue($this->product));
  781. $bundle = $this->createMock(\Magento\Bundle\Model\ResourceModel\Bundle::class);
  782. $this->bundleFactoryMock->expects($this->once())->method('create')->will($this->returnValue($bundle));
  783. $productSku = 'productSku';
  784. $optionId = 1;
  785. $productId = 1;
  786. $childSku = 'childSku';
  787. $this->product
  788. ->expects($this->any())
  789. ->method('getTypeId')
  790. ->will($this->returnValue(\Magento\Catalog\Model\Product\Type::TYPE_BUNDLE));
  791. $this->getRemoveOptions();
  792. $selection = $this->getMockBuilder(\Magento\Bundle\Model\Selection::class)
  793. ->setMethods(['getSku', 'getOptionId', 'getSelectionId', 'getProductId', '__wakeup'])
  794. ->disableOriginalConstructor()
  795. ->getMock();
  796. $selection->expects($this->any())->method('getSku')->will($this->returnValue($childSku));
  797. $selection->expects($this->any())->method('getOptionId')->will($this->returnValue($optionId));
  798. $selection->expects($this->any())->method('getSelectionId')->will($this->returnValue(55));
  799. $selection->expects($this->any())->method('getProductId')->willReturn($productId);
  800. $this->option->expects($this->any())->method('getSelections')->will($this->returnValue([$selection]));
  801. $this->metadataMock->expects($this->any())->method('getLinkField')->willReturn($this->linkField);
  802. $this->product->expects($this->any())
  803. ->method('getData')
  804. ->with($this->linkField)
  805. ->willReturn(3);
  806. $bundle->expects($this->once())->method('dropAllUnneededSelections')->with(3, []);
  807. $bundle->expects($this->once())->method('removeProductRelations')->with(3, [$productId]);
  808. //Params come in lowercase to method
  809. $this->assertTrue($this->model->removeChild($productSku, $optionId, $childSku));
  810. }
  811. /**
  812. * @expectedException \Magento\Framework\Exception\InputException
  813. */
  814. public function testRemoveChildForbidden()
  815. {
  816. $this->productRepository->expects($this->any())->method('get')->will($this->returnValue($this->product));
  817. $productSku = 'productSku';
  818. $optionId = 1;
  819. $childSku = 'childSku';
  820. $this->product
  821. ->expects($this->any())
  822. ->method('getTypeId')
  823. ->will($this->returnValue(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE));
  824. $this->model->removeChild($productSku, $optionId, $childSku);
  825. }
  826. /**
  827. * @expectedException \Magento\Framework\Exception\NoSuchEntityException
  828. */
  829. public function testRemoveChildInvalidOptionId()
  830. {
  831. $this->productRepository->expects($this->any())->method('get')->will($this->returnValue($this->product));
  832. $productSku = 'productSku';
  833. $optionId = 1;
  834. $childSku = 'childSku';
  835. $this->product
  836. ->expects($this->any())
  837. ->method('getTypeId')
  838. ->will($this->returnValue(\Magento\Catalog\Model\Product\Type::TYPE_BUNDLE));
  839. $this->getRemoveOptions();
  840. $selection = $this->getMockBuilder(\Magento\Bundle\Model\Selection::class)
  841. ->setMethods(['getSku', 'getOptionId', 'getSelectionId', 'getProductId', '__wakeup'])
  842. ->disableOriginalConstructor()
  843. ->getMock();
  844. $selection->expects($this->any())->method('getSku')->will($this->returnValue($childSku));
  845. $selection->expects($this->any())->method('getOptionId')->will($this->returnValue($optionId + 1));
  846. $selection->expects($this->any())->method('getSelectionId')->will($this->returnValue(55));
  847. $selection->expects($this->any())->method('getProductId')->will($this->returnValue(1));
  848. $this->option->expects($this->any())->method('getSelections')->will($this->returnValue([$selection]));
  849. $this->model->removeChild($productSku, $optionId, $childSku);
  850. }
  851. /**
  852. * @expectedException \Magento\Framework\Exception\NoSuchEntityException
  853. */
  854. public function testRemoveChildInvalidChildSku()
  855. {
  856. $this->productRepository->expects($this->any())->method('get')->will($this->returnValue($this->product));
  857. $productSku = 'productSku';
  858. $optionId = 1;
  859. $childSku = 'childSku';
  860. $this->product
  861. ->expects($this->any())
  862. ->method('getTypeId')
  863. ->will($this->returnValue(\Magento\Catalog\Model\Product\Type::TYPE_BUNDLE));
  864. $this->getRemoveOptions();
  865. $selection = $this->getMockBuilder(\Magento\Bundle\Model\Selection::class)
  866. ->setMethods(['getSku', 'getOptionId', 'getSelectionId', 'getProductId', '__wakeup'])
  867. ->disableOriginalConstructor()
  868. ->getMock();
  869. $selection->expects($this->any())->method('getSku')->will($this->returnValue($childSku . '_invalid'));
  870. $selection->expects($this->any())->method('getOptionId')->will($this->returnValue($optionId));
  871. $selection->expects($this->any())->method('getSelectionId')->will($this->returnValue(55));
  872. $selection->expects($this->any())->method('getProductId')->will($this->returnValue(1));
  873. $this->option->expects($this->any())->method('getSelections')->will($this->returnValue([$selection]));
  874. $this->model->removeChild($productSku, $optionId, $childSku);
  875. }
  876. private function getOptions()
  877. {
  878. $this->product->expects($this->any())->method('getTypeInstance')->will($this->returnValue($this->productType));
  879. $this->product->expects($this->once())->method('getStoreId')->will($this->returnValue($this->storeId));
  880. $this->productType->expects($this->once())->method('setStoreFilter')
  881. ->with($this->equalTo($this->storeId), $this->equalTo($this->product));
  882. $this->productType->expects($this->once())->method('getOptionsCollection')
  883. ->with($this->equalTo($this->product))
  884. ->will($this->returnValue($this->optionCollection));
  885. }
  886. public function getRemoveOptions()
  887. {
  888. $this->product->expects($this->any())->method('getTypeInstance')->will($this->returnValue($this->productType));
  889. $this->product->expects($this->once())->method('getStoreId')->will($this->returnValue(1));
  890. $this->productType->expects($this->once())->method('setStoreFilter');
  891. $this->productType->expects($this->once())->method('getOptionsCollection')
  892. ->with($this->equalTo($this->product))
  893. ->will($this->returnValue($this->optionCollection));
  894. $this->productType->expects($this->once())->method('getOptionsIds')->with($this->equalTo($this->product))
  895. ->will($this->returnValue([1, 2, 3]));
  896. $this->productType->expects($this->once())->method('getSelectionsCollection')
  897. ->will($this->returnValue([]));
  898. $this->optionCollection->expects($this->any())->method('appendSelections')
  899. ->with($this->equalTo([]), true)
  900. ->will($this->returnValue([$this->option]));
  901. }
  902. }