Skip to content

Commit adb613f

Browse files
author
Bohdan Korablov
committed
MAGETWO-87025: Generate SCD in Production mode
1 parent b7dc2b3 commit adb613f

File tree

4 files changed

+222
-90
lines changed

4 files changed

+222
-90
lines changed

lib/internal/Magento/Framework/App/StaticResource.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ class StaticResource implements \Magento\Framework\AppInterface
6262
*/
6363
private $filesystem;
6464

65+
/**
66+
* @var DeploymentConfig
67+
*/
68+
private $deploymentConfig;
69+
6570
/**
6671
* @var \Psr\Log\LoggerInterface
6772
*/
@@ -76,6 +81,7 @@ class StaticResource implements \Magento\Framework\AppInterface
7681
* @param \Magento\Framework\Module\ModuleList $moduleList
7782
* @param \Magento\Framework\ObjectManagerInterface $objectManager
7883
* @param ConfigLoaderInterface $configLoader
84+
* @param DeploymentConfig|null $deploymentConfig
7985
*/
8086
public function __construct(
8187
State $state,
@@ -85,7 +91,8 @@ public function __construct(
8591
\Magento\Framework\View\Asset\Repository $assetRepo,
8692
\Magento\Framework\Module\ModuleList $moduleList,
8793
\Magento\Framework\ObjectManagerInterface $objectManager,
88-
ConfigLoaderInterface $configLoader
94+
ConfigLoaderInterface $configLoader,
95+
DeploymentConfig $deploymentConfig = null
8996
) {
9097
$this->state = $state;
9198
$this->response = $response;
@@ -95,6 +102,7 @@ public function __construct(
95102
$this->moduleList = $moduleList;
96103
$this->objectManager = $objectManager;
97104
$this->configLoader = $configLoader;
105+
$this->deploymentConfig = $deploymentConfig ?: ObjectManager::getInstance()->get(DeploymentConfig::class);
98106
}
99107

100108
/**
@@ -108,7 +116,9 @@ public function launch()
108116
// disabling profiling when retrieving static resource
109117
\Magento\Framework\Profiler::reset();
110118
$appMode = $this->state->getMode();
111-
if ($appMode == \Magento\Framework\App\State::MODE_PRODUCTION) {
119+
if ($appMode == \Magento\Framework\App\State::MODE_PRODUCTION
120+
&& !$this->deploymentConfig->getConfigData('static_content_on_demand_in_production')
121+
) {
112122
$this->response->setHttpResponseCode(404);
113123
} else {
114124
$path = $this->request->get('resource');

lib/internal/Magento/Framework/App/Test/Unit/StaticResourceTest.php

Lines changed: 142 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -7,97 +7,124 @@
77
namespace Magento\Framework\App\Test\Unit;
88

99
use Magento\Framework\App\Bootstrap;
10-
use Magento\Framework\Filesystem;
10+
use Magento\Framework\App\DeploymentConfig;
11+
use Magento\Framework\App\State;
12+
use Magento\Framework\App\Response\FileInterface;
13+
use Magento\Framework\App\Request\Http as HttpRequest;
14+
use Magento\Framework\App\View\Asset\Publisher;
15+
use Magento\Framework\View\Asset\Repository;
16+
use Magento\Framework\Module\ModuleList;
17+
use Magento\Framework\ObjectManagerInterface;
18+
use Magento\Framework\App\ObjectManager\ConfigLoader;
19+
use Magento\Framework\App\StaticResource;
20+
use Psr\Log\LoggerInterface;
21+
use PHPUnit_Framework_MockObject_MockObject as MockObject;
1122

1223
/**
1324
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1425
*/
1526
class StaticResourceTest extends \PHPUnit\Framework\TestCase
1627
{
1728
/**
18-
* @var \Magento\Framework\App\State|\PHPUnit_Framework_MockObject_MockObject
29+
* @var State|MockObject
1930
*/
20-
private $state;
31+
private $stateMock;
2132

2233
/**
23-
* @var \Magento\Framework\App\Response\FileInterface|\PHPUnit_Framework_MockObject_MockObject
34+
* @var FileInterface|MockObject
2435
*/
25-
private $response;
36+
private $responseMock;
2637

2738
/**
28-
* @var \Magento\Framework\App\Request\Http|\PHPUnit_Framework_MockObject_MockObject
39+
* @var HttpRequest|MockObject
2940
*/
30-
private $request;
41+
private $requestMock;
3142

3243
/**
33-
* @var \Magento\Framework\App\View\Asset\Publisher|\PHPUnit_Framework_MockObject_MockObject
44+
* @var Publisher|MockObject
3445
*/
35-
private $publisher;
46+
private $publisherMock;
3647

3748
/**
38-
* @var \Magento\Framework\View\Asset\Repository|\PHPUnit_Framework_MockObject_MockObject
49+
* @var Repository|MockObject
3950
*/
40-
private $assetRepo;
51+
private $assetRepoMock;
4152

4253
/**
43-
* @var \Magento\Framework\Module\ModuleList|\PHPUnit_Framework_MockObject_MockObject
54+
* @var ModuleList|MockObject
4455
*/
45-
private $moduleList;
56+
private $moduleListMock;
4657

4758
/**
48-
* @var \Magento\Framework\ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject
59+
* @var ObjectManagerInterface|MockObject
4960
*/
50-
private $objectManager;
61+
private $objectManagerMock;
5162

5263
/**
53-
* @var \Magento\Framework\App\ObjectManager\ConfigLoader|\PHPUnit_Framework_MockObject_MockObject
64+
* @var ConfigLoader|MockObject
5465
*/
55-
private $configLoader;
66+
private $configLoaderMock;
5667

5768
/**
58-
* @var \Magento\Framework\App\StaticResource
69+
* @var LoggerInterface|MockObject
5970
*/
60-
private $object;
71+
private $loggerMock;
72+
73+
/**
74+
* @var DeploymentConfig|MockObject
75+
*/
76+
private $deploymentConfigMock;
6177

6278
/**
63-
* @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
79+
* @var StaticResource
6480
*/
65-
private $logger;
81+
private $object;
6682

6783
protected function setUp()
6884
{
69-
$this->state = $this->createMock(\Magento\Framework\App\State::class);
70-
$this->response = $this->createMock(\Magento\MediaStorage\Model\File\Storage\Response::class);
71-
$this->request = $this->createMock(\Magento\Framework\App\Request\Http::class);
72-
$this->publisher = $this->createMock(\Magento\Framework\App\View\Asset\Publisher::class);
73-
$this->assetRepo = $this->createMock(\Magento\Framework\View\Asset\Repository::class);
74-
$this->moduleList = $this->createMock(\Magento\Framework\Module\ModuleList::class);
75-
$this->objectManager = $this->getMockForAbstractClass(\Magento\Framework\ObjectManagerInterface::class);
76-
$this->logger = $this->getMockForAbstractClass(\Psr\Log\LoggerInterface::class);
77-
$this->configLoader = $this->createMock(\Magento\Framework\App\ObjectManager\ConfigLoader::class);
78-
$this->object = new \Magento\Framework\App\StaticResource(
79-
$this->state,
80-
$this->response,
81-
$this->request,
82-
$this->publisher,
83-
$this->assetRepo,
84-
$this->moduleList,
85-
$this->objectManager,
86-
$this->configLoader,
87-
$this->getMockForAbstractClass(\Magento\Framework\View\DesignInterface::class)
85+
$this->stateMock = $this->createMock(State::class);
86+
$this->responseMock = $this->getMockForAbstractClass(FileInterface::class);
87+
$this->requestMock = $this->createMock(HttpRequest::class);
88+
$this->publisherMock = $this->createMock(Publisher::class);
89+
$this->assetRepoMock = $this->createMock(Repository::class);
90+
$this->moduleListMock = $this->createMock(ModuleList::class);
91+
$this->objectManagerMock = $this->getMockForAbstractClass(ObjectManagerInterface::class);
92+
$this->loggerMock = $this->getMockForAbstractClass(LoggerInterface::class);
93+
$this->configLoaderMock = $this->createMock(ConfigLoader::class);
94+
$this->deploymentConfigMock = $this->createMock(DeploymentConfig::class);
95+
$this->object = new StaticResource(
96+
$this->stateMock,
97+
$this->responseMock,
98+
$this->requestMock,
99+
$this->publisherMock,
100+
$this->assetRepoMock,
101+
$this->moduleListMock,
102+
$this->objectManagerMock,
103+
$this->configLoaderMock,
104+
$this->deploymentConfigMock
88105
);
89106
}
90107

91108
public function testLaunchProductionMode()
92109
{
93-
$this->state->expects($this->once())
110+
$this->stateMock->expects($this->once())
94111
->method('getMode')
95-
->will($this->returnValue(\Magento\Framework\App\State::MODE_PRODUCTION));
96-
$this->response->expects($this->once())
112+
->willReturn(State::MODE_PRODUCTION);
113+
$this->responseMock->expects($this->once())
97114
->method('setHttpResponseCode')
98115
->with(404);
99-
$this->response->expects($this->never())
116+
$this->responseMock->expects($this->never())
100117
->method('setFilePath');
118+
$this->stateMock->expects($this->never())->method('setAreaCode');
119+
$this->configLoaderMock->expects($this->never())->method('load');
120+
$this->objectManagerMock->expects($this->never())->method('configure');
121+
$this->requestMock->expects($this->never())->method('get');
122+
$this->moduleListMock->expects($this->never())->method('has');
123+
$asset = $this->getMockForAbstractClass(\Magento\Framework\View\Asset\LocalInterface::class);
124+
$asset->expects($this->never())->method('getSourceFile');
125+
$this->assetRepoMock->expects($this->never())->method('createAsset');
126+
$this->publisherMock->expects($this->never())->method('publish');
127+
$this->responseMock->expects($this->never())->method('setFilePath');
101128
$this->object->launch();
102129
}
103130

@@ -108,6 +135,8 @@ public function testLaunchProductionMode()
108135
* @param bool $moduleExists
109136
* @param string $expectedFile
110137
* @param array $expectedParams
138+
* @param int $getConfigDataExpects
139+
* @param int $staticContentOmDemandInProduction
111140
*
112141
* @dataProvider launchDataProvider
113142
*/
@@ -117,37 +146,47 @@ public function testLaunch(
117146
$requestedModule,
118147
$moduleExists,
119148
$expectedFile,
120-
array $expectedParams
149+
array $expectedParams,
150+
$getConfigDataExpects,
151+
$staticContentOmDemandInProduction
121152
) {
122-
$this->state->expects($this->once())
153+
$this->deploymentConfigMock->expects($this->exactly($getConfigDataExpects))
154+
->method('getConfigData')
155+
->with('static_content_on_demand_in_production')
156+
->willReturn($staticContentOmDemandInProduction);
157+
$this->stateMock->expects($this->once())
123158
->method('getMode')
124-
->will($this->returnValue($mode));
125-
$this->state->expects($this->once())
159+
->willReturn($mode);
160+
$this->stateMock->expects($this->once())
126161
->method('setAreaCode')
127162
->with('area');
128-
$this->configLoader->expects($this->once())
163+
$this->configLoaderMock->expects($this->once())
129164
->method('load')
130165
->with('area')
131-
->will($this->returnValue(['config']));
132-
$this->objectManager->expects($this->once())
166+
->willReturn(['config']);
167+
$this->objectManagerMock->expects($this->once())
133168
->method('configure')
134169
->with(['config']);
135-
$this->request->expects($this->once())
170+
$this->requestMock->expects($this->once())
136171
->method('get')
137172
->with('resource')
138-
->will($this->returnValue($requestedPath));
139-
$this->moduleList->expects($this->any())
173+
->willReturn($requestedPath);
174+
$this->moduleListMock->expects($this->any())
140175
->method('has')
141176
->with($requestedModule)
142-
->will($this->returnValue($moduleExists));
177+
->willReturn($moduleExists);
143178
$asset = $this->getMockForAbstractClass(\Magento\Framework\View\Asset\LocalInterface::class);
144-
$asset->expects($this->once())->method('getSourceFile')->will($this->returnValue('resource/file.css'));
145-
$this->assetRepo->expects($this->once())
179+
$asset->expects($this->once())
180+
->method('getSourceFile')
181+
->willReturn('resource/file.css');
182+
$this->assetRepoMock->expects($this->once())
146183
->method('createAsset')
147184
->with($expectedFile, $expectedParams)
148-
->will($this->returnValue($asset));
149-
$this->publisher->expects($this->once())->method('publish')->with($asset);
150-
$this->response->expects($this->once())
185+
->willReturn($asset);
186+
$this->publisherMock->expects($this->once())
187+
->method('publish')
188+
->with($asset);
189+
$this->responseMock->expects($this->once())
151190
->method('setFilePath')
152191
->with('resource/file.css');
153192
$this->object->launch();
@@ -166,6 +205,8 @@ public function launchDataProvider()
166205
false,
167206
'dir/file.js',
168207
['area' => 'area', 'locale' => 'locale', 'module' => '', 'theme' => 'Magento/theme'],
208+
0,
209+
0,
169210
],
170211
'default mode with modular resource' => [
171212
\Magento\Framework\App\State::MODE_DEFAULT,
@@ -176,6 +217,30 @@ public function launchDataProvider()
176217
[
177218
'area' => 'area', 'locale' => 'locale', 'module' => 'Namespace_Module', 'theme' => 'Magento/theme'
178219
],
220+
0,
221+
0,
222+
],
223+
'production mode with static_content_on_demand_in_production and with non-modular resource' => [
224+
\Magento\Framework\App\State::MODE_PRODUCTION,
225+
'area/Magento/theme/locale/dir/file.js',
226+
'dir',
227+
false,
228+
'dir/file.js',
229+
['area' => 'area', 'locale' => 'locale', 'module' => '', 'theme' => 'Magento/theme'],
230+
1,
231+
1,
232+
],
233+
'production mode with static_content_on_demand_in_production and with modular resource' => [
234+
\Magento\Framework\App\State::MODE_PRODUCTION,
235+
'area/Magento/theme/locale/Namespace_Module/dir/file.js',
236+
'Namespace_Module',
237+
true,
238+
'dir/file.js',
239+
[
240+
'area' => 'area', 'locale' => 'locale', 'module' => 'Namespace_Module', 'theme' => 'Magento/theme'
241+
],
242+
1,
243+
1,
179244
],
180245
];
181246
}
@@ -186,29 +251,36 @@ public function launchDataProvider()
186251
*/
187252
public function testLaunchWrongPath()
188253
{
189-
$this->state->expects($this->once())
254+
$this->stateMock->expects($this->once())
190255
->method('getMode')
191256
->will($this->returnValue(\Magento\Framework\App\State::MODE_DEVELOPER));
192-
$this->request->expects($this->once())
257+
$this->requestMock->expects($this->once())
193258
->method('get')
194259
->with('resource')
195-
->will($this->returnValue('short/path.js'));
260+
->willReturn('short/path.js');
196261
$this->object->launch();
197262
}
198263

199264
public function testCatchExceptionDeveloperMode()
200265
{
201-
$this->objectManager->expects($this->once())
266+
$this->objectManagerMock->expects($this->once())
202267
->method('get')
203268
->with(\Psr\Log\LoggerInterface::class)
204-
->willReturn($this->logger);
205-
$this->logger->expects($this->once())
269+
->willReturn($this->loggerMock);
270+
$this->loggerMock->expects($this->once())
206271
->method('critical');
207-
$bootstrap = $this->getMockBuilder(Bootstrap::class)->disableOriginalConstructor()->getMock();
208-
$bootstrap->expects($this->once())->method('isDeveloperMode')->willReturn(true);
272+
$bootstrap = $this->getMockBuilder(Bootstrap::class)
273+
->disableOriginalConstructor()
274+
->getMock();
275+
$bootstrap->expects($this->once())
276+
->method('isDeveloperMode')
277+
->willReturn(true);
209278
$exception = new \Exception('Error: nothing works');
210-
$this->response->expects($this->once())->method('setHttpResponseCode')->with(404);
211-
$this->response->expects($this->once())->method('sendResponse');
279+
$this->responseMock->expects($this->once())
280+
->method('setHttpResponseCode')
281+
->with(404);
282+
$this->responseMock->expects($this->once())
283+
->method('sendResponse');
212284
$this->assertTrue($this->object->catchException($bootstrap, $exception));
213285
}
214286
}

0 commit comments

Comments
 (0)