@@ -12,19 +12,25 @@ class EnqueueTest extends TestCase {
12
12
* Mocked Template Directory Uri
13
13
* @var string
14
14
*/
15
- protected $ tdu = 'http://example.com/path/to/template/directory ' ;
15
+ protected $ templateDirectoryUri = 'http://example.com/path/to/template/directory ' ;
16
16
17
17
/**
18
- * Mocked Child Theme Directory Uri
18
+ * Mocked Stylesheet Directory Uri
19
19
* @var string
20
20
*/
21
- protected $ childThemeTemplateDirectoryUri = 'http://example.com/path/to/child_theme_template/directory ' ;
21
+ protected $ stylesheetDirectoryUri = 'http://example.com/path/to/child_theme_template/directory ' ;
22
22
23
23
/**
24
24
* Mocked Template Directory path.
25
25
* @var string
26
26
*/
27
- protected $ td = __DIR__ . '/../data ' ;
27
+ protected $ templateDirectory = __DIR__ . '/../data ' ;
28
+
29
+ /**
30
+ * Mocked Stylesheet Directory path.
31
+ * @var string
32
+ */
33
+ protected $ stylesheetDirectory = __DIR__ . '/../data ' ;
28
34
29
35
/**
30
36
* Mocked Plugin Uri.
@@ -43,9 +49,10 @@ public function setUp() {
43
49
// Stub out a few functions we will need
44
50
// with predefined output
45
51
\Brain \Monkey \Functions \stubs ([
46
- 'get_template_directory ' => $ this ->td ,
47
- 'get_template_directory_uri ' => $ this ->tdu ,
48
- 'get_stylesheet_directory_uri ' => $ this ->childThemeTemplateDirectoryUri ,
52
+ 'get_template_directory ' => $ this ->templateDirectory ,
53
+ 'get_template_directory_uri ' => $ this ->templateDirectoryUri ,
54
+ 'get_stylesheet_directory ' => $ this ->stylesheetDirectory ,
55
+ 'get_stylesheet_directory_uri ' => $ this ->stylesheetDirectoryUri ,
49
56
'plugins_url ' => $ this ->pu ,
50
57
]);
51
58
// Stub some returnFirstArguments function
@@ -55,18 +62,28 @@ public function setUp() {
55
62
'sanitize_title_with_dashes '
56
63
]);
57
64
}
65
+
66
+ /**
67
+ * @testdox Constructor adds WordPress actions
68
+ */
58
69
public function test_construct () {
59
70
$ enqueue = new \WPackio \Enqueue ( 'foo ' , 'dist ' , '1.0.0 ' , 'plugin ' , '/plugin/path/plugin.php ' );
60
71
// We expect hooks on both wp_head and admin_head
61
72
$ this ->assertTrue ( has_action ( 'wp_head ' , 'WPackio \\Enqueue->printPublicPath() ' , -1000 ) );
62
73
$ this ->assertTrue ( has_action ( 'admin_print_scripts ' , 'WPackio \\Enqueue->printPublicPath() ' , -1000 ) );
63
74
}
64
75
76
+ /**
77
+ * @testdox Constructor throws exception on invalid $type
78
+ */
65
79
public function test_construct_throws_on_invalid_type () {
66
80
$ this ->expectException ('\LogicException ' );
67
81
$ enqueue = new \WPackio \Enqueue ( 'foo ' , 'dist ' , '1.0.0 ' , 'aasdasd ' , '/plugin/path/plugin.php ' );
68
82
}
69
83
84
+ /**
85
+ * @testdox `printPublicPath` works for plugins
86
+ */
70
87
public function test_printPublicPath_for_plugin () {
71
88
$ enqueue = new \WPackio \Enqueue ( 'foo ' , 'dist ' , '1.0.0 ' , 'plugin ' , '/plugin/path/plugin.php ' );
72
89
ob_start ();
@@ -75,29 +92,55 @@ public function test_printPublicPath_for_plugin() {
75
92
$ this ->assertContains ( 'window.__wpackIofoodist= \'' . $ this ->pu . '/ \'' , $ result );
76
93
}
77
94
78
- public function test_printPublicPath_for_theme () {
95
+ /**
96
+ * @testdox `printPublicPath` works for regular themes
97
+ */
98
+ public function test_printPublicPath_for_regular_theme () {
79
99
$ enqueue = new \WPackio \Enqueue ( 'foo ' , 'dist ' , '1.0.0 ' , 'theme ' , false , 'regular ' );
80
100
ob_start ();
81
101
$ enqueue ->printPublicPath ();
82
102
$ result = ob_get_clean ();
83
- $ this ->assertContains ( 'window.__wpackIofoodist= \'' . $ this ->tdu . '/dist/ \'' , $ result );
103
+ $ this ->assertContains ( 'window.__wpackIofoodist= \'' . $ this ->templateDirectoryUri . '/dist/ \'' , $ result );
84
104
}
85
105
106
+ /**
107
+ * @testdox `printPublicPath` works child themes
108
+ */
109
+ public function test_printPublicPath_for_child_theme () {
110
+ $ enqueue = new \WPackio \Enqueue ( 'foo ' , 'dist ' , '1.0.0 ' , 'theme ' , false , 'child ' );
111
+ ob_start ();
112
+ $ enqueue ->printPublicPath ();
113
+ $ result = ob_get_clean ();
114
+ $ this ->assertContains ( 'window.__wpackIofoodist= \'' . $ this ->stylesheetDirectoryUri . '/dist/ \'' , $ result );
115
+ }
116
+
117
+ /**
118
+ * @testdox `getUrl` works for regular themes
119
+ */
86
120
public function test_getUrl_for_regular_theme () {
87
121
$ enqueue = new \WPackio \Enqueue ( 'foo ' , 'dist ' , '1.0.0 ' , 'theme ' , false , 'regular ' );
88
- $ this ->assertEquals ( $ this ->tdu . '/dist/app/main.js ' , $ enqueue ->getUrl ( 'app/main.js ' ) );
122
+ $ this ->assertEquals ( $ this ->templateDirectoryUri . '/dist/app/main.js ' , $ enqueue ->getUrl ( 'app/main.js ' ) );
89
123
}
90
124
125
+ /**
126
+ * @testdox `getUrl` works for child themes
127
+ */
91
128
public function test_getUrl_for_child_theme () {
92
129
$ enqueue = new \WPackio \Enqueue ( 'foo ' , 'dist ' , '1.0.0 ' , 'theme ' , false , 'child ' );
93
- $ this ->assertEquals ( $ this ->childThemeTemplateDirectoryUri . '/dist/app/main.js ' , $ enqueue ->getUrl ( 'app/main.js ' ) );
130
+ $ this ->assertEquals ( $ this ->stylesheetDirectoryUri . '/dist/app/main.js ' , $ enqueue ->getUrl ( 'app/main.js ' ) );
94
131
}
95
132
133
+ /**
134
+ * @testdox `getUrl` works for plugins
135
+ */
96
136
public function test_getUrl_for_plugin () {
97
137
$ enqueue = new \WPackio \Enqueue ( 'foo ' , 'dist ' , '1.0.0 ' , 'plugin ' , '/path/to/plugin.php ' );
98
138
$ this ->assertEquals ( $ this ->pu . '/app/main.js ' , $ enqueue ->getUrl ( 'app/main.js ' ) );
99
139
}
100
140
141
+ /**
142
+ * @testdox `getManifest` works if file is valid
143
+ */
101
144
public function test_getManifest () {
102
145
$ path_to_manifest = dirname ( $ this ->pp ) . '/dist/app/manifest.json ' ;
103
146
$ enqueue = new \WPackio \Enqueue ( 'foo ' , 'dist ' , '1.0.0 ' , 'plugin ' , $ this ->pp );
@@ -106,25 +149,37 @@ public function test_getManifest() {
106
149
$ this ->assertMatchesSnapshot ( $ manifest );
107
150
}
108
151
152
+ /**
153
+ * @testdox `getManifest` throws if file not found
154
+ */
109
155
public function test_getManifest_throws_if_file_not_found () {
110
156
$ this ->expectException ( '\LogicException ' );
111
157
$ enqueue = new \WPackio \Enqueue ( 'foo ' , 'dist ' , '1.0.0 ' , 'plugin ' , $ this ->pp );
112
158
$ enqueue ->getManifest ( 'noop ' );
113
159
}
114
160
161
+ /**
162
+ * @testdox `getManifest` throws if file not valid JSON
163
+ */
115
164
public function test_getManifest_throws_if_file_not_valid () {
116
165
$ this ->expectException ( '\LogicException ' );
117
166
$ enqueue = new \WPackio \Enqueue ( 'foo ' , 'dist ' , '1.0.0 ' , 'plugin ' , $ this ->pp );
118
167
$ enqueue ->getManifest ( 'broken ' );
119
168
}
120
169
170
+ /**
171
+ * @testdox `getAssets` throws on invalid entrypoint
172
+ */
121
173
public function test_getAssets_throws_on_invalid_entrypoint () {
122
174
$ this ->expectException ('\LogicException ' );
123
175
$ enqueue = new \WPackio \Enqueue ( 'foo ' , 'dist ' , '1.0.0 ' , 'theme ' , false , 'regular ' );
124
176
$ enqueue ->getAssets ( 'app ' , 'noop ' , [] );
125
177
}
126
178
127
- public function test_getAssets_for_theme () {
179
+ /**
180
+ * @testdox `getAssets` works for regular themes
181
+ */
182
+ public function test_getAssets_for_regular_theme () {
128
183
$ enqueue = new \WPackio \Enqueue ( 'foo ' , 'dist ' , '1.0.0 ' , 'theme ' , false , 'regular ' );
129
184
$ assets = $ enqueue ->getAssets ( 'app ' , 'main ' , [
130
185
'js ' => true ,
@@ -140,7 +195,41 @@ public function test_getAssets_for_theme() {
140
195
foreach ( $ assets ['js ' ] as $ js ) {
141
196
$ this ->assertArrayHasKey ( 'url ' , $ js );
142
197
$ this ->assertArrayHasKey ( 'handle ' , $ js );
143
- $ this ->assertContains ( $ this ->tdu . '/dist/app/ ' , $ js ['url ' ] );
198
+ $ this ->assertContains ( $ this ->templateDirectoryUri . '/dist/app/ ' , $ js ['url ' ] );
199
+ }
200
+
201
+
202
+ // expect on js
203
+ $ this ->assertArrayHasKey ( 'css ' , $ assets );
204
+ foreach ( $ assets ['css ' ] as $ css ) {
205
+ $ this ->assertArrayHasKey ( 'url ' , $ css );
206
+ $ this ->assertArrayHasKey ( 'handle ' , $ css );
207
+ $ this ->assertContains ( $ this ->templateDirectoryUri . '/dist/app/ ' , $ css ['url ' ] );
208
+ }
209
+
210
+ $ this ->assertMatchesSnapshot ( $ assets );
211
+ }
212
+
213
+ /**
214
+ * @testdox `getAssets` works for child themes
215
+ */
216
+ public function test_getAssets_for_child_theme () {
217
+ $ enqueue = new \WPackio \Enqueue ( 'foo ' , 'dist ' , '1.0.0 ' , 'theme ' , false , 'child ' );
218
+ $ assets = $ enqueue ->getAssets ( 'app ' , 'main ' , [
219
+ 'js ' => true ,
220
+ 'css ' => true ,
221
+ 'js_dep ' => [],
222
+ 'css_dep ' => [],
223
+ 'identifier ' => false ,
224
+ 'in_footer ' => true ,
225
+ 'media ' => 'all ' ,
226
+ ] );
227
+ // expect on js
228
+ $ this ->assertArrayHasKey ( 'js ' , $ assets );
229
+ foreach ( $ assets ['js ' ] as $ js ) {
230
+ $ this ->assertArrayHasKey ( 'url ' , $ js );
231
+ $ this ->assertArrayHasKey ( 'handle ' , $ js );
232
+ $ this ->assertContains ( $ this ->stylesheetDirectoryUri . '/dist/app/ ' , $ js ['url ' ] );
144
233
}
145
234
146
235
@@ -149,12 +238,15 @@ public function test_getAssets_for_theme() {
149
238
foreach ( $ assets ['css ' ] as $ css ) {
150
239
$ this ->assertArrayHasKey ( 'url ' , $ css );
151
240
$ this ->assertArrayHasKey ( 'handle ' , $ css );
152
- $ this ->assertContains ( $ this ->tdu . '/dist/app/ ' , $ css ['url ' ] );
241
+ $ this ->assertContains ( $ this ->stylesheetDirectoryUri . '/dist/app/ ' , $ css ['url ' ] );
153
242
}
154
243
155
244
$ this ->assertMatchesSnapshot ( $ assets );
156
245
}
157
246
247
+ /**
248
+ * @testdox `getAssets` works for plugins
249
+ */
158
250
public function test_getAssets_for_plugin () {
159
251
$ enqueue = new \WPackio \Enqueue ( 'foo ' , 'dist ' , '1.0.0 ' , 'plugin ' , $ this ->pp );
160
252
$ assets = $ enqueue ->getAssets ( 'app ' , 'main ' , [
@@ -185,6 +277,9 @@ public function test_getAssets_for_plugin() {
185
277
$ this ->assertMatchesSnapshot ( $ assets );
186
278
}
187
279
280
+ /**
281
+ * @testdox `getAssets` has same handle for every runtime asset
282
+ */
188
283
public function test_getAssets_has_same_handle_for_every_runtime () {
189
284
$ runtime_handles = [];
190
285
$ enqueue = new \WPackio \Enqueue ( 'foo ' , 'dist ' , '1.0.0 ' , 'plugin ' , $ this ->pp );
@@ -224,7 +319,9 @@ public function test_getAssets_has_same_handle_for_every_runtime() {
224
319
);
225
320
}
226
321
227
-
322
+ /**
323
+ * @testdox `register` calls proper WordPress APIs
324
+ */
228
325
public function test_register () {
229
326
// Get the manifest beforehand for assertion
230
327
$ path_to_manifest = dirname ( $ this ->pp ) . '/dist/app/manifest.json ' ;
@@ -267,6 +364,9 @@ public function test_register() {
267
364
$ this ->assertMatchesSnapshot ( $ enqueue_assets );
268
365
}
269
366
367
+ /**
368
+ * @testdox `enqueue` calls proper WordPress APIs
369
+ */
270
370
public function test_enqueue () {
271
371
// Arrange
272
372
$ config = [
@@ -324,6 +424,9 @@ public function test_enqueue() {
324
424
$ enqueue ->enqueue ( 'app ' , 'main ' , $ config );
325
425
}
326
426
427
+ /**
428
+ * @testdox `getHandle` throws exception on invalid type
429
+ */
327
430
public function test_getHandle_throws_exception () {
328
431
$ this ->expectException ( '\LogicException ' );
329
432
$ enqueue = new \WPackio \Enqueue ( 'foo ' , 'dist ' , '1.0.0 ' , 'plugin ' , $ this ->pp );
0 commit comments