NavigationTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\LayeredNavigation\Test\Unit\Block;
  7. class NavigationTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \PHPUnit_Framework_MockObject_MockObject
  11. */
  12. protected $catalogLayerMock;
  13. /**
  14. * @var \PHPUnit_Framework_MockObject_MockObject
  15. */
  16. protected $filterListMock;
  17. /**
  18. * @var \PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $layoutMock;
  21. /**
  22. * @var \PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $visibilityFlagMock;
  25. /**
  26. * @var \Magento\LayeredNavigation\Block\Navigation
  27. */
  28. protected $model;
  29. protected function setUp()
  30. {
  31. $this->catalogLayerMock = $this->createMock(\Magento\Catalog\Model\Layer::class);
  32. $this->filterListMock = $this->createMock(\Magento\Catalog\Model\Layer\FilterList::class);
  33. $this->visibilityFlagMock = $this->createMock(\Magento\Catalog\Model\Layer\AvailabilityFlagInterface::class);
  34. /** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Catalog\Model\Layer\Resolver $layerResolver */
  35. $layerResolver = $this->getMockBuilder(\Magento\Catalog\Model\Layer\Resolver::class)
  36. ->disableOriginalConstructor()
  37. ->setMethods(['get', 'create'])
  38. ->getMock();
  39. $layerResolver->expects($this->any())
  40. ->method($this->anything())
  41. ->will($this->returnValue($this->catalogLayerMock));
  42. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  43. $this->model = $objectManager->getObject(
  44. \Magento\LayeredNavigation\Block\Navigation::class,
  45. [
  46. 'layerResolver' => $layerResolver,
  47. 'filterList' => $this->filterListMock,
  48. 'visibilityFlag' => $this->visibilityFlagMock
  49. ]
  50. );
  51. $this->layoutMock = $this->createMock(\Magento\Framework\View\LayoutInterface::class);
  52. }
  53. public function testGetStateHtml()
  54. {
  55. $stateHtml = 'I feel good';
  56. $this->filterListMock->expects($this->any())->method('getFilters')->will($this->returnValue([]));
  57. $this->layoutMock->expects($this->at(0))->method('getChildName')
  58. ->with(null, 'state')
  59. ->will($this->returnValue('state block'));
  60. $this->layoutMock->expects($this->once())->method('renderElement')
  61. ->with('state block', true)
  62. ->will($this->returnValue($stateHtml));
  63. $this->model->setLayout($this->layoutMock);
  64. $this->assertEquals($stateHtml, $this->model->getStateHtml());
  65. }
  66. /**
  67. * @covers \Magento\LayeredNavigation\Block\Navigation::getLayer()
  68. * @covers \Magento\LayeredNavigation\Block\Navigation::getFilters()
  69. * @covers \Magento\LayeredNavigation\Block\Navigation::canShowBlock()
  70. */
  71. public function testCanShowBlock()
  72. {
  73. // getFilers()
  74. $filters = ['To' => 'be', 'or' => 'not', 'to' => 'be'];
  75. $this->filterListMock->expects($this->exactly(2))->method('getFilters')
  76. ->with($this->catalogLayerMock)
  77. ->will($this->returnValue($filters));
  78. $this->assertEquals($filters, $this->model->getFilters());
  79. // canShowBlock()
  80. $enabled = true;
  81. $this->visibilityFlagMock
  82. ->expects($this->once())
  83. ->method('isEnabled')
  84. ->with($this->catalogLayerMock, $filters)
  85. ->will($this->returnValue($enabled));
  86. $this->assertEquals($enabled, $this->model->canShowBlock());
  87. }
  88. public function testGetClearUrl()
  89. {
  90. $this->filterListMock->expects($this->any())->method('getFilters')->will($this->returnValue([]));
  91. $this->model->setLayout($this->layoutMock);
  92. $this->layoutMock->expects($this->once())->method('getChildName')->will($this->returnValue('sample block'));
  93. $blockMock = $this->getMockForAbstractClass(
  94. \Magento\Framework\View\Element\AbstractBlock::class,
  95. [],
  96. '',
  97. false
  98. );
  99. $clearUrl = 'very clear URL';
  100. $blockMock->setClearUrl($clearUrl);
  101. $this->layoutMock->expects($this->once())->method('getBlock')->will($this->returnValue($blockMock));
  102. $this->assertEquals($clearUrl, $this->model->getClearUrl());
  103. }
  104. }