Skip to content

Commit 0b74bb0

Browse files
committed
test: fix failing tests and add missing ones
Also improve the test output ui by using phpunit testdox.
1 parent 5f0cccf commit 0b74bb0

4 files changed

+175
-16
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
}
2727
},
2828
"scripts": {
29-
"test": "phpunit --colors=auto",
29+
"test": "phpunit --colors=always --testdox",
3030
"lint": "phpcs -v inc/*",
3131
"changelog": "npx auto-changelog --template keepachangelog -v"
3232
},

tests/testcases/EnqueueTest.php

+118-15
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,25 @@ class EnqueueTest extends TestCase {
1212
* Mocked Template Directory Uri
1313
* @var string
1414
*/
15-
protected $tdu = 'http://example.com/path/to/template/directory';
15+
protected $templateDirectoryUri = 'http://example.com/path/to/template/directory';
1616

1717
/**
18-
* Mocked Child Theme Directory Uri
18+
* Mocked Stylesheet Directory Uri
1919
* @var string
2020
*/
21-
protected $childThemeTemplateDirectoryUri = 'http://example.com/path/to/child_theme_template/directory';
21+
protected $stylesheetDirectoryUri = 'http://example.com/path/to/child_theme_template/directory';
2222

2323
/**
2424
* Mocked Template Directory path.
2525
* @var string
2626
*/
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';
2834

2935
/**
3036
* Mocked Plugin Uri.
@@ -43,9 +49,10 @@ public function setUp() {
4349
// Stub out a few functions we will need
4450
// with predefined output
4551
\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,
4956
'plugins_url' => $this->pu,
5057
]);
5158
// Stub some returnFirstArguments function
@@ -55,18 +62,28 @@ public function setUp() {
5562
'sanitize_title_with_dashes'
5663
]);
5764
}
65+
66+
/**
67+
* @testdox Constructor adds WordPress actions
68+
*/
5869
public function test_construct() {
5970
$enqueue = new \WPackio\Enqueue( 'foo', 'dist', '1.0.0', 'plugin', '/plugin/path/plugin.php' );
6071
// We expect hooks on both wp_head and admin_head
6172
$this->assertTrue( has_action( 'wp_head', 'WPackio\\Enqueue->printPublicPath()', -1000 ) );
6273
$this->assertTrue( has_action( 'admin_print_scripts', 'WPackio\\Enqueue->printPublicPath()', -1000 ) );
6374
}
6475

76+
/**
77+
* @testdox Constructor throws exception on invalid $type
78+
*/
6579
public function test_construct_throws_on_invalid_type() {
6680
$this->expectException('\LogicException');
6781
$enqueue = new \WPackio\Enqueue( 'foo', 'dist', '1.0.0', 'aasdasd', '/plugin/path/plugin.php' );
6882
}
6983

84+
/**
85+
* @testdox `printPublicPath` works for plugins
86+
*/
7087
public function test_printPublicPath_for_plugin() {
7188
$enqueue = new \WPackio\Enqueue( 'foo', 'dist', '1.0.0', 'plugin', '/plugin/path/plugin.php' );
7289
ob_start();
@@ -75,29 +92,55 @@ public function test_printPublicPath_for_plugin() {
7592
$this->assertContains( 'window.__wpackIofoodist=\'' . $this->pu . '/\'', $result );
7693
}
7794

78-
public function test_printPublicPath_for_theme() {
95+
/**
96+
* @testdox `printPublicPath` works for regular themes
97+
*/
98+
public function test_printPublicPath_for_regular_theme() {
7999
$enqueue = new \WPackio\Enqueue( 'foo', 'dist', '1.0.0', 'theme', false, 'regular' );
80100
ob_start();
81101
$enqueue->printPublicPath();
82102
$result = ob_get_clean();
83-
$this->assertContains( 'window.__wpackIofoodist=\'' . $this->tdu . '/dist/\'', $result );
103+
$this->assertContains( 'window.__wpackIofoodist=\'' . $this->templateDirectoryUri . '/dist/\'', $result );
84104
}
85105

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+
*/
86120
public function test_getUrl_for_regular_theme() {
87121
$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' ) );
89123
}
90124

125+
/**
126+
* @testdox `getUrl` works for child themes
127+
*/
91128
public function test_getUrl_for_child_theme() {
92129
$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' ) );
94131
}
95132

133+
/**
134+
* @testdox `getUrl` works for plugins
135+
*/
96136
public function test_getUrl_for_plugin() {
97137
$enqueue = new \WPackio\Enqueue( 'foo', 'dist', '1.0.0', 'plugin', '/path/to/plugin.php' );
98138
$this->assertEquals( $this->pu . '/app/main.js', $enqueue->getUrl( 'app/main.js' ) );
99139
}
100140

141+
/**
142+
* @testdox `getManifest` works if file is valid
143+
*/
101144
public function test_getManifest() {
102145
$path_to_manifest = dirname( $this->pp ) . '/dist/app/manifest.json';
103146
$enqueue = new \WPackio\Enqueue( 'foo', 'dist', '1.0.0', 'plugin', $this->pp );
@@ -106,25 +149,37 @@ public function test_getManifest() {
106149
$this->assertMatchesSnapshot( $manifest );
107150
}
108151

152+
/**
153+
* @testdox `getManifest` throws if file not found
154+
*/
109155
public function test_getManifest_throws_if_file_not_found() {
110156
$this->expectException( '\LogicException' );
111157
$enqueue = new \WPackio\Enqueue( 'foo', 'dist', '1.0.0', 'plugin', $this->pp );
112158
$enqueue->getManifest( 'noop' );
113159
}
114160

161+
/**
162+
* @testdox `getManifest` throws if file not valid JSON
163+
*/
115164
public function test_getManifest_throws_if_file_not_valid() {
116165
$this->expectException( '\LogicException' );
117166
$enqueue = new \WPackio\Enqueue( 'foo', 'dist', '1.0.0', 'plugin', $this->pp );
118167
$enqueue->getManifest( 'broken' );
119168
}
120169

170+
/**
171+
* @testdox `getAssets` throws on invalid entrypoint
172+
*/
121173
public function test_getAssets_throws_on_invalid_entrypoint() {
122174
$this->expectException('\LogicException');
123175
$enqueue = new \WPackio\Enqueue( 'foo', 'dist', '1.0.0', 'theme', false, 'regular' );
124176
$enqueue->getAssets( 'app', 'noop', [] );
125177
}
126178

127-
public function test_getAssets_for_theme() {
179+
/**
180+
* @testdox `getAssets` works for regular themes
181+
*/
182+
public function test_getAssets_for_regular_theme() {
128183
$enqueue = new \WPackio\Enqueue( 'foo', 'dist', '1.0.0', 'theme', false, 'regular' );
129184
$assets = $enqueue->getAssets( 'app', 'main', [
130185
'js' => true,
@@ -140,7 +195,41 @@ public function test_getAssets_for_theme() {
140195
foreach ( $assets['js'] as $js ) {
141196
$this->assertArrayHasKey( 'url', $js );
142197
$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'] );
144233
}
145234

146235

@@ -149,12 +238,15 @@ public function test_getAssets_for_theme() {
149238
foreach ( $assets['css'] as $css ) {
150239
$this->assertArrayHasKey( 'url', $css );
151240
$this->assertArrayHasKey( 'handle', $css );
152-
$this->assertContains( $this->tdu . '/dist/app/', $css['url'] );
241+
$this->assertContains( $this->stylesheetDirectoryUri . '/dist/app/', $css['url'] );
153242
}
154243

155244
$this->assertMatchesSnapshot( $assets );
156245
}
157246

247+
/**
248+
* @testdox `getAssets` works for plugins
249+
*/
158250
public function test_getAssets_for_plugin() {
159251
$enqueue = new \WPackio\Enqueue( 'foo', 'dist', '1.0.0', 'plugin', $this->pp );
160252
$assets = $enqueue->getAssets( 'app', 'main', [
@@ -185,6 +277,9 @@ public function test_getAssets_for_plugin() {
185277
$this->assertMatchesSnapshot( $assets );
186278
}
187279

280+
/**
281+
* @testdox `getAssets` has same handle for every runtime asset
282+
*/
188283
public function test_getAssets_has_same_handle_for_every_runtime() {
189284
$runtime_handles = [];
190285
$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() {
224319
);
225320
}
226321

227-
322+
/**
323+
* @testdox `register` calls proper WordPress APIs
324+
*/
228325
public function test_register() {
229326
// Get the manifest beforehand for assertion
230327
$path_to_manifest = dirname( $this->pp ) . '/dist/app/manifest.json';
@@ -267,6 +364,9 @@ public function test_register() {
267364
$this->assertMatchesSnapshot( $enqueue_assets );
268365
}
269366

367+
/**
368+
* @testdox `enqueue` calls proper WordPress APIs
369+
*/
270370
public function test_enqueue() {
271371
// Arrange
272372
$config = [
@@ -324,6 +424,9 @@ public function test_enqueue() {
324424
$enqueue->enqueue( 'app', 'main', $config );
325425
}
326426

427+
/**
428+
* @testdox `getHandle` throws exception on invalid type
429+
*/
327430
public function test_getHandle_throws_exception() {
328431
$this->expectException( '\LogicException' );
329432
$enqueue = new \WPackio\Enqueue( 'foo', 'dist', '1.0.0', 'plugin', $this->pp );
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php return array (
2+
'css' =>
3+
array (
4+
0 =>
5+
array (
6+
'handle' => 'wpackio_fooapp_app/main.css_style',
7+
'url' => 'http://example.com/path/to/child_theme_template/directory/dist/app/main.css',
8+
),
9+
),
10+
'js' =>
11+
array (
12+
0 =>
13+
array (
14+
'handle' => 'wpackio_fooapp_app/runtime.js_script',
15+
'url' => 'http://example.com/path/to/child_theme_template/directory/dist/app/runtime.js',
16+
),
17+
1 =>
18+
array (
19+
'handle' => 'wpackio_fooapp_app/vendor.js_script',
20+
'url' => 'http://example.com/path/to/child_theme_template/directory/dist/app/vendor.js',
21+
),
22+
2 =>
23+
array (
24+
'handle' => 'wpackio_fooapp_app/main.js_script',
25+
'url' => 'http://example.com/path/to/child_theme_template/directory/dist/app/main.js',
26+
),
27+
),
28+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php return array (
2+
'css' =>
3+
array (
4+
0 =>
5+
array (
6+
'handle' => 'wpackio_fooapp_app/main.css_style',
7+
'url' => 'http://example.com/path/to/template/directory/dist/app/main.css',
8+
),
9+
),
10+
'js' =>
11+
array (
12+
0 =>
13+
array (
14+
'handle' => 'wpackio_fooapp_app/runtime.js_script',
15+
'url' => 'http://example.com/path/to/template/directory/dist/app/runtime.js',
16+
),
17+
1 =>
18+
array (
19+
'handle' => 'wpackio_fooapp_app/vendor.js_script',
20+
'url' => 'http://example.com/path/to/template/directory/dist/app/vendor.js',
21+
),
22+
2 =>
23+
array (
24+
'handle' => 'wpackio_fooapp_app/main.js_script',
25+
'url' => 'http://example.com/path/to/template/directory/dist/app/main.js',
26+
),
27+
),
28+
);

0 commit comments

Comments
 (0)