Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
FilterRendererTest.php
Go to the documentation of this file.
1 <?php
8 
10 
11 class FilterRendererTest extends \PHPUnit\Framework\TestCase
12 {
14  protected $plugin;
15 
17  protected $swatchHelperMock;
18 
20  protected $layoutMock;
21 
23  protected $filterMock;
24 
27 
29  protected $blockMock;
30 
32  protected $closureMock;
33 
34  protected function setUp()
35  {
36  $this->layoutMock = $this->createPartialMock(\Magento\Framework\View\Layout::class, ['createBlock']);
37 
38  $this->swatchHelperMock = $this->createPartialMock(\Magento\Swatches\Helper\Data::class, ['isSwatchAttribute']);
39 
40  $this->blockMock = $this->createPartialMock(
41  \Magento\Swatches\Block\LayeredNavigation\RenderLayered::class,
42  ['setSwatchFilter', 'toHtml']
43  );
44 
45  $this->filterMock = $this->createPartialMock(
46  \Magento\Catalog\Model\Layer\Filter\AbstractFilter::class,
47  ['getAttributeModel', 'hasAttributeModel']
48  );
49 
50  $this->filterRendererMock = $this->createMock(
51  \Magento\LayeredNavigation\Block\Navigation\FilterRenderer::class
52  );
53 
54  $this->closureMock = function () {
55  return $this->filterMock;
56  };
57 
58  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
59  $this->plugin = $objectManager->getObject(
60  \Magento\Swatches\Model\Plugin\FilterRenderer::class,
61  [
62  'layout' => $this->layoutMock,
63  'swatchHelper' => $this->swatchHelperMock
64  ]
65  );
66  }
67 
68  public function testAroundRenderTrue()
69  {
70  $attributeMock = $this->createMock(\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class);
71  $this->filterMock->expects($this->atLeastOnce())->method('getAttributeModel')->willReturn($attributeMock);
72  $this->filterMock->expects($this->once())->method('hasAttributeModel')->willReturn(true);
73  $this->swatchHelperMock
74  ->expects($this->once())
75  ->method('isSwatchAttribute')
76  ->with($attributeMock)
77  ->willReturn(true);
78 
79  $this->layoutMock->expects($this->once())->method('createBlock')->willReturn($this->blockMock);
80  $this->blockMock->expects($this->once())->method('setSwatchFilter')->will($this->returnSelf());
81 
82  $this->plugin->aroundRender($this->filterRendererMock, $this->closureMock, $this->filterMock);
83  }
84 
85  public function testAroundRenderFalse()
86  {
87  $attributeMock = $this->createMock(\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class);
88  $this->filterMock->expects($this->atLeastOnce())->method('getAttributeModel')->willReturn($attributeMock);
89  $this->filterMock->expects($this->once())->method('hasAttributeModel')->willReturn(true);
90  $this->swatchHelperMock
91  ->expects($this->once())
92  ->method('isSwatchAttribute')
93  ->with($attributeMock)
94  ->willReturn(false);
95 
96  $result = $this->plugin->aroundRender($this->filterRendererMock, $this->closureMock, $this->filterMock);
97  $this->assertEquals($result, $this->filterMock);
98  }
99 }
$objectManager
Definition: bootstrap.php:17