From 2cb12a580501912ed78c8b7a7521121932b5c961 Mon Sep 17 00:00:00 2001 From: Dimitrios Pantazis Date: Tue, 17 Dec 2024 18:43:35 +0200 Subject: [PATCH 1/9] Enable multiple registrations --- src/Assets/Asset.php | 116 ++++++++++++++++++++++++++++++++++++++++++ src/Assets/Assets.php | 81 ++--------------------------- 2 files changed, 121 insertions(+), 76 deletions(-) diff --git a/src/Assets/Asset.php b/src/Assets/Asset.php index 874256c..3d5420c 100644 --- a/src/Assets/Asset.php +++ b/src/Assets/Asset.php @@ -3,6 +3,7 @@ namespace StellarWP\Assets; use InvalidArgumentException; +use RuntimeException; class Asset { /** @@ -1696,6 +1697,121 @@ public function set_as_registered() { return $this; } + /** + * Gives us the option to call any wp_*_script or wp_*_style function + * on the asset. + * + * example: $asset->register_asset( ...$args ); will call wp_register_script( $slug, ...$args ); or + * wp_register_style( $slug, ...$args ); based on if the asset is JS or CSS type. + * + * @since TBD + * + * @return static + */ + public function __call( $method, $args ) { + if ( ! strstr( $method, 'asset' ) ) { + throw new RuntimeException( "Method {$method} does not exist." ); + } + + $method = 'wp_' . str_replace( 'asset', $this->get_script_or_style(), $method ); + + if ( ! function_exists( $method ) ) { + throw new RuntimeException( "Method {$method} does not exist." ); + } + + $method( $this->get_slug(), ...$args ); + + return $this; + } + + /** + * Check if the asset is something. + * + * In the background uses wp_script_is or wp_style_is. + * + * @since TBD + * + * @param string $what The what to check against. + * + * @return bool + */ + public function asset_is( string $what ): bool { + return ( 'wp_' . $this->get_script_or_style() . '_is' )( $this->get_slug(), $what ); + } + + /** + * Get the script or style based on the asset type. + * + * @since TBD + * + * @return string + */ + protected function get_script_or_style(): string { + return 'js' === $this->get_type() ? 'script' : 'style'; + } + + /** + * Prints the asset + * + * @since TBD + * + * @return static + */ + public function do_print() { + if ( $this->should_print() && ! $this->is_printed() ) { + $this->set_as_printed(); + $this->print_assets(); + } + + // We print first, and tell the system it was enqueued, WP is smart not to do it twice. + $this->enqueue_asset(); + + if ( ! $this->is_css() ) { + return $this; + } + + foreach ( $this->get_style_data() as $key => $value ) { + wp_style_add_data( $this->get_slug(), $key, $value ); + } + + return $this; + } + + /** + * Performs the asset registration in WP. + * + * @since TBD + * + * @return static + */ + public function do_register() { + if ( $this->is_registered() ) { + return $this; + } + + $this->register_asset( $this->get_url(), $this->get_dependencies(), $this->get_version(), $this->is_js() ? $this->is_in_footer() : $this->get_media() ); + $this->set_as_registered(); + + if ( $this->is_js() ) { + if ( empty( $this->get_translation_path() ) || empty( $this->get_textdomain() ) ) { + return $this; + } + + wp_set_script_translations( $this->get_slug(), $this->get_textdomain(), $this->get_translation_path() ); + return $this; + } + + + $style_data = $this->get_style_data(); + if ( $style_data ) { + foreach ( $style_data as $datum_key => $datum_value ) { + wp_style_add_data( $this->get_slug(), $datum_key, $datum_value ); + } + } + + return $this; + } + /** * Set the asset enqueue status to false. * diff --git a/src/Assets/Assets.php b/src/Assets/Assets.php index e9f719a..0cdd42f 100755 --- a/src/Assets/Assets.php +++ b/src/Assets/Assets.php @@ -643,27 +643,7 @@ protected function do_enqueue( Asset $asset, bool $force_enqueue = false ): void return; } - if ( 'js' === $asset->get_type() ) { - if ( $asset->should_print() && ! $asset->is_printed() ) { - $asset->set_as_printed(); - wp_print_scripts( [ $slug ] ); - } - // We print first, and tell the system it was enqueued, WP is smart not to do it twice. - wp_enqueue_script( $slug ); - } else { - if ( $asset->should_print() && ! $asset->is_printed() ) { - $asset->set_as_printed(); - wp_print_styles( [ $slug ] ); - } - - // We print first, and tell the system it was enqueued, WP is smart not to do it twice. - wp_enqueue_style( $slug ); - - $style_data = $asset->get_style_data(); - foreach ( $style_data as $key => $value ) { - wp_style_add_data( $slug, $key, $value ); - } - } + $asset->do_print(); if ( ! empty( $asset->get_after_enqueue() ) && is_callable( $asset->get_after_enqueue() ) ) { call_user_func_array( $asset->get_after_enqueue(), [ $asset ] ); @@ -713,49 +693,7 @@ public function register_in_wp( $assets = null ) { continue; } - $asset_slug = $asset->get_slug(); - - if ( 'js' === $asset->get_type() ) { - // Script is already registered. - if ( wp_script_is( $asset_slug, 'registered' ) ) { - continue; - } - - wp_register_script( $asset_slug, $asset->get_url(), $asset->get_dependencies(), $asset->get_version(), $asset->is_in_footer() ); - - // Register that this asset is actually registered on the WP methods. - // @phpstan-ignore-next-line - if ( wp_script_is( $asset_slug, 'registered' ) ) { - $asset->set_as_registered(); - } - - if ( - ! empty( $asset->get_translation_path() ) - && ! empty( $asset->get_textdomain() ) - ) { - wp_set_script_translations( $asset_slug, $asset->get_textdomain(), $asset->get_translation_path() ); - } - } else { - // Style is already registered. - if ( wp_style_is( $asset_slug, 'registered' ) ) { - continue; - } - - wp_register_style( $asset_slug, $asset->get_url(), $asset->get_dependencies(), $asset->get_version(), $asset->get_media() ); - - // Register that this asset is actually registered on the WP methods. - // @phpstan-ignore-next-line - if ( wp_style_is( $asset_slug, 'registered' ) ) { - $asset->set_as_registered(); - } - - $style_data = $asset->get_style_data(); - if ( $style_data ) { - foreach ( $style_data as $datum_key => $datum_value ) { - wp_style_add_data( $asset_slug, $datum_key, $datum_value ); - } - } - } + $asset->do_register(); // If we don't have an action we don't even register the action to enqueue. if ( empty( $asset->get_action() ) ) { @@ -788,15 +726,7 @@ public function remove( $slug ) { return false; } - $type = $this->get( $slug )->get_type(); - - if ( $type === 'css' ) { - wp_dequeue_style( $slug ); - wp_deregister_style( $slug ); - } else { - wp_dequeue_script( $slug ); - wp_deregister_script( $slug ); - } + $this->get( $slug )->dequeue_asset()->deregister_asset(); unset( $this->assets[ $slug ] ); @@ -836,9 +766,8 @@ public function print_group( $group, $echo = true ) { if ( $asset->is_registered() ) { continue; } - 'js' === $asset->get_type() - ? wp_register_script( $slug, $asset->get_file(), $asset->get_dependencies(), $asset->get_version() ) - : wp_register_style( $slug, $asset->get_file(), $asset->get_dependencies(), $asset->get_version() ); + + $asset->register_asset( $asset->get_file(), $asset->get_dependencies(), $asset->get_version() ); } ob_start(); From 815003e3c7e3e4ae424a42fb26ab46b554e5d614 Mon Sep 17 00:00:00 2001 From: Dimitrios Pantazis Date: Tue, 17 Dec 2024 19:05:25 +0200 Subject: [PATCH 2/9] Fix static analysis --- src/Assets/Asset.php | 7 +++++++ src/Assets/Assets.php | 8 +++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Assets/Asset.php b/src/Assets/Asset.php index 3d5420c..6a9cc20 100644 --- a/src/Assets/Asset.php +++ b/src/Assets/Asset.php @@ -5,6 +5,13 @@ use InvalidArgumentException; use RuntimeException; +/** + * @method self print_assets() + * @method self enqueue_asset() + * @method self register_asset( string $url, array $dependencies = [], string $version = null, mixed $in_footer_or_media ) + * @method self dequeue_asset() + * @method self deregister_asset() + */ class Asset { /** * @var array The asset action. diff --git a/src/Assets/Assets.php b/src/Assets/Assets.php index 0cdd42f..46c81e3 100755 --- a/src/Assets/Assets.php +++ b/src/Assets/Assets.php @@ -726,7 +726,13 @@ public function remove( $slug ) { return false; } - $this->get( $slug )->dequeue_asset()->deregister_asset(); + $asset = $this->get( $slug ); + + if ( ! $asset instanceof Asset ) { + return true; + } + + $asset->dequeue_asset()->deregister_asset(); unset( $this->assets[ $slug ] ); From 9cb46f6ca674f0a0975c89a297b0480b88e0d533 Mon Sep 17 00:00:00 2001 From: Dimitrios Pantazis Date: Tue, 17 Dec 2024 20:00:29 +0200 Subject: [PATCH 3/9] Add testcases --- src/Assets/Asset.php | 10 ++--- tests/wpunit/AssetTest.php | 77 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 6 deletions(-) diff --git a/src/Assets/Asset.php b/src/Assets/Asset.php index 6a9cc20..5a23e71 100644 --- a/src/Assets/Asset.php +++ b/src/Assets/Asset.php @@ -1792,10 +1792,6 @@ public function do_print() { * @return static */ public function do_register() { - if ( $this->is_registered() ) { - return $this; - } - $this->register_asset( $this->get_url(), $this->get_dependencies(), $this->get_version(), $this->is_js() ? $this->is_in_footer() : $this->get_media() ); $this->set_as_registered(); @@ -1823,24 +1819,26 @@ public function do_register() { * Set the asset enqueue status to false. * * @since 1.0.0 + * @since TBD - Actually dequeues the asset. * * @return static */ public function set_as_unenqueued() { $this->is_enqueued = false; - return $this; + return $this->dequeue_asset(); } /** * Set the asset registration status to false. * * @since 1.0.0 + * @since TBD - Actually deregisters the asset. * * @return static */ public function set_as_unregistered() { $this->is_registered = false; - return $this; + return $this->deregister_asset(); } /** diff --git a/tests/wpunit/AssetTest.php b/tests/wpunit/AssetTest.php index 032ce72..98bde30 100644 --- a/tests/wpunit/AssetTest.php +++ b/tests/wpunit/AssetTest.php @@ -29,4 +29,81 @@ public function test_add_to_group_path_changes_resolution_path_to_group(): void $this->assertEquals( WP_PLUGIN_DIR . '/assets/tests/_data/', $asset->get_root_path() ); } + + public function test_it_can_enqueue_multiple_times_overwritting_previous(): void { + Config::reset(); + Config::set_hook_prefix( 'bork' ); + Config::set_version( '1.1.0' ); + Config::set_path( constant( 'WP_PLUGIN_DIR' ) . '/assets' ); + Config::set_relative_asset_path( 'tests/_data/' ); + + $checker = static fn( string $handle ): array => wp_scripts()->registered[ $handle ]->deps; + + $asset = Asset::add( 'test-script-unique-654321', 'fake.js', '1.0.0', codecept_data_dir() ); + $asset->enqueue_on( 'a_random_action_1234' ); + $asset->set_dependencies( 'jquery', 'jquery-ui-core' ); + + do_action( 'a_random_action_1234' ); + + $asset->enqueue(); + + $this->assertTrue( wp_script_is( $asset->get_slug(), 'enqueued' ) ); + $this->assertTrue( $asset->asset_is( 'enqueued' ) ); + $this->assertEquals( [ 'jquery', 'jquery-ui-core' ], $checker( $asset->get_slug() ) ); + $this->assertEquals( [ 'jquery', 'jquery-ui-core' ], $asset->get_dependencies() ); + + $asset->set_dependencies( 'jquery' ); + // It's not going to register again! We need to set it as unregistered first. + $asset->enqueue(); + + $this->assertTrue( wp_script_is( $asset->get_slug(), 'enqueued' ) ); + $this->assertTrue( $asset->asset_is( 'enqueued' ) ); + $this->assertEquals( [ 'jquery', 'jquery-ui-core' ], $checker( $asset->get_slug() ) ); + $this->assertEquals( [ 'jquery' ], $asset->get_dependencies() ); + + $asset->set_as_unregistered(); + $asset->enqueue(); + + $this->assertTrue( wp_script_is( $asset->get_slug(), 'enqueued' ) ); + $this->assertTrue( $asset->asset_is( 'enqueued' ) ); + $this->assertEquals( [ 'jquery' ], $checker( $asset->get_slug() ) ); + $this->assertEquals( [ 'jquery' ], $asset->get_dependencies() ); + } + + public function test_it_can_register_multiple_times_overwritting_previous(): void { + Config::reset(); + Config::set_hook_prefix( 'bork' ); + Config::set_version( '1.1.0' ); + Config::set_path( constant( 'WP_PLUGIN_DIR' ) . '/assets' ); + Config::set_relative_asset_path( 'tests/_data/' ); + + $checker = static fn( string $handle ): array => wp_scripts()->registered[ $handle ]->deps; + + $asset = new Asset( 'test-script-unique-123456', 'fake.js', '1.0.0', codecept_data_dir() ); + + $asset->set_dependencies( 'jquery', 'jquery-ui-core' ); + $asset->register(); + + $this->assertTrue( wp_script_is( $asset->get_slug(), 'registered' ) ); + $this->assertTrue( $asset->asset_is( 'registered' ) ); + $this->assertEquals( [ 'jquery', 'jquery-ui-core' ], $checker( $asset->get_slug() ) ); + $this->assertEquals( [ 'jquery', 'jquery-ui-core' ], $asset->get_dependencies() ); + + $asset->set_dependencies( 'jquery' ); + // It's not going to register again! We need to set it as unregistered first. + $asset->register(); + + $this->assertTrue( wp_script_is( $asset->get_slug(), 'registered' ) ); + $this->assertTrue( $asset->asset_is( 'registered' ) ); + $this->assertEquals( [ 'jquery', 'jquery-ui-core' ], $checker( $asset->get_slug() ) ); + $this->assertEquals( [ 'jquery' ], $asset->get_dependencies() ); + + $asset->set_as_unregistered(); + $asset->register(); + + $this->assertTrue( wp_script_is( $asset->get_slug(), 'registered' ) ); + $this->assertTrue( $asset->asset_is( 'registered' ) ); + $this->assertEquals( [ 'jquery' ], $checker( $asset->get_slug() ) ); + $this->assertEquals( [ 'jquery' ], $asset->get_dependencies() ); + } } From 23a3b6b840547307dce1a27e398c80891b2bae64 Mon Sep 17 00:00:00 2001 From: Dimitrios Pantazis Date: Tue, 17 Dec 2024 20:02:36 +0200 Subject: [PATCH 4/9] fix static analysis --- src/Assets/Asset.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Assets/Asset.php b/src/Assets/Asset.php index 5a23e71..20611ae 100644 --- a/src/Assets/Asset.php +++ b/src/Assets/Asset.php @@ -1825,7 +1825,8 @@ public function do_register() { */ public function set_as_unenqueued() { $this->is_enqueued = false; - return $this->dequeue_asset(); + $this->dequeue_asset(); + return $this; } /** @@ -1838,7 +1839,8 @@ public function set_as_unenqueued() { */ public function set_as_unregistered() { $this->is_registered = false; - return $this->deregister_asset(); + $this->deregister_asset(); + return $this; } /** From 520eca8dc8d71408aecf86339eac2dafa8d0aacb Mon Sep 17 00:00:00 2001 From: Dimitrios Pantazis Date: Fri, 10 Jan 2025 18:42:18 +0200 Subject: [PATCH 5/9] unroll magic method --- src/Assets/Asset.php | 81 ++++++++++++++++++++++++++++++++------------ 1 file changed, 60 insertions(+), 21 deletions(-) diff --git a/src/Assets/Asset.php b/src/Assets/Asset.php index 20611ae..7443b69 100644 --- a/src/Assets/Asset.php +++ b/src/Assets/Asset.php @@ -6,11 +6,7 @@ use RuntimeException; /** - * @method self print_assets() - * @method self enqueue_asset() - * @method self register_asset( string $url, array $dependencies = [], string $version = null, mixed $in_footer_or_media ) - * @method self dequeue_asset() - * @method self deregister_asset() + * Class Asset */ class Asset { /** @@ -1705,29 +1701,72 @@ public function set_as_registered() { } /** - * Gives us the option to call any wp_*_script or wp_*_style function - * on the asset. + * Prints the asset using wp_print_scripts or wp_print_styles. * - * example: $asset->register_asset( ...$args ); will call wp_register_script( $slug, ...$args ); or - * wp_register_style( $slug, ...$args ); based on if the asset is JS or CSS type. - * - * @since TBD + * @since 1.4.4 * * @return static */ - public function __call( $method, $args ) { - if ( ! strstr( $method, 'asset' ) ) { - throw new RuntimeException( "Method {$method} does not exist." ); - } + public function print_asset() { + $method = 'wp_print_' . $this->get_script_or_style() . 's'; + $method( $this->get_slug() ); + return $this; + } - $method = 'wp_' . str_replace( 'asset', $this->get_script_or_style(), $method ); + /** + * Enqueues the asset using wp_enqueue_script or wp_enqueue_style. + * + * @since 1.4.4 + * + * @return static + */ + public function enqueue_asset() { + $method = 'wp_enqueue_' . $this->get_script_or_style(); + $method( $this->get_slug() ); + return $this; + } - if ( ! function_exists( $method ) ) { - throw new RuntimeException( "Method {$method} does not exist." ); - } + /** + * Registers the asset using wp_register_script or wp_register_style. + * + * @since 1.4.4 + * + * @param string $url The URL to the asset. + * @param array $dependencies The dependencies for the asset. + * @param string $version The version of the asset. + * @param bool|string $in_footer_or_media Whether to enqueue in the footer or the media type for the asset. + * + * @return static + */ + public function register_asset( string $url, array $dependencies = [], string $version = null, $in_footer_or_media ) { + $method = 'wp_register_' . $this->get_script_or_style(); + $method( $this->get_slug(), $url, $dependencies, $version, $in_footer_or_media ); + return $this; + } - $method( $this->get_slug(), ...$args ); + /** + * Dequeues the asset using wp_dequeue_script or wp_dequeue_style. + * + * @since 1.4.4 + * + * @return static + */ + public function dequeue_asset() { + $method = 'wp_dequeue_' . $this->get_script_or_style(); + $method( $this->get_slug() ); + return $this; + } + /** + * Deregisters the asset using wp_deregister_script or wp_deregister_style. + * + * @since 1.4.4 + * + * @return static + */ + public function deregister_asset() { + $method = 'wp_deregister_' . $this->get_script_or_style(); + $method( $this->get_slug() ); return $this; } @@ -1767,7 +1806,7 @@ protected function get_script_or_style(): string { public function do_print() { if ( $this->should_print() && ! $this->is_printed() ) { $this->set_as_printed(); - $this->print_assets(); + $this->print_asset(); } // We print first, and tell the system it was enqueued, WP is smart not to do it twice. From 70c559a5c76eb51ff193e444ef1ff8057192fc90 Mon Sep 17 00:00:00 2001 From: Dimitrios Pantazis Date: Fri, 10 Jan 2025 18:46:15 +0200 Subject: [PATCH 6/9] fix static analysis --- src/Assets/Asset.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Assets/Asset.php b/src/Assets/Asset.php index 7443b69..c0fa21a 100644 --- a/src/Assets/Asset.php +++ b/src/Assets/Asset.php @@ -1738,7 +1738,7 @@ public function enqueue_asset() { * * @return static */ - public function register_asset( string $url, array $dependencies = [], string $version = null, $in_footer_or_media ) { + public function register_asset( string $url, array $dependencies = [], string $version = null, $in_footer_or_media = 'all' ) { $method = 'wp_register_' . $this->get_script_or_style(); $method( $this->get_slug(), $url, $dependencies, $version, $in_footer_or_media ); return $this; From ce549ec84af4d2b4ebdd8832cdb2eeacdc09396a Mon Sep 17 00:00:00 2001 From: Dimitrios Pantazis Date: Fri, 10 Jan 2025 18:49:23 +0200 Subject: [PATCH 7/9] update docblocks --- src/Assets/Asset.php | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/Assets/Asset.php b/src/Assets/Asset.php index c0fa21a..2f1d640 100644 --- a/src/Assets/Asset.php +++ b/src/Assets/Asset.php @@ -3,10 +3,9 @@ namespace StellarWP\Assets; use InvalidArgumentException; -use RuntimeException; /** - * Class Asset + * Class Asset. */ class Asset { /** @@ -1775,7 +1774,7 @@ public function deregister_asset() { * * In the background uses wp_script_is or wp_style_is. * - * @since TBD + * @since 1.4.4 * * @param string $what The what to check against. * @@ -1788,7 +1787,7 @@ public function asset_is( string $what ): bool { /** * Get the script or style based on the asset type. * - * @since TBD + * @since 1.4.4 * * @return string */ @@ -1799,7 +1798,7 @@ protected function get_script_or_style(): string { /** * Prints the asset * - * @since TBD + * @since 1.4.4 * * @return static */ @@ -1826,7 +1825,7 @@ public function do_print() { /** * Performs the asset registration in WP. * - * @since TBD + * @since 1.4.4 * * @return static */ @@ -1858,7 +1857,7 @@ public function do_register() { * Set the asset enqueue status to false. * * @since 1.0.0 - * @since TBD - Actually dequeues the asset. + * @since 1.4.4 - Actually dequeues the asset. * * @return static */ @@ -1872,7 +1871,7 @@ public function set_as_unenqueued() { * Set the asset registration status to false. * * @since 1.0.0 - * @since TBD - Actually deregisters the asset. + * @since 1.4.4 - Actually deregisters the asset. * * @return static */ From 1223e3b80de127bf8c99abad7a34faa893b43390 Mon Sep 17 00:00:00 2001 From: Dimitrios Pantazis Date: Fri, 10 Jan 2025 19:45:28 +0200 Subject: [PATCH 8/9] added default registration values --- src/Assets/Asset.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Assets/Asset.php b/src/Assets/Asset.php index 2f1d640..f998fa3 100644 --- a/src/Assets/Asset.php +++ b/src/Assets/Asset.php @@ -1737,7 +1737,12 @@ public function enqueue_asset() { * * @return static */ - public function register_asset( string $url, array $dependencies = [], string $version = null, $in_footer_or_media = 'all' ) { + public function register_asset( string $url = '', array $dependencies = [], string $version = null, $in_footer_or_media = 'all' ) { + $url = $url ? $url : $this->get_url(); + $dependencies = $dependencies ? $dependencies : $this->get_dependencies(); + $version = $version ?? $this->get_version(); + $in_footer_or_media = $in_footer_or_media ? $in_footer_or_media : ( $this->is_js() ? $this->is_in_footer() : $this->get_media() ); + $method = 'wp_register_' . $this->get_script_or_style(); $method( $this->get_slug(), $url, $dependencies, $version, $in_footer_or_media ); return $this; From 2616c766596815e3c992b652ef5e82162ae99849 Mon Sep 17 00:00:00 2001 From: Dimitrios Pantazis Date: Fri, 10 Jan 2025 19:45:34 +0200 Subject: [PATCH 9/9] added test coverage --- tests/wpunit/AssetTest.php | 74 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/tests/wpunit/AssetTest.php b/tests/wpunit/AssetTest.php index 98bde30..47614df 100644 --- a/tests/wpunit/AssetTest.php +++ b/tests/wpunit/AssetTest.php @@ -106,4 +106,78 @@ public function test_it_can_register_multiple_times_overwritting_previous(): voi $this->assertEquals( [ 'jquery' ], $checker( $asset->get_slug() ) ); $this->assertEquals( [ 'jquery' ], $asset->get_dependencies() ); } + + public function test_it_print_asset() { + Config::reset(); + Config::set_hook_prefix( 'bork' ); + Config::set_version( '1.1.0' ); + Config::set_path( constant( 'WP_PLUGIN_DIR' ) . '/assets' ); + Config::set_relative_asset_path( 'tests/_data/' ); + + $asset_js = new Asset( 'test-script', 'fake.js', '1.0.0' ); + $asset_css = new Asset( 'test-style', 'fake.css', '1.0.0' ); + + ob_start(); + $asset_js->register_asset(); + $asset_css->register_asset(); + $asset_js->print_asset(); + $asset_css->print_asset(); + $this->assertEquals( + ' + +', + ob_get_clean() + ); + } + + public function test_it_enqueues_asset() { + Config::reset(); + Config::set_hook_prefix( 'bork' ); + Config::set_version( '1.1.0' ); + Config::set_path( constant( 'WP_PLUGIN_DIR' ) . '/assets' ); + Config::set_relative_asset_path( 'tests/_data/' ); + + $asset_js = new Asset( 'test-script', 'fake.js', '1.0.0' ); + $asset_css = new Asset( 'test-style', 'fake.css', '1.0.0' ); + + $asset_js->register_asset(); + $asset_css->register_asset(); + + $this->assertTrue( wp_script_is( 'test-script', 'registered' ) ); + $this->assertSame( $asset_js->asset_is( 'registered' ), wp_script_is( 'test-script', 'registered' ) ); + $this->assertTrue( wp_style_is( 'test-style', 'registered' ) ); + $this->assertSame( $asset_css->asset_is( 'registered' ), wp_style_is( 'test-style', 'registered' ) ); + + $asset_js->enqueue_asset(); + $asset_css->enqueue_asset(); + + $this->assertTrue( wp_script_is( 'test-script', 'enqueued' ) ); + $this->assertSame( $asset_js->asset_is( 'enqueued' ), wp_script_is( 'test-script', 'enqueued' ) ); + $this->assertTrue( wp_style_is( 'test-style', 'enqueued' ) ); + $this->assertSame( $asset_css->asset_is( 'enqueued' ), wp_style_is( 'test-style', 'enqueued' ) ); + + $asset_js->dequeue_asset(); + $asset_css->dequeue_asset(); + + $this->assertFalse( wp_script_is( 'test-script', 'enqueued' ) ); + $this->assertSame( $asset_js->asset_is( 'enqueued' ), wp_script_is( 'test-script', 'enqueued' ) ); + $this->assertFalse( wp_style_is( 'test-style', 'enqueued' ) ); + $this->assertSame( $asset_css->asset_is( 'enqueued' ), wp_style_is( 'test-style', 'enqueued' ) ); + $this->assertTrue( wp_script_is( 'test-script', 'registered' ) ); + $this->assertSame( $asset_js->asset_is( 'registered' ), wp_script_is( 'test-script', 'registered' ) ); + $this->assertTrue( wp_style_is( 'test-style', 'registered' ) ); + $this->assertSame( $asset_css->asset_is( 'registered' ), wp_style_is( 'test-style', 'registered' ) ); + + $asset_js->deregister_asset(); + $asset_css->deregister_asset(); + + $this->assertFalse( wp_script_is( 'test-script', 'enqueued' ) ); + $this->assertSame( $asset_js->asset_is( 'enqueued' ), wp_script_is( 'test-script', 'enqueued' ) ); + $this->assertFalse( wp_style_is( 'test-style', 'enqueued' ) ); + $this->assertSame( $asset_css->asset_is( 'enqueued' ), wp_style_is( 'test-style', 'enqueued' ) ); + $this->assertFalse( wp_script_is( 'test-script', 'registered' ) ); + $this->assertSame( $asset_js->asset_is( 'registered' ), wp_script_is( 'test-script', 'registered' ) ); + $this->assertFalse( wp_style_is( 'test-style', 'registered' ) ); + $this->assertSame( $asset_css->asset_is( 'registered' ), wp_style_is( 'test-style', 'registered' ) ); + } }