StaticResourceTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\App\Test\Unit;
  7. use Magento\Framework\App\Bootstrap;
  8. use Magento\Framework\App\DeploymentConfig;
  9. use Magento\Framework\App\State;
  10. use Magento\Framework\App\Response\FileInterface;
  11. use Magento\Framework\App\Request\Http as HttpRequest;
  12. use Magento\Framework\App\View\Asset\Publisher;
  13. use Magento\Framework\View\Asset\Repository;
  14. use Magento\Framework\Module\ModuleList;
  15. use Magento\Framework\ObjectManagerInterface;
  16. use Magento\Framework\App\ObjectManager\ConfigLoader;
  17. use Magento\Framework\App\StaticResource;
  18. use Magento\Framework\Config\ConfigOptionsListConstants;
  19. use Psr\Log\LoggerInterface;
  20. use PHPUnit_Framework_MockObject_MockObject as MockObject;
  21. /**
  22. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  23. */
  24. class StaticResourceTest extends \PHPUnit\Framework\TestCase
  25. {
  26. /**
  27. * @var State|MockObject
  28. */
  29. private $stateMock;
  30. /**
  31. * @var FileInterface|MockObject
  32. */
  33. private $responseMock;
  34. /**
  35. * @var HttpRequest|MockObject
  36. */
  37. private $requestMock;
  38. /**
  39. * @var Publisher|MockObject
  40. */
  41. private $publisherMock;
  42. /**
  43. * @var Repository|MockObject
  44. */
  45. private $assetRepoMock;
  46. /**
  47. * @var ModuleList|MockObject
  48. */
  49. private $moduleListMock;
  50. /**
  51. * @var ObjectManagerInterface|MockObject
  52. */
  53. private $objectManagerMock;
  54. /**
  55. * @var ConfigLoader|MockObject
  56. */
  57. private $configLoaderMock;
  58. /**
  59. * @var LoggerInterface|MockObject
  60. */
  61. private $loggerMock;
  62. /**
  63. * @var DeploymentConfig|MockObject
  64. */
  65. private $deploymentConfigMock;
  66. /**
  67. * @var StaticResource
  68. */
  69. private $object;
  70. protected function setUp()
  71. {
  72. $this->stateMock = $this->createMock(State::class);
  73. $this->responseMock = $this->getMockForAbstractClass(FileInterface::class);
  74. $this->requestMock = $this->createMock(HttpRequest::class);
  75. $this->publisherMock = $this->createMock(Publisher::class);
  76. $this->assetRepoMock = $this->createMock(Repository::class);
  77. $this->moduleListMock = $this->createMock(ModuleList::class);
  78. $this->objectManagerMock = $this->getMockForAbstractClass(ObjectManagerInterface::class);
  79. $this->loggerMock = $this->getMockForAbstractClass(LoggerInterface::class);
  80. $this->configLoaderMock = $this->createMock(ConfigLoader::class);
  81. $this->deploymentConfigMock = $this->createMock(DeploymentConfig::class);
  82. $this->object = new StaticResource(
  83. $this->stateMock,
  84. $this->responseMock,
  85. $this->requestMock,
  86. $this->publisherMock,
  87. $this->assetRepoMock,
  88. $this->moduleListMock,
  89. $this->objectManagerMock,
  90. $this->configLoaderMock,
  91. $this->deploymentConfigMock
  92. );
  93. }
  94. public function testLaunchProductionMode()
  95. {
  96. $this->stateMock->expects($this->once())
  97. ->method('getMode')
  98. ->willReturn(State::MODE_PRODUCTION);
  99. $this->responseMock->expects($this->once())
  100. ->method('setHttpResponseCode')
  101. ->with(404);
  102. $this->responseMock->expects($this->never())
  103. ->method('setFilePath');
  104. $this->stateMock->expects($this->never())->method('setAreaCode');
  105. $this->configLoaderMock->expects($this->never())->method('load');
  106. $this->objectManagerMock->expects($this->never())->method('configure');
  107. $this->requestMock->expects($this->never())->method('get');
  108. $this->moduleListMock->expects($this->never())->method('has');
  109. $asset = $this->getMockForAbstractClass(\Magento\Framework\View\Asset\LocalInterface::class);
  110. $asset->expects($this->never())->method('getSourceFile');
  111. $this->assetRepoMock->expects($this->never())->method('createAsset');
  112. $this->publisherMock->expects($this->never())->method('publish');
  113. $this->responseMock->expects($this->never())->method('setFilePath');
  114. $this->object->launch();
  115. }
  116. /**
  117. * @param string $mode
  118. * @param string $requestedPath
  119. * @param string $requestedModule
  120. * @param bool $moduleExists
  121. * @param string $expectedFile
  122. * @param array $expectedParams
  123. * @param int $getConfigDataExpects
  124. * @param int $staticContentOmDemandInProduction
  125. *
  126. * @dataProvider launchDataProvider
  127. */
  128. public function testLaunch(
  129. $mode,
  130. $requestedPath,
  131. $requestedModule,
  132. $moduleExists,
  133. $expectedFile,
  134. array $expectedParams,
  135. $getConfigDataExpects,
  136. $staticContentOmDemandInProduction
  137. ) {
  138. $this->deploymentConfigMock->expects($this->exactly($getConfigDataExpects))
  139. ->method('getConfigData')
  140. ->with(ConfigOptionsListConstants::CONFIG_PATH_SCD_ON_DEMAND_IN_PRODUCTION)
  141. ->willReturn($staticContentOmDemandInProduction);
  142. $this->stateMock->expects($this->once())
  143. ->method('getMode')
  144. ->willReturn($mode);
  145. $this->stateMock->expects($this->once())
  146. ->method('setAreaCode')
  147. ->with('area');
  148. $this->configLoaderMock->expects($this->once())
  149. ->method('load')
  150. ->with('area')
  151. ->willReturn(['config']);
  152. $this->objectManagerMock->expects($this->once())
  153. ->method('configure')
  154. ->with(['config']);
  155. $this->requestMock->expects($this->once())
  156. ->method('get')
  157. ->with('resource')
  158. ->willReturn($requestedPath);
  159. $this->moduleListMock->expects($this->any())
  160. ->method('has')
  161. ->with($requestedModule)
  162. ->willReturn($moduleExists);
  163. $asset = $this->getMockForAbstractClass(\Magento\Framework\View\Asset\LocalInterface::class);
  164. $asset->expects($this->once())
  165. ->method('getSourceFile')
  166. ->willReturn('resource/file.css');
  167. $this->assetRepoMock->expects($this->once())
  168. ->method('createAsset')
  169. ->with($expectedFile, $expectedParams)
  170. ->willReturn($asset);
  171. $this->publisherMock->expects($this->once())
  172. ->method('publish')
  173. ->with($asset);
  174. $this->responseMock->expects($this->once())
  175. ->method('setFilePath')
  176. ->with('resource/file.css');
  177. $this->object->launch();
  178. }
  179. /**
  180. * @return array
  181. */
  182. public function launchDataProvider()
  183. {
  184. return [
  185. 'developer mode with non-modular resource' => [
  186. \Magento\Framework\App\State::MODE_DEVELOPER,
  187. 'area/Magento/theme/locale/dir/file.js',
  188. 'dir',
  189. false,
  190. 'dir/file.js',
  191. ['area' => 'area', 'locale' => 'locale', 'module' => '', 'theme' => 'Magento/theme'],
  192. 0,
  193. 0,
  194. ],
  195. 'default mode with modular resource' => [
  196. \Magento\Framework\App\State::MODE_DEFAULT,
  197. 'area/Magento/theme/locale/Namespace_Module/dir/file.js',
  198. 'Namespace_Module',
  199. true,
  200. 'dir/file.js',
  201. [
  202. 'area' => 'area', 'locale' => 'locale', 'module' => 'Namespace_Module', 'theme' => 'Magento/theme'
  203. ],
  204. 0,
  205. 0,
  206. ],
  207. 'production mode with static_content_on_demand_in_production and with non-modular resource' => [
  208. \Magento\Framework\App\State::MODE_PRODUCTION,
  209. 'area/Magento/theme/locale/dir/file.js',
  210. 'dir',
  211. false,
  212. 'dir/file.js',
  213. ['area' => 'area', 'locale' => 'locale', 'module' => '', 'theme' => 'Magento/theme'],
  214. 1,
  215. 1,
  216. ],
  217. 'production mode with static_content_on_demand_in_production and with modular resource' => [
  218. \Magento\Framework\App\State::MODE_PRODUCTION,
  219. 'area/Magento/theme/locale/Namespace_Module/dir/file.js',
  220. 'Namespace_Module',
  221. true,
  222. 'dir/file.js',
  223. [
  224. 'area' => 'area', 'locale' => 'locale', 'module' => 'Namespace_Module', 'theme' => 'Magento/theme'
  225. ],
  226. 1,
  227. 1,
  228. ],
  229. ];
  230. }
  231. /**
  232. * @expectedException \InvalidArgumentException
  233. * @expectedExceptionMessage Requested path 'short/path.js' is wrong
  234. */
  235. public function testLaunchWrongPath()
  236. {
  237. $this->stateMock->expects($this->once())
  238. ->method('getMode')
  239. ->will($this->returnValue(\Magento\Framework\App\State::MODE_DEVELOPER));
  240. $this->requestMock->expects($this->once())
  241. ->method('get')
  242. ->with('resource')
  243. ->willReturn('short/path.js');
  244. $this->object->launch();
  245. }
  246. public function testCatchExceptionDeveloperMode()
  247. {
  248. $this->objectManagerMock->expects($this->once())
  249. ->method('get')
  250. ->with(\Psr\Log\LoggerInterface::class)
  251. ->willReturn($this->loggerMock);
  252. $this->loggerMock->expects($this->once())
  253. ->method('critical');
  254. $bootstrap = $this->getMockBuilder(Bootstrap::class)
  255. ->disableOriginalConstructor()
  256. ->getMock();
  257. $bootstrap->expects($this->once())
  258. ->method('isDeveloperMode')
  259. ->willReturn(true);
  260. $exception = new \Exception('Error: nothing works');
  261. $this->responseMock->expects($this->once())
  262. ->method('setHttpResponseCode')
  263. ->with(404);
  264. $this->responseMock->expects($this->once())
  265. ->method('sendResponse');
  266. $this->assertTrue($this->object->catchException($bootstrap, $exception));
  267. }
  268. /**
  269. * @expectedException \InvalidArgumentException
  270. */
  271. public function testLaunchPathAbove()
  272. {
  273. $path = 'frontend/..\..\folder_above/././Magento_Ui/template/messages.html';
  274. $this->stateMock->expects($this->once())
  275. ->method('getMode')
  276. ->will($this->returnValue(State::MODE_DEVELOPER));
  277. $this->requestMock->expects($this->once())
  278. ->method('get')
  279. ->with('resource')
  280. ->willReturn('frontend/..\..\folder_above/././Magento_Ui/template/messages.html');
  281. $this->expectExceptionMessage("Requested path '$path' is wrong.");
  282. $this->object->launch();
  283. }
  284. }