Skip to content
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
51 changes: 38 additions & 13 deletions src/Assets/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, Asset> :
* ($slug is string ? Asset|null :
* ($slug is array ? array<string, Asset> : mixed))
* )
*
* @since 1.0.0
*/
public function get( $slug = null, $sort = true ) {
$obj = $this;
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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
*
Expand Down
96 changes: 85 additions & 11 deletions tests/wpunit/AssetsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand All @@ -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' );
Expand All @@ -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 );
Expand All @@ -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 ];
Expand Down Expand Up @@ -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 ) {
Expand All @@ -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 ) {
Expand Down Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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();

Expand Down
Loading