10
10
use Magento \Framework \Css \PreProcessor \ErrorHandlerInterface ;
11
11
use Magento \Framework \Css \PreProcessor \Instruction \Import ;
12
12
use Magento \Framework \Css \PreProcessor \Instruction \MagentoImport ;
13
+ use Magento \Framework \Module \Manager as ModuleManager ;
13
14
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
14
15
use Magento \Framework \View \Asset \File ;
15
16
use Magento \Framework \View \Asset \File \FallbackContext ;
@@ -30,32 +31,37 @@ class MagentoImportTest extends TestCase
30
31
/**
31
32
* @var DesignInterface|MockObject
32
33
*/
33
- private $ design ;
34
+ private $ designMock ;
34
35
35
36
/**
36
37
* @var CollectorInterface|MockObject
37
38
*/
38
- private $ fileSource ;
39
+ private $ fileSourceMock ;
39
40
40
41
/**
41
42
* @var ErrorHandlerInterface|MockObject
42
43
*/
43
- private $ errorHandler ;
44
+ private $ errorHandlerMock ;
44
45
45
46
/**
46
47
* @var File|MockObject
47
48
*/
48
- private $ asset ;
49
+ private $ assetMock ;
49
50
50
51
/**
51
52
* @var Repository|MockObject
52
53
*/
53
- private $ assetRepo ;
54
+ private $ assetRepoMock ;
54
55
55
56
/**
56
57
* @var ThemeProviderInterface|MockObject
57
58
*/
58
- private $ themeProvider ;
59
+ private $ themeProviderMock ;
60
+
61
+ /**
62
+ * @var ModuleManager|MockObject
63
+ */
64
+ private $ moduleManagerMock ;
59
65
60
66
/**
61
67
* @var Import
@@ -64,21 +70,23 @@ class MagentoImportTest extends TestCase
64
70
65
71
protected function setUp (): void
66
72
{
67
- $ this ->design = $ this ->getMockForAbstractClass (DesignInterface::class);
68
- $ this ->fileSource = $ this ->getMockForAbstractClass (CollectorInterface::class);
69
- $ this ->errorHandler = $ this ->getMockForAbstractClass (
70
- ErrorHandlerInterface ::class
71
- );
72
- $ this ->asset = $ this ->createMock (File ::class);
73
- $ this ->asset -> expects ( $ this ->any ())-> method ( ' getContentType ' )-> willReturn ( ' css ' );
74
- $ this ->assetRepo = $ this ->createMock (Repository ::class);
75
- $ this -> themeProvider = $ this -> getMockForAbstractClass (ThemeProviderInterface::class);
73
+ $ this ->designMock = $ this ->getMockForAbstractClass (DesignInterface::class);
74
+ $ this ->fileSourceMock = $ this ->getMockForAbstractClass (CollectorInterface::class);
75
+ $ this ->errorHandlerMock = $ this ->getMockForAbstractClass (ErrorHandlerInterface::class);
76
+ $ this -> assetMock = $ this -> createMock (File ::class);
77
+ $ this -> assetMock -> expects ( $ this -> any ())-> method ( ' getContentType ' )-> willReturn ( ' css ' );
78
+ $ this ->assetRepoMock = $ this ->createMock (Repository ::class);
79
+ $ this ->themeProviderMock = $ this ->getMockForAbstractClass (ThemeProviderInterface::class );
80
+ $ this ->moduleManagerMock = $ this ->createMock (ModuleManager ::class);
81
+
76
82
$ this ->object = (new ObjectManager ($ this ))->getObject (MagentoImport::class, [
77
- 'design ' => $ this ->design ,
78
- 'fileSource ' => $ this ->fileSource ,
79
- 'errorHandler ' => $ this ->errorHandler ,
80
- 'assetRepo ' => $ this ->assetRepo ,
81
- 'themeProvider ' => $ this ->themeProvider
83
+ 'design ' => $ this ->designMock ,
84
+ 'fileSource ' => $ this ->fileSourceMock ,
85
+ 'errorHandler ' => $ this ->errorHandlerMock ,
86
+ 'assetRepo ' => $ this ->assetRepoMock ,
87
+ 'moduleManager ' => $ this ->moduleManagerMock ,
88
+ // Mocking private property
89
+ 'themeProvider ' => $ this ->themeProviderMock ,
82
90
]);
83
91
}
84
92
@@ -88,24 +96,32 @@ protected function setUp(): void
88
96
* @param string $resolvedPath
89
97
* @param array $foundFiles
90
98
* @param string $expectedContent
99
+ * @param array $enabledModules
91
100
*
92
101
* @dataProvider processDataProvider
93
102
*/
94
- public function testProcess ($ originalContent , $ foundPath , $ resolvedPath , $ foundFiles , $ expectedContent )
103
+ public function testProcess (
104
+ string $ originalContent ,
105
+ string $ foundPath ,
106
+ string $ resolvedPath ,
107
+ array $ foundFiles ,
108
+ string $ expectedContent ,
109
+ array $ enabledModules
110
+ ): void
95
111
{
96
- $ chain = new Chain ($ this ->asset , $ originalContent , 'css ' , 'path ' );
112
+ $ chain = new Chain ($ this ->assetMock , $ originalContent , 'css ' , 'path ' );
97
113
$ relatedAsset = $ this ->createMock (File::class);
98
114
$ relatedAsset ->expects ($ this ->once ())
99
115
->method ('getFilePath ' )
100
116
->willReturn ($ resolvedPath );
101
117
$ context = $ this ->createMock (FallbackContext::class);
102
- $ this ->assetRepo ->expects ($ this ->once ())
118
+ $ this ->assetRepoMock ->expects ($ this ->once ())
103
119
->method ('createRelated ' )
104
- ->with ($ foundPath , $ this ->asset )
120
+ ->with ($ foundPath , $ this ->assetMock )
105
121
->willReturn ($ relatedAsset );
106
122
$ relatedAsset ->expects ($ this ->once ())->method ('getContext ' )->willReturn ($ context );
107
123
$ theme = $ this ->getMockForAbstractClass (ThemeInterface::class);
108
- $ this ->themeProvider ->expects ($ this ->once ())->method ('getThemeByFullPath ' )->willReturn ($ theme );
124
+ $ this ->themeProviderMock ->expects ($ this ->once ())->method ('getThemeByFullPath ' )->willReturn ($ theme );
109
125
$ files = [];
110
126
foreach ($ foundFiles as $ file ) {
111
127
$ fileObject = $ this ->createMock (\Magento \Framework \View \File::class);
@@ -117,10 +133,16 @@ public function testProcess($originalContent, $foundPath, $resolvedPath, $foundF
117
133
->willReturn ($ file ['filename ' ]);
118
134
$ files [] = $ fileObject ;
119
135
}
120
- $ this ->fileSource ->expects ($ this ->once ())
136
+ $ this ->fileSourceMock ->expects ($ this ->once ())
121
137
->method ('getFiles ' )
122
138
->with ($ theme , $ resolvedPath )
123
139
->willReturn ($ files );
140
+
141
+ $ this ->moduleManagerMock ->expects ($ this ->any ())->method ('isEnabled ' )
142
+ ->willReturnCallback (function ($ moduleName ) use ($ enabledModules ) {
143
+ return in_array ($ moduleName , $ enabledModules , true );
144
+ });
145
+
124
146
$ this ->object ->process ($ chain );
125
147
$ this ->assertEquals ($ expectedContent , $ chain ->getContent ());
126
148
$ this ->assertEquals ('css ' , $ chain ->getContentType ());
@@ -129,7 +151,7 @@ public function testProcess($originalContent, $foundPath, $resolvedPath, $foundF
129
151
/**
130
152
* @return array
131
153
*/
132
- public function processDataProvider ()
154
+ public function processDataProvider (): array
133
155
{
134
156
return [
135
157
'non-modular notation ' => [
@@ -141,6 +163,7 @@ public function processDataProvider()
141
163
['module ' => null , 'filename ' => 'theme/some/file.css ' ],
142
164
],
143
165
"@import 'some/file.css'; \n@import 'some/file.css'; \n" ,
166
+ [],
144
167
],
145
168
'modular ' => [
146
169
'//@magento_import "Magento_Module::some/file.css"; ' ,
@@ -151,6 +174,29 @@ public function processDataProvider()
151
174
['module ' => 'Magento_Two ' , 'filename ' => 'some/file.css ' ],
152
175
],
153
176
"@import 'Magento_Module::some/file.css'; \n@import 'Magento_Two::some/file.css'; \n" ,
177
+ ['Magento_Module ' , 'Magento_Two ' ],
178
+ ],
179
+ 'modular with disabled module ' => [
180
+ '//@magento_import "Magento_Module::some/file.css"; ' ,
181
+ 'Magento_Module::some/file.css ' ,
182
+ 'some/file.css ' ,
183
+ [
184
+ ['module ' => 'Magento_Module ' , 'filename ' => 'some/file.css ' ],
185
+ ['module ' => 'Magento_Two ' , 'filename ' => 'some/file.css ' ],
186
+ ],
187
+ "@import 'Magento_Two::some/file.css'; \n" ,
188
+ ['Magento_Two ' ],
189
+ ],
190
+ 'modular with disabled all modules ' => [
191
+ '//@magento_import "Magento_Module::some/file.css"; ' ,
192
+ 'Magento_Module::some/file.css ' ,
193
+ 'some/file.css ' ,
194
+ [
195
+ ['module ' => 'Magento_Module ' , 'filename ' => 'some/file.css ' ],
196
+ ['module ' => 'Magento_Two ' , 'filename ' => 'some/file.css ' ],
197
+ ],
198
+ '' ,
199
+ [],
154
200
],
155
201
'non-modular reference notation ' => [
156
202
'//@magento_import (reference) "some/file.css"; ' ,
@@ -161,6 +207,7 @@ public function processDataProvider()
161
207
['module ' => null , 'filename ' => 'theme/some/file.css ' ],
162
208
],
163
209
"@import (reference) 'some/file.css'; \n@import (reference) 'some/file.css'; \n" ,
210
+ [],
164
211
],
165
212
'modular reference ' => [
166
213
'//@magento_import (reference) "Magento_Module::some/file.css"; ' ,
@@ -172,35 +219,58 @@ public function processDataProvider()
172
219
],
173
220
"@import (reference) 'Magento_Module::some/file.css'; \n" .
174
221
"@import (reference) 'Magento_Two::some/file.css'; \n" ,
222
+ ['Magento_Module ' , 'Magento_Two ' ],
223
+ ],
224
+ 'modular reference with disabled module ' => [
225
+ '//@magento_import (reference) "Magento_Module::some/file.css"; ' ,
226
+ 'Magento_Module::some/file.css ' ,
227
+ 'some/file.css ' ,
228
+ [
229
+ ['module ' => 'Magento_Module ' , 'filename ' => 'some/file.css ' ],
230
+ ['module ' => 'Magento_Two ' , 'filename ' => 'some/file.css ' ],
231
+ ],
232
+ "@import (reference) 'Magento_Module::some/file.css'; \n" ,
233
+ ['Magento_Module ' ],
234
+ ],
235
+ 'modular reference with disabled all modules ' => [
236
+ '//@magento_import (reference) "Magento_Module::some/file.css"; ' ,
237
+ 'Magento_Module::some/file.css ' ,
238
+ 'some/file.css ' ,
239
+ [
240
+ ['module ' => 'Magento_Module ' , 'filename ' => 'some/file.css ' ],
241
+ ['module ' => 'Magento_Two ' , 'filename ' => 'some/file.css ' ],
242
+ ],
243
+ '' ,
244
+ [],
175
245
],
176
246
];
177
247
}
178
248
179
- public function testProcessNoImport ()
249
+ public function testProcessNoImport (): void
180
250
{
181
251
$ originalContent = 'color: #000000; ' ;
182
252
$ expectedContent = 'color: #000000; ' ;
183
- $ chain = new Chain ($ this ->asset , $ originalContent , 'css ' , 'orig ' );
184
- $ this ->assetRepo ->expects ($ this ->never ())
253
+ $ chain = new Chain ($ this ->assetMock , $ originalContent , 'css ' , 'orig ' );
254
+ $ this ->assetRepoMock ->expects ($ this ->never ())
185
255
->method ('createRelated ' );
186
256
$ this ->object ->process ($ chain );
187
257
$ this ->assertEquals ($ expectedContent , $ chain ->getContent ());
188
258
$ this ->assertEquals ('css ' , $ chain ->getContentType ());
189
259
}
190
260
191
- public function testProcessException ()
261
+ public function testProcessException (): void
192
262
{
193
263
$ chain = new Chain (
194
- $ this ->asset ,
264
+ $ this ->assetMock ,
195
265
'//@magento_import "some/file.css"; ' ,
196
266
'css ' ,
197
267
'path '
198
268
);
199
269
$ exception = new \LogicException ('Error happened ' );
200
- $ this ->assetRepo ->expects ($ this ->once ())
270
+ $ this ->assetRepoMock ->expects ($ this ->once ())
201
271
->method ('createRelated ' )
202
272
->willThrowException ($ exception );
203
- $ this ->errorHandler ->expects ($ this ->once ())
273
+ $ this ->errorHandlerMock ->expects ($ this ->once ())
204
274
->method ('processException ' )
205
275
->with ($ exception );
206
276
$ this ->object ->process ($ chain );
0 commit comments