Skip to content

Commit

Permalink
Refactor and unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
inpsyde-maticluznar committed Jul 26, 2024
1 parent 2a488cf commit c928908
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 29 deletions.
19 changes: 12 additions & 7 deletions src/Caching/IgnoreCacheHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,18 @@ public function execute(AssetManager $assetManager): void
new IgnoreSitegroundCache(),
];

Check warning on line 19 in src/Caching/IgnoreCacheHandler.php

View check run for this annotation

Codecov / codecov/patch

src/Caching/IgnoreCacheHandler.php#L16-L19

Added lines #L16 - L19 were not covered by tests

$assets = $assetManager->assets();
$assetHandles = $this->extractHandles($assetManager);

Check warning on line 21 in src/Caching/IgnoreCacheHandler.php

View check run for this annotation

Codecov / codecov/patch

src/Caching/IgnoreCacheHandler.php#L21

Added line #L21 was not covered by tests

foreach ($handlers as $ignorePluginHandler) {
if ($ignorePluginHandler->isInstalled()) {
$ignorePluginHandler->apply($assetHandles);

Check warning on line 25 in src/Caching/IgnoreCacheHandler.php

View check run for this annotation

Codecov / codecov/patch

src/Caching/IgnoreCacheHandler.php#L23-L25

Added lines #L23 - L25 were not covered by tests
}
}
}

protected function extractHandles(AssetManager $assetManager): array
{
$assets = $assetManager->assets();
$assetHandles = [
Script::class => [],
Style::class => [],
Expand All @@ -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;
}
}
22 changes: 14 additions & 8 deletions src/Caching/IgnoreSitegroundCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Check warning on line 26 in src/Caching/IgnoreSitegroundCache.php

View check run for this annotation

Codecov / codecov/patch

src/Caching/IgnoreSitegroundCache.php#L26

Added line #L26 was not covered by tests
assert(is_array($handles[Script::class]));
return array_merge($scripts, $handles[Script::class]);
return $this->applyExcludedHandles($scripts, $handles[Script::class]);
});

Check warning on line 29 in src/Caching/IgnoreSitegroundCache.php

View check run for this annotation

Codecov / codecov/patch

src/Caching/IgnoreSitegroundCache.php#L28-L29

Added lines #L28 - L29 were not covered by tests

