From c928908b2717d5bbdfc76a011454eb22bd0b37ae Mon Sep 17 00:00:00 2001 From: inpsyde-maticluznar Date: Fri, 26 Jul 2024 11:12:59 +0200 Subject: [PATCH] Refactor and unit test --- src/Caching/IgnoreCacheHandler.php | 19 +++-- src/Caching/IgnoreSitegroundCache.php | 22 +++--- src/Caching/IgnoreW3TotalCache.php | 31 ++++---- .../Unit/Caching/IgnoreCacheHandlerTest.php | 72 +++++++++++++++++++ .../Caching/IgnoreSitegroundCacheTest.php | 36 ++++++++++ .../Unit/Caching/IgnoreW3TotalCacheTest.php | 47 ++++++++++++ 6 files changed, 198 insertions(+), 29 deletions(-) create mode 100644 tests/phpunit/Unit/Caching/IgnoreCacheHandlerTest.php create mode 100644 tests/phpunit/Unit/Caching/IgnoreSitegroundCacheTest.php create mode 100644 tests/phpunit/Unit/Caching/IgnoreW3TotalCacheTest.php diff --git a/src/Caching/IgnoreCacheHandler.php b/src/Caching/IgnoreCacheHandler.php index 5b2126f..0de6757 100644 --- a/src/Caching/IgnoreCacheHandler.php +++ b/src/Caching/IgnoreCacheHandler.php @@ -18,8 +18,18 @@ public function execute(AssetManager $assetManager): void new IgnoreSitegroundCache(), ]; - $assets = $assetManager->assets(); + $assetHandles = $this->extractHandles($assetManager); + + foreach ($handlers as $ignorePluginHandler) { + if ($ignorePluginHandler->isInstalled()) { + $ignorePluginHandler->apply($assetHandles); + } + } + } + protected function extractHandles(AssetManager $assetManager): array + { + $assets = $assetManager->assets(); $assetHandles = [ Script::class => [], Style::class => [], @@ -30,11 +40,6 @@ public function execute(AssetManager $assetManager): void $assetHandles[$assetKey][] = $asset->handle(); } } - - foreach ($handlers as $ignorePluginHandler) { - if ($ignorePluginHandler->isInstalled()) { - $ignorePluginHandler->apply($assetHandles); - } - } + return $assetHandles; } } diff --git a/src/Caching/IgnoreSitegroundCache.php b/src/Caching/IgnoreSitegroundCache.php index 2b63029..3bf8d03 100644 --- a/src/Caching/IgnoreSitegroundCache.php +++ b/src/Caching/IgnoreSitegroundCache.php @@ -23,29 +23,35 @@ public function apply(array $handles): void /** * Ignore Javascript */ - add_filter('sgo_js_minify_exclude', static function (array $scripts) use ($handles) { + add_filter('sgo_js_minify_exclude', function (array $scripts) use ($handles) { assert(is_array($handles[Script::class])); - return array_merge($scripts, $handles[Script::class]); + return $this->applyExcludedHandles($scripts, $handles[Script::class]); }); add_filter( 'sgo_javascript_combine_exclude', - static function (array $scripts) use ($handles) { + function (array $scripts) use ($handles) { assert(is_array($handles[Script::class])); - return array_merge($scripts, $handles[Script::class]); + return $this->applyExcludedHandles($scripts, $handles[Script::class]); } ); /** * Ignore Styles */ - add_filter('sgo_css_minify_exclude', static function (array $styles) use ($handles) { + add_filter('sgo_css_minify_exclude', function (array $styles) use ($handles) { assert(is_array($handles[Style::class])); - return array_merge($styles, $handles[Style::class]); + return $this->applyExcludedHandles($styles, $handles[Style::class]); }); - add_filter('sgo_css_combine_exclude', static function (array $styles) use ($handles) { + add_filter('sgo_css_combine_exclude', function (array $styles) use ($handles) { assert(is_array($handles[Style::class])); - return array_merge($styles, $handles[Style::class]); + return $this->applyExcludedHandles($styles, $handles[Style::class]); }); } + + protected function applyExcludedHandles(array $excluded, array $toExclude): array + { + + return array_merge($excluded, $toExclude); + } } diff --git a/src/Caching/IgnoreW3TotalCache.php b/src/Caching/IgnoreW3TotalCache.php index 186f2a8..fdc505b 100644 --- a/src/Caching/IgnoreW3TotalCache.php +++ b/src/Caching/IgnoreW3TotalCache.php @@ -24,25 +24,28 @@ public function apply(array $handles): void /** * Ignore Javascript */ - add_filter('w3tc_minify_js_do_tag_minification', static function (bool $doMinification, string $scriptTag) use ($handles) { - foreach ($handles[Script::class] as $handle) { - if (strpos($scriptTag, (string)$handle) !== false) { - return false; - } - } - return $doMinification; + add_filter('w3tc_minify_js_do_tag_minification', function (bool $doMinification, string $scriptTag) use ($handles) { + assert(is_array($handles[Script::class])); + return $this->determineMinification($doMinification, $scriptTag, $handles[Script::class]); }, 10, 2); /** * Ignore Styles */ - add_filter('w3tc_minify_css_do_tag_minification', static function (bool $doMinification, string $scriptTag) use ($handles) { - foreach ($handles[Style::class] as $handle) { - if (strpos($scriptTag, (string)$handle) !== false) { - return false; - } - } - return $doMinification; + add_filter('w3tc_minify_css_do_tag_minification', function (bool $doMinification, string $scriptTag) use ($handles) { + assert(is_array($handles[Style::class])); + return $this->determineMinification($doMinification, $scriptTag, $handles[Style::class]); }, 10, 2); } + + protected function determineMinification(bool $doMinification, string $scriptTag, array $handles): bool + { + + foreach ($handles as $handle) { + if (strpos($scriptTag, (string)$handle) !== false) { + return false; + } + } + return $doMinification; + } } diff --git a/tests/phpunit/Unit/Caching/IgnoreCacheHandlerTest.php b/tests/phpunit/Unit/Caching/IgnoreCacheHandlerTest.php new file mode 100644 index 0000000..c5dd851 --- /dev/null +++ b/tests/phpunit/Unit/Caching/IgnoreCacheHandlerTest.php @@ -0,0 +1,72 @@ +justReturn(\Mockery::mock('WP_Scripts')); + Functions\when('wp_styles')->justReturn(\Mockery::mock('WP_Styles')); + } + + public function testExtractHandles(): void + { + $assetsManager = $this->factoryAssetManager(); + $ignoreCacheHandler = new IgnoreCacheHandler(); + $assetsManager->register( + new Script('example-1', 'script1.js'), + new Script('example-2', 'script2.js'), + new Style('example-3', 'style1.css'), + new Style('example-4', 'style2.css') + ); + + $ignoreCacheHandlerReflection = new \ReflectionClass(IgnoreCacheHandler::class); + $extractHandlesMethod = $ignoreCacheHandlerReflection->getMethod('extractHandles'); + $extractHandlesMethod->setAccessible(true); + + assertSame( + [ + Script::class => [ + 'example-1', 'example-2', + ], + Style::class => [ + 'example-3', 'example-4', + ], + ], + $extractHandlesMethod->invoke($ignoreCacheHandler, $assetsManager) + ); + } + + private function factoryAssetManager(?string $context = null): AssetManager + { + $wpContext = WpContext::new()->force($context ?? WpContext::FRONTOFFICE); + $resolver = new AssetHookResolver($wpContext); + return new AssetManager($resolver); + } +} diff --git a/tests/phpunit/Unit/Caching/IgnoreSitegroundCacheTest.php b/tests/phpunit/Unit/Caching/IgnoreSitegroundCacheTest.php new file mode 100644 index 0000000..93158cf --- /dev/null +++ b/tests/phpunit/Unit/Caching/IgnoreSitegroundCacheTest.php @@ -0,0 +1,36 @@ +getMethod('applyExcludedHandles'); + $applyExcludedHandlesMethod->setAccessible(true); + + assertSame( + ['excluded-1', 'excluded-2', 'to-exclude-1', 'to-exclude-2'], + $applyExcludedHandlesMethod->invoke(new IgnoreSitegroundCache(), $excluded, $toExclude) + ); + } +} diff --git a/tests/phpunit/Unit/Caching/IgnoreW3TotalCacheTest.php b/tests/phpunit/Unit/Caching/IgnoreW3TotalCacheTest.php new file mode 100644 index 0000000..ee3ce7d --- /dev/null +++ b/tests/phpunit/Unit/Caching/IgnoreW3TotalCacheTest.php @@ -0,0 +1,47 @@ +'; + + $ignoreW3TotalCacheReflection = new \ReflectionClass(IgnoreW3TotalCache::class); + $determineMinificationMethod = $ignoreW3TotalCacheReflection->getMethod('determineMinification'); + $determineMinificationMethod->setAccessible(true); + + assertSame( + false, + $determineMinificationMethod->invoke(new IgnoreW3TotalCache(), true, $scriptTag, ['assets-plugin-script']) + ); + assertSame( + true, + $determineMinificationMethod->invoke(new IgnoreW3TotalCache(), true, $scriptTag, ['assets-plugin-script-example']) + ); + assertSame( + true, + $determineMinificationMethod->invoke(new IgnoreW3TotalCache(), true, $scriptTag, []) + ); + assertSame( + false, + $determineMinificationMethod->invoke(new IgnoreW3TotalCache(), false, $scriptTag, []) + ); + } +}