123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Framework\App\Test\Unit;
- /**
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class ViewTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var \Magento\Framework\App\View
- */
- protected $_view;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $_layoutMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $_configScopeMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $_requestMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $_layoutProcessor;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $_actionFlagMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $_eventManagerMock;
- /**
- * @var \Magento\Framework\View\Result\Page|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $resultPage;
- /**
- * @var \Magento\Framework\App\Response\Http|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $response;
- protected function setUp()
- {
- $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
- $this->_layoutMock = $this->createMock(\Magento\Framework\View\Layout::class);
- $this->_requestMock = $this->createMock(\Magento\Framework\App\Request\Http::class);
- $this->_configScopeMock = $this->createMock(\Magento\Framework\Config\ScopeInterface::class);
- $this->_layoutProcessor = $this->createMock(\Magento\Framework\View\Model\Layout\Merge::class);
- $this->_layoutMock->expects($this->any())->method('getUpdate')
- ->will($this->returnValue($this->_layoutProcessor));
- $this->_actionFlagMock = $this->createMock(\Magento\Framework\App\ActionFlag::class);
- $this->_eventManagerMock = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
- $pageConfigMock = $this->getMockBuilder(
- \Magento\Framework\View\Page\Config::class
- )->disableOriginalConstructor()->getMock();
- $pageConfigMock->expects($this->any())
- ->method('publicBuild')
- ->willReturnSelf();
- $pageConfigRendererFactory = $this->getMockBuilder(\Magento\Framework\View\Page\Config\RendererFactory::class)
- ->disableOriginalConstructor()
- ->setMethods(['create'])
- ->getMock();
- $this->resultPage = $this->getMockBuilder(\Magento\Framework\View\Result\Page::class)
- ->setConstructorArgs(
- $helper->getConstructArguments(\Magento\Framework\View\Result\Page::class, [
- 'request' => $this->_requestMock,
- 'pageConfigRendererFactory' => $pageConfigRendererFactory,
- 'layout' => $this->_layoutMock
- ])
- )
- ->setMethods(['renderResult', 'getConfig'])
- ->getMock();
- $this->resultPage->expects($this->any())
- ->method('getConfig')
- ->will($this->returnValue($pageConfigMock));
- $pageFactory = $this->getMockBuilder(\Magento\Framework\View\Result\PageFactory::class)
- ->disableOriginalConstructor()
- ->setMethods(['create'])
- ->getMock();
- $pageFactory->expects($this->once())
- ->method('create')
- ->will($this->returnValue($this->resultPage));
- $this->response = $this->createMock(\Magento\Framework\App\Response\Http::class);
- $this->_view = $helper->getObject(
- \Magento\Framework\App\View::class,
- [
- 'layout' => $this->_layoutMock,
- 'request' => $this->_requestMock,
- 'response' => $this->response,
- 'configScope' => $this->_configScopeMock,
- 'eventManager' => $this->_eventManagerMock,
- 'actionFlag' => $this->_actionFlagMock,
- 'pageFactory' => $pageFactory
- ]
- );
- }
- public function testGetLayout()
- {
- $this->assertEquals($this->_layoutMock, $this->_view->getLayout());
- }
- /**
- * @expectedException \RuntimeException
- * @expectedExceptionMessage Layout must be loaded only once.
- */
- public function testLoadLayoutWhenLayoutAlreadyLoaded()
- {
- $this->_view->setIsLayoutLoaded(true);
- $this->_view->loadLayout();
- }
- public function testLoadLayoutWithDefaultSetup()
- {
- $this->_layoutProcessor->expects($this->at(0))->method('addHandle')->with('default');
- $this->_requestMock->expects(
- $this->any()
- )->method(
- 'getFullActionName'
- )->will(
- $this->returnValue('action_name')
- );
- $this->_view->loadLayout();
- }
- public function testLoadLayoutWhenBlocksNotGenerated()
- {
- $this->_view->loadLayout('', false, true);
- }
- public function testLoadLayoutWhenXmlNotGenerated()
- {
- $this->_view->loadLayout('', true, false);
- }
- public function testGetDefaultLayoutHandle()
- {
- $this->_requestMock->expects($this->once())
- ->method('getFullActionName')
- ->will($this->returnValue('ExpectedValue'));
- $this->assertEquals('expectedvalue', $this->_view->getDefaultLayoutHandle());
- }
- public function testAddActionLayoutHandlesWhenPageLayoutHandlesExist()
- {
- $this->_requestMock->expects($this->once())
- ->method('getFullActionName')
- ->will($this->returnValue('Full_Action_Name'));
- $this->_layoutProcessor->expects($this->once())
- ->method('addHandle')
- ->with('full_action_name');
- $this->_view->addActionLayoutHandles();
- }
- public function testAddPageLayoutHandles()
- {
- $pageHandles = ['full_action_name', 'full_action_name_key_value'];
- $this->_requestMock->expects($this->once())
- ->method('getFullActionName')
- ->will($this->returnValue('Full_Action_Name'));
- $this->_layoutProcessor->expects($this->once())
- ->method('addHandle')
- ->with($pageHandles);
- $this->_view->addPageLayoutHandles(['key' => 'value']);
- }
- public function testGenerateLayoutBlocksWhenFlagIsNotSet()
- {
- $valueMap = [
- ['', \Magento\Framework\App\ActionInterface::FLAG_NO_DISPATCH_BLOCK_EVENT, false],
- ['', \Magento\Framework\App\ActionInterface::FLAG_NO_DISPATCH_BLOCK_EVENT, false],
- ];
- $this->_actionFlagMock->expects($this->any())->method('get')->will($this->returnValueMap($valueMap));
- $this->_view->generateLayoutBlocks();
- }
- public function testGenerateLayoutBlocksWhenFlagIsSet()
- {
- $valueMap = [
- ['', \Magento\Framework\App\ActionInterface::FLAG_NO_DISPATCH_BLOCK_EVENT, true],
- ['', \Magento\Framework\App\ActionInterface::FLAG_NO_DISPATCH_BLOCK_EVENT, true],
- ];
- $this->_actionFlagMock->expects($this->any())->method('get')->will($this->returnValueMap($valueMap));
- $this->_eventManagerMock->expects($this->never())->method('dispatch');
- $this->_view->generateLayoutBlocks();
- }
- public function testRenderLayoutIfActionFlagExist()
- {
- $this->_actionFlagMock->expects(
- $this->once()
- )->method(
- 'get'
- )->with(
- '',
- 'no-renderLayout'
- )->will(
- $this->returnValue(true)
- );
- $this->_eventManagerMock->expects($this->never())->method('dispatch');
- $this->_view->renderLayout();
- }
- public function testRenderLayoutWhenOutputNotEmpty()
- {
- $this->_actionFlagMock->expects($this->once())
- ->method('get')
- ->with('', 'no-renderLayout')
- ->will($this->returnValue(false));
- $this->_layoutMock->expects($this->once())->method('addOutputElement')->with('output');
- $this->resultPage->expects($this->once())->method('renderResult')->with($this->response);
- $this->_view->renderLayout('output');
- }
- public function testRenderLayoutWhenOutputEmpty()
- {
- $this->_actionFlagMock->expects($this->once())
- ->method('get')
- ->with('', 'no-renderLayout')
- ->will($this->returnValue(false));
- $this->_layoutMock->expects($this->never())->method('addOutputElement');
- $this->resultPage->expects($this->once())->method('renderResult')->with($this->response);
- $this->_view->renderLayout();
- }
- }
|