add_filter(
'sgo_javascript_combine_exclude',
static function (array $scripts) use ($handles) {
function (array $scripts) use ($handles) {

Check warning on line 33 in src/Caching/IgnoreSitegroundCache.php

View check run for this annotation

Codecov / codecov/patch

src/Caching/IgnoreSitegroundCache.php#L31-L33

Added lines #L31 - L33 were not covered by tests
assert(is_array($handles[Script::class]));
return array_merge($scripts, $handles[Script::class]);
return $this->applyExcludedHandles($scripts, $handles[Script::class]);
}
);

Check warning on line 37 in src/Caching/IgnoreSitegroundCache.php

View check run for this annotation

Codecov / codecov/patch

src/Caching/IgnoreSitegroundCache.php#L35-L37

Added lines #L35 - L37 were not covered by tests

/**
* 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) {

Check warning on line 42 in src/Caching/IgnoreSitegroundCache.php

View check run for this annotation

Codecov / codecov/patch

src/Caching/IgnoreSitegroundCache.php#L42

Added line #L42 was not covered by tests
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) {

Check warning on line 46 in src/Caching/IgnoreSitegroundCache.php

View check run for this annotation

Codecov / codecov/patch

src/Caching/IgnoreSitegroundCache.php#L44-L46

Added lines #L44 - L46 were not covered by tests
assert(is_array($handles[Style::class]));
return array_merge($styles, $handles[Style::class]);
return $this->applyExcludedHandles($styles, $handles[Style::class]);
});

Check warning on line 49 in src/Caching/IgnoreSitegroundCache.php

View check run for this annotation

Codecov / codecov/patch

src/Caching/IgnoreSitegroundCache.php#L48-L49

Added lines #L48 - L49 were not covered by tests
}

protected function applyExcludedHandles(array $excluded, array $toExclude): array
{

return array_merge($excluded, $toExclude);
}
}
31 changes: 17 additions & 14 deletions src/Caching/IgnoreW3TotalCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Check warning on line 27 in src/Caching/IgnoreW3TotalCache.php

View check run for this annotation

Codecov / codecov/patch

src/Caching/IgnoreW3TotalCache.php#L27

Added line #L27 was not covered by tests
assert(is_array($handles[Script::class]));
return $this->determineMinification($doMinification, $scriptTag, $handles[Script::class]);
}, 10, 2);

Check warning on line 30 in src/Caching/IgnoreW3TotalCache.php

View check run for this annotation

Codecov / codecov/patch

src/Caching/IgnoreW3TotalCache.php#L29-L30

Added lines #L29 - L30 were not covered by tests

/**
* 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) {

Check warning on line 35 in src/Caching/IgnoreW3TotalCache.php

View check run for this annotation

Codecov / codecov/patch

src/Caching/IgnoreW3TotalCache.php#L35

Added line #L35 was not covered by tests
assert(is_array($handles[Style::class]));
return $this->determineMinification($doMinification, $scriptTag, $handles[Style::class]);
}, 10, 2);

Check warning on line 38 in src/Caching/IgnoreW3TotalCache.php

View check run for this annotation

Codecov / codecov/patch

src/Caching/IgnoreW3TotalCache.php#L37-L38

Added lines #L37 - L38 were not covered by tests
}

protected function determineMinification(bool $doMinification, string $scriptTag, array $handles): bool
{

foreach ($handles as $handle) {
if (strpos($scriptTag, (string)$handle) !== false) {
return false;
}
}
return $doMinification;
}
}
72 changes: 72 additions & 0 deletions tests/phpunit/Unit/Caching/IgnoreCacheHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Assets package.
*
* (c) Inpsyde GmbH
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Inpsyde\Assets\Tests\Unit\Caching;

use Brain\Monkey\Functions;
use Inpsyde\Assets\AssetManager;
use Inpsyde\Assets\Caching\IgnoreCacheHandler;
use Inpsyde\Assets\Script;
use Inpsyde\Assets\Style;
use Inpsyde\Assets\Tests\Unit\AbstractTestCase;
use Inpsyde\Assets\Util\AssetHookResolver;
use Inpsyde\WpContext;
use function PHPUnit\Framework\assertSame;

class IgnoreCacheHandlerTest extends AbstractTestCase
{
/**
* @return void
*/
protected function setUp(): void
{
parent::setUp();
Functions\when('wp_scripts')->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);
}
}
36 changes: 36 additions & 0 deletions tests/phpunit/Unit/Caching/IgnoreSitegroundCacheTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Assets package.
*
* (c) Inpsyde GmbH
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Inpsyde\Assets\Tests\Unit\Caching;

use Inpsyde\Assets\Caching\IgnoreSitegroundCache;
use Inpsyde\Assets\Tests\Unit\AbstractTestCase;
use function PHPUnit\Framework\assertSame;

class IgnoreSitegroundCacheTest extends AbstractTestCase
{
public function testApplyExcludedHandlers(): void
{
$excluded = ['excluded-1', 'excluded-2'];
$toExclude = ['to-exclude-1', 'to-exclude-2'];

$ignoreSitegroundCacheReflection = new \ReflectionClass(IgnoreSitegroundCache::class);
$applyExcludedHandlesMethod = $ignoreSitegroundCacheReflection->getMethod('applyExcludedHandles');
$applyExcludedHandlesMethod->setAccessible(true);

assertSame(
['excluded-1', 'excluded-2', 'to-exclude-1', 'to-exclude-2'],
$applyExcludedHandlesMethod->invoke(new IgnoreSitegroundCache(), $excluded, $toExclude)
);
}
}
47 changes: 47 additions & 0 deletions tests/phpunit/Unit/Caching/IgnoreW3TotalCacheTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Assets package.
*
* (c) Inpsyde GmbH
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Inpsyde\Assets\Tests\Unit\Caching;

use Inpsyde\Assets\Caching\IgnoreW3TotalCache;
use Inpsyde\Assets\Tests\Unit\AbstractTestCase;
use function PHPUnit\Framework\assertSame;

class IgnoreW3TotalCacheTest extends AbstractTestCase
{
public function testDetermineMinification(): void
{
$scriptTag = '<script src="example.js" id="assets-plugin-script-js"></script>';

$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, [])
);
}
}

0 comments on commit c928908

Please sign in to comment.