Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
RenderLayeredTest.php
Go to the documentation of this file.
1 <?php
7 
13 class RenderLayeredTest extends \PHPUnit\Framework\TestCase
14 {
16  protected $contextMock;
17 
19  protected $requestMock;
20 
22  protected $urlBuilder;
23 
25  protected $eavAttributeMock;
26 
29 
32 
34  protected $swatchHelperMock;
35 
37  protected $mediaHelperMock;
38 
40  protected $filterMock;
41 
43  protected $block;
44 
45  protected function setUp()
46  {
47  $this->contextMock = $this->createMock(\Magento\Framework\View\Element\Template\Context::class);
48  $this->requestMock = $this->createMock(\Magento\Framework\App\RequestInterface::class);
49  $this->urlBuilder = $this->createPartialMock(
50  \Magento\Framework\Url::class,
51  ['getCurrentUrl', 'getRedirectUrl', 'getUrl']
52  );
53  $this->contextMock->expects($this->any())->method('getRequest')->willReturn($this->requestMock);
54  $this->contextMock->expects($this->any())->method('getUrlBuilder')->willReturn($this->urlBuilder);
55  $this->eavAttributeMock = $this->createMock(\Magento\Eav\Model\Entity\Attribute::class);
56  $this->layerAttributeFactoryMock = $this->createPartialMock(
57  \Magento\Catalog\Model\ResourceModel\Layer\Filter\AttributeFactory::class,
58  ['create']
59  );
60  $this->layerAttributeMock = $this->createPartialMock(
61  \Magento\Catalog\Model\ResourceModel\Layer\Filter\Attribute::class,
62  ['getCount']
63  );
64  $this->swatchHelperMock = $this->createMock(\Magento\Swatches\Helper\Data::class);
65  $this->mediaHelperMock = $this->createMock(\Magento\Swatches\Helper\Media::class);
66  $this->filterMock = $this->createMock(\Magento\Catalog\Model\Layer\Filter\AbstractFilter::class);
67 
68  $this->block = $this->getMockBuilder(\Magento\Swatches\Block\LayeredNavigation\RenderLayered::class)
69  ->setMethods(['filter', 'eavAttribute'])
70  ->setConstructorArgs(
71  [
72  $this->contextMock,
73  $this->eavAttributeMock,
74  $this->layerAttributeFactoryMock,
75  $this->swatchHelperMock,
76  $this->mediaHelperMock,
77  [],
78  ]
79  )
80  ->getMock();
81  }
82 
83  public function testSetSwatchFilter()
84  {
85  $this->block->method('filter')->willReturn($this->filterMock);
86  $eavAttribute = $this->createMock(\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class);
87  $this->filterMock->expects($this->once())->method('getAttributeModel')->willReturn($eavAttribute);
88  $this->block->method('eavAttribute')->willReturn($eavAttribute);
89  $result = $this->block->setSwatchFilter($this->filterMock);
90  $this->assertEquals($result, $this->block);
91  }
92 
93  public function testGetSwatchData()
94  {
96  $item1 = $this->createMock(\Magento\Catalog\Model\Layer\Filter\Item::class);
97  $item2 = $this->createMock(\Magento\Catalog\Model\Layer\Filter\Item::class);
98  $item3 = $this->createMock(\Magento\Catalog\Model\Layer\Filter\Item::class);
99  $item4 = $this->createMock(\Magento\Catalog\Model\Layer\Filter\Item::class);
100 
101  $item1->expects($this->any())->method('__call')->withConsecutive(
102  ['getValue'],
103  ['getCount'],
104  ['getValue'],
105  ['getCount'],
106  ['getLabel']
107  )->willReturnOnConsecutiveCalls(
108  'yellow',
109  3,
110  'yellow',
111  3,
112  'Yellow'
113  );
114 
115  $item2->expects($this->any())->method('__call')->with('getValue')->willReturn('blue');
116 
117  $item3->expects($this->any())->method('__call')->withConsecutive(
118  ['getValue'],
119  ['getCount']
120  )->willReturnOnConsecutiveCalls(
121  'red',
122  0
123  );
124 
125  $item4->expects($this->any())->method('__call')->withConsecutive(
126  ['getValue'],
127  ['getCount'],
128  ['getValue'],
129  ['getCount'],
130  ['getLabel']
131  )->willReturnOnConsecutiveCalls(
132  'green',
133  3,
134  'green',
135  0,
136  'Green'
137  );
138 
139  $this->filterMock->method('getItems')->willReturnOnConsecutiveCalls(
140  [$item1],
141  [$item2],
142  [$item3],
143  [$item4]
144  );
145 
146  $this->block->method('filter')->willReturn($this->filterMock);
147 
148  $option1 = $this->createMock(\Magento\Eav\Model\Entity\Attribute\Option::class);
149  $option1->expects($this->any())->method('getValue')->willReturn('yellow');
150 
151  $option2 = $this->createMock(\Magento\Eav\Model\Entity\Attribute\Option::class);
152  $option2->expects($this->any())->method('getValue')->willReturn(null);
153 
154  $option3 = $this->createMock(\Magento\Eav\Model\Entity\Attribute\Option::class);
155  $option3->expects($this->any())->method('getValue')->willReturn('red');
156 
157  $option4 = $this->createMock(\Magento\Eav\Model\Entity\Attribute\Option::class);
158  $option4->expects($this->any())->method('getValue')->willReturn('green');
159 
160  $eavAttribute = $this->createMock(\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class);
161  $eavAttribute->expects($this->once())
162  ->method('getOptions')
163  ->willReturn([$option1, $option2, $option3, $option4]);
164  $eavAttribute->expects($this->any())->method('getIsFilterable')->willReturn(0);
165 
166  $this->filterMock->expects($this->once())->method('getAttributeModel')->willReturn($eavAttribute);
167  $this->block->method('eavAttribute')->willReturn($eavAttribute);
168  $this->block->setSwatchFilter($this->filterMock);
169 
170  $this->urlBuilder->expects($this->atLeastOnce())->method('getUrl')->willReturn('http://example.com/image.png');
171 
172  $optionsCount = [
173  14 => 1,
174  15 => 4,
175  ];
176  $this->layerAttributeMock
177  ->method('getCount')
178  ->willReturn($optionsCount);
179 
180  $this->block->getSwatchData();
181  }
182 
183  public function testGetSwatchDataException()
184  {
185  $this->block->method('filter')->willReturn($this->filterMock);
186  $this->block->setSwatchFilter($this->filterMock);
187  $this->expectException('\RuntimeException');
188  $this->block->getSwatchData();
189  }
190 
191  public function testGetSwatchPath()
192  {
193  $this->mediaHelperMock
194  ->expects($this->once())
195  ->method('getSwatchAttributeImage')
196  ->with('swatch_image', '/m/a/magento.jpg')
197  ->willReturn('http://domain.com/path_to_swatch_image/m/a/magento.jpg');
198  $result = $this->block->getSwatchPath('swatch_image', '/m/a/magento.jpg');
199  $this->assertEquals($result, 'http://domain.com/path_to_swatch_image/m/a/magento.jpg');
200  }
201 }