/**
  * @param string $sourceContentType
  * @param string $targetContentType
  * @param array $expectedResult
  *
  * @dataProvider getPreProcessorsDataProvider
  */
 public function testProcess($sourceContentType, $targetContentType, array $expectedResult)
 {
     $this->processorChain->expects($this->any())->method('getOrigContentType')->willReturn($sourceContentType);
     $this->processorChain->expects($this->any())->method('getTargetContentType')->willReturn($targetContentType);
     $processorMaps = [];
     foreach ($expectedResult as $processor) {
         $processorMock = $this->getMock($processor, ['process'], [], '', false);
         $processorMock->expects($this->any())->method('process')->with($this->processorChain);
         $processorMaps[] = [$processor, $processorMock];
     }
     $this->objectManager->expects(static::atLeastOnce())->method('get')->willReturnMap($processorMaps);
     $this->processorPool->process($this->processorChain);
 }
Exemple #2
0
    /**
     * Perform necessary preprocessing and materialization when the specified asset is requested
     *
     * Returns an array of two elements:
     * - directory code where the file is supposed to be found
     * - relative path to the file
     *
     * Automatically caches the obtained successful results or returns false if source file was not found
     *
     * @param LocalInterface $asset
     * @return array|bool
     */
    private function preProcess(LocalInterface $asset)
    {
        $sourceFile = $this->findSourceFile($asset);
        if ($sourceFile !== false) {
            $path = $this->rootDir->getRelativePath($sourceFile);
        } else {
            // No original file, the resulting file may be generated by a pre-processor
            $path = false;
        }
        $cacheId = $path . ':' . $asset->getPath();
        $cached = $this->cache->load($cacheId);
        if ($cached) {
            return unserialize($cached);
        }

        $chain = $this->createChain($asset, $path);
        $this->preProcessorPool->process($chain);
        $chain->assertValid();
        $dirCode = DirectoryList::ROOT;
        if ($chain->isChanged()) {
            $dirCode = DirectoryList::VAR_DIR;
            $path = DirectoryList::TMP_MATERIALIZATION_DIR . '/source/' . $chain->getTargetAssetPath();
            $this->varDir->writeFile($path, $chain->getContent());
        }
        $result = [$dirCode, $path];
        $this->cache->save(serialize($result), $cacheId);
        return $result;
    }
Exemple #3
0
 /**
  * Perform necessary preprocessing and materialization when the specified asset is requested
  *
  * Returns an array of two elements:
  * - directory code where the file is supposed to be found
  * - relative path to the file
  *
  * Automatically caches the obtained successful results or returns false if source file was not found
  *
  * @param LocalInterface $asset
  * @return array|bool
  */
 private function preProcess(LocalInterface $asset)
 {
     $sourceFile = $this->findSourceFile($asset);
     if (!$sourceFile) {
         return false;
     }
     $dirCode = \Magento\Framework\App\Filesystem::ROOT_DIR;
     $path = $this->rootDir->getRelativePath($sourceFile);
     $cacheId = $path . ':' . $asset->getPath();
     $cached = $this->cache->load($cacheId);
     if ($cached) {
         return unserialize($cached);
     }
     $chain = new \Magento\Framework\View\Asset\PreProcessor\Chain($asset, $this->rootDir->readFile($path), $this->getContentType($path));
     $preProcessors = $this->preProcessorPool->getPreProcessors($chain->getOrigContentType(), $chain->getTargetContentType());
     foreach ($preProcessors as $processor) {
         $processor->process($chain);
     }
     $chain->assertValid();
     if ($chain->isChanged()) {
         $dirCode = \Magento\Framework\App\Filesystem::VAR_DIR;
         $path = self::TMP_MATERIALIZATION_DIR . '/source/' . $asset->getPath();
         $this->varDir->writeFile($path, $chain->getContent());
     }
     $result = array($dirCode, $path);
     $this->cache->save(serialize($result), $cacheId);
     return $result;
 }
Exemple #4
0
    /**
     * Perform necessary preprocessing and materialization when the specified asset is requested
     *
     * Returns an array of two elements:
     * - directory code where the file is supposed to be found
     * - relative path to the file
     *
     * Automatically caches the obtained successful results or returns false if source file was not found
     *
     * @param LocalInterface $asset
     * @return array|bool
     */
    private function preProcess(LocalInterface $asset)
    {
        $sourceFile = $this->findSourceFile($asset);
        $dirCode = DirectoryList::ROOT;
        $path = $this->rootDir->getRelativePath($sourceFile);
        $cacheId = $path . ':' . $asset->getPath();
        $cached = $this->cache->load($cacheId);
        if ($cached) {
            return unserialize($cached);
        }

        $origContent = $path ? $this->rootDir->readFile($path) : '';
        $origContentType = $this->getContentType($path) ?: $asset->getContentType();

        $chain = $this->chainFactory->create(
            [
                'asset' => $asset,
                'origContent' => $origContent,
                'origContentType' => $origContentType,
                'origAssetPath' => $path
            ]
        );

        $this->preProcessorPool->process($chain);
        $chain->assertValid();
        if ($chain->isChanged()) {
            $dirCode = DirectoryList::VAR_DIR;
            $path = DirectoryList::TMP_MATERIALIZATION_DIR . '/source/' . $chain->getTargetAssetPath();
            $this->varDir->writeFile($path, $chain->getContent());
        }
        $result = [$dirCode, $path];
        $this->cache->save(serialize($result), $cacheId);
        return $result;
    }
