Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ViewTest.php
Go to the documentation of this file.
1 <?php
7 
11 class ViewTest extends \PHPUnit\Framework\TestCase
12 {
16  protected $_view;
17 
21  protected $_layoutMock;
22 
26  protected $_configScopeMock;
27 
31  protected $_requestMock;
32 
36  protected $_layoutProcessor;
37 
41  protected $_actionFlagMock;
42 
46  protected $_eventManagerMock;
47 
51  protected $resultPage;
52 
56  protected $response;
57 
58  protected function setUp()
59  {
60  $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
61  $this->_layoutMock = $this->createMock(\Magento\Framework\View\Layout::class);
62  $this->_requestMock = $this->createMock(\Magento\Framework\App\Request\Http::class);
63  $this->_configScopeMock = $this->createMock(\Magento\Framework\Config\ScopeInterface::class);
64  $this->_layoutProcessor = $this->createMock(\Magento\Framework\View\Model\Layout\Merge::class);
65  $this->_layoutMock->expects($this->any())->method('getUpdate')
66  ->will($this->returnValue($this->_layoutProcessor));
67  $this->_actionFlagMock = $this->createMock(\Magento\Framework\App\ActionFlag::class);
68  $this->_eventManagerMock = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
69  $pageConfigMock = $this->getMockBuilder(
70  \Magento\Framework\View\Page\Config::class
71  )->disableOriginalConstructor()->getMock();
72  $pageConfigMock->expects($this->any())
73  ->method('publicBuild')
74  ->willReturnSelf();
75 
76  $pageConfigRendererFactory = $this->getMockBuilder(\Magento\Framework\View\Page\Config\RendererFactory::class)
77  ->disableOriginalConstructor()
78  ->setMethods(['create'])
79  ->getMock();
80 
81  $this->resultPage = $this->getMockBuilder(\Magento\Framework\View\Result\Page::class)
82  ->setConstructorArgs(
83  $helper->getConstructArguments(\Magento\Framework\View\Result\Page::class, [
84  'request' => $this->_requestMock,
85  'pageConfigRendererFactory' => $pageConfigRendererFactory,
86  'layout' => $this->_layoutMock
87  ])
88  )
89  ->setMethods(['renderResult', 'getConfig'])
90  ->getMock();
91  $this->resultPage->expects($this->any())
92  ->method('getConfig')
93  ->will($this->returnValue($pageConfigMock));
94  $pageFactory = $this->getMockBuilder(\Magento\Framework\View\Result\PageFactory::class)
95  ->disableOriginalConstructor()
96  ->setMethods(['create'])
97  ->getMock();
98  $pageFactory->expects($this->once())
99  ->method('create')
100  ->will($this->returnValue($this->resultPage));
101 
102  $this->response = $this->createMock(\Magento\Framework\App\Response\Http::class);
103 
104  $this->_view = $helper->getObject(
105  \Magento\Framework\App\View::class,
106  [
107  'layout' => $this->_layoutMock,
108  'request' => $this->_requestMock,
109  'response' => $this->response,
110  'configScope' => $this->_configScopeMock,
111  'eventManager' => $this->_eventManagerMock,
112  'actionFlag' => $this->_actionFlagMock,
113  'pageFactory' => $pageFactory
114  ]
115  );
116  }
117 
118  public function testGetLayout()
119  {
120  $this->assertEquals($this->_layoutMock, $this->_view->getLayout());
121  }
122 
128  {
129  $this->_view->setIsLayoutLoaded(true);
130  $this->_view->loadLayout();
131  }
132 
134  {
135  $this->_layoutProcessor->expects($this->at(0))->method('addHandle')->with('default');
136  $this->_requestMock->expects(
137  $this->any()
138  )->method(
139  'getFullActionName'
140  )->will(
141  $this->returnValue('action_name')
142  );
143  $this->_view->loadLayout();
144  }
145 
147  {
148  $this->_view->loadLayout('', false, true);
149  }
150 
152  {
153  $this->_view->loadLayout('', true, false);
154  }
155 
156  public function testGetDefaultLayoutHandle()
157  {
158  $this->_requestMock->expects($this->once())
159  ->method('getFullActionName')
160  ->will($this->returnValue('ExpectedValue'));
161 
162  $this->assertEquals('expectedvalue', $this->_view->getDefaultLayoutHandle());
163  }
164 
166  {
167  $this->_requestMock->expects($this->once())
168  ->method('getFullActionName')
169  ->will($this->returnValue('Full_Action_Name'));
170 
171  $this->_layoutProcessor->expects($this->once())
172  ->method('addHandle')
173  ->with('full_action_name');
174 
175  $this->_view->addActionLayoutHandles();
176  }
177 
178  public function testAddPageLayoutHandles()
179  {
180  $pageHandles = ['full_action_name', 'full_action_name_key_value'];
181  $this->_requestMock->expects($this->once())
182  ->method('getFullActionName')
183  ->will($this->returnValue('Full_Action_Name'));
184 
185  $this->_layoutProcessor->expects($this->once())
186  ->method('addHandle')
187  ->with($pageHandles);
188  $this->_view->addPageLayoutHandles(['key' => 'value']);
189  }
190 
192  {
193  $valueMap = [
196  ];
197  $this->_actionFlagMock->expects($this->any())->method('get')->will($this->returnValueMap($valueMap));
198  $this->_view->generateLayoutBlocks();
199  }
200 
202  {
203  $valueMap = [
206  ];
207  $this->_actionFlagMock->expects($this->any())->method('get')->will($this->returnValueMap($valueMap));
208 
209  $this->_eventManagerMock->expects($this->never())->method('dispatch');
210  $this->_view->generateLayoutBlocks();
211  }
212 
214  {
215  $this->_actionFlagMock->expects(
216  $this->once()
217  )->method(
218  'get'
219  )->with(
220  '',
221  'no-renderLayout'
222  )->will(
223  $this->returnValue(true)
224  );
225  $this->_eventManagerMock->expects($this->never())->method('dispatch');
226  $this->_view->renderLayout();
227  }
228 
230  {
231  $this->_actionFlagMock->expects($this->once())
232  ->method('get')
233  ->with('', 'no-renderLayout')
234  ->will($this->returnValue(false));
235  $this->_layoutMock->expects($this->once())->method('addOutputElement')->with('output');
236  $this->resultPage->expects($this->once())->method('renderResult')->with($this->response);
237  $this->_view->renderLayout('output');
238  }
239 
241  {
242  $this->_actionFlagMock->expects($this->once())
243  ->method('get')
244  ->with('', 'no-renderLayout')
245  ->will($this->returnValue(false));
246 
247  $this->_layoutMock->expects($this->never())->method('addOutputElement');
248  $this->resultPage->expects($this->once())->method('renderResult')->with($this->response);
249  $this->_view->renderLayout();
250  }
251 }
$helper
Definition: iframe.phtml:13