Skip to content

Commit 47267e6

Browse files
authored
Merge pull request #2088 from magento-thunder/MAGETWO-87619
Fixed issue: MAGETWO-87619 Add ability minify html when SCD on demand in production is enabled
2 parents 87a4f92 + c8915b0 commit 47267e6

File tree

2 files changed

+71
-25
lines changed

2 files changed

+71
-25
lines changed

lib/internal/Magento/Framework/View/Design/FileResolution/Fallback/TemplateFile.php

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
use Magento\Framework\View\Asset\ConfigInterface;
1111
use Magento\Framework\View\Design\ThemeInterface;
1212
use Magento\Framework\View\Template\Html\MinifierInterface;
13+
use Magento\Framework\App\DeploymentConfig;
14+
use Magento\Framework\App\ObjectManager;
15+
use Magento\Framework\Config\ConfigOptionsListConstants;
1316

1417
/**
1518
* Provider of template view files
@@ -31,21 +34,29 @@ class TemplateFile extends File
3134
*/
3235
protected $assetConfig;
3336

37+
/**
38+
* @var DeploymentConfig
39+
*/
40+
private $deploymentConfig;
41+
3442
/**
3543
* @param ResolverInterface $resolver
3644
* @param MinifierInterface $templateMinifier
3745
* @param State $appState
3846
* @param ConfigInterface $assetConfig
47+
* @param DeploymentConfig $deploymentConfig
3948
*/
4049
public function __construct(
4150
ResolverInterface $resolver,
4251
MinifierInterface $templateMinifier,
4352
State $appState,
44-
ConfigInterface $assetConfig
53+
ConfigInterface $assetConfig,
54+
DeploymentConfig $deploymentConfig = null
4555
) {
4656
$this->appState = $appState;
4757
$this->templateMinifier = $templateMinifier;
4858
$this->assetConfig = $assetConfig;
59+
$this->deploymentConfig = $deploymentConfig ?: ObjectManager::getInstance()->get(DeploymentConfig::class);
4960
parent::__construct($resolver);
5061
}
5162

