diff --git a/src/Assets/Assets.php b/src/Assets/Assets.php index c3302ea..f389b04 100755 --- a/src/Assets/Assets.php +++ b/src/Assets/Assets.php @@ -2,6 +2,8 @@ namespace StellarWP\Assets; +use InvalidArgumentException; + class Assets { /** * @var ?Assets @@ -676,8 +678,9 @@ protected function do_enqueue( Asset $asset, bool $force_enqueue = false ): void * Register the Assets on the correct hooks. * * @since 1.0.0 + * @since 1.4.5 Ensure the method accepts only `null` or an `Asset` instance or an array of `Asset[]` instances. * - * @param array|Asset|null $assets Array of asset objects, single asset object, or null. + * @param Asset[]|Asset|null $assets Array of asset objects, single asset object, or null. * * @return void */ @@ -688,25 +691,27 @@ 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 ) { + if ( $assets === null || empty( $assets ) ) { return; } if ( ! is_array( $assets ) ) { - if ( ! $assets instanceof Asset ) { - throw new \InvalidArgumentException( 'Assets in register_in_wp() must be of type Asset' ); + $assets = [ $assets ]; + } + + foreach ( $assets as $asset ) { + if ( ! $asset instanceof Asset ) { + throw new InvalidArgumentException( 'Assets in register_in_wp() must be of type Asset' ); } - $assets = [ $assets->get_slug() => $assets ]; + // Register later, avoid the doing_it_wrong notice. + $this->assets[ $asset->get_slug() ] = $asset; } - // Register later, avoid the doing_it_wrong notice. - $this->assets = array_merge( $this->assets, $assets ); - return; } - if ( is_null( $assets ) ) { + if ( null === $assets ) { $assets = $this->get(); } @@ -714,7 +719,15 @@ public function register_in_wp( $assets = null ) { $assets = [ $assets ]; } + if ( empty( $assets ) ) { + return; + } + foreach ( $assets as $asset ) { + if ( ! $asset instanceof Asset ) { + throw new InvalidArgumentException( 'Assets in register_in_wp() must be of type Asset' ); + } + // Asset is already registered. if ( $asset->is_registered() ) { continue; diff --git a/tests/wpunit/AssetsTest.php b/tests/wpunit/AssetsTest.php index 86af111..5775d11 100644 --- a/tests/wpunit/AssetsTest.php +++ b/tests/wpunit/AssetsTest.php @@ -4,6 +4,10 @@ use StellarWP\Assets\Tests\AssetTestCase; use PHPUnit\Framework\Assert; +use stdClass; +use Closure; +use Generator; +use InvalidArgumentException; class AssetsTest extends AssetTestCase { /** @@ -43,6 +47,56 @@ public function unset_uopz_redefines() { self::$uopz_redefines = []; } + 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' ); + $asset_3 = Asset::add( 'fake2-script', 'fake2.js' ); + $asset_4 = Asset::add( 'fake2-style', 'fake2.css' ); + $asset_5 = Asset::add( 'fake3-script', 'fake3.js' ); + + $assets = Assets::init(); + + $assets->register_in_wp( null ); // No problemo... nothing happens though. + $assets->register_in_wp( [] ); // No problemo... nothing happens though. + + $this->assertFalse( $asset_1->is_registered() ); + $assets->register_in_wp( $asset_1 ); + $this->assertTrue( $asset_1->is_registered() ); + + $this->assertFalse( $asset_2->is_registered() ); + $this->assertFalse( $asset_3->is_registered() ); + $this->assertFalse( $asset_4->is_registered() ); + $this->assertFalse( $asset_5->is_registered() ); + $assets->register_in_wp( [ $asset_2, $asset_3, $asset_4, $asset_5 ] ); + $this->assertTrue( $asset_2->is_registered() ); + $this->assertTrue( $asset_3->is_registered() ); + $this->assertTrue( $asset_4->is_registered() ); + $this->assertTrue( $asset_5->is_registered() ); + } + + public function invalid_params_for_register_in_wp_provider(): Generator { + yield 'string' => [ fn() => 'string' ]; + yield 'int' => [ fn() => 1 ]; + yield 'float' => [ fn() => 1.1 ]; + yield 'bool - true' => [ fn() => true ]; + yield 'bool - false' => [ fn() => false ]; + yield 'object' => [ fn() => new stdClass() ]; + yield 'array - mixed' => [ fn () => [ Asset::add( 'fake1-script', 'fake1.js' ), 'string' ] ]; + } + + /** + * @test + * @dataProvider invalid_params_for_register_in_wp_provider + */ + public function it_should_throw_exception_when_invalid_params_are_passed_to_register_in_wp( Closure $fixture ) { + $assets = Assets::init(); + + $this->expectException( InvalidArgumentException::class ); + $this->expectExceptionMessage( 'Assets in register_in_wp() must be of type Asset' ); + + $assets->register_in_wp( $fixture() ); + } + /** * @test */