Exemple #5
0
    /**
     * Perform necessary preprocessing and materialization when the specified asset is requested
     *
     * Returns an array of two elements:
     * - directory where the file is supposed to be found
     * - relative path to the file
     *
     * returns false if source file was not found
     *
     * @param LocalInterface $asset
     * @return array|bool
     */
    private function preProcess(LocalInterface $asset)
    {
        $sourceFile = $this->findSourceFile($asset);
        $dir = $this->rootDir->getAbsolutePath();
        $path = '';
        if ($sourceFile) {
            $path = basename($sourceFile);
            $dir = dirname($sourceFile);
        }

        $chain = $this->createChain($asset, $dir, $path);
        $this->preProcessorPool->process($chain);
        $chain->assertValid();
        if ($chain->isChanged()) {
            $dir = $this->varDir->getAbsolutePath();
            $path = DirectoryList::TMP_MATERIALIZATION_DIR . '/source/' . $chain->getTargetAssetPath();
            $this->varDir->writeFile($path, $chain->getContent());
        }
        if (empty($path)) {
            $result = false;
        } else {
            $result = [$dir, $path];
        }
        return $result;
    }
Exemple #6
0
 /**
  * @param string $origFile
  * @param string $origPath
  * @param string $origContent
  * @param bool $isMaterialization
  * @param bool $isExist
  *
  * @dataProvider getFileDataProvider
  */
 public function testGetFile($origFile, $origPath, $origContent, $isMaterialization, $isExist)
 {
     $filePath = 'some/file.ext';
     $read = $this->getMock('Magento\Framework\Filesystem\Directory\Read', [], [], '', false);
     $read->expects($this->at(0))->method('readFile')->with($origPath)->willReturn($origContent);
     $this->readFactory->expects($this->atLeastOnce())->method('create')->willReturn($read);
     $this->viewFileResolution->expects($this->once())
         ->method('getFile')
         ->with('frontend', $this->theme, 'en_US', $filePath, 'Magento_Module')
         ->willReturn($origFile);
     $this->preProcessorPool->expects($this->once())
         ->method('process')
         ->with($this->chain);
     $this->staticDirRead->expects($this->any())
         ->method('isExist')
         ->willReturn($isExist);
     if ($isMaterialization || !$isExist) {
         $this->chain
             ->expects($this->once())
             ->method('isChanged')
             ->willReturn(true);
         $this->chain
             ->expects($this->once())
             ->method('getContent')
             ->willReturn('processed');
         $this->chain
             ->expects($this->once())
             ->method('getTargetAssetPath')
             ->willReturn($filePath);
         $this->varDir->expects($this->once())
             ->method('writeFile')
             ->with('view_preprocessed/source/some/file.ext', 'processed');
         $this->varDir->expects($this->once())
             ->method('getAbsolutePath')
             ->willReturn('var');
         $read->expects($this->once())
             ->method('getAbsolutePath')
             ->with('view_preprocessed/source/some/file.ext')
             ->willReturn('result');
     } else {
         $this->varDir->expects($this->never())->method('writeFile');
         $read->expects($this->at(1))
             ->method('getAbsolutePath')
             ->with('file.ext')
             ->willReturn('result');
     }
     $this->assertSame('result', $this->object->getFile($this->getAsset()));
 }
 /**
  * Perform necessary preprocessing and materialization when the specified asset is requested
  *
  * Returns an array of two elements:
  * - directory code where the file is supposed to be found
  * - relative path to the file
  *
  * returns false if source file was not found
  *
  * @param LocalInterface $asset
  * @return array|bool
  */
 private function preProcess(LocalInterface $asset)
 {
     $sourceFile = $this->findSourceFile($asset);
     $path = $this->rootDir->getRelativePath($sourceFile);
     $chain = $this->createChain($asset, $path);
     $this->preProcessorPool->process($chain);
     $chain->assertValid();
     $dirCode = DirectoryList::ROOT;
     if ($chain->isChanged()) {
         $dirCode = DirectoryList::VAR_DIR;
         $path = DirectoryList::TMP_MATERIALIZATION_DIR . '/source/' . $chain->getTargetAssetPath();
         $this->varDir->writeFile($path, $chain->getContent());
     }
     $result = [$dirCode, $path];
     return $result;
 }
 /**
  * @param $origFile
  * @param $origPath
  * @param $origContent
  * @param $isMaterialization
  *
  * @dataProvider getFileDataProvider
  */
 public function testGetFile($origFile, $origPath, $origContent, $isMaterialization)
 {
     $filePath = 'some/file.ext';
     $this->viewFileResolution->expects($this->once())->method('getFile')->with('frontend', $this->theme, 'en_US', $filePath, 'Magento_Module')->will($this->returnValue($origFile));
     $this->rootDirRead->expects($this->once())->method('getRelativePath')->with($origFile)->will($this->returnValue($origPath));
     $this->rootDirRead->expects($this->once())->method('readFile')->with($origPath)->will($this->returnValue($origContent));
     $this->preProcessorPool->expects($this->once())->method('process')->with($this->chain);
     if ($isMaterialization) {
         $this->chain->expects($this->once())->method('isChanged')->willReturn(true);
         $this->chain->expects($this->once())->method('getContent')->willReturn('processed');
         $this->chain->expects($this->once())->method('getTargetAssetPath')->willReturn($filePath);
         $this->varDir->expects($this->once())->method('writeFile')->with('view_preprocessed/source/some/file.ext', 'processed');
         $this->varDir->expects($this->once())->method('getAbsolutePath')->with('view_preprocessed/source/some/file.ext')->will($this->returnValue('result'));
     } else {
         $this->varDir->expects($this->never())->method('writeFile');
         $this->rootDirRead->expects($this->once())->method('getAbsolutePath')->with('source/some/file.ext')->will($this->returnValue('result'));
     }
     $this->assertSame('result', $this->object->getFile($this->getAsset()));
 }
