ConfigTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\App\Test\Unit\Route;
  7. class ConfigTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \Magento\Framework\App\Route\Config
  11. */
  12. protected $_config;
  13. /**
  14. * @var \Magento\Framework\App\Route\Config\Reader|\PHPUnit_Framework_MockObject_MockObject
  15. */
  16. protected $_readerMock;
  17. /**
  18. * @var \PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $_cacheMock;
  21. /**
  22. * @var \PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $_configScopeMock;
  25. /**
  26. * @var \PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $_areaList;
  29. /**
  30. * @var \Magento\Framework\Serialize\SerializerInterface|\PHPUnit_Framework_MockObject_MockObject
  31. */
  32. private $serializerMock;
  33. protected function setUp()
  34. {
  35. $this->_readerMock = $this->createMock(\Magento\Framework\App\Route\Config\Reader::class);
  36. $this->_cacheMock = $this->createMock(\Magento\Framework\Config\CacheInterface::class);
  37. $this->_configScopeMock = $this->createMock(\Magento\Framework\Config\ScopeInterface::class);
  38. $this->_areaList = $this->createMock(\Magento\Framework\App\AreaList::class);
  39. $this->_configScopeMock->expects($this->any())
  40. ->method('getCurrentScope')
  41. ->willReturn('areaCode');
  42. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  43. $this->_config = $objectManager->getObject(
  44. \Magento\Framework\App\Route\Config::class,
  45. [
  46. 'reader' => $this->_readerMock,
  47. 'cache' => $this->_cacheMock,
  48. 'configScope' => $this->_configScopeMock,
  49. 'areaList' => $this->_areaList
  50. ]
  51. );
  52. $this->serializerMock = $this->createMock(\Magento\Framework\Serialize\SerializerInterface::class);
  53. $objectManager->setBackwardCompatibleProperty($this->_config, 'serializer', $this->serializerMock);
  54. }
  55. public function testGetRouteFrontNameIfCacheIfRouterIdNotExist()
  56. {
  57. $this->_cacheMock->expects($this->once())
  58. ->method('load')
  59. ->with('areaCode::RoutesConfig')
  60. ->willReturn('["expected"]');
  61. $this->assertEquals('routerCode', $this->_config->getRouteFrontName('routerCode'));
  62. }
  63. public function testGetRouteByFrontName()
  64. {
  65. $data = ['routerCode' => ['frontName' => 'routerName']];
  66. $this->_cacheMock->expects($this->once())
  67. ->method('load')
  68. ->with('areaCode::RoutesConfig')
  69. ->willReturn('serializedData');
  70. $this->serializerMock->expects($this->once())
  71. ->method('unserialize')
  72. ->with('serializedData')
  73. ->willReturn($data);
  74. $this->assertEquals('routerCode', $this->_config->getRouteByFrontName('routerName'));
  75. }
  76. public function testGetRouteByFrontNameNoRoutes()
  77. {
  78. $this->_cacheMock->expects($this->once())
  79. ->method('load')
  80. ->with('areaCode::RoutesConfig')
  81. ->willReturn('serializedData');
  82. $this->serializerMock->expects($this->once())
  83. ->method('unserialize')
  84. ->with('serializedData')
  85. ->willReturn([]);
  86. $this->assertFalse($this->_config->getRouteByFrontName('routerName'));
  87. }
  88. public function testGetRouteByFrontNameNoCache()
  89. {
  90. $this->_cacheMock->expects($this->once())
  91. ->method('load')
  92. ->with('scope::RoutesConfig')
  93. ->willReturn('false');
  94. $routes = [
  95. 'routerCode' => [
  96. 'frontName' => 'routerName',
  97. ],
  98. ];
  99. $routers = [
  100. 'default_router' => [
  101. 'routes' => $routes,
  102. ],
  103. ];
  104. $serializedData = json_encode($routes);
  105. $this->_readerMock->expects(
  106. $this->once()
  107. )->method(
  108. 'read'
  109. )->with(
  110. 'scope'
  111. )->will(
  112. $this->returnValue($routers)
  113. );
  114. $this->_areaList->expects(
  115. $this->once()
  116. )->method(
  117. 'getDefaultRouter'
  118. )->with(
  119. 'scope'
  120. )->will(
  121. $this->returnValue('default_router')
  122. );
  123. $this->serializerMock->expects($this->once())
  124. ->method('serialize')
  125. ->willReturn($serializedData);
  126. $this->_cacheMock->expects($this->once())
  127. ->method('save')
  128. ->with($serializedData, 'scope::RoutesConfig');
  129. $this->assertEquals('routerCode', $this->_config->getRouteByFrontName('routerName', 'scope'));
  130. }
  131. public function testGetModulesByFrontName()
  132. {
  133. $data = ['routerCode' => ['frontName' => 'routerName', 'modules' => ['Module1']]];
  134. $this->_cacheMock->expects($this->once())
  135. ->method('load')
  136. ->with('areaCode::RoutesConfig')
  137. ->willReturn('serializedData');
  138. $this->serializerMock->expects($this->once())
  139. ->method('unserialize')
  140. ->with('serializedData')
  141. ->willReturn($data);
  142. $this->assertEquals(['Module1'], $this->_config->getModulesByFrontName('routerName'));
  143. }
  144. }