diff --git a/src/Assets/Assets.php b/src/Assets/Assets.php index 7061cff..fa88adb 100755 --- a/src/Assets/Assets.php +++ b/src/Assets/Assets.php @@ -190,13 +190,19 @@ public function filter_print_before_after_script( $tag, $handle ): string { /** * Get the Asset Object configuration. * - * @param string|array $slug Slug of the Asset. - * @param boolean $sort If we should do any sorting before returning. + * @param string|string[]|null $slug Slug of the Asset, array of slugs, or null for all assets. + * @param bool $sort If we should do any sorting before returning. * - * @return array|Asset Array of asset objects, single asset object, or null if looking for a single asset but - * it was not in the array of objects. - * @since 1.0.0 + * @return Asset[]|Asset|null Array of asset objects, a single Asset object, or null if looking + * for a single Asset but it was not found. + * + * @phpstan-return ( + * $slug is null ? array : + * ($slug is string ? Asset|null : + * ($slug is array ? array : mixed)) + * ) * + * @since 1.0.0 */ public function get( $slug = null, $sort = true ) { $obj = $this; @@ -249,11 +255,7 @@ public function get( $slug = null, $sort = true ) { // Prevent weird stuff here. $slug = sanitize_key( $slug ); - if ( ! empty( $this->assets[ $slug ] ) ) { - return $this->assets[ $slug ]; - } - - return []; + return $this->assets[ $slug ] ?? null; } /** @@ -696,7 +698,7 @@ public function register_in_wp( $assets = null ) { ) ) { // Registering the asset now would trigger a doing_it_wrong notice: queue the assets to be registered later. - if ( $assets === null || empty( $assets ) ) { + if ( empty( $assets ) ) { return; } @@ -829,9 +831,32 @@ public function remove( $slug ) { } /** - * Prints the `script` (JS) and `link` (CSS) HTML tags associated with one or more assets groups. + * Remove all assets. + * + * @since 1.5.0 + * + * @return int The number of assets successfully removed. + */ + public function remove_all() { + $removed = 0; + + foreach ( $this->assets as $slug => $asset ) { + if ( ! $this->remove( $slug ) ) { + continue; + } + + ++$removed; + } + + return $removed; + } + + /** + * Prints the `script` (JS) and `link` (CSS) HTML tags associated with one or more assets + * groups. * - * The method will force the scripts and styles to print overriding their registration and conditional. + * The method will force the scripts and styles to print overriding their registration and + * conditional. * * @since 1.0.0 * diff --git a/tests/wpunit/AssetsTest.php b/tests/wpunit/AssetsTest.php index f7b9229..83892e3 100644 --- a/tests/wpunit/AssetsTest.php +++ b/tests/wpunit/AssetsTest.php @@ -26,9 +26,11 @@ public function setUp() { Config::set_relative_asset_path( 'tests/_data/' ); } - public function tearDown() { - parent::tearDown(); + protected function tearDown() { + // Remove all assets. + Assets::init()->remove_all(); Config::reset(); + parent::tearDown(); } /** @@ -47,6 +49,9 @@ public function unset_uopz_redefines() { self::$uopz_redefines = []; } + /** + * @test + */ public function it_should_accept_instance_of_asset_or_array_of_assets_in_register_in_wp() { $asset_1 = Asset::add( 'fake1-script', 'fake1.js' ); $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 $assets = Assets::init(); - $assets->register_in_wp( null ); // No problemo... nothing happens though. - $assets->register_in_wp( [] ); // No problemo... nothing happens though. + // An empty array should not cause any assets to be registered. + $assets->register_in_wp( [] ); $this->assertFalse( $asset_1->is_registered() ); $assets->register_in_wp( $asset_1 ); @@ -74,6 +79,75 @@ public function it_should_accept_instance_of_asset_or_array_of_assets_in_registe $this->assertTrue( $asset_5->is_registered() ); } + /** + * @test + */ + public function it_should_remove_all_assets() { + Asset::add( 'asset-1', 'asset-1.js' ); + Asset::add( 'asset-2', 'asset-2.js' ); + Asset::add( 'asset-3', 'asset-3.js' ); + + $assets = Assets::init(); + + $this->assertCount( 3, $assets->get() ); + $this->assertSame( 3, $assets->remove_all() ); + $this->assertCount( 0, $assets->get() ); + } + + /** + * @test + */ + public function it_should_return_null_when_single_asset_not_found() { + Asset::add( 'asset-1', 'asset-1.js' ); + Asset::add( 'asset-2', 'asset-2.js' ); + Asset::add( 'asset-3', 'asset-3.js' ); + + $result = Assets::init()->get( 'unknown-slug' ); + $this->assertNull( $result ); + } + + /** + * @test + */ + public function it_should_return_empty_array_when_no_assets() { + $result = Assets::init()->get(); + $this->assertSame( [], $result ); + } + + /** + * @test + */ + public function it_gets_single_asset() { + Asset::add( 'my-single-asset', 'my-single-asset.js' ); + $result = Assets::init()->get( 'my-single-asset' ); + $this->assertInstanceOf( Asset::class, $result ); + } + + /** + * @test + */ + public function it_gets_a_subset_of_assets() { + Asset::add( 'asset-1', 'asset-1.js' ); + Asset::add( 'asset-2', 'asset-2.js' ); + Asset::add( 'asset-3', 'asset-3.js' ); + + $assets = Assets::init(); + + $result = $assets->get( ['asset-2', 'asset-3'] ); + $this->assertIsArray( $result ); + $this->assertCount( 2, $result ); + $this->assertArrayNotHasKey( 'asset-1', $result ); + $this->assertArrayHasKey( 'asset-2', $result ); + $this->assertArrayHasKey( 'asset-3', $result ); + + $single = $assets->get( ['asset-1'] ); + $this->assertIsArray( $single ); + $this->assertCount( 1, $single ); + $this->assertArrayHasKey( 'asset-1', $single ); + $this->assertArrayNotHasKey( 'asset-2', $single ); + $this->assertArrayNotHasKey( 'asset-3', $single ); + } + public function invalid_params_for_register_in_wp_provider(): Generator { yield 'string' => [ fn() => 'string' ]; yield 'int' => [ fn() => 1 ]; @@ -121,7 +195,7 @@ public function it_should_locate_minified_versions_of_external_assets() { $slugs = [ 'fake1' => [ true, false ], 'fake2' => [ false, false ], - 'fake3' => [ true, true ] + 'fake3' => [ true, true ], ]; 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 $slugs = [ 'fake1' => [ 'has_min' => true, 'has_only_min' => false ], 'fake2' => [ 'has_min' => false, 'has_only_min' => false ], - 'fake3' => [ 'has_min' => true, 'has_only_min' => true ] + 'fake3' => [ 'has_min' => true, 'has_only_min' => true ], ]; 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 $slugs = [ 'fake1' => [ true, false ], 'fake2' => [ false, false ], - 'fake3' => [ true, true ] + 'fake3' => [ true, true ], ]; 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 $slugs = [ 'fake1' => [ true, false ], 'fake2' => [ false, false ], - 'fake3' => [ true, true ] + 'fake3' => [ true, true ], ]; 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 $slugs = [ 'fake1' => [ 'has_min' => true, 'has_only_min' => false ], 'fake2' => [ 'has_min' => false, 'has_only_min' => false ], - 'fake3' => [ 'has_min' => true, 'has_only_min' => true ] + 'fake3' => [ 'has_min' => true, 'has_only_min' => true ], ]; foreach ( array_keys( $slugs ) as $slug ) { @@ -462,7 +536,7 @@ public function should_localize_data_correctly(): void { ->register(); Asset::add( 'my-second-script-mod', 'second-script-mod.js' ) ->add_localize_script( 'boomshakalakaProjectSecondScriptModData', [ - 'animal' => 'horse' + 'animal' => 'horse', ] ) ->register(); @@ -513,7 +587,7 @@ public function should_localize_dot_notation_data_correctly(): void { ->register(); Asset::add( 'my-second-ns-script-mod', 'second-script-mod.js' ) ->add_localize_script( 'boomshakalaka.project.secondScriptData', [ - 'animal' => 'horse' + 'animal' => 'horse', ] ) ->register();