Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SuggestedAttributeListTest.php
Go to the documentation of this file.
1 <?php
8 
9 class SuggestedAttributeListTest extends \PHPUnit\Framework\TestCase
10 {
15 
20 
25 
29  protected $collectionMock;
30 
34  protected $attributeMock;
35 
39  protected $labelPart = 'labelPart';
40 
41  protected function setUp()
42  {
43  $this->configurableAttributeHandler = $this->createMock(
44  \Magento\ConfigurableProduct\Model\ConfigurableAttributeHandler::class
45  );
46  $this->resourceHelperMock = $this->createMock(\Magento\Catalog\Model\ResourceModel\Helper::class);
47  $this->collectionMock = $this->createMock(
48  \Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection::class
49  );
50  $this->resourceHelperMock->expects(
51  $this->once()
52  )->method(
53  'addLikeEscape'
54  )->with(
55  $this->labelPart,
56  ['position' => 'any']
57  )->will(
58  $this->returnValue($this->labelPart)
59  );
60  $this->configurableAttributeHandler->expects(
61  $this->once()
62  )->method(
63  'getApplicableAttributes'
64  )->will(
65  $this->returnValue($this->collectionMock)
66  );
67  $valueMap = [
68  ['frontend_label', ['like' => $this->labelPart], $this->collectionMock],
69  ];
70  $this->collectionMock->expects(
71  $this->any()
72  )->method(
73  'addFieldToFilter'
74  )->will(
75  $this->returnValueMap($valueMap)
76  );
77  $this->attributeMock = $this->createPartialMock(
78  \Magento\Catalog\Model\ResourceModel\Eav\Attribute::class,
79  ['getId', 'getFrontendLabel', 'getAttributeCode', 'getSource']
80  );
81  $this->collectionMock->expects(
82  $this->once()
83  )->method(
84  'getItems'
85  )->will(
86  $this->returnValue(['id' => $this->attributeMock])
87  );
88  $this->suggestedListModel = new \Magento\ConfigurableProduct\Model\SuggestedAttributeList(
89  $this->configurableAttributeHandler,
90  $this->resourceHelperMock
91  );
92  }
93 
95  {
96  $source = $this->createMock(\Magento\Eav\Model\Entity\Attribute\Source\AbstractSource::class);
97  $result['id'] = ['id' => 'id', 'label' => 'label', 'code' => 'code', 'options' => 'options'];
98  $this->attributeMock->expects($this->once())->method('getId')->will($this->returnValue('id'));
99  $this->attributeMock->expects($this->once())->method('getFrontendLabel')->will($this->returnValue('label'));
100  $this->attributeMock->expects($this->once())->method('getAttributeCode')->will($this->returnValue('code'));
101  $this->attributeMock->expects($this->once())->method('getSource')->will($this->returnValue($source));
102  $source->expects($this->once())->method('getAllOptions')->with(false)->will($this->returnValue('options'));
103  $this->configurableAttributeHandler->expects($this->once())->method('isAttributeApplicable')
104  ->with($this->attributeMock)->willReturn(true);
105 
106  $this->assertEquals($result, $this->suggestedListModel->getSuggestedAttributes($this->labelPart));
107  }
108 
110  {
111  $this->attributeMock->expects($this->never())->method('getId');
112  $this->attributeMock->expects($this->never())->method('getFrontendLabel');
113  $this->attributeMock->expects($this->never())->method('getAttributeCode');
114  $this->attributeMock->expects($this->never())->method('getSource');
115  $this->configurableAttributeHandler->expects($this->once())->method('isAttributeApplicable')
116  ->with($this->attributeMock)->willReturn(false);
117 
118  $this->assertEquals([], $this->suggestedListModel->getSuggestedAttributes($this->labelPart));
119  }
120 }
$source
Definition: source.php:23