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(
51 [
'getCurrentUrl',
'getRedirectUrl',
'getUrl']
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(
60 $this->layerAttributeMock = $this->createPartialMock(
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);
68 $this->block = $this->getMockBuilder(\
Magento\Swatches\Block\LayeredNavigation\RenderLayered::class)
69 ->setMethods([
'filter',
'eavAttribute'])
73 $this->eavAttributeMock,
74 $this->layerAttributeFactoryMock,
75 $this->swatchHelperMock,
76 $this->mediaHelperMock,
85 $this->block->method(
'filter')->willReturn($this->filterMock);
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);
93 public function testGetSwatchData()
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);
101 $item1->expects($this->any())->method(
'__call')->withConsecutive(
107 )->willReturnOnConsecutiveCalls(
115 $item2->expects($this->any())->method(
'__call')->with(
'getValue')->willReturn(
'blue');
117 $item3->expects($this->any())->method(
'__call')->withConsecutive(
120 )->willReturnOnConsecutiveCalls(
125 $item4->expects($this->any())->method(
'__call')->withConsecutive(
131 )->willReturnOnConsecutiveCalls(
139 $this->filterMock->method(
'getItems')->willReturnOnConsecutiveCalls(
146 $this->block->method(
'filter')->willReturn($this->filterMock);
148 $option1 = $this->createMock(\
Magento\Eav\Model\Entity\Attribute\Option::class);
149 $option1->expects($this->any())->method(
'getValue')->willReturn(
'yellow');
151 $option2 = $this->createMock(\
Magento\Eav\Model\Entity\Attribute\Option::class);
152 $option2->expects($this->any())->method(
'getValue')->willReturn(
null);
154 $option3 = $this->createMock(\
Magento\Eav\Model\Entity\Attribute\Option::class);
155 $option3->expects($this->any())->method(
'getValue')->willReturn(
'red');
157 $option4 = $this->createMock(\
Magento\Eav\Model\Entity\Attribute\Option::class);
158 $option4->expects($this->any())->method(
'getValue')->willReturn(
'green');
161 $eavAttribute->expects($this->once())
162 ->method(
'getOptions')
163 ->willReturn([$option1, $option2, $option3, $option4]);
164 $eavAttribute->expects($this->any())->method(
'getIsFilterable')->willReturn(0);
166 $this->filterMock->expects($this->once())->method(
'getAttributeModel')->willReturn($eavAttribute);
167 $this->block->method(
'eavAttribute')->willReturn($eavAttribute);
168 $this->block->setSwatchFilter($this->filterMock);
170 $this->urlBuilder->expects($this->atLeastOnce())->method(
'getUrl')->willReturn(
'http://example.com/image.png');
176 $this->layerAttributeMock
178 ->willReturn($optionsCount);
180 $this->block->getSwatchData();
185 $this->block->method(
'filter')->willReturn($this->filterMock);
186 $this->block->setSwatchFilter($this->filterMock);
187 $this->expectException(
'\RuntimeException');
188 $this->block->getSwatchData();
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');
testGetSwatchDataException()
$layerAttributeFactoryMock