DebugHintsTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Developer\Test\Unit\Model\TemplateEngine\Plugin;
  7. use Magento\Developer\Model\TemplateEngine\Decorator\DebugHintsFactory;
  8. use Magento\Developer\Model\TemplateEngine\Plugin\DebugHints;
  9. use Magento\Store\Model\ScopeInterface;
  10. class DebugHintsTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  14. */
  15. protected $scopeConfigMock;
  16. /**
  17. * @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  18. */
  19. protected $storeManager;
  20. /**
  21. * @var \Magento\Developer\Helper\Data|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. protected $devHelperMock;
  24. /**
  25. * @var DebugHintsFactory | \PHPUnit_Framework_MockObject_MockObject
  26. */
  27. protected $debugHintsFactory;
  28. /**
  29. * @return void
  30. */
  31. protected function setUp()
  32. {
  33. $this->scopeConfigMock = $this->getMockBuilder(\Magento\Framework\App\Config\ScopeConfigInterface::class)
  34. ->getMockForAbstractClass();
  35. $this->storeManager = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
  36. ->getMockForAbstractClass();
  37. $this->devHelperMock = $this->getMockBuilder(\Magento\Developer\Helper\Data::class)
  38. ->disableOriginalConstructor()
  39. ->getMock();
  40. $this->debugHintsFactory = $this->getMockBuilder(
  41. \Magento\Developer\Model\TemplateEngine\Decorator\DebugHintsFactory::class
  42. )
  43. ->setMethods(['create'])
  44. ->disableOriginalConstructor()
  45. ->getMock();
  46. }
  47. /**
  48. * @param string $debugHintsPath
  49. * @param bool $showBlockHints
  50. * @return void
  51. * @dataProvider afterCreateActiveDataProvider
  52. */
  53. public function testAfterCreateActive(
  54. $debugHintsPath,
  55. $showBlockHints,
  56. $debugHintsWithParam,
  57. $debugHintsParameter
  58. ) {
  59. $this->devHelperMock->expects($this->once())
  60. ->method('isDevAllowed')
  61. ->willReturn(true);
  62. $this->setupConfigFixture($debugHintsPath, true, $showBlockHints);
  63. $engine = $this->createMock(\Magento\Framework\View\TemplateEngineInterface::class);
  64. $debugHintsDecorator = $this->getMockBuilder(
  65. \Magento\Developer\Model\TemplateEngine\Decorator\DebugHints::class
  66. )
  67. ->disableOriginalConstructor()
  68. ->getMock();
  69. $this->debugHintsFactory->expects($this->once())
  70. ->method('create')
  71. ->with([
  72. 'subject' => $engine,
  73. 'showBlockHints' => $showBlockHints,
  74. ])
  75. ->willReturn($debugHintsDecorator);
  76. $subjectMock = $this->getMockBuilder(\Magento\Framework\View\TemplateEngineFactory::class)
  77. ->disableOriginalConstructor()
  78. ->getMock();
  79. $this->httpMock = $this->createMock(\Magento\Framework\App\Request\Http::class);
  80. $debugHints = new DebugHints(
  81. $this->scopeConfigMock,
  82. $this->storeManager,
  83. $this->devHelperMock,
  84. $this->debugHintsFactory,
  85. $debugHintsPath,
  86. $this->httpMock,
  87. $debugHintsWithParam,
  88. $debugHintsParameter
  89. );
  90. $this->assertEquals($debugHintsDecorator, $debugHints->afterCreate($subjectMock, $engine));
  91. }
  92. /**
  93. * @return array
  94. */
  95. public function afterCreateActiveDataProvider()
  96. {
  97. return [
  98. ['dev/debug/template_hints_storefront', false, false, null],
  99. ['dev/debug/template_hints_storefront', true, false, null],
  100. ['dev/debug/template_hints_admin', false, false, null],
  101. ['dev/debug/template_hints_admin', true, false, null],
  102. ];
  103. }
  104. /**
  105. * @param string $debugHintsPath
  106. * @param bool $isDevAllowed
  107. * @param bool $showTemplateHints
  108. * @return void
  109. * @dataProvider afterCreateInactiveDataProvider
  110. */
  111. public function testAfterCreateInactive(
  112. $debugHintsPath,
  113. $isDevAllowed,
  114. $showTemplateHints,
  115. $debugHintsWithParam,
  116. $debugHintsParameter
  117. ) {
  118. $this->devHelperMock->expects($this->any())
  119. ->method('isDevAllowed')
  120. ->willReturn($isDevAllowed);
  121. $this->setupConfigFixture($debugHintsPath, $showTemplateHints, true);
  122. $engine = $this->createMock(\Magento\Framework\View\TemplateEngineInterface::class);
  123. $subjectMock = $this->getMockBuilder(\Magento\Framework\View\TemplateEngineFactory::class)
  124. ->disableOriginalConstructor()
  125. ->getMock();
  126. $this->httpMock = $this->createMock(\Magento\Framework\App\Request\Http::class);
  127. $debugHints = new DebugHints(
  128. $this->scopeConfigMock,
  129. $this->storeManager,
  130. $this->devHelperMock,
  131. $this->debugHintsFactory,
  132. $debugHintsPath,
  133. $this->httpMock,
  134. $debugHintsWithParam,
  135. $debugHintsParameter
  136. );
  137. $this->assertSame($engine, $debugHints->afterCreate($subjectMock, $engine));
  138. }
  139. /**
  140. * @return array
  141. */
  142. public function afterCreateInactiveDataProvider()
  143. {
  144. return [
  145. ['dev/debug/template_hints_storefront', false, false, false, null],
  146. ['dev/debug/template_hints_storefront', false, true, false, null],
  147. ['dev/debug/template_hints_storefront', true, false, false, null],
  148. ['dev/debug/template_hints_admin', false, false, false, null],
  149. ['dev/debug/template_hints_admin', false, true, false, null],
  150. ['dev/debug/template_hints_admin', true, false, false, null],
  151. ];
  152. }
  153. /**
  154. * Setup fixture values for store config
  155. *
  156. * @param string $debugHintsPath
  157. * @param bool $showTemplateHints
  158. * @param bool $showBlockHints
  159. * @return void
  160. */
  161. protected function setupConfigFixture($debugHintsPath, $showTemplateHints, $showBlockHints)
  162. {
  163. $storeCode = 'default';
  164. $storeMock = $this->createMock(\Magento\Store\Api\Data\StoreInterface::class);
  165. $storeMock->expects($this->once())
  166. ->method('getCode')
  167. ->willReturn($storeCode);
  168. $this->storeManager->expects($this->once())
  169. ->method('getStore')
  170. ->willReturn($storeMock);
  171. $this->scopeConfigMock->expects($this->atLeastOnce())
  172. ->method('getValue')
  173. ->willReturnMap([
  174. [
  175. $debugHintsPath,
  176. ScopeInterface::SCOPE_STORE,
  177. $storeCode,
  178. $showTemplateHints,
  179. ],
  180. [
  181. DebugHints::XML_PATH_DEBUG_TEMPLATE_HINTS_BLOCKS,
  182. ScopeInterface::SCOPE_STORE,
  183. $storeCode,
  184. $showBlockHints
  185. ]
  186. ]);
  187. }
  188. }