NavigationTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Catalog\Test\Unit\Block;
  7. class NavigationTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \Magento\Catalog\Block\Navigation
  11. */
  12. protected $block;
  13. /**
  14. * @var \Magento\Framework\Registry|\PHPUnit_Framework_MockObject_MockObject
  15. */
  16. protected $registry;
  17. /**
  18. * @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $storeManager;
  21. /**
  22. * @var \Magento\Framework\View\DesignInterface|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $design;
  25. /**
  26. * @var \Magento\Framework\App\Http\Context|\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $httpContext;
  29. protected function setUp()
  30. {
  31. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  32. $categoryFactory = $this->createPartialMock(\Magento\Catalog\Model\CategoryFactory::class, ['create']);
  33. $this->registry = $this->createMock(\Magento\Framework\Registry::class);
  34. $this->storeManager = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
  35. $this->design = $this->createMock(\Magento\Framework\View\DesignInterface::class);
  36. $this->httpContext = $this->createMock(\Magento\Framework\App\Http\Context::class);
  37. $this->block = $objectManager->getObject(
  38. \Magento\Catalog\Block\Navigation::class,
  39. [
  40. 'categoryFactory' => $categoryFactory,
  41. 'registry' => $this->registry,
  42. 'storeManager' => $this->storeManager,
  43. 'design' => $this->design,
  44. 'httpContext' => $this->httpContext
  45. ]
  46. );
  47. }
  48. public function testGetIdentities()
  49. {
  50. $this->assertEquals(
  51. [\Magento\Catalog\Model\Category::CACHE_TAG, \Magento\Store\Model\Group::CACHE_TAG],
  52. $this->block->getIdentities()
  53. );
  54. }
  55. public function testGetCurrentCategoryKey()
  56. {
  57. $categoryKey = 101;
  58. $category = $this->createMock(\Magento\Catalog\Model\Category::class);
  59. $category->expects($this->any())->method('getPath')->willReturn($categoryKey);
  60. $this->registry->expects($this->any())->method('registry')->with('current_category')->willReturn($category);
  61. $this->assertEquals($categoryKey, $this->block->getCurrentCategoryKey());
  62. }
  63. public function testGetCurrentCategoryKeyFromRootCategory()
  64. {
  65. $categoryKey = 102;
  66. $store = $this->createMock(\Magento\Store\Model\Store::class);
  67. $store->expects($this->any())->method('getRootCategoryId')->willReturn($categoryKey);
  68. $this->storeManager->expects($this->any())->method('getStore')->willReturn($store);
  69. $this->assertEquals($categoryKey, $this->block->getCurrentCategoryKey());
  70. }
  71. public function testGetCacheKeyInfo()
  72. {
  73. $store = $this->createMock(\Magento\Store\Model\Store::class);
  74. $store->expects($this->atLeastOnce())->method('getId')->willReturn(55);
  75. $store->expects($this->atLeastOnce())->method('getRootCategoryId')->willReturn(60);
  76. $this->storeManager->expects($this->atLeastOnce())->method('getStore')->willReturn($store);
  77. $theme = $this->createMock(\Magento\Framework\View\Design\ThemeInterface::class);
  78. $theme->expects($this->atLeastOnce())->method('getId')->willReturn(65);
  79. $this->design->expects($this->atLeastOnce())->method('getDesignTheme')->willReturn($theme);
  80. $this->httpContext->expects($this->atLeastOnce())
  81. ->method('getValue')
  82. ->with(\Magento\Customer\Model\Context::CONTEXT_GROUP)
  83. ->willReturn(70);
  84. $this->block->setTemplate('block_template');
  85. $this->block->setNameInLayout('block_name');
  86. $expectedResult = [
  87. 'CATALOG_NAVIGATION',
  88. 55,
  89. 65,
  90. 70,
  91. 'template' => 'block_template',
  92. 'name' => 'block_name',
  93. 60,
  94. 'category_path' => 60,
  95. 'short_cache_id' => 'c3de6d1160d1e7730b04d6cad409a2b4'
  96. ];
  97. $this->assertEquals($expectedResult, $this->block->getCacheKeyInfo());
  98. }
  99. }