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