17
17
use PHPUnit \Framework \MockObject \MockObject ;
18
18
use PHPUnit \Framework \TestCase ;
19
19
use Symfony \Component \Serializer \Encoder \JsonDecode ;
20
+ use Symfony \Component \Serializer \Exception \UnexpectedValueException ;
20
21
21
22
/**
22
23
* @covers \OleksiiBulba\WebpackEncorePlugin\Asset\EntrypointLookup
23
24
*/
24
25
class EntrypointLookupTest extends TestCase
25
26
{
27
+ public const DECODED_ENTRYPOINTS = [
28
+ 'entrypoints ' => [
29
+ 'entrypoint1 ' => [
30
+ 'js ' => [
31
+ '/some/path/to/jsFile.js ' ,
32
+ '/another/path/to/js/file.js ' ,
33
+ '/build/app.js ' ,
34
+ ],
35
+ ],
36
+ 'entrypoint2 ' => [
37
+ 'js ' => [
38
+ '/some/path/to/another/js/file.js ' ,
39
+ ],
40
+ 'css ' => [
41
+ '/some/path/to/js/file.css ' ,
42
+ ],
43
+ ],
44
+ ],
45
+ ];
46
+
26
47
private EntrypointLookup $ model ;
27
48
28
49
private WebpackEncorePluginConfigurationInterface |MockObject $ configurationMock ;
29
50
51
+ private JsonDecode |MockObject $ jsonDecodeMock ;
52
+
30
53
protected function setUp (): void
31
54
{
32
55
$ this ->configurationMock = $ this ->getMockBuilder (WebpackEncorePluginConfigurationInterface::class)
33
56
->disableOriginalConstructor ()
34
57
->onlyMethods (['getOutputPath ' ])
35
58
->getMockForAbstractClass ();
36
59
37
- $ this ->model = new EntrypointLookup ($ this ->configurationMock , new JsonDecode ([JsonDecode::ASSOCIATIVE => true ]));
60
+ $ this ->jsonDecodeMock = $ this ->getMockBuilder (JsonDecode::class)
61
+ ->disableOriginalConstructor ()
62
+ ->onlyMethods (['decode ' ])
63
+ ->getMock ();
64
+
65
+ $ this ->model = new EntrypointLookup ($ this ->configurationMock , $ this ->jsonDecodeMock );
38
66
}
39
67
40
68
/**
41
69
* @dataProvider getCssFilesDataProvider
42
70
*
43
71
* @covers \OleksiiBulba\WebpackEncorePlugin\Asset\EntrypointLookup::getCssFiles
44
72
*/
45
- public function testGetCssFiles (string $ entryName , array $ expectedResult ): void
46
- {
73
+ public function testGetCssFiles (
74
+ string $ entryName ,
75
+ array $ expectedResult ,
76
+ string $ expectedJson ,
77
+ ?array $ decodedJson
78
+ ): void {
47
79
$ this ->configurationMock ->expects ($ this ->any ())
48
80
->method ('getOutputPath ' )
49
81
->willReturn (__DIR__ .'/../fixtures/correct ' );
50
82
83
+ $ this ->jsonDecodeMock ->expects ($ this ->once ())
84
+ ->method ('decode ' )
85
+ ->with ($ expectedJson )
86
+ ->willReturn ($ decodedJson );
87
+
51
88
$ this ->assertEquals ($ expectedResult , $ this ->model ->getCssFiles ($ entryName ));
52
89
}
53
90
@@ -57,12 +94,16 @@ public function getCssFilesDataProvider(): array
57
94
[
58
95
'entryName ' => 'entrypoint1 ' ,
59
96
'expectedResult ' => [],
97
+ 'expectedJson ' => file_get_contents (__DIR__ .'/../fixtures/correct/entrypoints.json ' ),
98
+ 'decodedJson ' => self ::DECODED_ENTRYPOINTS ,
60
99
],
61
100
[
62
101
'entryName ' => 'entrypoint2 ' ,
63
102
'expectedResult ' => [
64
103
'/some/path/to/js/file.css ' ,
65
104
],
105
+ 'expectedJson ' => file_get_contents (__DIR__ .'/../fixtures/correct/entrypoints.json ' ),
106
+ 'decodedJson ' => self ::DECODED_ENTRYPOINTS ,
66
107
],
67
108
];
68
109
}
@@ -72,12 +113,21 @@ public function getCssFilesDataProvider(): array
72
113
*
73
114
* @covers \OleksiiBulba\WebpackEncorePlugin\Asset\EntrypointLookup::getJavaScriptFiles
74
115
*/
75
- public function testGetJavaScriptFiles (string $ entryName , array $ expectedResult ): void
76
- {
116
+ public function testGetJavaScriptFiles (
117
+ string $ entryName ,
118
+ array $ expectedResult ,
119
+ string $ expectedJson ,
120
+ ?array $ decodedJson
121
+ ): void {
77
122
$ this ->configurationMock ->expects ($ this ->any ())
78
123
->method ('getOutputPath ' )
79
124
->willReturn (__DIR__ .'/../fixtures/correct ' );
80
125
126
+ $ this ->jsonDecodeMock ->expects ($ this ->once ())
127
+ ->method ('decode ' )
128
+ ->with ($ expectedJson )
129
+ ->willReturn ($ decodedJson );
130
+
81
131
$ this ->assertEquals ($ expectedResult , $ this ->model ->getJavaScriptFiles ($ entryName ));
82
132
}
83
133
@@ -91,12 +141,16 @@ public function getJavaScriptFilesDataProvider(): array
91
141
'/another/path/to/js/file.js ' ,
92
142
'/build/app.js ' ,
93
143
],
144
+ 'expectedJson ' => file_get_contents (__DIR__ .'/../fixtures/correct/entrypoints.json ' ),
145
+ 'decodedJson ' => self ::DECODED_ENTRYPOINTS ,
94
146
],
95
147
[
96
148
'entryName ' => 'entrypoint2 ' ,
97
149
'expectedResult ' => [
98
150
'/some/path/to/another/js/file.js ' ,
99
151
],
152
+ 'expectedJson ' => file_get_contents (__DIR__ .'/../fixtures/correct/entrypoints.json ' ),
153
+ 'decodedJson ' => self ::DECODED_ENTRYPOINTS ,
100
154
],
101
155
];
102
156
}
@@ -106,12 +160,21 @@ public function getJavaScriptFilesDataProvider(): array
106
160
*
107
161
* @covers \OleksiiBulba\WebpackEncorePlugin\Asset\EntrypointLookup::entryExists
108
162
*/
109
- public function testEntryExists (string $ entryName , bool $ expectedResult ): void
110
- {
163
+ public function testEntryExists (
164
+ string $ entryName ,
165
+ bool $ expectedResult ,
166
+ string $ expectedJson ,
167
+ ?array $ decodedJson
168
+ ): void {
111
169
$ this ->configurationMock ->expects ($ this ->any ())
112
170
->method ('getOutputPath ' )
113
171
->willReturn (__DIR__ .'/../fixtures/correct ' );
114
172
173
+ $ this ->jsonDecodeMock ->expects ($ this ->once ())
174
+ ->method ('decode ' )
175
+ ->with ($ expectedJson )
176
+ ->willReturn ($ decodedJson );
177
+
115
178
$ this ->assertEquals ($ expectedResult , $ this ->model ->entryExists ($ entryName ));
116
179
}
117
180
@@ -121,27 +184,42 @@ public function entryExistsDataProvider(): array
121
184
[
122
185
'entryName ' => 'entrypoint1 ' ,
123
186
'expectedResult ' => true ,
187
+ 'expectedJson ' => file_get_contents (__DIR__ .'/../fixtures/correct/entrypoints.json ' ),
188
+ 'decodedJson ' => self ::DECODED_ENTRYPOINTS ,
124
189
],
125
190
[
126
191
'entryName ' => 'entrypoint2 ' ,
127
192
'expectedResult ' => true ,
193
+ 'expectedJson ' => file_get_contents (__DIR__ .'/../fixtures/correct/entrypoints.json ' ),
194
+ 'decodedJson ' => self ::DECODED_ENTRYPOINTS ,
128
195
],
129
196
[
130
197
'entryName ' => 'entrypoint3 ' ,
131
198
'expectedResult ' => false ,
199
+ 'expectedJson ' => file_get_contents (__DIR__ .'/../fixtures/correct/entrypoints.json ' ),
200
+ 'decodedJson ' => self ::DECODED_ENTRYPOINTS ,
132
201
],
133
202
];
134
203
}
135
204
136
205
/**
137
206
* @dataProvider exceptionsInValidateEntryNameDataProvider
138
207
*/
139
- public function testExceptionsInValidateEntryName (string $ entryName , string $ expectedExceptionMessageMatches ): void
140
- {
208
+ public function testExceptionsInValidateEntryName (
209
+ string $ entryName ,
210
+ string $ expectedExceptionMessageMatches ,
211
+ string $ expectedJson ,
212
+ ?array $ decodedJson
213
+ ): void {
141
214
$ this ->configurationMock ->expects ($ this ->any ())
142
215
->method ('getOutputPath ' )
143
216
->willReturn (__DIR__ .'/../fixtures/correct ' );
144
217
218
+ $ this ->jsonDecodeMock ->expects ($ this ->once ())
219
+ ->method ('decode ' )
220
+ ->with ($ expectedJson )
221
+ ->willReturn ($ decodedJson );
222
+
145
223
$ this ->expectException (EntrypointNotFoundException::class);
146
224
$ this ->expectExceptionMessageMatches ($ expectedExceptionMessageMatches );
147
225
$ this ->model ->getJavaScriptFiles ($ entryName );
@@ -153,23 +231,42 @@ public function exceptionsInValidateEntryNameDataProvider(): array
153
231
[
154
232
'entryName ' => 'entrypoint1.js ' ,
155
233
'expectedExceptionMessageMatches ' => '/Could not find the entry "entrypoint1.js"\. Try "entrypoint1" instead \(without the extension\)\./ ' ,
234
+ 'expectedJson ' => file_get_contents (__DIR__ .'/../fixtures/correct/entrypoints.json ' ),
235
+ 'decodedJson ' => self ::DECODED_ENTRYPOINTS ,
156
236
],
157
237
[
158
238
'entryName ' => 'entrypoint3 ' ,
159
239
'expectedExceptionMessageMatches ' => '/Could not find the entry "entrypoint3" in ".*". Found: entrypoint1, entrypoint2\./ ' ,
240
+ 'expectedJson ' => file_get_contents (__DIR__ .'/../fixtures/correct/entrypoints.json ' ),
241
+ 'decodedJson ' => self ::DECODED_ENTRYPOINTS ,
242
+ ],
243
+ [
244
+ 'entryName ' => 'entrypoint4.js ' ,
245
+ 'expectedExceptionMessageMatches ' => '/Could not find the entry "entrypoint4.js" in ".*". Found: entrypoint1, entrypoint2\./ ' ,
246
+ 'expectedJson ' => file_get_contents (__DIR__ .'/../fixtures/correct/entrypoints.json ' ),
247
+ 'decodedJson ' => self ::DECODED_ENTRYPOINTS ,
160
248
],
161
249
];
162
250
}
163
251
164
252
/**
165
253
* @dataProvider invalidArgumentExceptionInGetEntriesDataDataProvider
166
254
*/
167
- public function testInvalidArgumentExceptionInGetEntriesData (string $ entrypointFilePath , string $ expectedExceptionMessageMatches ): void
168
- {
255
+ public function testInvalidArgumentExceptionInGetEntriesData (
256
+ string $ entrypointFilePath ,
257
+ string $ expectedExceptionMessageMatches ,
258
+ string $ expectedJson ,
259
+ ?array $ decodedJson
260
+ ): void {
169
261
$ this ->configurationMock ->expects ($ this ->any ())
170
262
->method ('getOutputPath ' )
171
263
->willReturn (__DIR__ .$ entrypointFilePath );
172
264
265
+ $ this ->jsonDecodeMock ->expects ($ this ->any ())
266
+ ->method ('decode ' )
267
+ ->with ($ expectedJson )
268
+ ->willReturn ($ decodedJson );
269
+
173
270
$ this ->expectException (\InvalidArgumentException::class);
174
271
$ this ->expectExceptionMessageMatches ($ expectedExceptionMessageMatches );
175
272
$ this ->model ->entryExists ('some-entry-name ' );
@@ -181,14 +278,62 @@ public function invalidArgumentExceptionInGetEntriesDataDataProvider(): array
181
278
[
182
279
'entrypointFilePath ' => '/../fixtures/bad_json ' ,
183
280
'expectedExceptionMessageMatches ' => '/There was a problem JSON decoding the ".*\/fixtures\/bad_json\/entrypoints.json" file/ ' ,
281
+ 'expectedJson ' => file_get_contents (__DIR__ .'/../fixtures/bad_json/entrypoints.json ' ),
282
+ 'decodedJson ' => null ,
184
283
],
185
284
[
186
285
'entrypointFilePath ' => '/../fixtures/no_entrypoint_key ' ,
187
286
'expectedExceptionMessageMatches ' => '/Could not find an "entrypoints" key in the ".*\/fixtures\/no_entrypoint_key\/entrypoints.json" file/ ' ,
287
+ 'expectedJson ' => file_get_contents (__DIR__ .'/../fixtures/no_entrypoint_key/entrypoints.json ' ),
288
+ 'decodedJson ' => ['entrypoint ' => []],
188
289
],
189
290
[
190
291
'entrypointFilePath ' => '/../fixtures/no_file ' ,
191
292
'expectedExceptionMessageMatches ' => '/Could not find the entrypoints file from Webpack: the file ".*\/fixtures\/no_file\/entrypoints.json" does not exist\./ ' ,
293
+ 'expectedJson ' => '' ,
294
+ 'decodedJson ' => null ,
295
+ ],
296
+ [
297
+ 'entrypointFilePath ' => '/../fixtures/bad_json ' ,
298
+ 'expectedExceptionMessageMatches ' => '/There was a problem JSON decoding the ".*" file/ ' ,
299
+ 'expectedJson ' => file_get_contents (__DIR__ .'/../fixtures/bad_json/entrypoints.json ' ),
300
+ 'decodedJson ' => null ,
301
+ ],
302
+ ];
303
+ }
304
+
305
+ /**
306
+ * @dataProvider jsonDecoderThrowsExceptionDataProvider
307
+ */
308
+ public function testJsonDecoderThrowsException (
309
+ string $ entrypointFilePath ,
310
+ string $ expectedExceptionMessageMatches ,
311
+ string $ expectedJson
312
+ ): void {
313
+ $ this ->configurationMock ->expects ($ this ->any ())
314
+ ->method ('getOutputPath ' )
315
+ ->willReturn (__DIR__ .$ entrypointFilePath );
316
+
317
+ $ exception = new UnexpectedValueException ('UnexpectedValueException message ' );
318
+
319
+ $ this ->jsonDecodeMock ->expects ($ this ->any ())
320
+ ->method ('decode ' )
321
+ ->with ($ expectedJson )
322
+ ->willThrowException ($ exception );
323
+
324
+ $ this ->expectException (\InvalidArgumentException::class);
325
+ $ this ->expectExceptionMessageMatches ($ expectedExceptionMessageMatches );
326
+
327
+ $ this ->model ->entryExists ('some-entry-name ' );
328
+ }
329
+
330
+ public function jsonDecoderThrowsExceptionDataProvider (): array
331
+ {
332
+ return [
333
+ [
334
+ 'entrypointFilePath ' => '/../fixtures/bad_json ' ,
335
+ 'expectedExceptionMessageMatches ' => '/There was a problem JSON decoding the ".*" file/ ' ,
336
+ 'expectedJson ' => file_get_contents (__DIR__ .'/../fixtures/bad_json/entrypoints.json ' ),
192
337
],
193
338
];
194
339
}
0 commit comments