Skip to content

Commit 5053c0b

Browse files
committed
Enable it_should_accept_instance_of_asset_or_array_of_assets_in_register_in_wp test and fix it. Ass more get tests
1 parent 4735728 commit 5053c0b

File tree

1 file changed

+85
-11
lines changed

1 file changed

+85
-11
lines changed

tests/wpunit/AssetsTest.php

Lines changed: 85 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@ public function setUp() {
2626
Config::set_relative_asset_path( 'tests/_data/' );
2727
}
2828

29-
public function tearDown() {
30-
parent::tearDown();
29+
protected function tearDown() {
30+
// Remove all assets.
31+
Assets::init()->remove_all();
3132
Config::reset();
33+
parent::tearDown();
3234
}
3335

3436
/**
@@ -47,6 +49,9 @@ public function unset_uopz_redefines() {
4749
self::$uopz_redefines = [];
4850
}
4951

52+
/**
53+
* @test
54+
*/
5055
public function it_should_accept_instance_of_asset_or_array_of_assets_in_register_in_wp() {
5156
$asset_1 = Asset::add( 'fake1-script', 'fake1.js' );
5257
$asset_2 = Asset::add( 'fake1-style', 'fake1.css' );
@@ -56,8 +61,8 @@ public function it_should_accept_instance_of_asset_or_array_of_assets_in_registe
5661

5762
$assets = Assets::init();
5863

59-
$assets->register_in_wp( null ); // No problemo... nothing happens though.
60-
$assets->register_in_wp( [] ); // No problemo... nothing happens though.
64+
// An empty array should not cause any assets to be registered.
65+
$assets->register_in_wp( [] );
6166

6267
$this->assertFalse( $asset_1->is_registered() );
6368
$assets->register_in_wp( $asset_1 );
@@ -74,6 +79,75 @@ public function it_should_accept_instance_of_asset_or_array_of_assets_in_registe
7479
$this->assertTrue( $asset_5->is_registered() );
7580
}
7681

82+
/**
83+
* @test
84+
*/
85+
public function it_should_remove_all_assets() {
86+
Asset::add( 'asset-1', 'asset-1.js' );
87+
Asset::add( 'asset-2', 'asset-2.js' );
88+
Asset::add( 'asset-3', 'asset-3.js' );
89+
90+
$assets = Assets::init();
91+
92+
$this->assertCount( 3, $assets->get() );
93+
$this->assertSame( 3, $assets->remove_all() );
94+
$this->assertCount( 0, $assets->get() );
95+
}
96+
97+
/**
98+
* @test
99+
*/
100+
public function it_should_return_null_when_single_asset_not_found() {
101+
Asset::add( 'asset-1', 'asset-1.js' );
102+
Asset::add( 'asset-2', 'asset-2.js' );
103+
Asset::add( 'asset-3', 'asset-3.js' );
104+
105+
$result = Assets::init()->get( 'unknown-slug' );
106+
$this->assertNull( $result );
107+
}
108+
109+
/**
110+
* @test
111+
*/
112+
public function it_should_return_empty_array_when_no_assets() {
113+
$result = Assets::init()->get();
114+
$this->assertSame( [], $result );
115+
}
116+
117+
/**
118+
* @test
119+
*/
120+
public function it_gets_single_asset() {
121+
Asset::add( 'my-single-asset', 'my-single-asset.js' );
122+
$result = Assets::init()->get( 'my-single-asset' );
123+
$this->assertInstanceOf( Asset::class, $result );
124+
}
125+
126+
/**
127+
* @test
128+
*/
129+
public function it_gets_a_subset_of_assets() {
130+
Asset::add( 'asset-1', 'asset-1.js' );
131+
Asset::add( 'asset-2', 'asset-2.js' );
132+
Asset::add( 'asset-3', 'asset-3.js' );
133+
134+
$assets = Assets::init();
135+
136+
$result = $assets->get( ['asset-2', 'asset-3'] );
137+
$this->assertIsArray( $result );
138+
$this->assertCount( 2, $result );
139+
$this->assertArrayNotHasKey( 'asset-1', $result );
140+
$this->assertArrayHasKey( 'asset-2', $result );
141+
$this->assertArrayHasKey( 'asset-3', $result );
142+
143+
$single = $assets->get( ['asset-1'] );
144+
$this->assertIsArray( $single );
145+
$this->assertCount( 1, $single );
146+
$this->assertArrayHasKey( 'asset-1', $single );
147+
$this->assertArrayNotHasKey( 'asset-2', $single );
148+
$this->assertArrayNotHasKey( 'asset-3', $single );
149+
}
150+
77151
public function invalid_params_for_register_in_wp_provider(): Generator {
78152
yield 'string' => [ fn() => 'string' ];
79153
yield 'int' => [ fn() => 1 ];
@@ -121,7 +195,7 @@ public function it_should_locate_minified_versions_of_external_assets() {
121195
$slugs = [
122196
'fake1' => [ true, false ],
123197
'fake2' => [ false, false ],
124-
'fake3' => [ true, true ]
198+
'fake3' => [ true, true ],
125199
];
126200

127201
foreach ( array_keys( $slugs ) as $slug ) {
@@ -143,7 +217,7 @@ public function it_should_get_the_correct_url_when_wp_content_dir_and_wp_content
143217
$slugs = [
144218
'fake1' => [ 'has_min' => true, 'has_only_min' => false ],
145219
'fake2' => [ 'has_min' => false, 'has_only_min' => false ],
146-
'fake3' => [ 'has_min' => true, 'has_only_min' => true ]
220+
'fake3' => [ 'has_min' => true, 'has_only_min' => true ],
147221
];
148222

149223
foreach ( array_keys( $slugs ) as $slug ) {
@@ -183,7 +257,7 @@ public function it_should_get_the_correct_url_when_wp_content_dir_and_wp_content
183257
$slugs = [
184258
'fake1' => [ true, false ],
185259
'fake2' => [ false, false ],
186-
'fake3' => [ true, true ]
260+
'fake3' => [ true, true ],
187261
];
188262

189263
foreach ( array_keys( $slugs ) as $slug ) {
@@ -224,7 +298,7 @@ public function it_should_get_the_correct_url_when_wp_content_dir_and_wp_content
224298
$slugs = [
225299
'fake1' => [ true, false ],
226300
'fake2' => [ false, false ],
227-
'fake3' => [ true, true ]
301+
'fake3' => [ true, true ],
228302
];
229303

230304
foreach ( array_keys( $slugs ) as $slug ) {
@@ -265,7 +339,7 @@ public function it_should_get_the_correct_url_when_wp_content_dir_and_wp_content
265339
$slugs = [
266340
'fake1' => [ 'has_min' => true, 'has_only_min' => false ],
267341
'fake2' => [ 'has_min' => false, 'has_only_min' => false ],
268-
'fake3' => [ 'has_min' => true, 'has_only_min' => true ]
342+
'fake3' => [ 'has_min' => true, 'has_only_min' => true ],
269343
];
270344

271345
foreach ( array_keys( $slugs ) as $slug ) {
@@ -462,7 +536,7 @@ public function should_localize_data_correctly(): void {
462536
->register();
463537
Asset::add( 'my-second-script-mod', 'second-script-mod.js' )
464538
->add_localize_script( 'boomshakalakaProjectSecondScriptModData', [
465-
'animal' => 'horse'
539+
'animal' => 'horse',
466540
] )
467541
->register();
468542

@@ -513,7 +587,7 @@ public function should_localize_dot_notation_data_correctly(): void {
513587
->register();
514588
Asset::add( 'my-second-ns-script-mod', 'second-script-mod.js' )
515589
->add_localize_script( 'boomshakalaka.project.secondScriptData', [
516-
'animal' => 'horse'
590+
'animal' => 'horse',
517591
] )
518592
->register();
519593

0 commit comments

Comments
 (0)