From 9dd6f86eb48f7c9adf54fd2727d0194bdfe25698 Mon Sep 17 00:00:00 2001 From: Dimitrios Pantazis Date: Wed, 5 Jun 2024 17:24:40 +0300 Subject: [PATCH] tests updated --- tests/_data/css/fake.min.css | 0 tests/_data/css/fake2.css | 0 tests/_data/css/fake3.min.css | 0 tests/_data/js/fake.min.js | 0 tests/_data/js/fake2.js | 0 tests/_data/js/fake3.min.js | 0 tests/wpunit/AssetsTest.php | 112 +++++++++++++++++++++++++++++++--- 7 files changed, 104 insertions(+), 8 deletions(-) create mode 100644 tests/_data/css/fake.min.css create mode 100644 tests/_data/css/fake2.css create mode 100644 tests/_data/css/fake3.min.css create mode 100644 tests/_data/js/fake.min.js create mode 100644 tests/_data/js/fake2.js create mode 100644 tests/_data/js/fake3.min.js diff --git a/tests/_data/css/fake.min.css b/tests/_data/css/fake.min.css new file mode 100644 index 0000000..e69de29 diff --git a/tests/_data/css/fake2.css b/tests/_data/css/fake2.css new file mode 100644 index 0000000..e69de29 diff --git a/tests/_data/css/fake3.min.css b/tests/_data/css/fake3.min.css new file mode 100644 index 0000000..e69de29 diff --git a/tests/_data/js/fake.min.js b/tests/_data/js/fake.min.js new file mode 100644 index 0000000..e69de29 diff --git a/tests/_data/js/fake2.js b/tests/_data/js/fake2.js new file mode 100644 index 0000000..e69de29 diff --git a/tests/_data/js/fake3.min.js b/tests/_data/js/fake3.min.js new file mode 100644 index 0000000..e69de29 diff --git a/tests/wpunit/AssetsTest.php b/tests/wpunit/AssetsTest.php index 3f92974..4c9bfa2 100644 --- a/tests/wpunit/AssetsTest.php +++ b/tests/wpunit/AssetsTest.php @@ -22,22 +22,53 @@ public function tearDown() { /** * @test */ - public function it_should_should_register_multiple_assets() { + public function it_should_register_multiple_assets() { Asset::add( 'my-script', 'fake.js' )->register(); Asset::add( 'my-style', 'fake.css' )->register(); - $this->assertTrue( Assets::init()->exists( 'my-script' ) ); - $this->assertTrue( Assets::init()->exists( 'my-style' ) ); - $this->assertTrue( wp_script_is( 'my-script', 'registered' ) ); - $this->assertTrue( wp_style_is( 'my-style', 'registered' ) ); - $this->assertEquals( 'my-script', Assets::init()->get( 'my-script' )->get_slug() ); - $this->assertEquals( 'my-style', Assets::init()->get( 'my-style' )->get_slug() ); + $this->existence_assertions( 'my' ); + } + + /** + * @test + */ + public function it_should_locate_minified_versions_of_external_assets() { + if ( defined( SCRIPT_DEBUG ) ) { + $old_script_debug = SCRIPT_DEBUG; + uopz_undefine( 'SCRIPT_DEBUG' ); + } + + Asset::add( 'fake-script', 'fake.js' )->register(); + Asset::add( 'fake-style', 'fake.css' )->register(); + Asset::add( 'fake2-script', 'fake2.js' )->register(); + Asset::add( 'fake2-style', 'fake2.css' )->register(); + Asset::add( 'fake3-script', 'fake3.js' )->register(); + Asset::add( 'fake3-style', 'fake3.css' )->register(); + + $slugs = [ + 'fake' => [ true, false ], + 'fake2' => [ false, false ], + 'fake3' => [ true, true ] + ]; + + foreach ( array_keys( $slugs ) as $slug ) { + $this->existence_assertions( $slug ); + } + + foreach ( $slugs as $slug => $data ) { + $this->assert_minified_found( $slug, true, ...$data ); + $this->assert_minified_found( $slug, false, ...$data ); + } + + if ( isset( $old_script_debug ) ) { + uopz_redefine( 'SCRIPT_DEBUG', $old_script_debug ); + } } /** * @test */ - public function it_should_should_remove_assets() { + public function it_should_remove_assets() { Asset::add( 'my-script', 'fake.js' )->register(); Asset::add( 'my-style', 'fake.css' )->register(); @@ -332,4 +363,69 @@ public function should_allow_setting_dependencies_with_a_callable(): void { ob_get_clean() ); } + + /** + * Evaluates if a script and style have been registered. + */ + protected function existence_assertions( $test_slug_prefix ) { + $this->assertTrue( Assets::init()->exists( $test_slug_prefix . '-script' ) ); + $this->assertTrue( Assets::init()->exists( $test_slug_prefix . '-style' ) ); + $this->assertTrue( wp_script_is( $test_slug_prefix . '-script', 'registered' ) ); + $this->assertTrue( wp_style_is( $test_slug_prefix . '-style', 'registered' ) ); + $this->assertEquals( $test_slug_prefix . '-script', Assets::init()->get( $test_slug_prefix . '-script' )->get_slug() ); + $this->assertEquals( $test_slug_prefix . '-style', Assets::init()->get( $test_slug_prefix. '-style' )->get_slug() ); + } + + /** + * Asserts that the minified version of a script or style is found. + * + * @param string $slug_prefix + * @param bool $is_js + * @param bool $has_min + * @param bool $has_only_min + */ + protected function assert_minified_found( $slug_prefix, $is_js = true, $has_min = true, $has_only_min = false ) { + $asset = Assets::init()->get( $slug_prefix . '-' . ( $is_js ? 'script' : 'style' ) ); + + $url = get_site_url() . '/wp-content/plugins/assets/tests/_data/' . ( $is_js ? 'js' : 'css' ) . '/' . $slug_prefix; + + $urls = []; + + uopz_redefine( 'SCRIPT_DEBUG', false ); + + $this->assertFalse( SCRIPT_DEBUG ); + + if ( $has_only_min ) { + $urls[] = $url . '.min' . ( $is_js ? '.js' : '.css' ); + $urls[] = $url . '.min' . ( $is_js ? '.js' : '.css' ); + } elseif ( $has_min ) { + $urls[] = $url . ( $is_js ? '.min.js' : '.min.css' ); + $urls[] = $url . ( $is_js ? '.js' : '.css' ); + } else { + $urls[] = $url . ( $is_js ? '.js' : '.css' ); + $urls[] = $url . ( $is_js ? '.js' : '.css' ); + } + + $this->assertEquals( + $urls['0'], + $asset->get_url() + ); + + uopz_redefine( 'SCRIPT_DEBUG', true ); + + $this->assertTrue( SCRIPT_DEBUG ); + + // Remove and re add to clear cache. + Assets::init()->remove( $slug_prefix . '-' . ( $is_js ? 'script' : 'style' ) ); + Asset::add( $slug_prefix . '-' . ( $is_js ? 'script' : 'style' ), $slug_prefix . '.' . ( $is_js ? 'js' : 'css' ) )->register(); + + $asset = Assets::init()->get( $slug_prefix . '-' . ( $is_js ? 'script' : 'style' ) ); + + $this->assertEquals( + $urls['1'], + $asset->get_url() + ); + + uopz_undefine( 'SCRIPT_DEBUG' ); + } }