123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Framework\App\Test\Unit\Route;
- class ConfigTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var \Magento\Framework\App\Route\Config
- */
- protected $_config;
- /**
- * @var \Magento\Framework\App\Route\Config\Reader|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $_readerMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $_cacheMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $_configScopeMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $_areaList;
- /**
- * @var \Magento\Framework\Serialize\SerializerInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $serializerMock;
- protected function setUp()
- {
- $this->_readerMock = $this->createMock(\Magento\Framework\App\Route\Config\Reader::class);
- $this->_cacheMock = $this->createMock(\Magento\Framework\Config\CacheInterface::class);
- $this->_configScopeMock = $this->createMock(\Magento\Framework\Config\ScopeInterface::class);
- $this->_areaList = $this->createMock(\Magento\Framework\App\AreaList::class);
- $this->_configScopeMock->expects($this->any())
- ->method('getCurrentScope')
- ->willReturn('areaCode');
- $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
- $this->_config = $objectManager->getObject(
- \Magento\Framework\App\Route\Config::class,
- [
- 'reader' => $this->_readerMock,
- 'cache' => $this->_cacheMock,
- 'configScope' => $this->_configScopeMock,
- 'areaList' => $this->_areaList
- ]
- );
- $this->serializerMock = $this->createMock(\Magento\Framework\Serialize\SerializerInterface::class);
- $objectManager->setBackwardCompatibleProperty($this->_config, 'serializer', $this->serializerMock);
- }
- public function testGetRouteFrontNameIfCacheIfRouterIdNotExist()
- {
- $this->_cacheMock->expects($this->once())
- ->method('load')
- ->with('areaCode::RoutesConfig')
- ->willReturn('["expected"]');
- $this->assertEquals('routerCode', $this->_config->getRouteFrontName('routerCode'));
- }
- public function testGetRouteByFrontName()
- {
- $data = ['routerCode' => ['frontName' => 'routerName']];
- $this->_cacheMock->expects($this->once())
- ->method('load')
- ->with('areaCode::RoutesConfig')
- ->willReturn('serializedData');
- $this->serializerMock->expects($this->once())
- ->method('unserialize')
- ->with('serializedData')
- ->willReturn($data);
- $this->assertEquals('routerCode', $this->_config->getRouteByFrontName('routerName'));
- }
- public function testGetRouteByFrontNameNoRoutes()
- {
- $this->_cacheMock->expects($this->once())
- ->method('load')
- ->with('areaCode::RoutesConfig')
- ->willReturn('serializedData');
- $this->serializerMock->expects($this->once())
- ->method('unserialize')
- ->with('serializedData')
- ->willReturn([]);
- $this->assertFalse($this->_config->getRouteByFrontName('routerName'));
- }
- public function testGetRouteByFrontNameNoCache()
- {
- $this->_cacheMock->expects($this->once())
- ->method('load')
- ->with('scope::RoutesConfig')
- ->willReturn('false');
- $routes = [
- 'routerCode' => [
- 'frontName' => 'routerName',
- ],
- ];
- $routers = [
- 'default_router' => [
- 'routes' => $routes,
- ],
- ];
- $serializedData = json_encode($routes);
- $this->_readerMock->expects(
- $this->once()
- )->method(
- 'read'
- )->with(
- 'scope'
- )->will(
- $this->returnValue($routers)
- );
- $this->_areaList->expects(
- $this->once()
- )->method(
- 'getDefaultRouter'
- )->with(
- 'scope'
- )->will(
- $this->returnValue('default_router')
- );
- $this->serializerMock->expects($this->once())
- ->method('serialize')
- ->willReturn($serializedData);
- $this->_cacheMock->expects($this->once())
- ->method('save')
- ->with($serializedData, 'scope::RoutesConfig');
- $this->assertEquals('routerCode', $this->_config->getRouteByFrontName('routerName', 'scope'));
- }
- public function testGetModulesByFrontName()
- {
- $data = ['routerCode' => ['frontName' => 'routerName', 'modules' => ['Module1']]];
- $this->_cacheMock->expects($this->once())
- ->method('load')
- ->with('areaCode::RoutesConfig')
- ->willReturn('serializedData');
- $this->serializerMock->expects($this->once())
- ->method('unserialize')
- ->with('serializedData')
- ->willReturn($data);
- $this->assertEquals(['Module1'], $this->_config->getModulesByFrontName('routerName'));
- }
- }
|