PageRenderTime 43ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/dev/tests/integration/testsuite/Magento/Framework/View/Asset/MinifierTest.php

https://gitlab.com/crazybutterfly815/magento2
PHP | 252 lines | 159 code | 24 blank | 69 comment | 1 complexity | e26d2e2b7c98e91f4e487380edcbe319 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View\Asset;
  7. use Magento\Framework\App\Filesystem\DirectoryList;
  8. use Magento\TestFramework\Helper\Bootstrap;
  9. use Magento\Framework\App\State as AppState;
  10. /**
  11. * Tests for minifier
  12. *
  13. * @magentoComponentsDir Magento/Framework/View/_files/static/theme
  14. * @magentoDbIsolation enabled
  15. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  16. */
  17. class MinifierTest extends \PHPUnit_Framework_TestCase
  18. {
  19. /**
  20. * @var \Magento\Framework\Filesystem\Directory\WriteInterface
  21. */
  22. private $staticDir;
  23. /**
  24. * @var \Magento\TestFramework\ObjectManager
  25. */
  26. protected $objectManager;
  27. /**
  28. * @var string
  29. */
  30. private $origMode;
  31. /**
  32. * {@inheritDoc}
  33. */
  34. protected function setUp()
  35. {
  36. parent::setUp();
  37. $this->objectManager = Bootstrap::getInstance()->getObjectManager();
  38. /** @var \Magento\Theme\Model\Theme\Registration $registration */
  39. $registration = $this->objectManager->get(
  40. \Magento\Theme\Model\Theme\Registration::class
  41. );
  42. $registration->register();
  43. /** @var \Magento\TestFramework\App\State $appState */
  44. $appState = $this->objectManager->get(\Magento\TestFramework\App\State::class);
  45. $this->origMode = $appState->getMode();
  46. $appState->setMode(AppState::MODE_DEFAULT);
  47. /** @var \Magento\Framework\Filesystem $filesystem */
  48. $filesystem = Bootstrap::getObjectManager()->get(\Magento\Framework\Filesystem::class);
  49. $this->staticDir = $filesystem->getDirectoryWrite(DirectoryList::STATIC_VIEW);
  50. }
  51. /**
  52. * {@inheritDoc}
  53. */
  54. protected function tearDown()
  55. {
  56. /** @var \Magento\TestFramework\App\State $appState */
  57. $appState = $this->objectManager->get(\Magento\TestFramework\App\State::class);
  58. $appState->setMode($this->origMode);
  59. if ($this->staticDir->isExist('frontend/FrameworkViewMinifier')) {
  60. $this->staticDir->delete('frontend/FrameworkViewMinifier');
  61. }
  62. parent::tearDown();
  63. }
  64. /**
  65. * CSS Minifier library test
  66. *
  67. * When fails on library update or minification handler replacement:
  68. * 1 - minify `_files/static/css/styles.css` with new library manually
  69. * 2 - use DIFF tools to see difference between new minified CSS and old minified one
  70. * 3 - ensure that all differences are acceptable
  71. * 4 - ensure that new minified CSS is fully workable in all supported browsers
  72. * 5 - replace `_files/static/css/styles.magento.min.css` with new minified css
  73. */
  74. public function testCSSminLibrary()
  75. {
  76. /** @var \Magento\Framework\Code\Minifier\AdapterInterface $adapter */
  77. $adapter = $this->objectManager->get('cssMinificationAdapter');
  78. $this->assertEquals(
  79. file_get_contents(dirname(__DIR__) . '/_files/static/expected/styles.magento.min.css'),
  80. $adapter->minify(file_get_contents(dirname(__DIR__) . '/_files/static/theme/web/css/styles.css')),
  81. 'Minified CSS differs from initial minified CSS snapshot. '
  82. . 'Ensure that new CSS is fully valid for all supported browsers '
  83. . 'and replace old minified snapshot with new one.'
  84. );
  85. }
  86. /**
  87. * Test JS minification library
  88. *
  89. * @return void
  90. */
  91. public function testJshrinkLibrary()
  92. {
  93. /** @var \Magento\Framework\Code\Minifier\AdapterInterface $adapter */
  94. $adapter = $this->objectManager->get('jsMinificationAdapter');
  95. $this->assertEquals(
  96. file_get_contents(dirname(__DIR__) . '/_files/static/expected/test.min.js'),
  97. $adapter->minify(file_get_contents(dirname(__DIR__) . '/_files/static/theme/web/js/test.js')),
  98. 'Minified JS differs from initial minified JS snapshot. '
  99. . 'Ensure that new JS is fully valid for all supported browsers '
  100. . 'and replace old minified snapshot with new one.'
  101. );
  102. }
  103. /**
  104. * Test CSS minification
  105. *
  106. * @param string $requestedUri
  107. * @param callable $assertionCallback
  108. * @throws \Magento\Framework\Exception\LocalizedException
  109. */
  110. protected function _testCssMinification($requestedUri, $assertionCallback)
  111. {
  112. /** @var \Magento\Framework\App\Request\Http $request */
  113. $request = $this->objectManager->get(\Magento\Framework\App\Request\Http::class);
  114. $request->setRequestUri($requestedUri);
  115. $request->setParam('resource', $requestedUri);
  116. $response = $this->getMockBuilder(\Magento\Framework\App\Response\FileInterface::class)
  117. ->setMethods(['setFilePath'])
  118. ->getMockForAbstractClass();
  119. $response
  120. ->expects($this->any())
  121. ->method('setFilePath')
  122. ->will($this->returnCallback($assertionCallback));
  123. /** @var \Magento\Framework\App\StaticResource $staticResourceApp */
  124. $staticResourceApp = $this->objectManager->create(
  125. \Magento\Framework\App\StaticResource::class,
  126. ['response' => $response]
  127. );
  128. $staticResourceApp->launch();
  129. }
  130. /**
  131. * @magentoConfigFixture current_store dev/css/minify_files 1
  132. */
  133. public function testCssMinification()
  134. {
  135. $this->_testCssMinification(
  136. '/frontend/FrameworkViewMinifier/default/en_US/css/styles.min.css',
  137. function ($path) {
  138. $this->assertEquals(
  139. file_get_contents(
  140. dirname(__DIR__)
  141. . '/_files/static/expected/styles.magento.min.css'
  142. ),
  143. file_get_contents($path),
  144. 'Minified files are not equal or minification did not work for requested CSS'
  145. );
  146. }
  147. );
  148. }
  149. /**
  150. * @magentoConfigFixture current_store dev/css/minify_files 0
  151. */
  152. public function testCssMinificationOff()
  153. {
  154. $this->_testCssMinification(
  155. '/frontend/FrameworkViewMinifier/default/en_US/css/styles.css',
  156. function ($path) {
  157. $content = file_get_contents($path);
  158. $this->assertNotEmpty($content);
  159. $this->assertContains('FrameworkViewMinifier/frontend', $content);
  160. $this->assertNotEquals(
  161. file_get_contents(
  162. dirname(__DIR__)
  163. . '/_files/static/expected/styles.magento.min.css'
  164. ),
  165. $content,
  166. 'CSS is minified when minification turned off'
  167. );
  168. }
  169. );
  170. }
  171. /**
  172. * @magentoConfigFixture current_store dev/css/minify_files 1
  173. */
  174. public function testCssMinificationForMinifiedFiles()
  175. {
  176. $this->_testCssMinification(
  177. '/frontend/FrameworkViewMinifier/default/en_US/css/preminified-styles.min.css',
  178. function ($path) {
  179. $content = file_get_contents($path);
  180. $this->assertNotEmpty($content);
  181. $this->assertContains('Magento/backend', $content);
  182. $this->assertContains('semi-minified file', $content);
  183. }
  184. );
  185. }
  186. /**
  187. * @magentoConfigFixture current_store dev/css/minify_files 1
  188. */
  189. public function testDeploymentWithMinifierEnabled()
  190. {
  191. $staticPath = $this->staticDir->getAbsolutePath();
  192. $fileToBePublished = $staticPath . '/frontend/FrameworkViewMinifier/default/en_US/css/styles.min.css';
  193. $fileToTestPublishing = dirname(__DIR__) . '/_files/static/theme/web/css/styles.css';
  194. $omFactory = $this->getMock(\Magento\Framework\App\ObjectManagerFactory::class, ['create'], [], '', false);
  195. $omFactory->expects($this->any())
  196. ->method('create')
  197. ->will($this->returnValue($this->objectManager));
  198. $output = $this->objectManager->create(
  199. \Symfony\Component\Console\Output\ConsoleOutput::class
  200. );
  201. $filesUtil = $this->getMock(\Magento\Framework\App\Utility\Files::class, [], [], '', false);
  202. $filesUtil->expects($this->any())
  203. ->method('getStaticLibraryFiles')
  204. ->will($this->returnValue([]));
  205. $filesUtil->expects($this->any())
  206. ->method('getPhtmlFiles')
  207. ->will($this->returnValue([]));
  208. $filesUtil->expects($this->any())
  209. ->method('getStaticPreProcessingFiles')
  210. ->will($this->returnValue(
  211. [
  212. ['frontend', 'FrameworkViewMinifier/default', '', '', 'css/styles.css', $fileToTestPublishing]
  213. ]
  214. ));
  215. /** @var \Magento\Deploy\Model\Deployer $deployer */
  216. $deployer = $this->objectManager->create(
  217. \Magento\Deploy\Model\Deployer::class,
  218. ['filesUtil' => $filesUtil, 'output' => $output, 'isDryRun' => false]
  219. );
  220. $deployer->deploy($omFactory, ['en_US']);
  221. $this->assertFileExists($fileToBePublished);
  222. $this->assertEquals(
  223. file_get_contents(dirname(__DIR__) . '/_files/static/expected/styles.magento.min.css'),
  224. file_get_contents($fileToBePublished),
  225. 'Minified file is not equal or minification did not work for deployed CSS'
  226. );
  227. }
  228. }