/** * @param $groupOne * @param $groupTwo * @param $expectedResult * @dataProvider dataProviderRenderAssets */ public function testRenderAssets($groupOne, $groupTwo, $expectedResult) { $assetUrl = 'url'; $assetNoRoutUrl = 'no_route_url'; $exception = new \Magento\Framework\Exception('my message'); $assetMockOne = $this->getMock('Magento\\Framework\\View\\Asset\\AssetInterface'); $assetMockOne->expects($this->exactly(2))->method('getUrl')->willReturn($assetUrl); $groupAssetsOne = [$assetMockOne, $assetMockOne]; $groupMockOne = $this->getMockBuilder('Magento\\Framework\\View\\Asset\\PropertyGroup')->disableOriginalConstructor()->getMock(); $groupMockOne->expects($this->once())->method('getAll')->willReturn($groupAssetsOne); $groupMockOne->expects($this->any())->method('getProperty')->willReturnMap([[GroupedCollection::PROPERTY_CAN_MERGE, true], [GroupedCollection::PROPERTY_CONTENT_TYPE, $groupOne['type']], ['attributes', $groupOne['attributes']], ['ie_condition', $groupOne['condition']]]); $assetMockTwo = $this->getMock('Magento\\Framework\\View\\Asset\\AssetInterface'); $assetMockTwo->expects($this->once())->method('getUrl')->willThrowException($exception); $groupAssetsTwo = [$assetMockTwo]; $groupMockTwo = $this->getMockBuilder('Magento\\Framework\\View\\Asset\\PropertyGroup')->disableOriginalConstructor()->getMock(); $groupMockTwo->expects($this->once())->method('getAll')->willReturn($groupAssetsTwo); $groupMockTwo->expects($this->any())->method('getProperty')->willReturnMap([[GroupedCollection::PROPERTY_CAN_MERGE, true], [GroupedCollection::PROPERTY_CONTENT_TYPE, $groupTwo['type']], ['attributes', $groupTwo['attributes']], ['ie_condition', $groupTwo['condition']]]); $this->pageConfigMock->expects($this->once())->method('getAssetCollection')->willReturn($this->assetsCollection); $this->assetsCollection->expects($this->once())->method('getGroups')->willReturn([$groupMockOne, $groupMockTwo]); $this->assetMinifyServiceMock->expects($this->exactly(2))->method('getAssets')->willReturnArgument(0); $this->assetMergeServiceMock->expects($this->exactly(1))->method('getMergedAssets')->willReturnArgument(0); $this->loggerMock->expects($this->once())->method('critical')->with($exception); $this->urlBuilderMock->expects($this->once())->method('getUrl')->with('', ['_direct' => 'core/index/notFound'])->willReturn($assetNoRoutUrl); $this->assertEquals($expectedResult, $this->renderer->renderAssets($this->renderer->getAvailableResultGroups())); }
/** * Deploy a static view file * * @param string $filePath * @param string $area * @param string $themePath * @param string $locale * @param string $module * @return void */ private function deployFile($filePath, $area, $themePath, $locale, $module) { $requestedPath = $filePath; if (substr($filePath, -5) == '.less') { $requestedPath = preg_replace('/.less$/', '.css', $filePath); } $logMessage = "Processing file '{$filePath}' for area '{$area}', theme '{$themePath}', locale '{$locale}'"; if ($module) { $logMessage .= ", module '{$module}'"; } $this->logger->logDebug($logMessage); try { $asset = $this->assetRepo->createAsset($requestedPath, ['area' => $area, 'theme' => $themePath, 'locale' => $locale, 'module' => $module]); $asset = $this->minifyService->getAssets([$asset], true)[0]; $this->logger->logDebug("\tDeploying the file to '{$asset->getPath()}'", '.'); if ($this->isDryRun) { $asset->getContent(); } else { $this->assetPublisher->publish($asset); $this->bundleManager->addAsset($asset); } $this->count++; } catch (\Magento\Framework\View\Asset\File\NotFoundException $e) { // File was not found by Fallback (possibly because it's wrong context for it) - there is nothing to publish $this->logger->logDebug("\tNotice: Could not find file '{$filePath}'. This file may not be relevant for the theme or area."); } catch (\Less_Exception_Compiler $e) { $this->logger->logDebug("\tNotice: Could not parse LESS file '{$filePath}'. " . "This may indicate that the file is incomplete, but this is acceptable. " . "The file '{$filePath}' will be combined with another LESS file."); } catch (\Exception $e) { $this->logger->logError($e->getMessage() . " ({$logMessage})"); $this->logger->logDebug((string) $e); $this->errorCount++; } }
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]); }
/** * Returns rendered HTML for an Asset Group * * @param \Magento\Framework\View\Asset\PropertyGroup $group * @return string */ protected function renderAssetGroup(\Magento\Framework\View\Asset\PropertyGroup $group) { $groupAssets = $this->assetMinifyService->getAssets($group->getAll()); $groupAssets = $this->processMerge($groupAssets, $group); $attributes = $this->getGroupAttributes($group); $attributes = $this->addDefaultAttributes($group->getProperty(GroupedCollection::PROPERTY_CONTENT_TYPE), $attributes); $groupTemplate = $this->getAssetTemplate($group->getProperty(GroupedCollection::PROPERTY_CONTENT_TYPE), $attributes); $groupHtml = $this->renderAssetHtml($groupTemplate, $groupAssets); $groupHtml = $this->processIeCondition($groupHtml, $group); return $groupHtml; }
/** * Render HTML for the added head items * * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) * * @return string */ public function getCssJsHtml() { foreach ($this->getLayout()->getChildBlocks($this->getNameInLayout()) as $block) { /** @var $block \Magento\Framework\View\Element\AbstractBlock */ if ($block instanceof \Magento\Theme\Block\Html\Head\AssetBlockInterface) { /** @var \Magento\Framework\View\Asset\AssetInterface $asset */ $asset = $block->getAsset(); $this->_pageAssets->add($block->getNameInLayout(), $asset, (array) $block->getProperties()); } } $result = ''; /** @var $group \Magento\Framework\View\Asset\PropertyGroup */ foreach ($this->_pageAssets->getGroups() as $group) { $contentType = $group->getProperty(\Magento\Framework\View\Asset\GroupedCollection::PROPERTY_CONTENT_TYPE); $canMerge = $group->getProperty(\Magento\Framework\View\Asset\GroupedCollection::PROPERTY_CAN_MERGE); $attributes = $group->getProperty('attributes'); $ieCondition = $group->getProperty('ie_condition'); $flagName = $group->getProperty('flag_name'); if ($flagName && !$this->getData($flagName)) { continue; } $groupAssets = $group->getAll(); $groupAssets = $this->_assetMinifyService->getAssets($groupAssets); if ($canMerge && count($groupAssets) > 1) { $groupAssets = $this->_assetMergeService->getMergedAssets($groupAssets, $contentType); } if (!empty($attributes)) { if (is_array($attributes)) { $attributesString = ''; foreach ($attributes as $name => $value) { $attributesString .= ' ' . $name . '="' . $this->escapeHtml($value) . '"'; } $attributes = $attributesString; } else { $attributes = ' ' . $attributes; } } if ($contentType == 'js') { $groupTemplate = '<script' . $attributes . ' type="text/javascript" src="%s"></script>' . "\n"; } else { if ($contentType == 'css') { $attributes = ' rel="stylesheet" type="text/css"' . ($attributes ?: ' media="all"'); } $groupTemplate = '<link' . $attributes . ' href="%s" />' . "\n"; } $groupHtml = $this->_renderHtml($groupTemplate, $groupAssets); if (!empty($ieCondition)) { $groupHtml = '<!--[if ' . $ieCondition . ']>' . "\n" . $groupHtml . '<![endif]-->' . "\n"; } $result .= $groupHtml; } return $result; }
/** * @param string $mode * @param string $requestedPath * @param string $requestedModule * @param bool $moduleExists * @param string $expectedFile * @param array $expectedParams * * @dataProvider launchDataProvider */ public function testLaunch($mode, $requestedPath, $requestedModule, $moduleExists, $expectedFile, array $expectedParams) { $this->state->expects($this->once())->method('getMode')->will($this->returnValue($mode)); $this->state->expects($this->once())->method('setAreaCode')->with('area'); $this->configLoader->expects($this->once())->method('load')->with('area')->will($this->returnValue(['config'])); $this->objectManager->expects($this->once())->method('configure')->with(['config']); $this->request->expects($this->once())->method('get')->with('resource')->will($this->returnValue($requestedPath)); $this->moduleList->expects($this->any())->method('has')->with($requestedModule)->will($this->returnValue($moduleExists)); $asset = $this->getMockForAbstractClass('\\Magento\\Framework\\View\\Asset\\LocalInterface'); $asset->expects($this->once())->method('getSourceFile')->will($this->returnValue('resource/file.css')); $this->assetRepo->expects($this->once())->method('createAsset')->with($expectedFile, $expectedParams)->will($this->returnValue($asset)); $this->publisher->expects($this->once())->method('publish')->with($asset); $this->response->expects($this->once())->method('setFilePath')->with('resource/file.css'); $this->minifyService->expects($this->once())->method('getAssets')->willReturnArgument(0); $this->object->launch(); }
/** * Deploy a static view file * * @param string $filePath * @param string $area * @param string $themePath * @param string $locale * @param string $module * @return void * @SuppressWarnings(PHPMD.NPathComplexity) */ private function deployFile($filePath, $area, $themePath, $locale, $module) { $requestedPath = $filePath; if (substr($filePath, -5) == '.less') { $requestedPath = preg_replace('/.less$/', '.css', $filePath); } $logMessage = "Processing file '$filePath' for area '$area', theme '$themePath', locale '$locale'"; if ($module) { $logMessage .= ", module '$module'"; } if ($this->output->isVeryVerbose()) { $this->output->writeln($logMessage); } try { $asset = $this->assetRepo->createAsset( $requestedPath, ['area' => $area, 'theme' => $themePath, 'locale' => $locale, 'module' => $module] ); $asset = $this->minifyService->getAssets([$asset], true)[0]; if ($this->output->isVeryVerbose()) { $this->output->writeln("\tDeploying the file to '{$asset->getPath()}'"); } else { $this->output->write('.'); } if ($this->isDryRun) { $asset->getContent(); } else { $this->assetPublisher->publish($asset); $this->bundleManager->addAsset($asset); } $this->count++; } catch (\Less_Exception_Compiler $e) { $this->verboseLog( "\tNotice: Could not parse LESS file '$filePath'. " . "This may indicate that the file is incomplete, but this is acceptable. " . "The file '$filePath' will be combined with another LESS file." ); $this->verboseLog("\tCompiler error: " . $e->getMessage()); } catch (\Exception $e) { $this->output->writeln($e->getMessage() . " ($logMessage)"); $this->verboseLog($e->getTraceAsString()); $this->errorCount++; } }
/** * @param $contentType * @param $attributes * @param $ieCondition * @param $expectedResult * @dataProvider dataProviderRenderAsset */ public function testRenderAsset($contentType, $attributes, $ieCondition, $expectedResult) { $assetUrl = 'url'; $assetNoRoutUrl = 'no_route_url'; $exception = new \Magento\Framework\Exception('my message'); $assetMock1 = $this->getMock('Magento\\Framework\\View\\Asset\\AssetInterface'); $assetMock1->expects($this->once())->method('getUrl')->willReturn($assetUrl); $assetMock2 = $this->getMock('Magento\\Framework\\View\\Asset\\AssetInterface'); $assetMock2->expects($this->once())->method('getUrl')->willThrowException($exception); $groupAssets = [$assetMock1, $assetMock2]; $this->pageConfigMock->expects($this->once())->method('getAssetCollection')->willReturn($this->assetsCollection); $this->assetsCollection->expects($this->once())->method('getGroups')->willReturn([$this->propertyGroupMock]); $this->propertyGroupMock->expects($this->once())->method('getAll')->willReturn($groupAssets); $this->propertyGroupMock->expects($this->any())->method('getProperty')->willReturnMap([[GroupedCollection::PROPERTY_CAN_MERGE, true], [GroupedCollection::PROPERTY_CONTENT_TYPE, $contentType], ['attributes', $attributes], ['ie_condition', $ieCondition]]); $this->assetMinifyServiceMock->expects($this->once())->method('getAssets')->with($groupAssets)->willReturn($groupAssets); $this->assetMergeServiceMock->expects($this->once())->method('getMergedAssets')->with($groupAssets, $contentType)->willReturnArgument(0); $this->loggerMock->expects($this->once())->method('critical')->with($exception); $this->urlBuilderMock->expects($this->once())->method('getUrl')->with('', ['_direct' => 'core/index/notFound'])->willReturn($assetNoRoutUrl); $this->assertEquals($expectedResult, $this->renderer->renderAssets()); }
/** * Finds requested resource and provides it to the client * * @return \Magento\Framework\App\ResponseInterface * @throws \Exception */ public function launch() { // disabling profiling when retrieving static resource \Magento\Framework\Profiler::reset(); $appMode = $this->state->getMode(); if ($appMode == \Magento\Framework\App\State::MODE_PRODUCTION) { $this->response->setHttpResponseCode(404); } else { $path = $this->request->get('resource'); $params = $this->parsePath($path); $this->state->setAreaCode($params['area']); $this->objectManager->configure($this->configLoader->load($params['area'])); $file = $params['file']; unset($params['file']); $asset = $this->assetRepo->createAsset($file, $params); $asset = $this->minifyService->getAssets([$asset], true)[0]; $this->response->setFilePath($asset->getSourceFile()); $this->publisher->publish($asset); } return $this->response; }