LayoutTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View\Test\Unit\Result;
  7. /**
  8. * Class LayoutTest
  9. * @covers \Magento\Framework\View\Result\Layout
  10. */
  11. class LayoutTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var \Magento\Framework\App\Request\Http|\PHPUnit_Framework_MockObject_MockObject
  15. */
  16. protected $request;
  17. /**
  18. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Event\ManagerInterface
  19. */
  20. protected $eventManager;
  21. /**
  22. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\View\Layout
  23. */
  24. protected $layout;
  25. /**
  26. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Translate\InlineInterface
  27. */
  28. protected $translateInline;
  29. /**
  30. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\View\Result\Layout
  31. */
  32. protected $resultLayout;
  33. protected function setUp()
  34. {
  35. $this->layout = $this->createMock(\Magento\Framework\View\Layout::class);
  36. $this->request = $this->createMock(\Magento\Framework\App\Request\Http::class);
  37. $this->eventManager = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
  38. $this->translateInline = $this->createMock(\Magento\Framework\Translate\InlineInterface::class);
  39. $context = $this->createMock(\Magento\Framework\View\Element\Template\Context::class);
  40. $context->expects($this->any())->method('getLayout')->will($this->returnValue($this->layout));
  41. $context->expects($this->any())->method('getRequest')->will($this->returnValue($this->request));
  42. $context->expects($this->any())->method('getEventManager')->will($this->returnValue($this->eventManager));
  43. $this->resultLayout = (new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this))
  44. ->getObject(
  45. \Magento\Framework\View\Result\Layout::class,
  46. ['context' => $context, 'translateInline' => $this->translateInline]
  47. );
  48. }
  49. /**
  50. * @covers \Magento\Framework\View\Result\Layout::getLayout()
  51. */
  52. public function testGetLayout()
  53. {
  54. $this->assertSame($this->layout, $this->resultLayout->getLayout());
  55. }
  56. public function testGetDefaultLayoutHandle()
  57. {
  58. $this->request->expects($this->once())
  59. ->method('getFullActionName')
  60. ->willReturn('Module_Controller_Action');
  61. $this->assertEquals('module_controller_action', $this->resultLayout->getDefaultLayoutHandle());
  62. }
  63. public function testAddHandle()
  64. {
  65. $processor = $this->createMock(\Magento\Framework\View\Layout\ProcessorInterface::class);
  66. $processor->expects($this->once())->method('addHandle')->with('module_controller_action');
  67. $this->layout->expects($this->once())->method('getUpdate')->will($this->returnValue($processor));
  68. $this->assertSame($this->resultLayout, $this->resultLayout->addHandle('module_controller_action'));
  69. }
  70. public function testAddUpdate()
  71. {
  72. $processor = $this->createMock(\Magento\Framework\View\Layout\ProcessorInterface::class);
  73. $processor->expects($this->once())->method('addUpdate')->with('handle_name');
  74. $this->layout->expects($this->once())->method('getUpdate')->will($this->returnValue($processor));
  75. $this->resultLayout->addUpdate('handle_name');
  76. }
  77. /**
  78. * @param int|string $httpCode
  79. * @param string $headerName
  80. * @param string $headerValue
  81. * @param bool $replaceHeader
  82. * @param \PHPUnit\Framework\MockObject\Matcher\InvokedCount $setHttpResponseCodeCount
  83. * @param \PHPUnit\Framework\MockObject\Matcher\InvokedCount $setHeaderCount
  84. * @dataProvider renderResultDataProvider
  85. */
  86. public function testRenderResult(
  87. $httpCode,
  88. $headerName,
  89. $headerValue,
  90. $replaceHeader,
  91. $setHttpResponseCodeCount,
  92. $setHeaderCount
  93. ) {
  94. $layoutOutput = 'output';
  95. $this->layout->expects($this->once())->method('getOutput')->will($this->returnValue($layoutOutput));
  96. $this->request->expects($this->once())->method('getFullActionName')
  97. ->will($this->returnValue('Module_Controller_Action'));
  98. $this->eventManager->expects($this->exactly(2))->method('dispatch')->withConsecutive(
  99. ['layout_render_before'],
  100. ['layout_render_before_Module_Controller_Action']
  101. );
  102. $this->translateInline->expects($this->once())
  103. ->method('processResponseBody')
  104. ->with($layoutOutput)
  105. ->willReturnSelf();
  106. /** @var \Magento\Framework\App\Response\Http|\PHPUnit_Framework_MockObject_MockObject $response */
  107. $response = $this->createMock(\Magento\Framework\App\Response\Http::class);
  108. $response->expects($setHttpResponseCodeCount)->method('setHttpResponseCode')->with($httpCode);
  109. $response->expects($setHeaderCount)->method('setHeader')->with($headerName, $headerValue, $replaceHeader);
  110. $response->expects($this->once())->method('appendBody')->with($layoutOutput);
  111. $this->resultLayout->setHttpResponseCode($httpCode);
  112. if ($headerName && $headerValue) {
  113. $this->resultLayout->setHeader($headerName, $headerValue, $replaceHeader);
  114. }
  115. $this->resultLayout->renderResult($response);
  116. }
  117. /**
  118. * @return array
  119. */
  120. public function renderResultDataProvider()
  121. {
  122. return [
  123. [200, 'content-type', 'text/html', true, $this->once(), $this->once()],
  124. [0, '', '', false, $this->never(), $this->never()]
  125. ];
  126. }
  127. public function testAddDefaultHandle()
  128. {
  129. $processor = $this->createMock(\Magento\Framework\View\Layout\ProcessorInterface::class);
  130. $processor->expects($this->once())->method('addHandle')->with('module_controller_action');
  131. $this->layout->expects($this->once())->method('getUpdate')->will($this->returnValue($processor));
  132. $this->request->expects($this->once())->method('getFullActionName')
  133. ->will($this->returnValue('Module_Controller_Action'));
  134. $this->assertSame($this->resultLayout, $this->resultLayout->addDefaultHandle());
  135. }
  136. }