7
7
namespace Magento \Framework \App \Test \Unit ;
8
8
9
9
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 ;
11
23
12
24
/**
13
25
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
14
26
*/
15
27
class StaticResourceTest extends \PHPUnit \Framework \TestCase
16
28
{
17
29
/**
18
- * @var \Magento\Framework\App\ State|\PHPUnit_Framework_MockObject_MockObject
30
+ * @var State|MockObject
19
31
*/
20
- private $ state ;
32
+ private $ stateMock ;
21
33
22
34
/**
23
- * @var \Magento\Framework\App\Response\ FileInterface|\PHPUnit_Framework_MockObject_MockObject
35
+ * @var FileInterface|MockObject
24
36
*/
25
- private $ response ;
37
+ private $ responseMock ;
26
38
27
39
/**
28
- * @var \Magento\Framework\App\Request\Http|\PHPUnit_Framework_MockObject_MockObject
40
+ * @var HttpRequest|MockObject
29
41
*/
30
- private $ request ;
42
+ private $ requestMock ;
31
43
32
44
/**
33
- * @var \Magento\Framework\App\View\Asset\ Publisher|\PHPUnit_Framework_MockObject_MockObject
45
+ * @var Publisher|MockObject
34
46
*/
35
- private $ publisher ;
47
+ private $ publisherMock ;
36
48
37
49
/**
38
- * @var \Magento\Framework\View\Asset\ Repository|\PHPUnit_Framework_MockObject_MockObject
50
+ * @var Repository|MockObject
39
51
*/
40
- private $ assetRepo ;
52
+ private $ assetRepoMock ;
41
53
42
54
/**
43
- * @var \Magento\Framework\Module\ ModuleList|\PHPUnit_Framework_MockObject_MockObject
55
+ * @var ModuleList|MockObject
44
56
*/
45
- private $ moduleList ;
57
+ private $ moduleListMock ;
46
58
47
59
/**
48
- * @var \Magento\Framework\ ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject
60
+ * @var ObjectManagerInterface|MockObject
49
61
*/
50
- private $ objectManager ;
62
+ private $ objectManagerMock ;
51
63
52
64
/**
53
- * @var \Magento\Framework\App\ObjectManager\ ConfigLoader|\PHPUnit_Framework_MockObject_MockObject
65
+ * @var ConfigLoader|MockObject
54
66
*/
55
- private $ configLoader ;
67
+ private $ configLoaderMock ;
56
68
57
69
/**
58
- * @var \Magento\Framework\App\StaticResource
70
+ * @var LoggerInterface|MockObject
59
71
*/
60
- private $ object ;
72
+ private $ loggerMock ;
73
+
74
+ /**
75
+ * @var DeploymentConfig|MockObject
76
+ */
77
+ private $ deploymentConfigMock ;
61
78
62
79
/**
63
- * @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
80
+ * @var StaticResource
64
81
*/
65
- private $ logger ;
82
+ private $ object ;
66
83
67
84
protected function setUp ()
68
85
{
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
88
106
);
89
107
}
90
108
91
109
public function testLaunchProductionMode ()
92
110
{
93
- $ this ->state ->expects ($ this ->once ())
111
+ $ this ->stateMock ->expects ($ this ->once ())
94
112
->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 ())
97
115
->method ('setHttpResponseCode ' )
98
116
->with (404 );
99
- $ this ->response ->expects ($ this ->never ())
117
+ $ this ->responseMock ->expects ($ this ->never ())
100
118
->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 ' );
101
129
$ this ->object ->launch ();
102
130
}
103
131
@@ -108,6 +136,8 @@ public function testLaunchProductionMode()
108
136
* @param bool $moduleExists
109
137
* @param string $expectedFile
110
138
* @param array $expectedParams
139
+ * @param int $getConfigDataExpects
140
+ * @param int $staticContentOmDemandInProduction
111
141
*
112
142
* @dataProvider launchDataProvider
113
143
*/
@@ -117,37 +147,47 @@ public function testLaunch(
117
147
$ requestedModule ,
118
148
$ moduleExists ,
119
149
$ expectedFile ,
120
- array $ expectedParams
150
+ array $ expectedParams ,
151
+ $ getConfigDataExpects ,
152
+ $ staticContentOmDemandInProduction
121
153
) {
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 ())
123
159
->method ('getMode ' )
124
- ->will ( $ this -> returnValue ( $ mode) );
125
- $ this ->state ->expects ($ this ->once ())
160
+ ->willReturn ( $ mode );
161
+ $ this ->stateMock ->expects ($ this ->once ())
126
162
->method ('setAreaCode ' )
127
163
->with ('area ' );
128
- $ this ->configLoader ->expects ($ this ->once ())
164
+ $ this ->configLoaderMock ->expects ($ this ->once ())
129
165
->method ('load ' )
130
166
->with ('area ' )
131
- ->will ( $ this -> returnValue ( ['config ' ]) );
132
- $ this ->objectManager ->expects ($ this ->once ())
167
+ ->willReturn ( ['config ' ]);
168
+ $ this ->objectManagerMock ->expects ($ this ->once ())
133
169
->method ('configure ' )
134
170
->with (['config ' ]);
135
- $ this ->request ->expects ($ this ->once ())
171
+ $ this ->requestMock ->expects ($ this ->once ())
136
172
->method ('get ' )
137
173
->with ('resource ' )
138
- ->will ( $ this -> returnValue ( $ requestedPath) );
139
- $ this ->moduleList ->expects ($ this ->any ())
174
+ ->willReturn ( $ requestedPath );
175
+ $ this ->moduleListMock ->expects ($ this ->any ())
140
176
->method ('has ' )
141
177
->with ($ requestedModule )
142
- ->will ( $ this -> returnValue ( $ moduleExists) );
178
+ ->willReturn ( $ moduleExists );
143
179
$ 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 ())
146
184
->method ('createAsset ' )
147
185
->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 ())
151
191
->method ('setFilePath ' )
152
192
->with ('resource/file.css ' );
153
193
$ this ->object ->launch ();
@@ -166,6 +206,8 @@ public function launchDataProvider()
166
206
false ,
167
207
'dir/file.js ' ,
168
208
['area ' => 'area ' , 'locale ' => 'locale ' , 'module ' => '' , 'theme ' => 'Magento/theme ' ],
209
+ 0 ,
210
+ 0 ,
169
211
],
170
212
'default mode with modular resource ' => [
171
213
\Magento \Framework \App \State::MODE_DEFAULT ,
@@ -176,6 +218,30 @@ public function launchDataProvider()
176
218
[
177
219
'area ' => 'area ' , 'locale ' => 'locale ' , 'module ' => 'Namespace_Module ' , 'theme ' => 'Magento/theme '
178
220
],
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 ,
179
245
],
180
246
];
181
247
}
@@ -186,29 +252,36 @@ public function launchDataProvider()
186
252
*/
187
253
public function testLaunchWrongPath ()
188
254
{
189
- $ this ->state ->expects ($ this ->once ())
255
+ $ this ->stateMock ->expects ($ this ->once ())
190
256
->method ('getMode ' )
191
257
->will ($ this ->returnValue (\Magento \Framework \App \State::MODE_DEVELOPER ));
192
- $ this ->request ->expects ($ this ->once ())
258
+ $ this ->requestMock ->expects ($ this ->once ())
193
259
->method ('get ' )
194
260
->with ('resource ' )
195
- ->will ( $ this -> returnValue ( 'short/path.js ' ) );
261
+ ->willReturn ( 'short/path.js ' );
196
262
$ this ->object ->launch ();
197
263
}
198
264
199
265
public function testCatchExceptionDeveloperMode ()
200
266
{
201
- $ this ->objectManager ->expects ($ this ->once ())
267
+ $ this ->objectManagerMock ->expects ($ this ->once ())
202
268
->method ('get ' )
203
269
->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 ())
206
272
->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 );
209
279
$ 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 ' );
212
285
$ this ->assertTrue ($ this ->object ->catchException ($ bootstrap , $ exception ));
213
286
}
214
287
}
0 commit comments