Exemple #9
0
 /**
  * @param string $origFile
  * @param string $origPath
  * @param string $origContentType
  * @param string $origContent
  * @param string $isMaterialization
  * @dataProvider getFileDataProvider
  */
 public function testGetFile($origFile, $origPath, $origContentType, $origContent, $isMaterialization)
 {
     $filePath = 'some/file.ext';
     $cacheValue = "{$origPath}:{$filePath}";
     $this->viewFileResolution->expects($this->once())->method('getFile')->with('frontend', $this->theme, 'en_US', $filePath, 'Magento_Module')->will($this->returnValue($origFile));
     $this->rootDirRead->expects($this->once())->method('getRelativePath')->with($origFile)->will($this->returnValue($origPath));
     $this->cache->expects($this->once())->method('load')->will($this->returnValue(false));
     $this->rootDirRead->expects($this->once())->method('readFile')->with($origPath)->will($this->returnValue($origContent));
     $processor = $this->getMockForAbstractClass('Magento\\Framework\\View\\Asset\\PreProcessorInterface');
     $this->preProcessorPool->expects($this->once())->method('getPreProcessors')->with($origContentType, 'ext')->will($this->returnValue([$processor]));
     $processor->expects($this->once())->method('process')->will($this->returnCallback(array($this, 'chainTestCallback')));
     if ($isMaterialization) {
         $this->varDir->expects($this->once())->method('writeFile')->with('view_preprocessed/source/some/file.ext', 'processed');
         $this->cache->expects($this->once())->method('save')->with(serialize([\Magento\Framework\App\Filesystem::VAR_DIR, 'view_preprocessed/source/some/file.ext']), $cacheValue);
         $this->varDir->expects($this->once())->method('getAbsolutePath')->with('view_preprocessed/source/some/file.ext')->will($this->returnValue('result'));
     } else {
         $this->varDir->expects($this->never())->method('writeFile');
         $this->cache->expects($this->once())->method('save')->with(serialize([\Magento\Framework\App\Filesystem::ROOT_DIR, 'source/some/file.ext']), $cacheValue);
         $this->rootDirRead->expects($this->once())->method('getAbsolutePath')->with('source/some/file.ext')->will($this->returnValue('result'));
     }
     $this->assertSame('result', $this->object->getFile($this->getAsset()));
 }
Exemple #10
0
 /**
  * Run test for process method (exception)
  *
  * @expectedException \UnexpectedValueException
  * @expectedExceptionMessage "stdClass" has to implement the PreProcessorInterface.
  */
 public function testProcessBadInterface()
 {
     $preprocessors = [self::CONTENT_TYPE => ['test' => [Pool::PREPROCESSOR_CLASS => 'stdClass']]];
     $pool = new Pool($this->objectManagerMock, $this->sorterMock, self::DEFAULT_PREPROCESSOR, $preprocessors);
     $this->sorterMock->expects(self::once())->method('sort')->with($preprocessors[self::CONTENT_TYPE])->willReturn($preprocessors[self::CONTENT_TYPE]);
     $chainMock = $this->getChainMock(self::CONTENT_TYPE);
     $this->objectManagerMock->expects(self::once())->method('get')->with('stdClass')->willReturn(new \stdClass());
     $pool->process($chainMock);
 }
Exemple #11
0
 /**
  * @param string $sourceContentType
  * @param string $targetContentType
  * @param array $expectedResult
  *
  * @dataProvider getPreProcessorsDataProvider
  */
 public function testGetPreProcessors($sourceContentType, $targetContentType, array $expectedResult)
 {
     // Make the object manager to return strings for simplicity of mocking
     $this->objectManager->expects($this->any())->method('get')->with($this->anything())->will($this->returnArgument(0));
     $this->assertSame($expectedResult, $this->factory->getPreProcessors($sourceContentType, $targetContentType));
 }