public function testGetFileIfMinificationIsDisabled() { $this->assetConfig->expects($this->once())->method('isMinifyHtml')->willReturn(false); $theme = $this->getMockForAbstractClass('\\Magento\\Framework\\View\\Design\\ThemeInterface'); $expected = 'some/file.ext'; $this->resolver->expects($this->once())->method('resolve')->with(RulePool::TYPE_TEMPLATE_FILE, 'file.ext', 'frontend', $theme, null, 'Magento_Module')->will($this->returnValue($expected)); $this->state->expects($this->never())->method('getMode'); $actual = $this->object->getFile('frontend', $theme, 'file.ext', 'Magento_Module'); $this->assertSame($expected, $actual); }
/** * @param LocalInterface $asset * @return bool */ protected function isAssetMinification(LocalInterface $asset) { $sourceFile = $asset->getSourceFile(); if (in_array($asset->getFilePath(), $this->excluded)) { return false; } if ($this->assetConfig->isAssetMinification($asset->getContentType())) { if (strpos($sourceFile, '.min.') !== false) { $this->excluded[] = str_replace('.min.', '', $sourceFile); return true; } $extension = $asset->getContentType(); $minAbsolutePath = str_replace($extension, "min.{$extension}", $sourceFile); if (file_exists($minAbsolutePath)) { return false; } return true; } if (strpos($sourceFile, '.min.') !== false) { $absolutePath = str_replace('.min.', '', $asset->getFilePath()); if (file_exists($absolutePath)) { return false; } } else { $extension = $asset->getContentType(); $this->excluded[] = str_replace($extension, "min.{$extension}", $asset->getFilePath()); } return true; }
/** * Get existing file name, using fallback mechanism * * @param string $area * @param ThemeInterface $themeModel * @param string $file * @param string|null $module * @return string|bool */ public function getFile($area, ThemeInterface $themeModel, $file, $module = null) { $template = parent::getFile($area, $themeModel, $file, $module); if ($template && $this->assetConfig->isMinifyHtml()) { switch ($this->appState->getMode()) { case State::MODE_PRODUCTION: return $this->templateMinifier->getPathToMinified($template); case State::MODE_DEFAULT: return $this->templateMinifier->getMinified($template); case State::MODE_DEVELOPER: default: return $template; } } return $template; }
public function testSetLayout() { $this->bundleConfig->expects($this->once())->method('isBundlingJsFiles')->willReturn(true); $filePath = 'require_js_fie_path'; $asset = $this->getMockForAbstractClass('\\Magento\\Framework\\View\\Asset\\LocalInterface'); $asset->expects($this->atLeastOnce())->method('getFilePath')->willReturn($filePath); $requireJsAsset = $this->getMockForAbstractClass('\\Magento\\Framework\\View\\Asset\\LocalInterface'); $requireJsAsset->expects($this->atLeastOnce())->method('getFilePath')->willReturn('/path/to/require/require.js'); $this->fileManager->expects($this->once())->method('createRequireJsConfigAsset')->will($this->returnValue($requireJsAsset)); $this->fileManager->expects($this->once())->method('createStaticJsAsset')->will($this->returnValue($requireJsAsset)); $this->fileManager->expects($this->once())->method('createBundleJsPool')->will($this->returnValue([$asset])); $layout = $this->getMock('Magento\\Framework\\View\\LayoutInterface'); $assetCollection = $this->getMockBuilder('Magento\\Framework\\View\\Asset\\GroupedCollection')->disableOriginalConstructor()->getMock(); $this->pageConfig->expects($this->atLeastOnce())->method('getAssetCollection')->willReturn($assetCollection); $assetCollection->expects($this->atLeastOnce())->method('insert')->willReturn(true); $object = new Config($this->context, $this->config, $this->fileManager, $this->pageConfig, $this->bundleConfig); $object->setLayout($layout); }
/** * Return merged assets, if merging is enabled for a given content type * * @param MergeableInterface[] $assets * @param string $contentType * @return array|\Iterator * @throws \InvalidArgumentException */ public function getMergedAssets(array $assets, $contentType) { $isCss = $contentType == 'css'; $isJs = $contentType == 'js'; if (!$isCss && !$isJs) { throw new \InvalidArgumentException("Merge for content type '{$contentType}' is not supported."); } $isCssMergeEnabled = $this->config->isMergeCssFiles(); $isJsMergeEnabled = $this->config->isMergeJsFiles(); if ($isCss && $isCssMergeEnabled || $isJs && $isJsMergeEnabled) { $mergeStrategyClass = \Magento\Framework\View\Asset\MergeStrategy\FileExists::class; if ($this->state->getMode() === \Magento\Framework\App\State::MODE_DEVELOPER) { $mergeStrategyClass = \Magento\Framework\View\Asset\MergeStrategy\Checksum::class; } $mergeStrategy = $this->objectManager->get($mergeStrategyClass); $assets = $this->objectManager->create('Magento\\Framework\\View\\Asset\\Merged', ['assets' => $assets, 'mergeStrategy' => $mergeStrategy]); } return $assets; }
public function testGetAssetsInvalidAdapter() { $this->setExpectedException('\\Magento\\Framework\\Exception', 'Invalid adapter: \'stdClass\'. Expected: \\Magento\\Framework\\Code\\Minifier\\AdapterInterface'); $asset = $this->getMockForAbstractClass('Magento\\Framework\\View\\Asset\\LocalInterface'); $asset->expects($this->once())->method('getContentType')->will($this->returnValue('js')); $this->_config->expects($this->once())->method('isAssetMinification')->with('js')->will($this->returnValue(true)); $this->_config->expects($this->once())->method('getAssetMinificationAdapter')->with('js')->will($this->returnValue('StdClass')); $obj = new \StdClass(); $this->_objectManager->expects($this->once())->method('get')->with('StdClass')->will($this->returnValue($obj)); $this->_model->getAssets([$asset]); }
public function testAddAsset() { $context = $this->getMockBuilder('Magento\Framework\View\Asset\File\FallbackContext') ->disableOriginalConstructor() ->getMock(); $configView = $this->getMockBuilder('Magento\Framework\Config\View') ->disableOriginalConstructor() ->getMock(); $this->asset->expects($this->atLeastOnce()) ->method('getContentType') ->willReturn('js'); $this->asset->expects($this->once()) ->method('getSourceFile') ->willReturn('/source/file'); $this->asset->expects($this->atLeastOnce()) ->method('getFilePath') ->willReturn('file/path.js'); $this->assetConfig->expects($this->once()) ->method('isAssetMinification') ->willReturn(false); $this->asset->expects($this->atLeastOnce()) ->method('getContext') ->willReturn($context); $this->bundleConfig ->expects($this->atLeastOnce()) ->method('getConfig') ->with($context) ->willReturn($configView); $configView->expects($this->once()) ->method('getExcludedFiles') ->willReturn([]); $configView->expects($this->once()) ->method('getExcludedDir') ->willReturn([]); $this->bundle->expects($this->once()) ->method('addAsset') ->with($this->asset); $this->assertTrue($this->manager->addAsset($this->asset)); }