Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure reigster_in_wp accepts only null or Asset or Asset[] #32

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions src/Assets/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace StellarWP\Assets;

use InvalidArgumentException;

class Assets {
/**
* @var ?Assets
Expand Down Expand Up @@ -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
*/
Expand All @@ -688,33 +691,43 @@ 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();
}

if ( ! is_array( $assets ) ) {
$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;
Expand Down
54 changes: 54 additions & 0 deletions tests/wpunit/AssetsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand Down Expand Up @@ -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
*/
Expand Down
Loading