FileManagerTest.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\RequireJs\Test\Unit\Model;
  7. use \Magento\RequireJs\Model\FileManager;
  8. use Magento\Framework\App\Filesystem\DirectoryList;
  9. class FileManagerTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /**
  12. * @var \Magento\Framework\RequireJs\Config|\PHPUnit_Framework_MockObject_MockObject
  13. */
  14. private $configMock;
  15. /**
  16. * @var \Magento\Framework\Filesystem|\PHPUnit_Framework_MockObject_MockObject
  17. */
  18. private $fileSystem;
  19. /**
  20. * @var \Magento\Framework\Filesystem\Directory\WriteInterface|\PHPUnit_Framework_MockObject_MockObject
  21. */
  22. private $dir;
  23. /**
  24. * @var \Magento\Framework\App\State|\PHPUnit_Framework_MockObject_MockObject
  25. */
  26. private $appState;
  27. /**
  28. * @var \Magento\Framework\View\Asset\File|\PHPUnit_Framework_MockObject_MockObject
  29. */
  30. private $asset;
  31. /**
  32. * @var \Magento\RequireJs\Model\FileManager
  33. */
  34. private $object;
  35. /**
  36. * @var \Magento\Framework\View\Asset\Repository|\PHPUnit_Framework_MockObject_MockObject
  37. */
  38. private $assetRepoMock;
  39. protected function setUp()
  40. {
  41. $this->configMock = $this->createMock(\Magento\Framework\RequireJs\Config::class);
  42. $this->fileSystem = $this->createMock(\Magento\Framework\Filesystem::class);
  43. $this->appState = $this->createMock(\Magento\Framework\App\State::class);
  44. $this->assetRepoMock = $this->createMock(\Magento\Framework\View\Asset\Repository::class);
  45. $this->object = new FileManager($this->configMock, $this->fileSystem, $this->appState, $this->assetRepoMock);
  46. $this->dir = $this->getMockForAbstractClass(\Magento\Framework\Filesystem\Directory\WriteInterface::class);
  47. $this->asset = $this->createMock(\Magento\Framework\View\Asset\File::class);
  48. }
  49. /**
  50. * @param bool $exists
  51. * @dataProvider createRequireJsAssetDataProvider
  52. */
  53. public function testCreateRequireJsConfigAsset($exists)
  54. {
  55. $this->configMock->expects($this->once())
  56. ->method('getConfigFileRelativePath')
  57. ->will($this->returnValue('requirejs/file.js'));
  58. $this->fileSystem->expects($this->once())
  59. ->method('getDirectoryWrite')
  60. ->with(DirectoryList::STATIC_VIEW)
  61. ->will($this->returnValue($this->dir));
  62. $this->assetRepoMock->expects($this->once())
  63. ->method('createArbitrary')
  64. ->with('requirejs/file.js', '')
  65. ->will($this->returnValue($this->asset));
  66. $this->appState->expects($this->once())->method('getMode')->will($this->returnValue('anything'));
  67. $this->dir->expects($this->once())
  68. ->method('isExist')
  69. ->with('requirejs/file.js')
  70. ->will($this->returnValue($exists));
  71. if ($exists) {
  72. $this->configMock->expects($this->never())->method('getConfig');
  73. $this->dir->expects($this->never())->method('writeFile');
  74. } else {
  75. $data = 'requirejs config data';
  76. $this->configMock->expects($this->once())->method('getConfig')->will($this->returnValue($data));
  77. $this->dir->expects($this->once())->method('writeFile')->with('requirejs/file.js', $data);
  78. }
  79. $this->assertSame($this->asset, $this->object->createRequireJsConfigAsset());
  80. }
  81. /**
  82. * @return array
  83. */
  84. public function createRequireJsAssetDataProvider()
  85. {
  86. return [[true], [false]];
  87. }
  88. public function testCreateRequireJsAssetDevMode()
  89. {
  90. $this->configMock->expects($this->once())
  91. ->method('getConfigFileRelativePath')
  92. ->will($this->returnValue('requirejs/file.js'));
  93. $this->fileSystem->expects($this->once())
  94. ->method('getDirectoryWrite')
  95. ->with(DirectoryList::STATIC_VIEW)
  96. ->will($this->returnValue($this->dir));
  97. $this->assetRepoMock->expects($this->once())
  98. ->method('createArbitrary')
  99. ->with('requirejs/file.js', '')
  100. ->will($this->returnValue($this->asset));
  101. $this->appState->expects($this->once())
  102. ->method('getMode')
  103. ->will($this->returnValue(\Magento\Framework\App\State::MODE_DEVELOPER));
  104. $this->dir->expects($this->never())->method('isExist');
  105. $data = 'requirejs config data';
  106. $this->configMock->expects($this->once())->method('getConfig')->will($this->returnValue($data));
  107. $this->dir->expects($this->once())->method('writeFile')->with('requirejs/file.js', $data);
  108. $this->assertSame($this->asset, $this->object->createRequireJsConfigAsset());
  109. }
  110. public function testCreateBundleJsPool()
  111. {
  112. unset($this->configMock);
  113. $dirRead = $this->getMockBuilder(\Magento\Framework\Filesystem\Directory\Read::class)
  114. ->setMockClassName('libDir')
  115. ->disableOriginalConstructor()
  116. ->getMock();
  117. $context = $this->createMock(\Magento\Framework\View\Asset\File\FallbackContext::class);
  118. $assetRepo = $this->createMock(\Magento\Framework\View\Asset\Repository::class);
  119. $config = $this->createMock(\Magento\Framework\RequireJs\Config::class);
  120. $config
  121. ->expects($this->never())
  122. ->method('getConfigFileRelativePath')
  123. ->willReturn(null);
  124. $context
  125. ->expects($this->once())
  126. ->method('getPath')
  127. ->willReturn('path/to/bundle/dir');
  128. $dirRead
  129. ->expects($this->once())
  130. ->method('isExist')
  131. ->with('path/to/bundle/dir/js/bundle')
  132. ->willReturn(true);
  133. $dirRead
  134. ->expects($this->once())
  135. ->method('read')
  136. ->with('path/to/bundle/dir/js/bundle')
  137. ->willReturn(['bundle1.js', 'bundle2.js', 'some_file.not_js']);
  138. $dirRead
  139. ->expects($this->exactly(2))
  140. ->method('getRelativePath')
  141. ->willReturnMap([
  142. 'path/to/bundle1.js',
  143. 'path/to/bundle2.js'
  144. ]);
  145. $assetRepo
  146. ->expects($this->exactly(2))
  147. ->method('createArbitrary')
  148. ->willReturnMap([
  149. $this->asset,
  150. $this->asset
  151. ]);
  152. $assetRepo
  153. ->expects($this->once())
  154. ->method('getStaticViewFileContext')
  155. ->willReturn($context);
  156. $this->appState
  157. ->expects($this->once())
  158. ->method('getMode')
  159. ->willReturn('production');
  160. $this->fileSystem
  161. ->expects($this->once())
  162. ->method('getDirectoryRead')
  163. ->with('static')
  164. ->willReturn($dirRead);
  165. $object = new FileManager($config, $this->fileSystem, $this->appState, $assetRepo);
  166. $result = $object->createBundleJsPool();
  167. $this->assertArrayHasKey('0', $result);
  168. $this->assertArrayHasKey('1', $result);
  169. }
  170. public function testCreateMinResolverAsset()
  171. {
  172. $this->configMock
  173. ->expects($this->any())
  174. ->method('getMinResolverRelativePath')
  175. ->willReturn('relative path');
  176. $this->assetRepoMock
  177. ->expects($this->once())
  178. ->method('createArbitrary')
  179. ->with('relative path');
  180. $this->fileSystem->expects($this->once())
  181. ->method('getDirectoryWrite')
  182. ->with(DirectoryList::STATIC_VIEW)
  183. ->will($this->returnValue($this->dir));
  184. $this->object->createMinResolverAsset();
  185. }
  186. public function testCreateRequireJsMixinsAsset()
  187. {
  188. $path = 'relative path';
  189. $this->configMock
  190. ->expects($this->once())
  191. ->method('getMixinsFileRelativePath')
  192. ->will($this->returnValue($path));
  193. $this->assetRepoMock
  194. ->expects($this->once())
  195. ->method('createArbitrary')
  196. ->with($path, '')
  197. ->willReturn($this->asset);
  198. $this->assertSame($this->asset, $this->object->createRequireJsMixinsAsset());
  199. }
  200. public function testClearBundleJsPool()
  201. {
  202. $context = $this->getMockBuilder(\Magento\Framework\View\Asset\File\FallbackContext::class)
  203. ->disableOriginalConstructor()
  204. ->getMock();
  205. $this->fileSystem->expects($this->once())
  206. ->method('getDirectoryWrite')
  207. ->with(DirectoryList::STATIC_VIEW)
  208. ->willReturn($this->dir);
  209. $this->assetRepoMock
  210. ->expects($this->once())
  211. ->method('getStaticViewFileContext')
  212. ->willReturn($context);
  213. $context->expects($this->once())
  214. ->method('getPath')
  215. ->willReturn('/path/to/directory');
  216. $this->dir->expects($this->once())
  217. ->method('delete')
  218. ->with('/path/to/directory/' . \Magento\Framework\RequireJs\Config::BUNDLE_JS_DIR)
  219. ->willReturn(true);
  220. $this->assertTrue($this->object->clearBundleJsPool());
  221. }
  222. }