1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045 |
- <?php
- /**
- *
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Bundle\Test\Unit\Model;
- use Magento\Bundle\Model\LinkManagement;
- use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
- /**
- * Class LinkManagementTest
- *
- * @SuppressWarnings(PHPMD.TooManyFields)
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class LinkManagementTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var \Magento\Bundle\Model\LinkManagement
- */
- protected $model;
- /**
- * @var \Magento\Catalog\Model\ProductRepository|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $productRepository;
- /**
- * @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $product;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $linkFactory;
- /**
- * @var \Magento\Catalog\Model\Product\Type\Interceptor|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $productType;
- /**
- * @var \Magento\Bundle\Model\ResourceModel\Option\Collection|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $optionCollection;
- /**
- * @var \Magento\Bundle\Model\ResourceModel\Selection\Collection|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $selectionCollection;
- /**
- * @var \Magento\Bundle\Model\Option|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $option;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Bundle\Model\SelectionFactory
- */
- protected $bundleSelectionMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Bundle\Model\ResourceModel\BundleFactory
- */
- protected $bundleFactoryMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Bundle\Model\ResourceModel\Option\CollectionFactory
- */
- protected $optionCollectionFactoryMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Store\Model\StoreManagerInterface
- */
- protected $storeManagerMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $link;
- /**
- * @var int
- */
- protected $storeId = 2;
- /**
- * @var array
- */
- protected $optionIds = [1, 2, 3];
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $dataObjectHelperMock;
- /**
- * @var \Magento\Framework\EntityManager\MetadataPool|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $metadataPoolMock;
- /**
- * @var \Magento\Framework\EntityManager\EntityMetadata|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $metadataMock;
- /**
- * @var string
- */
- protected $linkField = 'product_id';
- protected function setUp()
- {
- $helper = new ObjectManager($this);
- $this->productRepository = $this->getMockBuilder(\Magento\Catalog\Model\ProductRepository::class)
- ->setMethods(['get'])
- ->disableOriginalConstructor()
- ->getMock();
- $this->productType = $this->getMockBuilder(\Magento\Bundle\Model\Product\Type\Interceptor::class)
- ->setMethods(['getOptionsCollection', 'setStoreFilter', 'getSelectionsCollection', 'getOptionsIds'])
- ->disableOriginalConstructor()
- ->getMock();
- $this->option = $this->getMockBuilder(\Magento\Bundle\Model\Option::class)
- ->setMethods(['getSelections', 'getOptionId', '__wakeup'])
- ->disableOriginalConstructor()
- ->getMock();
- $this->optionCollection = $this->getMockBuilder(\Magento\Bundle\Model\ResourceModel\Option\Collection::class)
- ->setMethods(['appendSelections'])
- ->disableOriginalConstructor()
- ->getMock();
- $this->selectionCollection = $this->getMockBuilder(
- \Magento\Bundle\Model\ResourceModel\Selection\Collection::class
- )->disableOriginalConstructor()->getMock();
- $this->product = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
- ->setMethods(['getTypeInstance', 'getStoreId', 'getTypeId', '__wakeup', 'getId', 'getData'])
- ->disableOriginalConstructor()
- ->getMock();
- $this->link = $this->getMockBuilder(\Magento\Bundle\Api\Data\LinkInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->linkFactory = $this->getMockBuilder(\Magento\Bundle\Api\Data\LinkInterfaceFactory::class)
- ->setMethods(['create'])
- ->disableOriginalConstructor()
- ->getMock();
- $this->bundleSelectionMock = $this->createPartialMock(
- \Magento\Bundle\Model\SelectionFactory::class,
- ['create']
- );
- $this->bundleFactoryMock = $this->createPartialMock(
- \Magento\Bundle\Model\ResourceModel\BundleFactory::class,
- ['create']
- );
- $this->optionCollectionFactoryMock = $this->createPartialMock(
- \Magento\Bundle\Model\ResourceModel\Option\CollectionFactory::class,
- ['create']
- );
- $this->storeManagerMock = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
- $this->metadataPoolMock = $this->getMockBuilder(\Magento\Framework\EntityManager\MetadataPool::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->metadataMock = $this->getMockBuilder(\Magento\Framework\EntityManager\EntityMetadata::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->metadataPoolMock->expects($this->any())->method('getMetadata')
- ->with(\Magento\Catalog\Api\Data\ProductInterface::class)
- ->willReturn($this->metadataMock);
- $this->dataObjectHelperMock = $this->getMockBuilder(\Magento\Framework\Api\DataObjectHelper::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->model = $helper->getObject(
- LinkManagement::class,
- [
- 'productRepository' => $this->productRepository,
- 'linkFactory' => $this->linkFactory,
- 'bundleFactory' => $this->bundleFactoryMock,
- 'bundleSelection' => $this->bundleSelectionMock,
- 'optionCollection' => $this->optionCollectionFactoryMock,
- 'storeManager' => $this->storeManagerMock,
- 'dataObjectHelper' => $this->dataObjectHelperMock,
- ]
- );
- $refClass = new \ReflectionClass(LinkManagement::class);
- $refProperty = $refClass->getProperty('metadataPool');
- $refProperty->setAccessible(true);
- $refProperty->setValue($this->model, $this->metadataPoolMock);
- }
- public function testGetChildren()
- {
- $productSku = 'productSku';
- $this->getOptions();
- $this->productRepository->expects($this->any())->method('get')->with($this->equalTo($productSku))
- ->will($this->returnValue($this->product));
- $this->product->expects($this->once())->method('getTypeId')->will($this->returnValue('bundle'));
- $this->productType->expects($this->once())->method('setStoreFilter')->with(
- $this->equalTo($this->storeId),
- $this->product
- );
- $this->productType->expects($this->once())->method('getSelectionsCollection')
- ->with($this->equalTo($this->optionIds), $this->equalTo($this->product))
- ->will($this->returnValue($this->selectionCollection));
- $this->productType->expects($this->once())->method('getOptionsIds')->with($this->equalTo($this->product))
- ->will($this->returnValue($this->optionIds));
- $this->optionCollection->expects($this->once())->method('appendSelections')
- ->with($this->equalTo($this->selectionCollection))
- ->will($this->returnValue([$this->option]));
- $this->option->expects($this->any())->method('getSelections')->willReturn([$this->product]);
- $this->product->expects($this->any())->method('getData')->willReturn([]);
- $this->dataObjectHelperMock->expects($this->once())
- ->method('populateWithArray')
- ->with($this->link, $this->anything(), \Magento\Bundle\Api\Data\LinkInterface::class)
- ->willReturnSelf();
- $this->link->expects($this->once())->method('setIsDefault')->willReturnSelf();
- $this->link->expects($this->once())->method('setQty')->willReturnSelf();
- $this->link->expects($this->once())->method('setCanChangeQuantity')->willReturnSelf();
- $this->link->expects($this->once())->method('setPrice')->willReturnSelf();
- $this->link->expects($this->once())->method('setPriceType')->willReturnSelf();
- $this->link->expects($this->once())->method('setId')->willReturnSelf();
- $this->linkFactory->expects($this->once())->method('create')->willReturn($this->link);
- $this->assertEquals([$this->link], $this->model->getChildren($productSku));
- }
- public function testGetChildrenWithOptionId()
- {
- $productSku = 'productSku';
- $this->getOptions();
- $this->productRepository->expects($this->any())->method('get')->with($this->equalTo($productSku))
- ->will($this->returnValue($this->product));
- $this->product->expects($this->once())->method('getTypeId')->will($this->returnValue('bundle'));
- $this->productType->expects($this->once())->method('setStoreFilter')->with(
- $this->equalTo($this->storeId),
- $this->product
- );
- $this->productType->expects($this->once())->method('getSelectionsCollection')
- ->with($this->equalTo($this->optionIds), $this->equalTo($this->product))
- ->will($this->returnValue($this->selectionCollection));
- $this->productType->expects($this->once())->method('getOptionsIds')->with($this->equalTo($this->product))
- ->will($this->returnValue($this->optionIds));
- $this->optionCollection->expects($this->once())->method('appendSelections')
- ->with($this->equalTo($this->selectionCollection))
- ->will($this->returnValue([$this->option]));
- $this->option->expects($this->any())->method('getOptionId')->will($this->returnValue(10));
- $this->option->expects($this->once())->method('getSelections')->willReturn([1, 2]);
- $this->dataObjectHelperMock->expects($this->never())->method('populateWithArray');
- $this->assertEquals([], $this->model->getChildren($productSku, 1));
- }
- /**
- * @expectedException \Magento\Framework\Exception\InputException
- */
- public function testGetChildrenException()
- {
- $productSku = 'productSku';
- $this->productRepository->expects($this->once())->method('get')->with($this->equalTo($productSku))
- ->will($this->returnValue($this->product));
- $this->product->expects($this->once())->method('getTypeId')->will($this->returnValue('simple'));
- $this->assertEquals([$this->link], $this->model->getChildren($productSku));
- }
- /**
- * @expectedException \Magento\Framework\Exception\InputException
- */
- public function testAddChildToNotBundleProduct()
- {
- $productLink = $this->createMock(\Magento\Bundle\Api\Data\LinkInterface::class);
- $productLink->expects($this->any())->method('getOptionId')->will($this->returnValue(1));
- $productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
- $productMock->expects($this->once())->method('getTypeId')->will($this->returnValue(
- \Magento\Catalog\Model\Product\Type::TYPE_SIMPLE
- ));
- $this->model->addChild($productMock, 1, $productLink);
- }
- /**
- * @expectedException \Magento\Framework\Exception\InputException
- */
- public function testAddChildNonExistingOption()
- {
- $productLink = $this->createMock(\Magento\Bundle\Api\Data\LinkInterface::class);
- $productLink->expects($this->any())->method('getOptionId')->will($this->returnValue(1));
- $productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
- $productMock->expects($this->once())->method('getTypeId')->will($this->returnValue(
- \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE
- ));
- $store = $this->createMock(\Magento\Store\Model\Store::class);
- $this->storeManagerMock->expects($this->any())->method('getStore')->will($this->returnValue($store));
- $store->expects($this->any())->method('getId')->will($this->returnValue(0));
- $emptyOption = $this->getMockBuilder(\Magento\Bundle\Model\Option::class)->disableOriginalConstructor()
- ->setMethods(['getId', '__wakeup'])
- ->getMock();
- $emptyOption->expects($this->once())
- ->method('getId')
- ->will($this->returnValue(null));
- $optionsCollectionMock = $this->createMock(\Magento\Bundle\Model\ResourceModel\Option\Collection::class);
- $optionsCollectionMock->expects($this->once())
- ->method('setIdFilter')
- ->with($this->equalTo(1))
- ->will($this->returnSelf());
- $optionsCollectionMock->expects($this->once())
- ->method('getFirstItem')
- ->will($this->returnValue($emptyOption));
- $this->optionCollectionFactoryMock->expects($this->any())->method('create')->will(
- $this->returnValue($optionsCollectionMock)
- );
- $this->model->addChild($productMock, 1, $productLink);
- }
- /**
- * @expectedException \Magento\Framework\Exception\InputException
- * @expectedExceptionMessage The bundle product can't contain another composite product.
- */
- public function testAddChildLinkedProductIsComposite()
- {
- $productLink = $this->createMock(\Magento\Bundle\Api\Data\LinkInterface::class);
- $productLink->expects($this->any())->method('getSku')->will($this->returnValue('linked_product_sku'));
- $productLink->expects($this->any())->method('getOptionId')->will($this->returnValue(1));
- $this->metadataMock->expects($this->once())->method('getLinkField')->willReturn($this->linkField);
- $productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
- $productMock->expects($this->once())->method('getTypeId')->will($this->returnValue(
- \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE
- ));
- $productMock->expects($this->any())
- ->method('getData')
- ->with($this->linkField)
- ->willReturn($this->linkField);
- $linkedProductMock = $this->createMock(\Magento\Catalog\Model\Product::class);
- $linkedProductMock->expects($this->any())->method('getId')->will($this->returnValue(13));
- $linkedProductMock->expects($this->once())->method('isComposite')->will($this->returnValue(true));
- $this->productRepository
- ->expects($this->once())
- ->method('get')
- ->with('linked_product_sku')
- ->will($this->returnValue($linkedProductMock));
- $store = $this->createMock(\Magento\Store\Model\Store::class);
- $this->storeManagerMock->expects($this->any())->method('getStore')->will($this->returnValue($store));
- $store->expects($this->any())->method('getId')->will($this->returnValue(0));
- $option = $this->getMockBuilder(\Magento\Bundle\Model\Option::class)->disableOriginalConstructor()
- ->setMethods(['getId', '__wakeup'])
- ->getMock();
- $option->expects($this->once())->method('getId')->will($this->returnValue(1));
- $optionsCollectionMock = $this->createMock(\Magento\Bundle\Model\ResourceModel\Option\Collection::class);
- $optionsCollectionMock->expects($this->once())
- ->method('setIdFilter')
- ->with($this->equalTo('1'))
- ->will($this->returnSelf());
- $optionsCollectionMock->expects($this->once())
- ->method('getFirstItem')
- ->will($this->returnValue($option));
- $this->optionCollectionFactoryMock->expects($this->any())->method('create')->will(
- $this->returnValue($optionsCollectionMock)
- );
- $bundle = $this->createMock(\Magento\Bundle\Model\ResourceModel\Bundle::class);
- $bundle->expects($this->once())->method('getSelectionsData')->with($this->linkField)->willReturn([]);
- $this->bundleFactoryMock->expects($this->once())->method('create')->will($this->returnValue($bundle));
- $this->model->addChild($productMock, 1, $productLink);
- }
- /**
- * @expectedException \Magento\Framework\Exception\CouldNotSaveException
- */
- public function testAddChildProductAlreadyExistsInOption()
- {
- $productLink = $this->getMockBuilder(\Magento\Bundle\Api\Data\LinkInterface::class)
- ->setMethods(['getSku', 'getOptionId', 'getSelectionId'])
- ->disableOriginalConstructor()
- ->getMockForAbstractClass();
- $productLink->expects($this->any())->method('getSku')->will($this->returnValue('linked_product_sku'));
- $productLink->expects($this->any())->method('getOptionId')->will($this->returnValue(1));
- $productLink->expects($this->any())->method('getSelectionId')->will($this->returnValue(1));
- $this->metadataMock->expects($this->once())->method('getLinkField')->willReturn($this->linkField);
- $productMock = $this->createPartialMock(
- \Magento\Catalog\Model\Product::class,
- ['getTypeId', 'getCopyFromView', 'getData', 'getTypeInstance', 'getSku']
- );
- $productMock->expects($this->once())->method('getTypeId')->willReturn(
- \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE
- );
- $productMock->expects($this->any())
- ->method('getData')
- ->with($this->linkField)
- ->willReturn($this->linkField);
- $productMock->expects($this->any())->method('getCopyFromView')->will($this->returnValue(false));
- $linkedProductMock = $this->createMock(\Magento\Catalog\Model\Product::class);
- $linkedProductMock->expects($this->any())->method('getEntityId')->will($this->returnValue(13));
- $linkedProductMock->expects($this->once())->method('isComposite')->will($this->returnValue(false));
- $this->productRepository
- ->expects($this->once())
- ->method('get')
- ->with('linked_product_sku')
- ->will($this->returnValue($linkedProductMock));
- $store = $this->createMock(\Magento\Store\Model\Store::class);
- $this->storeManagerMock->expects($this->any())->method('getStore')->will($this->returnValue($store));
- $store->expects($this->any())->method('getId')->will($this->returnValue(0));
- $option = $this->getMockBuilder(\Magento\Bundle\Model\Option::class)->disableOriginalConstructor()
- ->setMethods(['getId', '__wakeup'])
- ->getMock();
- $option->expects($this->once())->method('getId')->will($this->returnValue(1));
- $optionsCollectionMock = $this->createMock(\Magento\Bundle\Model\ResourceModel\Option\Collection::class);
- $optionsCollectionMock->expects($this->once())
- ->method('setIdFilter')
- ->with($this->equalTo(1))
- ->will($this->returnSelf());
- $optionsCollectionMock->expects($this->once())
- ->method('getFirstItem')
- ->will($this->returnValue($option));
- $this->optionCollectionFactoryMock->expects($this->any())->method('create')->will(
- $this->returnValue($optionsCollectionMock)
- );
- $selections = [
- ['option_id' => 1, 'product_id' => 12, 'parent_product_id' => 'product_id'],
- ['option_id' => 1, 'product_id' => 13, 'parent_product_id' => 'product_id'],
- ];
- $bundle = $this->createMock(\Magento\Bundle\Model\ResourceModel\Bundle::class);
- $bundle->expects($this->once())->method('getSelectionsData')
- ->with($this->linkField)
- ->will($this->returnValue($selections));
- $this->bundleFactoryMock->expects($this->once())->method('create')->will($this->returnValue($bundle));
- $this->model->addChild($productMock, 1, $productLink);
- }
- /**
- * @expectedException \Magento\Framework\Exception\CouldNotSaveException
- */
- public function testAddChildCouldNotSave()
- {
- $productLink = $this->getMockBuilder(\Magento\Bundle\Api\Data\LinkInterface::class)
- ->setMethods(['getSku', 'getOptionId', 'getSelectionId'])
- ->disableOriginalConstructor()
- ->getMockForAbstractClass();
- $productLink->expects($this->any())->method('getSku')->will($this->returnValue('linked_product_sku'));
- $productLink->expects($this->any())->method('getOptionId')->will($this->returnValue(1));
- $productLink->expects($this->any())->method('getSelectionId')->will($this->returnValue(1));
- $this->metadataMock->expects($this->once())->method('getLinkField')->willReturn($this->linkField);
- $productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
- $productMock->expects($this->once())->method('getTypeId')->will($this->returnValue(
- \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE
- ));
- $productMock->expects($this->any())
- ->method('getData')
- ->with($this->linkField)
- ->willReturn($this->linkField);
- $linkedProductMock = $this->createMock(\Magento\Catalog\Model\Product::class);
- $linkedProductMock->expects($this->any())->method('getId')->will($this->returnValue(13));
- $linkedProductMock->expects($this->once())->method('isComposite')->will($this->returnValue(false));
- $this->productRepository
- ->expects($this->once())
- ->method('get')
- ->with('linked_product_sku')
- ->will($this->returnValue($linkedProductMock));
- $store = $this->createMock(\Magento\Store\Model\Store::class);
- $this->storeManagerMock->expects($this->any())->method('getStore')->will($this->returnValue($store));
- $store->expects($this->any())->method('getId')->will($this->returnValue(0));
- $option = $this->getMockBuilder(\Magento\Bundle\Model\Option::class)->disableOriginalConstructor()
- ->setMethods(['getId', '__wakeup'])
- ->getMock();
- $option->expects($this->once())->method('getId')->will($this->returnValue(1));
- $optionsCollectionMock = $this->createMock(\Magento\Bundle\Model\ResourceModel\Option\Collection::class);
- $optionsCollectionMock->expects($this->once())
- ->method('setIdFilter')
- ->with($this->equalTo(1))
- ->will($this->returnSelf());
- $optionsCollectionMock->expects($this->once())
- ->method('getFirstItem')
- ->will($this->returnValue($option));
- $this->optionCollectionFactoryMock->expects($this->any())->method('create')->will(
- $this->returnValue($optionsCollectionMock)
- );
- $selections = [
- ['option_id' => 1, 'product_id' => 11],
- ['option_id' => 1, 'product_id' => 12],
- ];
- $bundle = $this->createMock(\Magento\Bundle\Model\ResourceModel\Bundle::class);
- $bundle->expects($this->once())->method('getSelectionsData')
- ->with($this->linkField)
- ->will($this->returnValue($selections));
- $this->bundleFactoryMock->expects($this->once())->method('create')->will($this->returnValue($bundle));
- $selection = $this->createPartialMock(\Magento\Bundle\Model\Selection::class, ['save']);
- $selection->expects($this->once())->method('save')
- ->will(
- $this->returnCallback(
- function () {
- throw new \Exception('message');
- }
- )
- );
- $this->bundleSelectionMock->expects($this->once())->method('create')->will($this->returnValue($selection));
- $this->model->addChild($productMock, 1, $productLink);
- }
- public function testAddChild()
- {
- $productLink = $this->getMockBuilder(\Magento\Bundle\Api\Data\LinkInterface::class)
- ->setMethods(['getSku', 'getOptionId', 'getSelectionId'])
- ->disableOriginalConstructor()
- ->getMockForAbstractClass();
- $productLink->expects($this->any())->method('getSku')->will($this->returnValue('linked_product_sku'));
- $productLink->expects($this->any())->method('getOptionId')->will($this->returnValue(1));
- $productLink->expects($this->any())->method('getSelectionId')->will($this->returnValue(1));
- $this->metadataMock->expects($this->once())->method('getLinkField')->willReturn($this->linkField);
- $productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
- $productMock->expects($this->once())->method('getTypeId')->will($this->returnValue(
- \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE
- ));
- $productMock->expects($this->any())
- ->method('getData')
- ->with($this->linkField)
- ->willReturn($this->linkField);
- $linkedProductMock = $this->createMock(\Magento\Catalog\Model\Product::class);
- $linkedProductMock->expects($this->any())->method('getId')->will($this->returnValue(13));
- $linkedProductMock->expects($this->once())->method('isComposite')->will($this->returnValue(false));
- $this->productRepository
- ->expects($this->once())
- ->method('get')
- ->with('linked_product_sku')
- ->will($this->returnValue($linkedProductMock));
- $store = $this->createMock(\Magento\Store\Model\Store::class);
- $this->storeManagerMock->expects($this->any())->method('getStore')->will($this->returnValue($store));
- $store->expects($this->any())->method('getId')->will($this->returnValue(0));
- $option = $this->getMockBuilder(\Magento\Bundle\Model\Option::class)->disableOriginalConstructor()
- ->setMethods(['getId', '__wakeup'])
- ->getMock();
- $option->expects($this->once())->method('getId')->will($this->returnValue(1));
- $optionsCollectionMock = $this->createMock(\Magento\Bundle\Model\ResourceModel\Option\Collection::class);
- $optionsCollectionMock->expects($this->once())
- ->method('setIdFilter')
- ->with($this->equalTo(1))
- ->will($this->returnSelf());
- $optionsCollectionMock->expects($this->once())
- ->method('getFirstItem')
- ->will($this->returnValue($option));
- $this->optionCollectionFactoryMock->expects($this->any())->method('create')->will(
- $this->returnValue($optionsCollectionMock)
- );
- $selections = [
- ['option_id' => 1, 'product_id' => 11],
- ['option_id' => 1, 'product_id' => 12],
- ];
- $bundle = $this->createMock(\Magento\Bundle\Model\ResourceModel\Bundle::class);
- $bundle->expects($this->once())->method('getSelectionsData')
- ->with($this->linkField)
- ->will($this->returnValue($selections));
- $this->bundleFactoryMock->expects($this->once())->method('create')->will($this->returnValue($bundle));
- $selection = $this->createPartialMock(\Magento\Bundle\Model\Selection::class, ['save', 'getId']);
- $selection->expects($this->once())->method('save');
- $selection->expects($this->once())->method('getId')->will($this->returnValue(42));
- $this->bundleSelectionMock->expects($this->once())->method('create')->will($this->returnValue($selection));
- $result = $this->model->addChild($productMock, 1, $productLink);
- $this->assertEquals(42, $result);
- }
- public function testSaveChild()
- {
- $id = 12;
- $optionId = 1;
- $position = 3;
- $qty = 2;
- $priceType = 1;
- $price = 10.5;
- $canChangeQuantity = true;
- $isDefault = true;
- $linkProductId = 45;
- $parentProductId = 32;
- $bundleProductSku = 'bundleProductSku';
- $productLink = $this->getMockBuilder(\Magento\Bundle\Api\Data\LinkInterface::class)
- ->setMethods(['getSku', 'getOptionId', 'getSelectionId'])
- ->disableOriginalConstructor()
- ->getMockForAbstractClass();
- $productLink->expects($this->any())->method('getSku')->will($this->returnValue('linked_product_sku'));
- $productLink->expects($this->any())->method('getId')->will($this->returnValue($id));
- $productLink->expects($this->any())->method('getOptionId')->will($this->returnValue($optionId));
- $productLink->expects($this->any())->method('getPosition')->will($this->returnValue($position));
- $productLink->expects($this->any())->method('getQty')->will($this->returnValue($qty));
- $productLink->expects($this->any())->method('getPriceType')->will($this->returnValue($priceType));
- $productLink->expects($this->any())->method('getPrice')->will($this->returnValue($price));
- $productLink->expects($this->any())->method('getCanChangeQuantity')
- ->will($this->returnValue($canChangeQuantity));
- $productLink->expects($this->any())->method('getIsDefault')->will($this->returnValue($isDefault));
- $productLink->expects($this->any())->method('getSelectionId')->will($this->returnValue($optionId));
- $this->metadataMock->expects($this->once())->method('getLinkField')->willReturn($this->linkField);
- $productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
- $productMock->expects($this->once())->method('getTypeId')->will($this->returnValue(
- \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE
- ));
- $productMock->expects($this->any())
- ->method('getData')
- ->with($this->linkField)
- ->willReturn($parentProductId);
- $linkedProductMock = $this->createMock(\Magento\Catalog\Model\Product::class);
- $linkedProductMock->expects($this->any())->method('getId')->will($this->returnValue($linkProductId));
- $linkedProductMock->expects($this->once())->method('isComposite')->will($this->returnValue(false));
- $this->productRepository
- ->expects($this->at(0))
- ->method('get')
- ->with($bundleProductSku)
- ->will($this->returnValue($productMock));
- $this->productRepository
- ->expects($this->at(1))
- ->method('get')
- ->with('linked_product_sku')
- ->will($this->returnValue($linkedProductMock));
- $store = $this->createMock(\Magento\Store\Model\Store::class);
- $this->storeManagerMock->expects($this->any())->method('getStore')->will($this->returnValue($store));
- $store->expects($this->any())->method('getId')->will($this->returnValue(0));
- $selection = $this->createPartialMock(\Magento\Bundle\Model\Selection::class, [
- 'save',
- 'getId',
- 'load',
- 'setProductId',
- 'setParentProductId',
- 'setOptionId',
- 'setPosition',
- 'setSelectionQty',
- 'setSelectionPriceType',
- 'setSelectionPriceValue',
- 'setSelectionCanChangeQty',
- 'setIsDefault'
- ]);
- $selection->expects($this->once())->method('save');
- $selection->expects($this->once())->method('load')->with($id)->will($this->returnSelf());
- $selection->expects($this->any())->method('getId')->will($this->returnValue($id));
- $selection->expects($this->once())->method('setProductId')->with($linkProductId);
- $selection->expects($this->once())->method('setParentProductId')->with($parentProductId);
- $selection->expects($this->once())->method('setOptionId')->with($optionId);
- $selection->expects($this->once())->method('setPosition')->with($position);
- $selection->expects($this->once())->method('setSelectionQty')->with($qty);
- $selection->expects($this->once())->method('setSelectionPriceType')->with($priceType);
- $selection->expects($this->once())->method('setSelectionPriceValue')->with($price);
- $selection->expects($this->once())->method('setSelectionCanChangeQty')->with($canChangeQuantity);
- $selection->expects($this->once())->method('setIsDefault')->with($isDefault);
- $this->bundleSelectionMock->expects($this->once())->method('create')->will($this->returnValue($selection));
- $this->assertTrue($this->model->saveChild($bundleProductSku, $productLink));
- }
- /**
- * @expectedException \Magento\Framework\Exception\CouldNotSaveException
- */
- public function testSaveChildFailedToSave()
- {
- $id = 12;
- $linkProductId = 45;
- $parentProductId = 32;
- $productLink = $this->getMockBuilder(\Magento\Bundle\Api\Data\LinkInterface::class)
- ->setMethods(['getSku', 'getOptionId', 'getSelectionId'])
- ->disableOriginalConstructor()
- ->getMockForAbstractClass();
- $productLink->expects($this->any())->method('getSku')->will($this->returnValue('linked_product_sku'));
- $productLink->expects($this->any())->method('getId')->will($this->returnValue($id));
- $productLink->expects($this->any())->method('getSelectionId')->will($this->returnValue(1));
- $bundleProductSku = 'bundleProductSku';
- $productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
- $productMock->expects($this->once())->method('getTypeId')->will($this->returnValue(
- \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE
- ));
- $productMock->expects($this->any())->method('getId')->will($this->returnValue($parentProductId));
- $linkedProductMock = $this->createMock(\Magento\Catalog\Model\Product::class);
- $linkedProductMock->expects($this->any())->method('getId')->will($this->returnValue($linkProductId));
- $linkedProductMock->expects($this->once())->method('isComposite')->will($this->returnValue(false));
- $this->productRepository
- ->expects($this->at(0))
- ->method('get')
- ->with($bundleProductSku)
- ->will($this->returnValue($productMock));
- $this->productRepository
- ->expects($this->at(1))
- ->method('get')
- ->with('linked_product_sku')
- ->will($this->returnValue($linkedProductMock));
- $store = $this->createMock(\Magento\Store\Model\Store::class);
- $this->storeManagerMock->expects($this->any())->method('getStore')->will($this->returnValue($store));
- $store->expects($this->any())->method('getId')->will($this->returnValue(0));
- $selection = $this->createPartialMock(\Magento\Bundle\Model\Selection::class, [
- 'save',
- 'getId',
- 'load',
- 'setProductId',
- 'setParentProductId',
- 'setSelectionId',
- 'setOptionId',
- 'setPosition',
- 'setSelectionQty',
- 'setSelectionPriceType',
- 'setSelectionPriceValue',
- 'setSelectionCanChangeQty',
- 'setIsDefault'
- ]);
- $mockException = $this->createMock(\Exception::class);
- $selection->expects($this->once())->method('save')->will($this->throwException($mockException));
- $selection->expects($this->once())->method('load')->with($id)->will($this->returnSelf());
- $selection->expects($this->any())->method('getId')->will($this->returnValue($id));
- $selection->expects($this->once())->method('setProductId')->with($linkProductId);
- $this->bundleSelectionMock->expects($this->once())->method('create')->will($this->returnValue($selection));
- $this->model->saveChild($bundleProductSku, $productLink);
- }
- /**
- * @expectedException \Magento\Framework\Exception\InputException
- */
- public function testSaveChildWithoutId()
- {
- $bundleProductSku = "bundleSku";
- $linkedProductSku = 'simple';
- $productLink = $this->createMock(\Magento\Bundle\Api\Data\LinkInterface::class);
- $productLink->expects($this->any())->method('getId')->will($this->returnValue(null));
- $productLink->expects($this->any())->method('getSku')->will($this->returnValue($linkedProductSku));
- $productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
- $productMock->expects($this->once())->method('getTypeId')->will($this->returnValue(
- \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE
- ));
- $linkedProductMock = $this->createMock(\Magento\Catalog\Model\Product::class);
- $linkedProductMock->expects($this->once())->method('isComposite')->will($this->returnValue(false));
- $this->productRepository
- ->expects($this->at(0))
- ->method('get')
- ->with($bundleProductSku)
- ->will($this->returnValue($productMock));
- $this->productRepository
- ->expects($this->at(1))
- ->method('get')
- ->with($linkedProductSku)
- ->will($this->returnValue($linkedProductMock));
- $this->model->saveChild($bundleProductSku, $productLink);
- }
- /**
- * @expectedException \Magento\Framework\Exception\InputException
- * @expectedExceptionMessage The product link with the "12345" ID field wasn't found. Verify the ID and try again.
- */
- public function testSaveChildWithInvalidId()
- {
- $id = 12345;
- $linkedProductSku = 'simple';
- $bundleProductSku = "bundleProductSku";
- $productLink = $this->createMock(\Magento\Bundle\Api\Data\LinkInterface::class);
- $productLink->expects($this->any())->method('getId')->will($this->returnValue($id));
- $productLink->expects($this->any())->method('getSku')->will($this->returnValue($linkedProductSku));
- $productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
- $productMock->expects($this->once())->method('getTypeId')->will($this->returnValue(
- \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE
- ));
- $linkedProductMock = $this->createMock(\Magento\Catalog\Model\Product::class);
- $linkedProductMock->expects($this->once())->method('isComposite')->will($this->returnValue(false));
- $this->productRepository
- ->expects($this->at(0))
- ->method('get')
- ->with($bundleProductSku)
- ->will($this->returnValue($productMock));
- $this->productRepository
- ->expects($this->at(1))
- ->method('get')
- ->with($linkedProductSku)
- ->will($this->returnValue($linkedProductMock));
- $selection = $this->createPartialMock(\Magento\Bundle\Model\Selection::class, [
- 'getId',
- 'load',
- ]);
- $selection->expects($this->once())->method('load')->with($id)->will($this->returnSelf());
- $selection->expects($this->any())->method('getId')->will($this->returnValue(null));
- $this->bundleSelectionMock->expects($this->once())->method('create')->will($this->returnValue($selection));
- $this->model->saveChild($bundleProductSku, $productLink);
- }
- /**
- * @expectedException \Magento\Framework\Exception\InputException
- */
- public function testSaveChildWithCompositeProductLink()
- {
- $bundleProductSku = "bundleProductSku";
- $id = 12;
- $linkedProductSku = 'simple';
- $productLink = $this->createMock(\Magento\Bundle\Api\Data\LinkInterface::class);
- $productLink->expects($this->any())->method('getId')->will($this->returnValue($id));
- $productLink->expects($this->any())->method('getSku')->will($this->returnValue($linkedProductSku));
- $productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
- $productMock->expects($this->once())->method('getTypeId')->will($this->returnValue(
- \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE
- ));
- $linkedProductMock = $this->createMock(\Magento\Catalog\Model\Product::class);
- $linkedProductMock->expects($this->once())->method('isComposite')->will($this->returnValue(true));
- $this->productRepository
- ->expects($this->at(0))
- ->method('get')
- ->with($bundleProductSku)
- ->will($this->returnValue($productMock));
- $this->productRepository
- ->expects($this->at(1))
- ->method('get')
- ->with($linkedProductSku)
- ->will($this->returnValue($linkedProductMock));
- $this->model->saveChild($bundleProductSku, $productLink);
- }
- /**
- * @expectedException \Magento\Framework\Exception\InputException
- */
- public function testSaveChildWithSimpleProduct()
- {
- $id = 12;
- $linkedProductSku = 'simple';
- $bundleProductSku = "bundleProductSku";
- $productLink = $this->createMock(\Magento\Bundle\Api\Data\LinkInterface::class);
- $productLink->expects($this->any())->method('getId')->will($this->returnValue($id));
- $productLink->expects($this->any())->method('getSku')->will($this->returnValue($linkedProductSku));
- $productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
- $productMock->expects($this->once())->method('getTypeId')->will($this->returnValue(
- \Magento\Catalog\Model\Product\Type::TYPE_SIMPLE
- ));
- $this->productRepository->expects($this->once())->method('get')->with($bundleProductSku)
- ->willReturn($productMock);
- $this->model->saveChild($bundleProductSku, $productLink);
- }
- public function testRemoveChild()
- {
- $this->productRepository->expects($this->any())->method('get')->will($this->returnValue($this->product));
- $bundle = $this->createMock(\Magento\Bundle\Model\ResourceModel\Bundle::class);
- $this->bundleFactoryMock->expects($this->once())->method('create')->will($this->returnValue($bundle));
- $productSku = 'productSku';
- $optionId = 1;
- $productId = 1;
- $childSku = 'childSku';
- $this->product
- ->expects($this->any())
- ->method('getTypeId')
- ->will($this->returnValue(\Magento\Catalog\Model\Product\Type::TYPE_BUNDLE));
- $this->getRemoveOptions();
- $selection = $this->getMockBuilder(\Magento\Bundle\Model\Selection::class)
- ->setMethods(['getSku', 'getOptionId', 'getSelectionId', 'getProductId', '__wakeup'])
- ->disableOriginalConstructor()
- ->getMock();
- $selection->expects($this->any())->method('getSku')->will($this->returnValue($childSku));
- $selection->expects($this->any())->method('getOptionId')->will($this->returnValue($optionId));
- $selection->expects($this->any())->method('getSelectionId')->will($this->returnValue(55));
- $selection->expects($this->any())->method('getProductId')->willReturn($productId);
- $this->option->expects($this->any())->method('getSelections')->will($this->returnValue([$selection]));
- $this->metadataMock->expects($this->any())->method('getLinkField')->willReturn($this->linkField);
- $this->product->expects($this->any())
- ->method('getData')
- ->with($this->linkField)
- ->willReturn(3);
- $bundle->expects($this->once())->method('dropAllUnneededSelections')->with(3, []);
- $bundle->expects($this->once())->method('removeProductRelations')->with(3, [$productId]);
- //Params come in lowercase to method
- $this->assertTrue($this->model->removeChild($productSku, $optionId, $childSku));
- }
- /**
- * @expectedException \Magento\Framework\Exception\InputException
- */
- public function testRemoveChildForbidden()
- {
- $this->productRepository->expects($this->any())->method('get')->will($this->returnValue($this->product));
- $productSku = 'productSku';
- $optionId = 1;
- $childSku = 'childSku';
- $this->product
- ->expects($this->any())
- ->method('getTypeId')
- ->will($this->returnValue(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE));
- $this->model->removeChild($productSku, $optionId, $childSku);
- }
- /**
- * @expectedException \Magento\Framework\Exception\NoSuchEntityException
- */
- public function testRemoveChildInvalidOptionId()
- {
- $this->productRepository->expects($this->any())->method('get')->will($this->returnValue($this->product));
- $productSku = 'productSku';
- $optionId = 1;
- $childSku = 'childSku';
- $this->product
- ->expects($this->any())
- ->method('getTypeId')
- ->will($this->returnValue(\Magento\Catalog\Model\Product\Type::TYPE_BUNDLE));
- $this->getRemoveOptions();
- $selection = $this->getMockBuilder(\Magento\Bundle\Model\Selection::class)
- ->setMethods(['getSku', 'getOptionId', 'getSelectionId', 'getProductId', '__wakeup'])
- ->disableOriginalConstructor()
- ->getMock();
- $selection->expects($this->any())->method('getSku')->will($this->returnValue($childSku));
- $selection->expects($this->any())->method('getOptionId')->will($this->returnValue($optionId + 1));
- $selection->expects($this->any())->method('getSelectionId')->will($this->returnValue(55));
- $selection->expects($this->any())->method('getProductId')->will($this->returnValue(1));
- $this->option->expects($this->any())->method('getSelections')->will($this->returnValue([$selection]));
- $this->model->removeChild($productSku, $optionId, $childSku);
- }
- /**
- * @expectedException \Magento\Framework\Exception\NoSuchEntityException
- */
- public function testRemoveChildInvalidChildSku()
- {
- $this->productRepository->expects($this->any())->method('get')->will($this->returnValue($this->product));
- $productSku = 'productSku';
- $optionId = 1;
- $childSku = 'childSku';
- $this->product
- ->expects($this->any())
- ->method('getTypeId')
- ->will($this->returnValue(\Magento\Catalog\Model\Product\Type::TYPE_BUNDLE));
- $this->getRemoveOptions();
- $selection = $this->getMockBuilder(\Magento\Bundle\Model\Selection::class)
- ->setMethods(['getSku', 'getOptionId', 'getSelectionId', 'getProductId', '__wakeup'])
- ->disableOriginalConstructor()
- ->getMock();
- $selection->expects($this->any())->method('getSku')->will($this->returnValue($childSku . '_invalid'));
- $selection->expects($this->any())->method('getOptionId')->will($this->returnValue($optionId));
- $selection->expects($this->any())->method('getSelectionId')->will($this->returnValue(55));
- $selection->expects($this->any())->method('getProductId')->will($this->returnValue(1));
- $this->option->expects($this->any())->method('getSelections')->will($this->returnValue([$selection]));
- $this->model->removeChild($productSku, $optionId, $childSku);
- }
- private function getOptions()
- {
- $this->product->expects($this->any())->method('getTypeInstance')->will($this->returnValue($this->productType));
- $this->product->expects($this->once())->method('getStoreId')->will($this->returnValue($this->storeId));
- $this->productType->expects($this->once())->method('setStoreFilter')
- ->with($this->equalTo($this->storeId), $this->equalTo($this->product));
- $this->productType->expects($this->once())->method('getOptionsCollection')
- ->with($this->equalTo($this->product))
- ->will($this->returnValue($this->optionCollection));
- }
- public function getRemoveOptions()
- {
- $this->product->expects($this->any())->method('getTypeInstance')->will($this->returnValue($this->productType));
- $this->product->expects($this->once())->method('getStoreId')->will($this->returnValue(1));
- $this->productType->expects($this->once())->method('setStoreFilter');
- $this->productType->expects($this->once())->method('getOptionsCollection')
- ->with($this->equalTo($this->product))
- ->will($this->returnValue($this->optionCollection));
- $this->productType->expects($this->once())->method('getOptionsIds')->with($this->equalTo($this->product))
- ->will($this->returnValue([1, 2, 3]));
- $this->productType->expects($this->once())->method('getSelectionsCollection')
- ->will($this->returnValue([]));
- $this->optionCollection->expects($this->any())->method('appendSelections')
- ->with($this->equalTo([]), true)
- ->will($this->returnValue([$this->option]));
- }
- }
|