Skip to content

Commit

Permalink
Add hook tests
Browse files Browse the repository at this point in the history
  • Loading branch information
inpsyde-maticluznar committed Jul 29, 2024
1 parent bdc0cb7 commit 985ac96
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Caching/IgnoreCacheHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ public function execute(AssetManager $assetManager): void

$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

if (
count($assetHandles[Script::class]) === 0 &&
count($assetHandles[Style::class]) === 0

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

View check run for this annotation

Codecov / codecov/patch

src/Caching/IgnoreCacheHandler.php#L24-L25

Added lines #L24 - L25 were not covered by tests
) {
return;

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

View check run for this annotation

Codecov / codecov/patch

src/Caching/IgnoreCacheHandler.php#L27

Added line #L27 was not covered by tests
}

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

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

View check run for this annotation

Codecov / codecov/patch

src/Caching/IgnoreCacheHandler.php#L30-L32

Added lines #L30 - L32 were not covered by tests
Expand Down
27 changes: 27 additions & 0 deletions tests/phpunit/Unit/Caching/IgnoreSitegroundCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,33 @@

class IgnoreSitegroundCacheTest extends AbstractTestCase
{
public function testIsInstalled()
{
if (!class_exists('SiteGround_Optimizer\Loader\Loader')) {
eval('namespace SiteGround_Optimizer\Loader { class Loader {} }');
}

$ignoreSitegroundCache = new IgnoreSitegroundCache();
$this->assertTrue($ignoreSitegroundCache->isInstalled());
}

public function testApply(): void
{

$ignoreSitegroundCache = new IgnoreSitegroundCache();
$ignoreSitegroundCache->apply([]);

self::assertNotFalse(has_filter('sgo_js_minify_exclude', 'function (array $scripts)'));
self::assertNotFalse(
has_filter(
'sgo_javascript_combine_exclude',
'function (array $scripts)'
)
);
self::assertNotFalse(has_filter('sgo_css_minify_exclude', 'function (array $styles)'));
self::assertNotFalse(has_filter('sgo_css_combine_exclude', 'function (array $styles)'));
}

public function testApplyExcludedHandlers(): void
{
$excluded = ['excluded-1', 'excluded-2'];
Expand Down
30 changes: 30 additions & 0 deletions tests/phpunit/Unit/Caching/IgnoreW3TotalCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,43 @@

namespace Inpsyde\Assets\Tests\Unit\Caching;

use Inpsyde\Assets\Caching\IgnoreSitegroundCache;
use Inpsyde\Assets\Caching\IgnoreW3TotalCache;
use Inpsyde\Assets\Tests\Unit\AbstractTestCase;

use function PHPUnit\Framework\assertSame;

class IgnoreW3TotalCacheTest extends AbstractTestCase
{
public function testIsInstalled(): void
{
if (!class_exists('W3TC\Root_Loader')) {
eval('namespace W3TC { class Root_Loader {} }');
}

$IgnoreW3TotalCache = new IgnoreW3TotalCache();
$this->assertTrue($IgnoreW3TotalCache->isInstalled());
}
public function testApply(): void
{

$ignoreSitegroundCache = new IgnoreW3TotalCache();
$ignoreSitegroundCache->apply([]);

self::assertNotFalse(
has_filter(
'w3tc_minify_js_do_tag_minification',
'function (bool $doMinification, string $scriptTag)'
)
);
self::assertNotFalse(
has_filter(
'w3tc_minify_css_do_tag_minification',
'function (bool $doMinification, string $scriptTag)'
)
);
}

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

0 comments on commit 985ac96

Please sign in to comment.