SourceTest.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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\Asset;
  7. use Magento\Framework\App\Filesystem\DirectoryList;
  8. use Magento\Framework\Filesystem\DriverPool;
  9. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  10. use Magento\Framework\View\Asset\PreProcessor\ChainFactoryInterface;
  11. use Magento\Framework\View\Asset\PreProcessor\Chain;
  12. use Magento\Framework\View\Asset\Source;
  13. use Magento\Framework\View\Design\Theme\ThemeProviderInterface;
  14. /**
  15. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  16. */
  17. class SourceTest extends \PHPUnit\Framework\TestCase
  18. {
  19. /**
  20. * @var \Magento\Framework\Filesystem|\PHPUnit_Framework_MockObject_MockObject
  21. */
  22. private $filesystem;
  23. /**
  24. * @var \Magento\Framework\Filesystem\Directory\ReadInterface|\PHPUnit_Framework_MockObject_MockObject
  25. */
  26. private $rootDirRead;
  27. /**
  28. * @var \Magento\Framework\Filesystem\Directory\WriteInterface|\PHPUnit_Framework_MockObject_MockObject
  29. */
  30. private $tmpDir;
  31. /**
  32. * @var \Magento\Framework\Filesystem\Directory\WriteInterface|\PHPUnit_Framework_MockObject_MockObject
  33. */
  34. private $staticDirRead;
  35. /**
  36. * @var \Magento\Framework\View\Asset\PreProcessor\Pool|\PHPUnit_Framework_MockObject_MockObject
  37. */
  38. private $preProcessorPool;
  39. /**
  40. * @var \Magento\Framework\View\Design\FileResolution\Fallback\StaticFile|\PHPUnit_Framework_MockObject_MockObject
  41. */
  42. private $viewFileResolution;
  43. /**
  44. * @var \Magento\Framework\View\Design\ThemeInterface|\PHPUnit_Framework_MockObject_MockObject
  45. */
  46. private $theme;
  47. /**
  48. * @var Source
  49. */
  50. private $object;
  51. /**
  52. * @var ChainFactoryInterface | \PHPUnit_Framework_MockObject_MockObject
  53. */
  54. private $chainFactory;
  55. /**
  56. * @var Chain | \PHPUnit_Framework_MockObject_MockObject
  57. */
  58. private $chain;
  59. /**
  60. * @var \Magento\Framework\Filesystem\Directory\ReadFactory|\PHPUnit_Framework_MockObject_MockObject
  61. */
  62. private $readFactory;
  63. protected function setUp()
  64. {
  65. $this->preProcessorPool = $this->createMock(\Magento\Framework\View\Asset\PreProcessor\Pool::class);
  66. $this->viewFileResolution = $this->createMock(
  67. \Magento\Framework\View\Design\FileResolution\Fallback\StaticFile::class
  68. );
  69. $this->theme = $this->getMockForAbstractClass(\Magento\Framework\View\Design\ThemeInterface::class);
  70. /** @var \Magento\Framework\App\Config\ScopeConfigInterface $config */
  71. $this->chainFactory = $this->getMockBuilder(
  72. \Magento\Framework\View\Asset\PreProcessor\ChainFactoryInterface::class
  73. )->getMock();
  74. $this->chain = $this->getMockBuilder(\Magento\Framework\View\Asset\PreProcessor\Chain::class)
  75. ->disableOriginalConstructor()
  76. ->setMethods([])
  77. ->getMock();
  78. $this->chainFactory->expects($this->any())
  79. ->method('create')
  80. ->willReturn($this->chain);
  81. $themeProvider = $this->createMock(ThemeProviderInterface::class);
  82. $themeProvider->expects($this->any())
  83. ->method('getThemeByFullPath')
  84. ->with('frontend/magento_theme')
  85. ->willReturn($this->theme);
  86. $this->readFactory = $this->createMock(\Magento\Framework\Filesystem\Directory\ReadFactory::class);
  87. $this->initFilesystem();
  88. $this->object = (new ObjectManager($this))->getObject(Source::class, [
  89. 'filesystem' => $this->filesystem,
  90. 'readFactory' => $this->readFactory,
  91. 'preProcessorPool' => $this->preProcessorPool,
  92. 'fallback' => $this->viewFileResolution,
  93. 'themeProvider' => $themeProvider,
  94. 'chainFactory' => $this->chainFactory
  95. ]);
  96. }
  97. /**
  98. * @param string $origFile
  99. * @param string $origPath
  100. * @param string $origContent
  101. * @param bool $isMaterialization
  102. * @param bool $isExist
  103. *
  104. * @dataProvider getFileDataProvider
  105. */
  106. public function testGetFile($origFile, $origPath, $origContent, $isMaterialization, $isExist)
  107. {
  108. $filePath = 'some/file.ext';
  109. $read = $this->createMock(\Magento\Framework\Filesystem\Directory\Read::class);
  110. $read->expects($this->at(0))->method('readFile')->with($origPath)->willReturn($origContent);
  111. $this->readFactory->expects($this->atLeastOnce())->method('create')->willReturn($read);
  112. $this->viewFileResolution->expects($this->once())
  113. ->method('getFile')
  114. ->with('frontend', $this->theme, 'en_US', $filePath, 'Magento_Module')
  115. ->willReturn($origFile);
  116. $this->preProcessorPool->expects($this->once())
  117. ->method('process')
  118. ->with($this->chain);
  119. $this->staticDirRead->expects($this->any())
  120. ->method('isExist')
  121. ->willReturn($isExist);
  122. if ($isMaterialization || !$isExist) {
  123. $this->chain
  124. ->expects($this->once())
  125. ->method('isChanged')
  126. ->willReturn(true);
  127. $this->chain
  128. ->expects($this->once())
  129. ->method('getContent')
  130. ->willReturn('processed');
  131. $this->chain
  132. ->expects($this->once())
  133. ->method('getTargetAssetPath')
  134. ->willReturn($filePath);
  135. $this->tmpDir->expects($this->once())
  136. ->method('writeFile')
  137. ->with('some/file.ext', 'processed');
  138. $this->tmpDir->expects($this->once())
  139. ->method('getAbsolutePath')
  140. ->willReturn('view_preprocessed');
  141. $read->expects($this->once())
  142. ->method('getAbsolutePath')
  143. ->with('some/file.ext')
  144. ->willReturn('result');
  145. } else {
  146. $this->tmpDir->expects($this->never())->method('writeFile');
  147. $read->expects($this->at(1))
  148. ->method('getAbsolutePath')
  149. ->with('file.ext')
  150. ->willReturn('result');
  151. }
  152. $this->assertSame('result', $this->object->getFile($this->getAsset()));
  153. }
  154. /**
  155. * @param string $path
  156. * @param string $expected
  157. * @dataProvider getContentTypeDataProvider
  158. */
  159. public function testGetContentType($path, $expected)
  160. {
  161. $this->assertEquals($expected, $this->object->getContentType($path));
  162. }
  163. /**
  164. * @return array
  165. */
  166. public function getContentTypeDataProvider()
  167. {
  168. return [
  169. ['', ''],
  170. ['path/file', ''],
  171. ['path/file.ext', 'ext'],
  172. ];
  173. }
  174. /**
  175. * A callback for affecting preprocessor chain in the test
  176. *
  177. * @param Chain $chain
  178. */
  179. public function chainTestCallback(Chain $chain)
  180. {
  181. $chain->setContentType('ext');
  182. $chain->setContent('processed');
  183. }
  184. /**
  185. * @return array
  186. */
  187. public function getFileDataProvider()
  188. {
  189. return [
  190. ['/root/some/file.ext', 'file.ext', 'processed', false, true],
  191. ['/root/some/file.ext', 'file.ext', 'not_processed', true, false],
  192. ['/root/some/file.ext2', 'file.ext2', 'processed', true, true],
  193. ['/root/some/file.ext2', 'file.ext2', 'not_processed', true, false],
  194. ];
  195. }
  196. protected function initFilesystem()
  197. {
  198. $this->filesystem = $this->createMock(\Magento\Framework\Filesystem::class);
  199. $this->rootDirRead = $this->getMockForAbstractClass(
  200. \Magento\Framework\Filesystem\Directory\ReadInterface::class
  201. );
  202. $this->staticDirRead = $this->getMockForAbstractClass(
  203. \Magento\Framework\Filesystem\Directory\ReadInterface::class
  204. );
  205. $this->tmpDir = $this->getMockForAbstractClass(\Magento\Framework\Filesystem\Directory\WriteInterface::class);
  206. $readDirMap = [
  207. [DirectoryList::ROOT, DriverPool::FILE, $this->rootDirRead],
  208. [DirectoryList::STATIC_VIEW, DriverPool::FILE, $this->staticDirRead],
  209. [DirectoryList::TMP_MATERIALIZATION_DIR, DriverPool::FILE, $this->tmpDir],
  210. ];
  211. $this->filesystem->expects($this->any())
  212. ->method('getDirectoryRead')
  213. ->willReturnMap($readDirMap);
  214. $this->filesystem->expects($this->any())
  215. ->method('getDirectoryWrite')
  216. ->with(DirectoryList::TMP_MATERIALIZATION_DIR)
  217. ->willReturn($this->tmpDir);
  218. }
  219. /**
  220. * Create an asset mock
  221. *
  222. * @param bool $isFallback
  223. * @return \Magento\Framework\View\Asset\File|\PHPUnit_Framework_MockObject_MockObject
  224. */
  225. protected function getAsset($isFallback = true)
  226. {
  227. if ($isFallback) {
  228. $context = new \Magento\Framework\View\Asset\File\FallbackContext(
  229. 'http://example.com/static/',
  230. 'frontend',
  231. 'magento_theme',
  232. 'en_US'
  233. );
  234. } else {
  235. $context = new \Magento\Framework\View\Asset\File\Context(
  236. 'http://example.com/static/',
  237. DirectoryList::STATIC_VIEW,
  238. ''
  239. );
  240. }
  241. $asset = $this->createMock(\Magento\Framework\View\Asset\File::class);
  242. $asset->expects($this->any())
  243. ->method('getContext')
  244. ->willReturn($context);
  245. $asset->expects($this->any())
  246. ->method('getFilePath')
  247. ->willReturn('some/file.ext');
  248. $asset->expects($this->any())
  249. ->method('getPath')
  250. ->willReturn('some/file.ext');
  251. $asset->expects($this->any())
  252. ->method('getModule')
  253. ->willReturn('Magento_Module');
  254. $asset->expects($this->any())
  255. ->method('getContentType')
  256. ->willReturn('ext');
  257. return $asset;
  258. }
  259. }