RenderLayeredTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Swatches\Test\Unit\Block\LayeredNavigation;
  7. /**
  8. * Class RenderLayered Render Swatches at Layered Navigation
  9. *
  10. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  11. */
  12. class RenderLayeredTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /** @var \PHPUnit_Framework_MockObject_MockObject */
  15. protected $contextMock;
  16. /** @var \PHPUnit_Framework_MockObject_MockObject */
  17. protected $requestMock;
  18. /** @var \PHPUnit_Framework_MockObject_MockObject */
  19. protected $urlBuilder;
  20. /** @var \PHPUnit_Framework_MockObject_MockObject */
  21. protected $eavAttributeMock;
  22. /** @var \PHPUnit_Framework_MockObject_MockObject */
  23. protected $layerAttributeFactoryMock;
  24. /** @var \PHPUnit_Framework_MockObject_MockObject */
  25. protected $layerAttributeMock;
  26. /** @var \PHPUnit_Framework_MockObject_MockObject */
  27. protected $swatchHelperMock;
  28. /** @var \PHPUnit_Framework_MockObject_MockObject */
  29. protected $mediaHelperMock;
  30. /** @var \PHPUnit_Framework_MockObject_MockObject */
  31. protected $filterMock;
  32. /** @var \PHPUnit_Framework_MockObject_MockObject */
  33. protected $block;
  34. protected function setUp()
  35. {
  36. $this->contextMock = $this->createMock(\Magento\Framework\View\Element\Template\Context::class);
  37. $this->requestMock = $this->createMock(\Magento\Framework\App\RequestInterface::class);
  38. $this->urlBuilder = $this->createPartialMock(
  39. \Magento\Framework\Url::class,
  40. ['getCurrentUrl', 'getRedirectUrl', 'getUrl']
  41. );
  42. $this->contextMock->expects($this->any())->method('getRequest')->willReturn($this->requestMock);
  43. $this->contextMock->expects($this->any())->method('getUrlBuilder')->willReturn($this->urlBuilder);
  44. $this->eavAttributeMock = $this->createMock(\Magento\Eav\Model\Entity\Attribute::class);
  45. $this->layerAttributeFactoryMock = $this->createPartialMock(
  46. \Magento\Catalog\Model\ResourceModel\Layer\Filter\AttributeFactory::class,
  47. ['create']
  48. );
  49. $this->layerAttributeMock = $this->createPartialMock(
  50. \Magento\Catalog\Model\ResourceModel\Layer\Filter\Attribute::class,
  51. ['getCount']
  52. );
  53. $this->swatchHelperMock = $this->createMock(\Magento\Swatches\Helper\Data::class);
  54. $this->mediaHelperMock = $this->createMock(\Magento\Swatches\Helper\Media::class);
  55. $this->filterMock = $this->createMock(\Magento\Catalog\Model\Layer\Filter\AbstractFilter::class);
  56. $this->block = $this->getMockBuilder(\Magento\Swatches\Block\LayeredNavigation\RenderLayered::class)
  57. ->setMethods(['filter', 'eavAttribute'])
  58. ->setConstructorArgs(
  59. [
  60. $this->contextMock,
  61. $this->eavAttributeMock,
  62. $this->layerAttributeFactoryMock,
  63. $this->swatchHelperMock,
  64. $this->mediaHelperMock,
  65. [],
  66. ]
  67. )
  68. ->getMock();
  69. }
  70. public function testSetSwatchFilter()
  71. {
  72. $this->block->method('filter')->willReturn($this->filterMock);
  73. $eavAttribute = $this->createMock(\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class);
  74. $this->filterMock->expects($this->once())->method('getAttributeModel')->willReturn($eavAttribute);
  75. $this->block->method('eavAttribute')->willReturn($eavAttribute);
  76. $result = $this->block->setSwatchFilter($this->filterMock);
  77. $this->assertEquals($result, $this->block);
  78. }
  79. public function testGetSwatchData()
  80. {
  81. /** @var \PHPUnit_Framework_MockObject_MockObject $item */
  82. $item1 = $this->createMock(\Magento\Catalog\Model\Layer\Filter\Item::class);
  83. $item2 = $this->createMock(\Magento\Catalog\Model\Layer\Filter\Item::class);
  84. $item3 = $this->createMock(\Magento\Catalog\Model\Layer\Filter\Item::class);
  85. $item4 = $this->createMock(\Magento\Catalog\Model\Layer\Filter\Item::class);
  86. $item1->expects($this->any())->method('__call')->withConsecutive(
  87. ['getValue'],
  88. ['getCount'],
  89. ['getValue'],
  90. ['getCount'],
  91. ['getLabel']
  92. )->willReturnOnConsecutiveCalls(
  93. 'yellow',
  94. 3,
  95. 'yellow',
  96. 3,
  97. 'Yellow'
  98. );
  99. $item2->expects($this->any())->method('__call')->with('getValue')->willReturn('blue');
  100. $item3->expects($this->any())->method('__call')->withConsecutive(
  101. ['getValue'],
  102. ['getCount']
  103. )->willReturnOnConsecutiveCalls(
  104. 'red',
  105. 0
  106. );
  107. $item4->expects($this->any())->method('__call')->withConsecutive(
  108. ['getValue'],
  109. ['getCount'],
  110. ['getValue'],
  111. ['getCount'],
  112. ['getLabel']
  113. )->willReturnOnConsecutiveCalls(
  114. 'green',
  115. 3,
  116. 'green',
  117. 0,
  118. 'Green'
  119. );
  120. $this->filterMock->method('getItems')->willReturnOnConsecutiveCalls(
  121. [$item1],
  122. [$item2],
  123. [$item3],
  124. [$item4]
  125. );
  126. $this->block->method('filter')->willReturn($this->filterMock);
  127. $option1 = $this->createMock(\Magento\Eav\Model\Entity\Attribute\Option::class);
  128. $option1->expects($this->any())->method('getValue')->willReturn('yellow');
  129. $option2 = $this->createMock(\Magento\Eav\Model\Entity\Attribute\Option::class);
  130. $option2->expects($this->any())->method('getValue')->willReturn(null);
  131. $option3 = $this->createMock(\Magento\Eav\Model\Entity\Attribute\Option::class);
  132. $option3->expects($this->any())->method('getValue')->willReturn('red');
  133. $option4 = $this->createMock(\Magento\Eav\Model\Entity\Attribute\Option::class);
  134. $option4->expects($this->any())->method('getValue')->willReturn('green');
  135. $eavAttribute = $this->createMock(\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class);
  136. $eavAttribute->expects($this->once())
  137. ->method('getOptions')
  138. ->willReturn([$option1, $option2, $option3, $option4]);
  139. $eavAttribute->expects($this->any())->method('getIsFilterable')->willReturn(0);
  140. $this->filterMock->expects($this->once())->method('getAttributeModel')->willReturn($eavAttribute);
  141. $this->block->method('eavAttribute')->willReturn($eavAttribute);
  142. $this->block->setSwatchFilter($this->filterMock);
  143. $this->urlBuilder->expects($this->atLeastOnce())->method('getUrl')->willReturn('http://example.com/image.png');
  144. $optionsCount = [
  145. 14 => 1,
  146. 15 => 4,
  147. ];
  148. $this->layerAttributeMock
  149. ->method('getCount')
  150. ->willReturn($optionsCount);
  151. $this->block->getSwatchData();
  152. }
  153. public function testGetSwatchDataException()
  154. {
  155. $this->block->method('filter')->willReturn($this->filterMock);
  156. $this->block->setSwatchFilter($this->filterMock);
  157. $this->expectException('\RuntimeException');
  158. $this->block->getSwatchData();
  159. }
  160. public function testGetSwatchPath()
  161. {
  162. $this->mediaHelperMock
  163. ->expects($this->once())
  164. ->method('getSwatchAttributeImage')
  165. ->with('swatch_image', '/m/a/magento.jpg')
  166. ->willReturn('http://domain.com/path_to_swatch_image/m/a/magento.jpg');
  167. $result = $this->block->getSwatchPath('swatch_image', '/m/a/magento.jpg');
  168. $this->assertEquals($result, 'http://domain.com/path_to_swatch_image/m/a/magento.jpg');
  169. }
  170. }