TemplateTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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\Element;
  7. use Magento\Framework\App\Filesystem\DirectoryList;
  8. use Magento\Framework\Filesystem\DriverPool;
  9. use Magento\Framework\UrlInterface;
  10. use Magento\Store\Model\Store;
  11. use Magento\Store\Model\StoreManager;
  12. /**
  13. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  14. */
  15. class TemplateTest extends \PHPUnit\Framework\TestCase
  16. {
  17. /**
  18. * @var \Magento\Framework\View\Element\Template
  19. */
  20. protected $block;
  21. /**
  22. * @var \Magento\Framework\Filesystem|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $filesystem;
  25. /**
  26. * @var \Magento\Framework\View\TemplateEngineInterface|\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $templateEngine;
  29. /**
  30. * @var \Magento\Framework\View\Element\Template\File\Resolver|\PHPUnit_Framework_MockObject_MockObject
  31. */
  32. protected $resolver;
  33. /**
  34. * @var \Magento\Framework\View\Element\Template\File\Validator|\PHPUnit_Framework_MockObject_MockObject
  35. */
  36. protected $validator;
  37. /**
  38. * @var \Magento\Framework\Filesystem\Directory\Read|\PHPUnit_Framework_MockObject_MockObject
  39. */
  40. private $rootDirMock;
  41. /**
  42. * @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
  43. */
  44. protected $loggerMock;
  45. /**
  46. * @var \Magento\Framework\App\State|\PHPUnit_Framework_MockObject_MockObject
  47. */
  48. protected $appState;
  49. protected function setUp()
  50. {
  51. $this->resolver = $this->createMock(\Magento\Framework\View\Element\Template\File\Resolver::class);
  52. $this->validator = $this->createMock(\Magento\Framework\View\Element\Template\File\Validator::class);
  53. $this->rootDirMock = $this->createMock(\Magento\Framework\Filesystem\Directory\Read::class);
  54. $this->rootDirMock->expects($this->any())
  55. ->method('getRelativePath')
  56. ->willReturnArgument(0);
  57. $this->filesystem = $this->createMock(\Magento\Framework\Filesystem::class);
  58. $this->filesystem->expects($this->any())
  59. ->method('getDirectoryRead')
  60. ->with(DirectoryList::ROOT, DriverPool::FILE)
  61. ->willReturn($this->rootDirMock);
  62. $this->templateEngine = $this->createPartialMock(
  63. \Magento\Framework\View\TemplateEnginePool::class,
  64. ['render', 'get']
  65. );
  66. $this->loggerMock = $this->createMock(\Psr\Log\LoggerInterface::class);
  67. $this->templateEngine->expects($this->any())->method('get')->willReturn($this->templateEngine);
  68. $this->appState = $this->createPartialMock(\Magento\Framework\App\State::class, ['getAreaCode', 'getMode']);
  69. $this->appState->expects($this->any())->method('getAreaCode')->willReturn('frontend');
  70. $storeManagerMock = $this->createMock(StoreManager::class);
  71. $storeMock = $this->createMock(Store::class);
  72. $storeManagerMock->expects($this->any())
  73. ->method('getStore')
  74. ->willReturn($storeMock);
  75. $storeMock->expects($this->any())
  76. ->method('getCode')
  77. ->willReturn('storeCode');
  78. $urlBuilderMock = $this->createMock(UrlInterface::class);
  79. $urlBuilderMock->expects($this->any())
  80. ->method('getBaseUrl')
  81. ->willReturn('baseUrl');
  82. $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  83. $this->block = $helper->getObject(
  84. \Magento\Framework\View\Element\Template::class,
  85. [
  86. 'filesystem' => $this->filesystem,
  87. 'enginePool' => $this->templateEngine,
  88. 'resolver' => $this->resolver,
  89. 'validator' => $this->validator,
  90. 'appState' => $this->appState,
  91. 'logger' => $this->loggerMock,
  92. 'storeManager' => $storeManagerMock,
  93. 'urlBuilder' => $urlBuilderMock,
  94. 'data' => ['template' => 'template.phtml', 'module_name' => 'Fixture_Module']
  95. ]
  96. );
  97. }
  98. public function testGetTemplateFile()
  99. {
  100. $params = ['module' => 'Fixture_Module', 'area' => 'frontend'];
  101. $this->resolver->expects($this->once())->method('getTemplateFileName')->with('template.phtml', $params);
  102. $this->block->getTemplateFile();
  103. }
  104. public function testFetchView()
  105. {
  106. $this->expectOutputString('');
  107. $template = 'themedir/template.phtml';
  108. $this->validator->expects($this->once())
  109. ->method('isValid')
  110. ->with($template)
  111. ->willReturn(true);
  112. $output = '<h1>Template Contents</h1>';
  113. $vars = ['var1' => 'value1', 'var2' => 'value2'];
  114. $this->templateEngine->expects($this->once())->method('render')->willReturn($output);
  115. $this->block->assign($vars);
  116. $this->assertEquals($output, $this->block->fetchView($template));
  117. }
  118. public function testFetchViewWithNoFileName()
  119. {
  120. $output = '';
  121. $template = false;
  122. $templatePath = 'wrong_template_path.pthml';
  123. $moduleName = 'Acme';
  124. $blockName = 'acme_test_module_test_block';
  125. $exception = "Invalid template file: '{$templatePath}' in module: '{$moduleName}' block's name: '{$blockName}'";
  126. $this->block->setTemplate($templatePath);
  127. $this->block->setData('module_name', $moduleName);
  128. $this->block->setNameInLayout($blockName);
  129. $this->validator->expects($this->once())
  130. ->method('isValid')
  131. ->with($template)
  132. ->willReturn(false);
  133. $this->loggerMock->expects($this->once())
  134. ->method('critical')
  135. ->with($exception)
  136. ->willReturn(null);
  137. $this->assertEquals($output, $this->block->fetchView($template));
  138. }
  139. public function testFetchViewWithNoFileNameDeveloperMode()
  140. {
  141. $template = false;
  142. $templatePath = 'wrong_template_path.pthml';
  143. $moduleName = 'Acme';
  144. $blockName = 'acme_test_module_test_block';
  145. $exception = "Invalid template file: '{$templatePath}' in module: '{$moduleName}' block's name: '{$blockName}'";
  146. $this->block->setTemplate($templatePath);
  147. $this->block->setData('module_name', $moduleName);
  148. $this->block->setNameInLayout($blockName);
  149. $this->validator->expects($this->once())
  150. ->method('isValid')
  151. ->with($template)
  152. ->willReturn(false);
  153. $this->loggerMock->expects($this->never())
  154. ->method('critical');
  155. $this->appState->expects($this->once())
  156. ->method('getMode')
  157. ->willReturn(\Magento\Framework\App\State::MODE_DEVELOPER);
  158. $this->expectException(\Magento\Framework\Exception\ValidatorException::class);
  159. $this->expectExceptionMessage($exception);
  160. $this->block->fetchView($template);
  161. }
  162. public function testSetTemplateContext()
  163. {
  164. $template = 'themedir/template.phtml';
  165. $context = new \Magento\Framework\DataObject();
  166. $this->validator->expects($this->once())
  167. ->method('isValid')
  168. ->with($template)
  169. ->willReturn(true);
  170. $this->templateEngine->expects($this->once())->method('render')->with($context);
  171. $this->block->setTemplateContext($context);
  172. $this->block->fetchView($template);
  173. }
  174. public function testGetCacheKeyInfo()
  175. {
  176. $this->assertEquals(
  177. [
  178. 'BLOCK_TPL',
  179. 'storeCode',
  180. null,
  181. 'base_url' => 'baseUrl',
  182. 'template' => 'template.phtml',
  183. ],
  184. $this->block->getCacheKeyInfo()
  185. );
  186. }
  187. }