ConfigTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\RequireJs\Test\Unit;
  7. use \Magento\Framework\RequireJs\Config;
  8. use Magento\Framework\View\Asset\RepositoryMap;
  9. class ConfigTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /**
  12. * @var \Magento\Framework\RequireJs\Config\File\Collector\Aggregated|\PHPUnit_Framework_MockObject_MockObject
  13. */
  14. private $fileSource;
  15. /**
  16. * @var \Magento\Framework\View\DesignInterface|\PHPUnit_Framework_MockObject_MockObject
  17. */
  18. private $design;
  19. /**
  20. * @var \Magento\Framework\Filesystem\File\Read|\PHPUnit_Framework_MockObject_MockObject
  21. */
  22. private $fileReader;
  23. /**
  24. * @var \Magento\Framework\View\Asset\ContextInterface|\PHPUnit_Framework_MockObject_MockObject
  25. */
  26. private $context;
  27. /**
  28. * @var Config
  29. */
  30. private $object;
  31. /**
  32. * @var \Magento\Framework\View\Asset\Minification|\PHPUnit_Framework_MockObject_MockObject
  33. */
  34. private $minificationMock;
  35. /**
  36. * @var \Magento\Framework\Code\Minifier\AdapterInterface|\PHPUnit_Framework_MockObject_MockObject
  37. */
  38. private $minifyAdapterMock;
  39. /**
  40. * @var RepositoryMap|\PHPUnit_Framework_MockObject_MockObject
  41. */
  42. private $repositoryMapMock;
  43. protected function setUp()
  44. {
  45. $this->fileSource = $this->createMock(\Magento\Framework\RequireJs\Config\File\Collector\Aggregated::class);
  46. $this->design = $this->getMockForAbstractClass(\Magento\Framework\View\DesignInterface::class);
  47. $readFactory = $this->createMock(\Magento\Framework\Filesystem\File\ReadFactory::class);
  48. $this->fileReader = $this->createMock(\Magento\Framework\Filesystem\File\Read::class);
  49. $readFactory->expects($this->any())
  50. ->method('create')
  51. ->will($this->returnValue($this->fileReader));
  52. $repo = $this->createMock(\Magento\Framework\View\Asset\Repository::class);
  53. $this->context = $this->getMockBuilder(\Magento\Framework\View\Asset\ContextInterface::class)
  54. ->setMethods(
  55. [
  56. 'getConfigPath',
  57. 'getPath',
  58. 'getBaseUrl',
  59. 'getAreaCode',
  60. 'getThemePath',
  61. 'getLocale'
  62. ]
  63. )
  64. ->getMock();
  65. $repo->expects($this->once())->method('getStaticViewFileContext')->will($this->returnValue($this->context));
  66. $this->minificationMock = $this->getMockBuilder(\Magento\Framework\View\Asset\Minification::class)
  67. ->disableOriginalConstructor()
  68. ->getMock();
  69. $this->minifyAdapterMock = $this->getMockBuilder(\Magento\Framework\Code\Minifier\AdapterInterface::class)
  70. ->getMockForAbstractClass();
  71. $this->repositoryMapMock = $this->getMockBuilder(RepositoryMap::class)
  72. ->disableOriginalConstructor()
  73. ->getMock();
  74. $this->object = new Config(
  75. $this->fileSource,
  76. $this->design,
  77. $readFactory,
  78. $repo,
  79. $this->minifyAdapterMock,
  80. $this->minificationMock,
  81. $this->repositoryMapMock
  82. );
  83. }
  84. public function testGetConfig()
  85. {
  86. $this->fileReader->expects($this->any())
  87. ->method('readAll')
  88. ->will($this->returnCallback(function ($file) {
  89. return $file . ' content';
  90. }));
  91. $fileOne = $this->createMock(\Magento\Framework\View\File::class);
  92. $fileOne->expects($this->once())
  93. ->method('getFilename')
  94. ->will($this->returnValue('some/full/relative/path/file_one.js'));
  95. $fileOne->expects($this->once())
  96. ->method('getName')
  97. ->will($this->returnValue('file_one.js'));
  98. $fileOne->expects($this->once())
  99. ->method('getModule')
  100. ->will($this->returnValue('Module_One'));
  101. $fileTwo = $this->createMock(\Magento\Framework\View\File::class);
  102. $fileTwo->expects($this->once())
  103. ->method('getFilename')
  104. ->will($this->returnValue('some/full/relative/path/file_two.js'));
  105. $fileTwo->expects($this->once())
  106. ->method('getName')
  107. ->will($this->returnValue('file_two.js'));
  108. $theme = $this->getMockForAbstractClass(\Magento\Framework\View\Design\ThemeInterface::class);
  109. $this->design->expects($this->once())
  110. ->method('getDesignTheme')
  111. ->will($this->returnValue($theme));
  112. $this->fileSource->expects($this->once())
  113. ->method('getFiles')
  114. ->with($theme, Config::CONFIG_FILE_NAME)
  115. ->will($this->returnValue([$fileOne, $fileTwo]));
  116. $this->minificationMock
  117. ->expects($this->atLeastOnce())
  118. ->method('isEnabled')
  119. ->with('js')
  120. ->willReturn(true);
  121. $expected = <<<expected
  122. (function(require){
  123. (function() {
  124. file_one.js content
  125. require.config(config);
  126. })();
  127. (function() {
  128. file_two.js content
  129. require.config(config);
  130. })();
  131. })(require);
  132. expected;
  133. $this->minifyAdapterMock
  134. ->expects($this->once())
  135. ->method('minify')
  136. ->with($expected)
  137. ->willReturnArgument(0);
  138. $actual = $this->object->getConfig();
  139. $this->assertEquals($actual, $expected);
  140. }
  141. public function testGetMinResolverCode()
  142. {
  143. $this->minificationMock
  144. ->expects($this->once())
  145. ->method('getExcludes')
  146. ->with('js')
  147. ->willReturn(['\.min\.']);
  148. $this->minificationMock
  149. ->expects($this->once())
  150. ->method('isEnabled')
  151. ->with('js')
  152. ->willReturn(true);
  153. $this->minifyAdapterMock
  154. ->expects($this->once())
  155. ->method('minify')
  156. ->willReturnArgument(0);
  157. $expected = <<<code
  158. var ctx = require.s.contexts._,
  159. origNameToUrl = ctx.nameToUrl;
  160. ctx.nameToUrl = function() {
  161. var url = origNameToUrl.apply(ctx, arguments);
  162. if (!url.match(/\.min\./)) {
  163. url = url.replace(/(\.min)?\.js$/, '.min.js');
  164. }
  165. return url;
  166. };
  167. code;
  168. $this->assertEquals($expected, $this->object->getMinResolverCode());
  169. }
  170. public function testGetConfigFileRelativePath()
  171. {
  172. $this->minificationMock
  173. ->expects($this->any())
  174. ->method('addMinifiedSign')
  175. ->willReturnArgument(0);
  176. $this->context->expects($this->once())->method('getConfigPath')->will($this->returnValue('path'));
  177. $actual = $this->object->getConfigFileRelativePath();
  178. $this->assertSame('path/requirejs-config.js', $actual);
  179. }
  180. public function testGetMixinsFileRelativePath()
  181. {
  182. $this->minificationMock
  183. ->expects($this->any())
  184. ->method('addMinifiedSign')
  185. ->willReturnArgument(0);
  186. $this->context->expects($this->once())->method('getPath')->will($this->returnValue('path'));
  187. $actual = $this->object->getMixinsFileRelativePath();
  188. $this->assertSame('path/mage/requirejs/mixins.js', $actual);
  189. }
  190. public function testGetMinResolverRelativePath()
  191. {
  192. $this->minificationMock
  193. ->expects($this->any())
  194. ->method('addMinifiedSign')
  195. ->willReturnArgument(0);
  196. $this->context->expects($this->once())->method('getConfigPath')->will($this->returnValue('path'));
  197. $actual = $this->object->getMinResolverRelativePath();
  198. $this->assertSame('path/requirejs-min-resolver.js', $actual);
  199. }
  200. public function testGetBaseConfig()
  201. {
  202. $this->context->expects($this->once())->method('getPath')->will($this->returnValue('area/theme/locale'));
  203. $this->context->expects($this->once())
  204. ->method('getBaseUrl')
  205. ->will($this->returnValue('http://base.url/'));
  206. $expected = <<<expected
  207. require.config({"baseUrl":"http://base.url/area/theme/locale"});
  208. expected;
  209. $actual = $this->object->getBaseConfig();
  210. $this->assertSame($expected, $actual);
  211. }
  212. }