/** * Include RequireJs configuration as an asset on the page * * @return \Magento\Framework\View\Asset\LocalInterface */ public function getAsset() { if (!$this->asset) { $this->asset = $this->fileManager->createRequireJsAsset(); } return $this->asset; }
public function testGetAsset() { $asset = $this->getMockForAbstractClass('\\Magento\\Framework\\View\\Asset\\LocalInterface'); $this->fileManager->expects($this->once())->method('createRequireJsAsset')->will($this->returnValue($asset)); $object = new Config($this->context, $this->config, $this->fileManager); $this->assertSame($asset, $object->getAsset()); $this->assertSame($asset, $object->getAsset(), 'Asset is supposed to be cached in-memory'); }
public function testCreateRequireJsAssetDevMode() { $this->appState->expects($this->once())->method('getMode')->will($this->returnValue(\Magento\Framework\App\State::MODE_DEVELOPER)); $this->dir->expects($this->never())->method('isExist'); $data = 'requirejs config data'; $this->config->expects($this->once())->method('getConfig')->will($this->returnValue($data)); $this->dir->expects($this->once())->method('writeFile')->with('requirejs/file.js', $data); $this->assertSame($this->asset, $this->object->createRequireJsAsset()); }
public function testSetLayout() { $filePath = 'require_js_fie_path'; $asset = $this->getMockForAbstractClass('\\Magento\\Framework\\View\\Asset\\LocalInterface'); $asset->expects($this->atLeastOnce())->method('getFilePath')->willReturn($filePath); $this->fileManager->expects($this->once())->method('createRequireJsAsset')->will($this->returnValue($asset)); $layout = $this->getMock('Magento\\Framework\\View\\LayoutInterface'); $assetCollection = $this->getMockBuilder('Magento\\Framework\\View\\Asset\\GroupedCollection')->disableOriginalConstructor()->getMock(); $assetCollection->expects($this->once())->method('add')->with($filePath, $asset); $this->pageConfig->expects($this->atLeastOnce())->method('getAssetCollection')->willReturn($assetCollection); $object = new Config($this->context, $this->config, $this->fileManager, $this->pageConfig); $object->setLayout($layout); }
public function testCreateMinResolverAsset() { $this->configMock->expects($this->any())->method('getMinResolverRelativePath')->willReturn('relative path'); $this->assetRepoMock->expects($this->once())->method('createArbitrary')->with('relative path'); $this->fileSystem->expects($this->once())->method('getDirectoryWrite')->with(DirectoryList::STATIC_VIEW)->will($this->returnValue($this->dir)); $this->object->createMinResolverAsset(); }
public function testClearBundleJsPool() { $context = $this->getMockBuilder('Magento\\Framework\\View\\Asset\\File\\FallbackContext')->disableOriginalConstructor()->getMock(); $this->fileSystem->expects($this->once())->method('getDirectoryWrite')->with(DirectoryList::STATIC_VIEW)->willReturn($this->dir); $this->assetRepoMock->expects($this->once())->method('getStaticViewFileContext')->willReturn($context); $context->expects($this->once())->method('getPath')->willReturn('/path/to/directory'); $this->dir->expects($this->once())->method('delete')->with('/path/to/directory/' . \Magento\Framework\RequireJs\Config::BUNDLE_JS_DIR)->willReturn(true); $this->assertTrue($this->object->clearBundleJsPool()); }
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); }
/** * Include RequireJs configuration as an asset on the page * * @return $this */ protected function _prepareLayout() { $requireJsConfig = $this->fileManager->createRequireJsConfigAsset(); $assetCollection = $this->pageConfig->getAssetCollection(); $assetCollection->insert($requireJsConfig->getFilePath(), $requireJsConfig, RequireJsConfig::REQUIRE_JS_FILE_NAME); if ($this->bundleConfig->isBundlingJsFiles()) { $bundleAssets = $this->fileManager->createBundleJsPool(); $staticAsset = $this->fileManager->createStaticJsAsset(); /** @var \Magento\Framework\View\Asset\File $bundleAsset */ if (!empty($bundleAssets) && $staticAsset !== false) { $bundleAssets = array_reverse($bundleAssets); foreach ($bundleAssets as $bundleAsset) { $assetCollection->insert($bundleAsset->getFilePath(), $bundleAsset, RequireJsConfig::REQUIRE_JS_FILE_NAME); } $assetCollection->insert($staticAsset->getFilePath(), $staticAsset, RequireJsConfig::CONFIG_FILE_NAME); } } return parent::_prepareLayout(); }
/** * Include RequireJs configuration as an asset on the page * * @return $this */ protected function _prepareLayout() { $requireJsConfig = $this->fileManager->createRequireJsConfigAsset(); $requireJsMixinsConfig = $this->fileManager->createRequireJsMixinsAsset(); $assetCollection = $this->pageConfig->getAssetCollection(); $after = RequireJsConfig::REQUIRE_JS_FILE_NAME; if ($this->minification->isEnabled('js')) { $minResolver = $this->fileManager->createMinResolverAsset(); $assetCollection->insert($minResolver->getFilePath(), $minResolver, $after); $after = $minResolver->getFilePath(); } if ($this->bundleConfig->isBundlingJsFiles()) { $bundleAssets = $this->fileManager->createBundleJsPool(); $staticAsset = $this->fileManager->createStaticJsAsset(); /** @var \Magento\Framework\View\Asset\File $bundleAsset */ if (!empty($bundleAssets) && $staticAsset !== false) { $bundleAssets = array_reverse($bundleAssets); foreach ($bundleAssets as $bundleAsset) { $assetCollection->insert($bundleAsset->getFilePath(), $bundleAsset, $after); } $assetCollection->insert($staticAsset->getFilePath(), $staticAsset, reset($bundleAssets)->getFilePath()); $after = $staticAsset->getFilePath(); } } $assetCollection->insert($requireJsConfig->getFilePath(), $requireJsConfig, $after); $assetCollection->insert($requireJsMixinsConfig->getFilePath(), $requireJsMixinsConfig, $after); return parent::_prepareLayout(); }
public function testCreateBundleJsPool() { unset($this->config); $dirRead = $this->getMock('Magento\\Framework\\Filesystem\\Directory\\Read', [], [], 'libDir', false); $context = $this->getMock('Magento\\Framework\\View\\Asset\\File\\FallbackContext', [], [], '', false); $assetRepo = $this->getMock('Magento\\Framework\\View\\Asset\\Repository', [], [], '', false); $config = $this->getMock('\\Magento\\Framework\\RequireJs\\Config', [], [], '', false); $config->expects($this->never())->method('getConfigFileRelativePath')->willReturn(null); $context->expects($this->once())->method('getPath')->willReturn('path/to/bundle/dir'); $dirRead->expects($this->once())->method('isExist')->with('path/to/bundle/dir/js/bundle')->willReturn(true); $dirRead->expects($this->once())->method('read')->with('path/to/bundle/dir/js/bundle')->willReturn(['bundle1.js', 'bundle2.js']); $dirRead->expects($this->exactly(2))->method('getRelativePath')->willReturnMap(['path/to/bundle1.js', 'path/to/bundle2.js']); $assetRepo->expects($this->exactly(2))->method('createArbitrary')->willReturnMap([$this->asset, $this->asset]); $assetRepo->expects($this->once())->method('getStaticViewFileContext')->willReturn($context); $this->appState->expects($this->once())->method('getMode')->willReturn('production'); $this->fileSystem->expects($this->once())->method('getDirectoryRead')->with('static')->willReturn($dirRead); $object = new FileManager($config, $this->fileSystem, $this->appState, $assetRepo); $result = $object->createBundleJsPool(); $this->assertArrayHasKey('0', $result); $this->assertArrayHasKey('1', $result); }