Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
LinkTypeProviderTest.php
Go to the documentation of this file.
1 <?php
8 
9 class LinkTypeProviderTest extends \PHPUnit\Framework\TestCase
10 {
14  protected $model;
15 
20 
25 
29  protected $linkFactoryMock;
30 
34  protected $linkTypes;
35 
36  protected function setUp()
37  {
38  $this->linkTypeFactoryMock = $this->createPartialMock(
39  \Magento\Catalog\Api\Data\ProductLinkTypeInterfaceFactory::class,
40  ['create']
41  );
42  $this->linkAttributeFactoryMock = $this->createPartialMock(
43  \Magento\Catalog\Api\Data\ProductLinkAttributeInterfaceFactory::class,
44  ['create']
45  );
46  $this->linkFactoryMock = $this->createPartialMock(
47  \Magento\Catalog\Model\Product\LinkFactory::class,
48  ['create']
49  );
50  $this->linkTypes = [
51  'test_product_link_1' => 'test_code_1',
52  'test_product_link_2' => 'test_code_2',
53  'test_product_link_3' => 'test_code_3',
54  ];
55  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
56  $this->model = $objectManager->getObject(
57  \Magento\Catalog\Model\Product\LinkTypeProvider::class,
58  [
59  'linkTypeFactory' => $this->linkTypeFactoryMock,
60  'linkAttributeFactory' => $this->linkAttributeFactoryMock,
61  'linkFactory' => $this->linkFactoryMock,
62  'linkTypes' => $this->linkTypes
63  ]
64  );
65  }
66 
70  public function testGetItems()
71  {
72  $expectedResult = [];
73  $linkTypeMocks = [];
74  foreach ($this->linkTypes as $type => $typeCode) {
75  $value = ['name' => $type, 'code' => $typeCode];
76  $linkTypeMock = $this->createMock(\Magento\Catalog\Api\Data\ProductLinkTypeInterface::class);
77  $linkTypeMock->expects($this->once())
78  ->method('setName')
79  ->with($type)
80  ->willReturnSelf();
81  $linkTypeMock->expects($this->once())
82  ->method('setCode')
83  ->with($typeCode)
84  ->willReturnSelf();
85  $linkTypeMocks[] = $linkTypeMock;
86  $expectedResult[] = $linkTypeMock;
87  }
88  $this->linkTypeFactoryMock->expects($this->exactly(3))
89  ->method('create')
90  ->will($this->onConsecutiveCalls($linkTypeMocks[0], $linkTypeMocks[1], $linkTypeMocks[2]));
91  $this->assertEquals($expectedResult, $this->model->getItems());
92  }
93 
97  public function testGetItemAttributes($type, $typeId)
98  {
99  $attributes = [
100  ['code' => 'test_code_1', 'type' => 'test_type_1'],
101  ];
102  $linkAttributeMock = $this->createMock(\Magento\Catalog\Api\Data\ProductLinkAttributeInterface::class);
103  $linkAttributeMock->expects($this->once())
104  ->method('setCode')
105  ->with($attributes[0]['code'])
106  ->willReturnSelf();
107  $linkAttributeMock->expects($this->once())
108  ->method('setType')
109  ->with($attributes[0]['type'])
110  ->willReturnSelf();
111  $expectedResult = [
112  $linkAttributeMock,
113  ];
114  $linkMock = $this->createPartialMock(\Magento\Catalog\Model\Product\Link::class, ['getAttributes']);
115  $linkMock->expects($this->once())->method('getAttributes')->willReturn($attributes);
116  $this->linkFactoryMock->expects($this->once())->method('create')->with($typeId)->willReturn($linkMock);
117  $this->linkAttributeFactoryMock->expects($this->once())->method('create')->willReturn($linkAttributeMock);
118  $this->assertEquals($expectedResult, $this->model->getItemAttributes($type));
119  }
120 
125  {
126  return [
127  ['test_product_link_2', ['data' => ['link_type_id' => 'test_code_2']]],
128  ['null_product', ['data' => ['link_type_id' => null]]]
129  ];
130  }
131 }
$objectManager
Definition: bootstrap.php:17
$type
Definition: item.phtml:13
$value
Definition: gender.phtml:16
$attributes
Definition: matrix.phtml:13