PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/magento/framework/View/Test/Unit/Asset/SourceTest.php

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