@@ -73,7 +84,7 @@ public function getFile($area, ThemeInterface $themeModel, $file, $module = null
7384
if ($template && $this->assetConfig->isMinifyHtml()) {
7485
switch ($this->appState->getMode()) {
7586
case State::MODE_PRODUCTION:
76-
return $this->templateMinifier->getPathToMinified($template);
87+
return $this->getMinifiedTemplateInProduction($template);
7788
case State::MODE_DEFAULT:
7889
return $this->templateMinifier->getMinified($template);
7990
case State::MODE_DEVELOPER:
@@ -83,4 +94,24 @@ public function getFile($area, ThemeInterface $themeModel, $file, $module = null
8394
}
8495
return $template;
8596
}
97+
98+
/**
99+
* Returns path to minified template file
100+
*
101+
* If SCD on demand in production is disabled - returns the path to minified template file.
102+
* Otherwise returns the path to minified template file,
103+
* or minify if file not exist and returns path.
104+
*
105+
* @param string $template
106+
* @return string
107+
*/
108+
private function getMinifiedTemplateInProduction($template)
109+
{
110+
if ($this->deploymentConfig->getConfigData(
111+
ConfigOptionsListConstants::CONFIG_PATH_SCD_ON_DEMAND_IN_PRODUCTION
112+
)) {
113+
return $this->templateMinifier->getMinified($template);
114+
}
115+
return $this->templateMinifier->getPathToMinified($template);
116+
}
86117
}

lib/internal/Magento/Framework/View/Test/Unit/Design/FileResolution/Fallback/TemplateFileTest.php

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
use Magento\Framework\View\Design\Fallback\RulePool;
1111
use Magento\Framework\View\Design\FileResolution\Fallback\TemplateFile;
1212
use Magento\Framework\View\Design\FileResolution\Fallback\ResolverInterface;
13+
use Magento\Framework\View\Template\Html\MinifierInterface;
14+
use Magento\Framework\View\Asset\ConfigInterface;
15+
use Magento\Framework\App\DeploymentConfig;
16+
use Magento\Framework\Config\ConfigOptionsListConstants;
1317

1418
class TemplateFileTest extends \PHPUnit\Framework\TestCase
1519
{
@@ -19,12 +23,12 @@ class TemplateFileTest extends \PHPUnit\Framework\TestCase
1923
protected $resolver;
2024

2125
/**
22-
* @var \Magento\Framework\View\Template\Html\MinifierInterface|\PHPUnit_Framework_MockObject_MockObject
26+
* @var MinifierInterface|\PHPUnit_Framework_MockObject_MockObject
2327
*/
2428
protected $minifier;
2529

2630
/**
27-
* @var \Magento\Framework\App\State|\PHPUnit_Framework_MockObject_MockObject
31+
* @var State|\PHPUnit_Framework_MockObject_MockObject
2832
*/
2933
protected $state;
3034

@@ -34,26 +38,29 @@ class TemplateFileTest extends \PHPUnit\Framework\TestCase
3438
protected $object;
3539

3640
/**
37-
* @var \Magento\Framework\View\Asset\ConfigInterface|\PHPUnit_Framework_MockObject_MockObject
41+
* @var DeploymentConfig|\PHPUnit_Framework_MockObject_MockObject
42+
*/
43+
private $deploymentConfigMock;
44+
45+
/**
46+
* @var ConfigInterface|\PHPUnit_Framework_MockObject_MockObject
3847
*/
3948
protected $assetConfig;
4049

4150
protected function setUp()
4251
{
43-
$this->resolver = $this->createMock(
44-
\Magento\Framework\View\Design\FileResolution\Fallback\ResolverInterface::class
45-
);
46-
$this->minifier = $this->createMock(\Magento\Framework\View\Template\Html\MinifierInterface::class);
47-
$this->state = $this->getMockBuilder(
48-
\Magento\Framework\App\State::class
49-
)->disableOriginalConstructor()->getMock();
50-
$this->assetConfig = $this->getMockForAbstractClass(
51-
\Magento\Framework\View\Asset\ConfigInterface::class,
52-
[],
53-
'',
54-
false
52+
$this->resolver = $this->getMockForAbstractClass(ResolverInterface::class);
53+
$this->minifier = $this->getMockForAbstractClass(MinifierInterface::class);
54+
$this->state = $this->createMock(State::class);
55+
$this->assetConfig = $this->getMockForAbstractClass(ConfigInterface::class);
56+
$this->deploymentConfigMock = $this->createMock(DeploymentConfig::class);
57+
$this->object = new TemplateFile(
58+
$this->resolver,
59+
$this->minifier,
60+
$this->state,
61+
$this->assetConfig,
62+
$this->deploymentConfigMock
5563
);
56-
$this->object = new TemplateFile($this->resolver, $this->minifier, $this->state, $this->assetConfig);
5764
}
5865

5966
/**
@@ -75,7 +82,7 @@ public function testGetFileWhenStateDeveloper()
7582
$this->resolver->expects($this->once())
7683
->method('resolve')
7784
->with(RulePool::TYPE_TEMPLATE_FILE, 'file.ext', 'frontend', $theme, null, 'Magento_Module')
78-
->will($this->returnValue($expected));
85+
->willReturn($expected);
7986

8087
$actual = $this->object->getFile('frontend', $theme, 'file.ext', 'Magento_Module');
8188
$this->assertSame($expected, $actual);
@@ -84,10 +91,11 @@ public function testGetFileWhenStateDeveloper()
8491
/**
8592
* Cover getFile when mode is default
8693
* @param string $mode
94+
* @param integer $onDemandInProduction
8795
* @param string $method
8896
* @dataProvider getMinifiedDataProvider
8997
*/
90-
public function testGetFileWhenModifiedNeeded($mode, $method)
98+
public function testGetFileWhenModifiedNeeded($mode, $onDemandInProduction, $method)
9199
{
92100
$this->assetConfig
93101
->expects($this->once())
@@ -98,13 +106,17 @@ public function testGetFileWhenModifiedNeeded($mode, $method)
98106
$expected = 'some/file.ext';
99107
$expectedMinified = '/path/to/minified/some/file.ext';
100108

109+
$this->deploymentConfigMock->expects($this->any())
110+
->method('getConfigData')
111+
->with(ConfigOptionsListConstants::CONFIG_PATH_SCD_ON_DEMAND_IN_PRODUCTION)
112+
->willReturn($onDemandInProduction);
101113
$this->state->expects($this->once())
102114
->method('getMode')
103115
->willReturn($mode);
104116
$this->resolver->expects($this->once())
105117
->method('resolve')
106118
->with(RulePool::TYPE_TEMPLATE_FILE, 'file.ext', 'frontend', $theme, null, 'Magento_Module')
107-
->will($this->returnValue($expected));
119+
->willReturn($expected);
108120
$this->minifier->expects($this->once())
109121
->method($method)
110122
->with($expected)
@@ -127,9 +139,10 @@ public function testGetFileIfMinificationIsDisabled()
127139
$this->resolver->expects($this->once())
128140
->method('resolve')
129141
->with(RulePool::TYPE_TEMPLATE_FILE, 'file.ext', 'frontend', $theme, null, 'Magento_Module')
130-
->will($this->returnValue($expected));
142+
->willReturn($expected);
131143

132-
$this->state->expects($this->never())->method('getMode');
144+
$this->state->expects($this->never())
145+
->method('getMode');
133146

134147
$actual = $this->object->getFile('frontend', $theme, 'file.ext', 'Magento_Module');
135148
$this->assertSame($expected, $actual);
@@ -143,8 +156,10 @@ public function testGetFileIfMinificationIsDisabled()
143156
public function getMinifiedDataProvider()
144157
{
145158
return [
146-
'default' => [State::MODE_DEFAULT, 'getMinified'],
147-
'production' => [State::MODE_PRODUCTION, 'getPathToMinified'],
159+
'default with on demand' => [State::MODE_DEFAULT, 1, 'getMinified'],
160+
'default without on demand' => [State::MODE_DEFAULT, 0, 'getMinified'],
161+
'production with on demand' => [State::MODE_PRODUCTION, 1, 'getMinified'],
162+
'production without on demand' => [State::MODE_PRODUCTION, 0, 'getPathToMinified'],
148163
];
149164
}
150165
}

0 commit comments

Comments
 (0)