Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
NavigationTest.php
Go to the documentation of this file.
1 <?php
8 
9 class NavigationTest extends \PHPUnit\Framework\TestCase
10 {
14  protected $catalogLayerMock;
15 
19  protected $filterListMock;
20 
24  protected $layoutMock;
25 
30 
34  protected $model;
35 
36  protected function setUp()
37  {
38  $this->catalogLayerMock = $this->createMock(\Magento\Catalog\Model\Layer::class);
39  $this->filterListMock = $this->createMock(\Magento\Catalog\Model\Layer\FilterList::class);
40  $this->visibilityFlagMock = $this->createMock(\Magento\Catalog\Model\Layer\AvailabilityFlagInterface::class);
41 
43  $layerResolver = $this->getMockBuilder(\Magento\Catalog\Model\Layer\Resolver::class)
44  ->disableOriginalConstructor()
45  ->setMethods(['get', 'create'])
46  ->getMock();
47  $layerResolver->expects($this->any())
48  ->method($this->anything())
49  ->will($this->returnValue($this->catalogLayerMock));
50 
51  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
52  $this->model = $objectManager->getObject(
53  \Magento\LayeredNavigation\Block\Navigation::class,
54  [
55  'layerResolver' => $layerResolver,
56  'filterList' => $this->filterListMock,
57  'visibilityFlag' => $this->visibilityFlagMock
58  ]
59  );
60  $this->layoutMock = $this->createMock(\Magento\Framework\View\LayoutInterface::class);
61  }
62 
63  public function testGetStateHtml()
64  {
65  $stateHtml = 'I feel good';
66  $this->filterListMock->expects($this->any())->method('getFilters')->will($this->returnValue([]));
67  $this->layoutMock->expects($this->at(0))->method('getChildName')
68  ->with(null, 'state')
69  ->will($this->returnValue('state block'));
70 
71  $this->layoutMock->expects($this->once())->method('renderElement')
72  ->with('state block', true)
73  ->will($this->returnValue($stateHtml));
74 
75  $this->model->setLayout($this->layoutMock);
76  $this->assertEquals($stateHtml, $this->model->getStateHtml());
77  }
78 
84  public function testCanShowBlock()
85  {
86  // getFilers()
87  $filters = ['To' => 'be', 'or' => 'not', 'to' => 'be'];
88 
89  $this->filterListMock->expects($this->exactly(2))->method('getFilters')
90  ->with($this->catalogLayerMock)
91  ->will($this->returnValue($filters));
92  $this->assertEquals($filters, $this->model->getFilters());
93 
94  // canShowBlock()
95  $enabled = true;
96  $this->visibilityFlagMock
97  ->expects($this->once())
98  ->method('isEnabled')
99  ->with($this->catalogLayerMock, $filters)
100  ->will($this->returnValue($enabled));
101  $this->assertEquals($enabled, $this->model->canShowBlock());
102  }
103 
104  public function testGetClearUrl()
105  {
106  $this->filterListMock->expects($this->any())->method('getFilters')->will($this->returnValue([]));
107  $this->model->setLayout($this->layoutMock);
108  $this->layoutMock->expects($this->once())->method('getChildName')->will($this->returnValue('sample block'));
109 
110  $blockMock = $this->getMockForAbstractClass(
111  \Magento\Framework\View\Element\AbstractBlock::class,
112  [],
113  '',
114  false
115  );
116  $clearUrl = 'very clear URL';
117  $blockMock->setClearUrl($clearUrl);
118 
119  $this->layoutMock->expects($this->once())->method('getBlock')->will($this->returnValue($blockMock));
120  $this->assertEquals($clearUrl, $this->model->getClearUrl());
121  }
122 }
$objectManager
Definition: bootstrap.php:17
$filters
Definition: uploader.phtml:11