Skip to content
This repository was archived by the owner on Apr 29, 2019. It is now read-only.

Commit f76f280

Browse files
authored
Merge pull request #2054 from magento-thunder/MAGETWO-87025
[thunder] MAGETWO-87025: Generate SCD in Production mode
2 parents 1dcb55d + 9be225f commit f76f280

File tree

5 files changed

+236
-90
lines changed

5 files changed

+236
-90
lines changed

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Magento\Framework\App\Filesystem\DirectoryList;
99
use Magento\Framework\ObjectManager\ConfigLoaderInterface;
1010
use Magento\Framework\Filesystem;
11+
use Magento\Framework\Config\ConfigOptionsListConstants;
1112
use Psr\Log\LoggerInterface;
1213

1314
/**
@@ -62,6 +63,11 @@ class StaticResource implements \Magento\Framework\AppInterface
6263
*/
6364
private $filesystem;
6465

66+
/**
67+
* @var DeploymentConfig
68+
*/
69+
private $deploymentConfig;
70+
6571
/**
6672
* @var \Psr\Log\LoggerInterface
6773
*/
@@ -76,6 +82,7 @@ class StaticResource implements \Magento\Framework\AppInterface
7682
* @param \Magento\Framework\Module\ModuleList $moduleList
7783
* @param \Magento\Framework\ObjectManagerInterface $objectManager
7884
* @param ConfigLoaderInterface $configLoader
85+
* @param DeploymentConfig|null $deploymentConfig
7986
*/
8087
public function __construct(
8188
State $state,
@@ -85,7 +92,8 @@ public function __construct(
8592
\Magento\Framework\View\Asset\Repository $assetRepo,
8693
\Magento\Framework\Module\ModuleList $moduleList,
8794
\Magento\Framework\ObjectManagerInterface $objectManager,
88-
ConfigLoaderInterface $configLoader
95+
ConfigLoaderInterface $configLoader,
96+
DeploymentConfig $deploymentConfig = null
8997
) {
9098
$this->state = $state;
9199
$this->response = $response;
@@ -95,6 +103,7 @@ public function __construct(
95103
$this->moduleList = $moduleList;
96104
$this->objectManager = $objectManager;
97105
$this->configLoader = $configLoader;
106+
$this->deploymentConfig = $deploymentConfig ?: ObjectManager::getInstance()->get(DeploymentConfig::class);
98107
}
99108

100109
/**
@@ -108,7 +117,11 @@ public function launch()
108117
// disabling profiling when retrieving static resource
109118
\Magento\Framework\Profiler::reset();
110119
$appMode = $this->state->getMode();
111-
if ($appMode == \Magento\Framework\App\State::MODE_PRODUCTION) {
120+
if ($appMode == \Magento\Framework\App\State::MODE_PRODUCTION
121+
&& !$this->deploymentConfig->getConfigData(
122+
ConfigOptionsListConstants::CONFIG_PATH_SCD_ON_DEMAND_IN_PRODUCTION
123+
)
124+
) {
112125
$this->response->setHttpResponseCode(404);
113126
} else {
114127
$path = $this->request->get('resource');

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

Lines changed: 143 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -7,97 +7,125 @@
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 Magento\Framework\Config\ConfigOptionsListConstants;
21+
use Psr\Log\LoggerInterface;
22+
use PHPUnit_Framework_MockObject_MockObject as MockObject;
1123

1224
/**
1325
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1426
*/
1527
class StaticResourceTest extends \PHPUnit\Framework\TestCase
1628
{
1729
/**
18-
* @var \Magento\Framework\App\State|\PHPUnit_Framework_MockObject_MockObject
30+
* @var State|MockObject
1931
*/
20-
private $state;
32+
private $stateMock;
2133

2234
/**
23-
* @var \Magento\Framework\App\Response\FileInterface|\PHPUnit_Framework_MockObject_MockObject
35+
* @var FileInterface|MockObject
2436
*/
25-
private $response;
37+
private $responseMock;
2638

2739
/**
28-
* @var \Magento\Framework\App\Request\Http|\PHPUnit_Framework_MockObject_MockObject
40+
* @var HttpRequest|MockObject
2941
*/
30-
private $request;
42+
private $requestMock;
3143

3244
/**
33-
* @var \Magento\Framework\App\View\Asset\Publisher|\PHPUnit_Framework_MockObject_MockObject
45+
* @var Publisher|MockObject
3446
*/
35-
private $publisher;
47+
private $publisherMock;
3648

3749
/**
38-
* @var \Magento\Framework\View\Asset\Repository|\PHPUnit_Framework_MockObject_MockObject
50+
* @var Repository|MockObject
3951
*/
40-
private $assetRepo;
52+
private $assetRepoMock;
4153

4254
/**
43-
* @var \Magento\Framework\Module\ModuleList|\PHPUnit_Framework_MockObject_MockObject
55+
* @var ModuleList|MockObject
4456
*/
45-
private $moduleList;
57+
private $moduleListMock;
4658

4759
/**
48-
* @var \Magento\Framework\ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject
60+
* @var ObjectManagerInterface|MockObject
4961
*/
50-
private $objectManager;
62+
private $objectManagerMock;
5163

5264
/**
53-
* @var \Magento\Framework\App\ObjectManager\ConfigLoader|\PHPUnit_Framework_MockObject_MockObject
65+
* @var ConfigLoader|MockObject
5466
*/
55-
private $configLoader;
67+
private $configLoaderMock;
5668

5769
/**
58-
* @var \Magento\Framework\App\StaticResource
70+
* @var LoggerInterface|MockObject
5971
*/
60-
private $object;
72+
private $loggerMock;
73+
74+
/**
75+
* @var DeploymentConfig|MockObject
76+
*/
77+
private $deploymentConfigMock;
6178

6279
/**
63-
* @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
80+
* @var StaticResource
6481
*/
65-
private $logger;
82+
private $object;
6683

6784
protected function setUp()
6885
{
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)
86+
$this->stateMock = $this->createMock(State::class);
87+
$this->responseMock = $this->getMockForAbstractClass(FileInterface::class);
88+
$this->requestMock = $this->createMock(HttpRequest::class);
89+
$this->publisherMock = $this->createMock(Publisher::class);
90+
$this->assetRepoMock = $this->createMock(Repository::class);
91+
$this->moduleListMock = $this->createMock(ModuleList::class);
92+
$this->objectManagerMock = $this->getMockForAbstractClass(ObjectManagerInterface::class);
93+
$this->loggerMock = $this->getMockForAbstractClass(LoggerInterface::class);
94+
$this->configLoaderMock = $this->createMock(ConfigLoader::class);
95+
$this->deploymentConfigMock = $this->createMock(DeploymentConfig::class);
96+
$this->object = new StaticResource(
97+
$this->stateMock,
98+
$this->responseMock,
99+
$this->requestMock,
100+
$this->publisherMock,
101+
$this->assetRepoMock,
102+
$this->moduleListMock,
103+
$this->objectManagerMock,
104+
$this->configLoaderMock,
105+
$this->deploymentConfigMock
88106
);
89107
}
90108

91109
public function testLaunchProductionMode()
92110
{
93-
$this->state->expects($this->once())
111+
$this->stateMock->expects($this->once())
94112
->method('getMode')
95-
->will($this->returnValue(\Magento\Framework\App\State::MODE_PRODUCTION));
96-
$this->response->expects($this->once())
113+
->willReturn(State::MODE_PRODUCTION);
114+
$this->responseMock->expects($this->once())
97115
->method('setHttpResponseCode')
98116
->with(404);
99-
$this->response->expects($this->never())
117+
$this->responseMock->expects($this->never())
100118
->method('setFilePath');
119+
$this->stateMock->expects($this->never())->method('setAreaCode');
120+
$this->configLoaderMock->expects($this->never())->method('load');
121+
$this->objectManagerMock->expects($this->never())->method('configure');
122+
$this->requestMock->expects($this->never())->method('get');
123+
$this->moduleListMock->expects($this->never())->method('has');
124+
$asset = $this->getMockForAbstractClass(\Magento\Framework\View\Asset\LocalInterface::class);
125+
$asset->expects($this->never())->method('getSourceFile');
126+
$this->assetRepoMock->expects($this->never())->method('createAsset');
127+
$this->publisherMock->expects($this->never())->method('publish');
128+
$this->responseMock->expects($this->never())->method('setFilePath');
101129
$this->object->launch();
102130
}
103131

@@ -108,6 +136,8 @@ public function testLaunchProductionMode()
108136
* @param bool $moduleExists
109137
* @param string $expectedFile
110138
* @param array $expectedParams
139+
* @param int $getConfigDataExpects
140+
* @param int $staticContentOmDemandInProduction
111141
*
112142
* @dataProvider launchDataProvider
113143
*/
@@ -117,37 +147,47 @@ public function testLaunch(
117147
$requestedModule,
118148
$moduleExists,
119149
$expectedFile,
120-
array $expectedParams
150+
array $expectedParams,
151+
$getConfigDataExpects,
152+
$staticContentOmDemandInProduction
121153
) {
122-
$this->state->expects($this->once())
154+
$this->deploymentConfigMock->expects($this->exactly($getConfigDataExpects))
155+
->method('getConfigData')
156+
->with(ConfigOptionsListConstants::CONFIG_PATH_SCD_ON_DEMAND_IN_PRODUCTION)
157+
->willReturn($staticContentOmDemandInProduction);
158+
$this->stateMock->expects($this->once())
123159
->method('getMode')
124-
->will($this->returnValue($mode));
125-
$this->state->expects($this->once())
160+
->willReturn($mode);
161+
$this->stateMock->expects($this->once())
126162
->method('setAreaCode')
127163
->with('area');
128-
$this->configLoader->expects($this->once())
164+
$this->configLoaderMock->expects($this->once())
129165
->method('load')
130166
->with('area')
131-
->will($this->returnValue(['config']));
132-
$this->objectManager->expects($this->once())
167+
->willReturn(['config']);
168+
$this->objectManagerMock->expects($this->once())
133169
->method('configure')
134170
->with(['config']);
135-
$this->request->expects($this->once())
171+
$this->requestMock->expects($this->once())
136172
->method('get')
137173
->with('resource')
138-
->will($this->returnValue($requestedPath));
139-
$this->moduleList->expects($this->any())
174+
->willReturn($requestedPath);
175+
$this->moduleListMock->expects($this->any())
140176
->method('has')
141177
->with($requestedModule)
142-
->will($this->returnValue($moduleExists));
178+
->willReturn($moduleExists);
143179
$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())
180+
$asset->expects($this->once())
181+
->method('getSourceFile')
182+
->willReturn('resource/file.css');
183+
$this->assetRepoMock->expects($this->once())
146184
->method('createAsset')
147185
->with($expectedFile, $expectedParams)
148-
->will($this->returnValue($asset));
149-
$this->publisher->expects($this->once())->method('publish')->with($asset);
150-
$this->response->expects($this->once())
186+
->willReturn($asset);
187+
$this->publisherMock->expects($this->once())
188+
->method('publish')
189+
->with($asset);
190+
$this->responseMock->expects($this->once())
151191
->method('setFilePath')
152192
->with('resource/file.css');
153193
$this->object->launch();
@@ -166,6 +206,8 @@ public function launchDataProvider()
166206
false,
167207
'dir/file.js',
168208
['area' => 'area', 'locale' => 'locale', 'module' => '', 'theme' => 'Magento/theme'],
209+
0,
210+
0,
169211
],
170212
'default mode with modular resource' => [
171213
\Magento\Framework\App\State::MODE_DEFAULT,
@@ -176,6 +218,30 @@ public function launchDataProvider()
176218
[
177219
'area' => 'area', 'locale' => 'locale', 'module' => 'Namespace_Module', 'theme' => 'Magento/theme'
178220
],
221+
0,
222+
0,
223+
],
224+
'production mode with static_content_on_demand_in_production and with non-modular resource' => [
225+
\Magento\Framework\App\State::MODE_PRODUCTION,
226+
'area/Magento/theme/locale/dir/file.js',
227+
'dir',
228+
false,
229+
'dir/file.js',
230+
['area' => 'area', 'locale' => 'locale', 'module' => '', 'theme' => 'Magento/theme'],
231+
1,
232+
1,
233+
],
234+
'production mode with static_content_on_demand_in_production and with modular resource' => [
235+
\Magento\Framework\App\State::MODE_PRODUCTION,
236+
'area/Magento/theme/locale/Namespace_Module/dir/file.js',
237+
'Namespace_Module',
238+
true,
239+
'dir/file.js',
240+
[
241+
'area' => 'area', 'locale' => 'locale', 'module' => 'Namespace_Module', 'theme' => 'Magento/theme'
242+
],
243+
1,
244+
1,
179245
],
180246
];
181247
}
@@ -186,29 +252,36 @@ public function launchDataProvider()
186252
*/
187253
public function testLaunchWrongPath()
188254
{
189-
$this->state->expects($this->once())
255+
$this->stateMock->expects($this->once())
190256
->method('getMode')
191257
->will($this->returnValue(\Magento\Framework\App\State::MODE_DEVELOPER));
192-
$this->request->expects($this->once())
258+
$this->requestMock->expects($this->once())
193259
->method('get')
194260
->with('resource')
195-
->will($this->returnValue('short/path.js'));
261+
->willReturn('short/path.js');
196262
$this->object->launch();
197263
}
198264

