Skip to content

Commit

Permalink
Merge remote-tracking branch 'tango/MAGETWO-53991' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleksii Korshenko committed Jun 8, 2016
2 parents 7020a60 + ff35c6d commit 7797df0
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 163 deletions.
9 changes: 5 additions & 4 deletions lib/internal/Magento/Framework/View/Asset/MergeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,12 @@ public function getMergedAssets(array $assets, $contentType)
$isCssMergeEnabled = $this->config->isMergeCssFiles();
$isJsMergeEnabled = $this->config->isMergeJsFiles();
if (($isCss && $isCssMergeEnabled) || ($isJs && $isJsMergeEnabled)) {
if ($this->state->getMode() == \Magento\Framework\App\State::MODE_PRODUCTION) {
$mergeStrategyClass = 'Magento\Framework\View\Asset\MergeStrategy\FileExists';
} else {
$mergeStrategyClass = 'Magento\Framework\View\Asset\MergeStrategy\Checksum';
$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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function resolve($type, $file, $area = null, ThemeInterface $theme = null
{
$file = $this->minification->addMinifiedSign($file);
$path = $this->fallback->resolve($type, $file, $area, $theme, $locale, $module);
if (!$path && ($newFile = $this->minification->removeMinifiedSign($file))) {
if (!$path && $file != ($newFile = $this->minification->removeMinifiedSign($file))) {
$path = $this->fallback->resolve($type, $newFile, $area, $theme, $locale, $module);
}
return $path;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,65 +5,77 @@
*/
namespace Magento\Framework\View\Test\Unit\Asset;

use Magento\Framework\App\State;
use Magento\Framework\Filesystem;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Framework\View\Asset\AssetInterface;
use Magento\Framework\View\Asset\Merged;
use Magento\Framework\View\Asset\MergeService;
use Magento\Framework\View\Asset\ConfigInterface;
use Magento\Framework\View\Asset\MergeStrategy\Checksum;
use Magento\Framework\View\Asset\MergeStrategy\FileExists;

/**
* Class MergeServiceTest
*/
class MergeServiceTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \Magento\Framework\View\Asset\MergeService
* @var MergeService
*/
protected $_object;
private $object;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
* @var ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $_objectManager;
private $objectManagerMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
* @var ConfigInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $_config;
private $configMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
* @var Filesystem|\PHPUnit_Framework_MockObject_MockObject
*/
protected $_filesystem;
private $filesystemMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
* @var Filesystem\Directory\Write|\PHPUnit_Framework_MockObject_MockObject
*/
protected $_directory;
protected $directoryMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
* @var State|\PHPUnit_Framework_MockObject_MockObject
*/
protected $_state;
protected $stateMock;

protected function setUp()
{
$this->_objectManager = $this->getMock('Magento\Framework\ObjectManagerInterface');
$this->_config = $this->getMock('Magento\Framework\View\Asset\ConfigInterface', [], [], '', false);
$this->_filesystem = $this->getMock('Magento\Framework\Filesystem', [], [], '', false);
$this->_directory = $this->getMock(
'\Magento\Framework\Filesystem\Directory\Write',
[],
[],
'',
false
);
$this->_state = $this->getMock('Magento\Framework\App\State', [], [], '', false);
$this->_filesystem->expects(
$this->any()
)->method(
'getDirectoryWrite'
)->will(
$this->returnValue($this->_directory)
);

$this->_object = new \Magento\Framework\View\Asset\MergeService(
$this->_objectManager,
$this->_config,
$this->_filesystem,
$this->_state
);
$this->objectManagerMock = $this->getMockBuilder(ObjectManagerInterface::class)
->getMockForAbstractClass();
$this->configMock = $this->getMockBuilder(ConfigInterface::class)
->getMockForAbstractClass();
$this->filesystemMock = $this->getMockBuilder(Filesystem::class)
->disableOriginalConstructor()
->getMock();
$this->directoryMock = $this->getMockBuilder(Filesystem\Directory\Write::class)
->disableOriginalConstructor()
->getMock();
$this->stateMock = $this->getMockBuilder(State::class)
->disableOriginalConstructor()
->getMock();
$this->filesystemMock->expects($this->any())
->method('getDirectoryWrite')
->willReturn($this->directoryMock);

$this->object = (new ObjectManager($this))->getObject(MergeService::class, [
'objectManager' => $this->objectManagerMock,
'config' => $this->configMock,
'filesystem' => $this->filesystemMock,
'state' => $this->stateMock,
]);
}

/**
Expand All @@ -72,7 +84,7 @@ protected function setUp()
*/
public function testGetMergedAssetsWrongContentType()
{
$this->_object->getMergedAssets([], 'unknown');
$this->object->getMergedAssets([], 'unknown');
}

/**
Expand All @@ -84,34 +96,25 @@ public function testGetMergedAssetsWrongContentType()
*/
public function testGetMergedAssets(array $assets, $contentType, $appMode, $mergeStrategy)
{
$mergedAsset = $this->getMock('Magento\Framework\View\Asset\AssetInterface');
$this->_config->expects($this->once())->method('isMergeCssFiles')->will($this->returnValue(true));
$this->_config->expects($this->once())->method('isMergeJsFiles')->will($this->returnValue(true));

$mergedAsset = $this->getMock(AssetInterface::class);
$mergeStrategyMock = $this->getMock($mergeStrategy, [], [], '', false);

$this->_objectManager->expects(
$this->once()
)->method(
'create'
)->with(
'Magento\Framework\View\Asset\Merged',
['assets' => $assets, 'mergeStrategy' => $mergeStrategyMock]
)->will(
$this->returnValue($mergedAsset)
);

$this->_objectManager->expects(
$this->once()
)->method(
'get'
)->with(
$mergeStrategy
)->will(
$this->returnValue($mergeStrategyMock)
);
$this->_state->expects($this->once())->method('getMode')->will($this->returnValue($appMode));
$this->assertSame($mergedAsset, $this->_object->getMergedAssets($assets, $contentType));
$this->configMock->expects($this->once())->method('isMergeCssFiles')->willReturn(true);
$this->configMock->expects($this->once())->method('isMergeJsFiles')->willReturn(true);

$this->objectManagerMock->expects($this->once())
->method('create')
->with(Merged::class, ['assets' => $assets, 'mergeStrategy' => $mergeStrategyMock])
->willReturn($mergedAsset);
$this->objectManagerMock->expects($this->once())
->method('get')
->with($mergeStrategy)
->willReturn($mergeStrategyMock);
$this->stateMock->expects($this->once())
->method('getMode')
->willReturn($appMode);

$this->assertSame($mergedAsset, $this->object->getMergedAssets($assets, $contentType));
}

public static function getMergedAssetsDataProvider()
Expand All @@ -128,47 +131,50 @@ public static function getMergedAssetsDataProvider()
'js production mode' => [
$jsAssets,
'js',
\Magento\Framework\App\State::MODE_PRODUCTION,
'Magento\Framework\View\Asset\MergeStrategy\FileExists',
State::MODE_PRODUCTION,
FileExists::class,
],
'css production mode' => [
$cssAssets,
'css',
\Magento\Framework\App\State::MODE_PRODUCTION,
'Magento\Framework\View\Asset\MergeStrategy\FileExists',
State::MODE_PRODUCTION,
FileExists::class,
],
'js default mode' => [
$jsAssets,
'js',
\Magento\Framework\App\State::MODE_DEFAULT,
'Magento\Framework\View\Asset\MergeStrategy\Checksum',
State::MODE_DEFAULT,
FileExists::class,
],
'css default mode' => [
$cssAssets,
'js',
\Magento\Framework\App\State::MODE_DEFAULT,
'Magento\Framework\View\Asset\MergeStrategy\Checksum',
State::MODE_DEFAULT,
FileExists::class,
],
'js developer mode' => [
$jsAssets,
'js',
\Magento\Framework\App\State::MODE_DEVELOPER,
'Magento\Framework\View\Asset\MergeStrategy\Checksum',
State::MODE_DEVELOPER,
Checksum::class,
],
'css developer mode' => [
$cssAssets,
'css',
\Magento\Framework\App\State::MODE_DEVELOPER,
'Magento\Framework\View\Asset\MergeStrategy\Checksum',
Checksum::class,
]
];
}

public function testCleanMergedJsCss()
{
$mergedDir = \Magento\Framework\View\Asset\Merged::getRelativeDir();
$this->_directory->expects($this->once())->method('delete')->with($mergedDir);

$this->_object->cleanMergedJsCss();
$this->directoryMock->expects($this->once())
->method('delete')
->with($mergedDir);

$this->object->cleanMergedJsCss();
}
}
Loading

0 comments on commit 7797df0

Please sign in to comment.