ConfigTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Backend\Test\Unit\Model\Menu;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  8. class ConfigTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var \Magento\Framework\App\Cache\Type\Config|\PHPUnit_Framework_MockObject_MockObject
  12. */
  13. private $cacheInstanceMock;
  14. /**
  15. * @var \Magento\Backend\Model\Menu\Config\Reader|\PHPUnit_Framework_MockObject_MockObject
  16. */
  17. private $configReaderMock;
  18. /**
  19. * @var \Magento\Backend\Model\Menu|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. private $menuMock;
  22. /**
  23. * @var \Magento\Backend\Model\Menu\Builder|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. private $menuBuilderMock;
  26. /**
  27. * @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. private $logger;
  30. /**
  31. * @var \Magento\Backend\Model\Menu\Config
  32. */
  33. private $model;
  34. protected function setUp()
  35. {
  36. $this->cacheInstanceMock = $this->createMock(\Magento\Framework\App\Cache\Type\Config::class);
  37. $menuFactoryMock = $this->createPartialMock(\Magento\Backend\Model\MenuFactory::class, ['create']);
  38. $this->configReaderMock = $this->createMock(\Magento\Backend\Model\Menu\Config\Reader::class);
  39. $this->logger = $this->createMock(\Psr\Log\LoggerInterface::class);
  40. $this->menuMock = $this->createMock(\Magento\Backend\Model\Menu::class);
  41. $this->menuBuilderMock = $this->createMock(\Magento\Backend\Model\Menu\Builder::class);
  42. $menuFactoryMock->expects($this->any())->method('create')->will($this->returnValue($this->menuMock));
  43. $this->configReaderMock->expects($this->any())->method('read')->will($this->returnValue([]));
  44. $appState = $this->createPartialMock(\Magento\Framework\App\State::class, ['getAreaCode']);
  45. $appState->expects(
  46. $this->any()
  47. )->method(
  48. 'getAreaCode'
  49. )->will(
  50. $this->returnValue(\Magento\Backend\App\Area\FrontNameResolver::AREA_CODE)
  51. );
  52. $this->model = (new ObjectManager($this))->getObject(
  53. \Magento\Backend\Model\Menu\Config::class,
  54. [
  55. 'menuBuilder' => $this->menuBuilderMock,
  56. 'menuFactory' => $menuFactoryMock,
  57. 'configReader' => $this->configReaderMock,
  58. 'configCacheType' => $this->cacheInstanceMock,
  59. 'logger' => $this->logger,
  60. 'appState' => $appState,
  61. ]
  62. );
  63. }
  64. public function testGetMenuWithCachedObjectReturnsUnserializedObject()
  65. {
  66. $this->cacheInstanceMock->expects(
  67. $this->once()
  68. )->method(
  69. 'load'
  70. )->with(
  71. $this->equalTo(\Magento\Backend\Model\Menu\Config::CACHE_MENU_OBJECT)
  72. )->will(
  73. $this->returnValue('menu_cache')
  74. );
  75. $this->menuMock->expects($this->once())->method('unserialize')->with('menu_cache');
  76. $this->assertEquals($this->menuMock, $this->model->getMenu());
  77. }
  78. public function testGetMenuWithNotCachedObjectBuildsObject()
  79. {
  80. $this->cacheInstanceMock->expects(
  81. $this->at(0)
  82. )->method(
  83. 'load'
  84. )->with(
  85. $this->equalTo(\Magento\Backend\Model\Menu\Config::CACHE_MENU_OBJECT)
  86. )->will(
  87. $this->returnValue(false)
  88. );
  89. $this->configReaderMock->expects($this->once())->method('read')->will($this->returnValue([]));
  90. $this->menuBuilderMock->expects(
  91. $this->exactly(1)
  92. )->method(
  93. 'getResult'
  94. )->will(
  95. $this->returnValue($this->menuMock)
  96. );
  97. $this->assertEquals($this->menuMock, $this->model->getMenu());
  98. }
  99. /**
  100. * @param string $expectedException
  101. *
  102. * @dataProvider getMenuExceptionLoggedDataProvider
  103. */
  104. public function testGetMenuExceptionLogged($expectedException)
  105. {
  106. $this->expectException($expectedException);
  107. $this->menuBuilderMock->expects(
  108. $this->exactly(1)
  109. )->method(
  110. 'getResult'
  111. )->will(
  112. $this->throwException(new $expectedException())
  113. );
  114. $this->model->getMenu();
  115. }
  116. /**
  117. * @return array
  118. */
  119. public function getMenuExceptionLoggedDataProvider()
  120. {
  121. return [
  122. 'InvalidArgumentException' => ['InvalidArgumentException'],
  123. 'BadMethodCallException' => ['BadMethodCallException'],
  124. 'OutOfRangeException' => ['OutOfRangeException']
  125. ];
  126. }
  127. public function testGetMenuGenericExceptionIsNotLogged()
  128. {
  129. $this->logger->expects($this->never())->method('critical');
  130. $this->menuBuilderMock->expects(
  131. $this->exactly(1)
  132. )->method(
  133. 'getResult'
  134. )->will(
  135. $this->throwException(new \Exception())
  136. );
  137. try {
  138. $this->model->getMenu();
  139. } catch (\Exception $e) {
  140. return;
  141. }
  142. $this->fail("Generic \Exception was not thrown");
  143. }
  144. }