199265
public function testCatchExceptionDeveloperMode()
200266
{
201-
$this->objectManager->expects($this->once())
267+
$this->objectManagerMock->expects($this->once())
202268
->method('get')
203269
->with(\Psr\Log\LoggerInterface::class)
204-
->willReturn($this->logger);
205-
$this->logger->expects($this->once())
270+
->willReturn($this->loggerMock);
271+
$this->loggerMock->expects($this->once())
206272
->method('critical');
207-
$bootstrap = $this->getMockBuilder(Bootstrap::class)->disableOriginalConstructor()->getMock();
208-
$bootstrap->expects($this->once())->method('isDeveloperMode')->willReturn(true);
273+
$bootstrap = $this->getMockBuilder(Bootstrap::class)
274+
->disableOriginalConstructor()
275+
->getMock();
276+
$bootstrap->expects($this->once())
277+
->method('isDeveloperMode')
278+
->willReturn(true);
209279
$exception = new \Exception('Error: nothing works');
210-
$this->response->expects($this->once())->method('setHttpResponseCode')->with(404);
211-
$this->response->expects($this->once())->method('sendResponse');
280+
$this->responseMock->expects($this->once())
281+
->method('setHttpResponseCode')
282+
->with(404);
283+
$this->responseMock->expects($this->once())
284+
->method('sendResponse');
212285
$this->assertTrue($this->object->catchException($bootstrap, $exception));
213286
}
214287
}

0 commit comments

Comments
 (0)