Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
LinksListTest.php
Go to the documentation of this file.
1 <?php
9 
10 use \Magento\Bundle\Model\Product\LinksList;
11 
12 class LinksListTest extends \PHPUnit\Framework\TestCase
13 {
17  protected $model;
18 
22  protected $linkFactoryMock;
23 
27  protected $productMock;
28 
32  protected $productTypeMock;
33 
37  protected $selectionMock;
38 
43 
44  protected function setUp()
45  {
46  $this->linkFactoryMock = $this->createPartialMock(\Magento\Bundle\Api\Data\LinkInterfaceFactory::class, [
47  'create',
48  ]);
49  $this->dataObjectHelperMock = $this->getMockBuilder(\Magento\Framework\Api\DataObjectHelper::class)
50  ->disableOriginalConstructor()
51  ->getMock();
52  $this->selectionMock = $this->createPartialMock(\Magento\Catalog\Model\Product::class, [
53  'getSelectionPriceType',
54  'getSelectionPriceValue',
55  'getData',
56  'getIsDefault',
57  'getSelectionQty',
58  'getSelectionCanChangeQty',
59  'getSelectionId',
60  '__wakeup'
61  ]);
62  $this->productMock = $this->createPartialMock(\Magento\Catalog\Model\Product::class, [
63  'getTypeInstance',
64  'getStoreId',
65  'getPriceType',
66  '__wakeup'
67  ]);
68  $this->productTypeMock = $this->createMock(\Magento\Bundle\Model\Product\Type::class);
69  $this->model = new LinksList($this->linkFactoryMock, $this->productTypeMock, $this->dataObjectHelperMock);
70  }
71 
72  public function testLinksList()
73  {
74  $optionId = 665;
75  $selectionId = 1345;
76  $this->productTypeMock->expects($this->once())
77  ->method('getSelectionsCollection')
78  ->with([$optionId], $this->productMock)
79  ->willReturn([$this->selectionMock]);
80  $this->productMock->expects($this->exactly(2))->method('getPriceType')->willReturn('price_type');
81  $this->selectionMock->expects($this->once())
82  ->method('getSelectionPriceType')
83  ->willReturn('selection_price_type');
84  $this->selectionMock->expects($this->once())->method('getSelectionPriceValue')->willReturn(12);
85  $this->selectionMock->expects($this->once())->method('getData')->willReturn(['some data']);
86  $this->selectionMock->expects($this->once())->method('getSelectionId')->willReturn($selectionId);
87  $this->selectionMock->expects($this->once())->method('getIsDefault')->willReturn(true);
88  $this->selectionMock->expects($this->once())->method('getSelectionQty')->willReturn(66);
89  $this->selectionMock->expects($this->once())->method('getSelectionCanChangeQty')->willReturn(22);
90  $linkMock = $this->createMock(\Magento\Bundle\Api\Data\LinkInterface::class);
91  $this->dataObjectHelperMock->expects($this->once())
92  ->method('populateWithArray')
93  ->with($linkMock, ['some data'], \Magento\Bundle\Api\Data\LinkInterface::class)->willReturnSelf();
94  $linkMock->expects($this->once())->method('setIsDefault')->with(true)->willReturnSelf();
95  $linkMock->expects($this->once())->method('setQty')->with(66)->willReturnSelf();
96  $linkMock->expects($this->once())->method('setCanChangeQuantity')->with(22)->willReturnSelf();
97  $linkMock->expects($this->once())->method('setPrice')->with(12)->willReturnSelf();
98  $linkMock->expects($this->once())->method('setId')->with($selectionId)->willReturnSelf();
99  $linkMock->expects($this->once())
100  ->method('setPriceType')->with('selection_price_type')->willReturnSelf();
101  $this->linkFactoryMock->expects($this->once())->method('create')->willReturn($linkMock);
102 
103  $this->assertEquals([$linkMock], $this->model->getItems($this->productMock, $optionId));
104  }
105 }