Skip to content

Commit

Permalink
Add handler tests
Browse files Browse the repository at this point in the history
  • Loading branch information
inpsyde-maticluznar committed Jul 29, 2024
1 parent 985ac96 commit ad7026e
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/AssetManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,6 @@ private function ensureSetup(): void

public function ignoreCache(): void

Check warning on line 329 in src/AssetManager.php

View check run for this annotation

Codecov / codecov/patch

src/AssetManager.php#L329

Added line #L329 was not covered by tests
{
$this->ignoreCacheHandler->execute($this);
$this->ignoreCacheHandler->run($this);

Check warning on line 331 in src/AssetManager.php

View check run for this annotation

Codecov / codecov/patch

src/AssetManager.php#L331

Added line #L331 was not covered by tests
}
}
5 changes: 3 additions & 2 deletions src/Caching/IgnoreCacheHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class IgnoreCacheHandler
{
public function execute(AssetManager $assetManager): void
public function run(AssetManager $assetManager): bool
{
/** @var IgnorePluginCacheInterface[] $handlers */
$handlers = [
Expand All @@ -24,14 +24,15 @@ public function execute(AssetManager $assetManager): void
count($assetHandles[Script::class]) === 0 &&
count($assetHandles[Style::class]) === 0
) {
return;
return false;
}

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#L32

Added line #L32 was not covered by tests
}
}
return true;
}

protected function extractHandles(AssetManager $assetManager): array
Expand Down
15 changes: 13 additions & 2 deletions tests/phpunit/Unit/Caching/IgnoreCacheHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
use Inpsyde\Assets\Util\AssetHookResolver;
use Inpsyde\WpContext;

use function PHPUnit\Framework\assertSame;

class IgnoreCacheHandlerTest extends AbstractTestCase
{
/**
Expand All @@ -36,6 +34,19 @@ protected function setUp(): void
Functions\when('wp_styles')->justReturn(\Mockery::mock('WP_Styles'));
}

public function testRun(): void
{
$ignoreCacheHandler = new IgnoreCacheHandler();
$assetsManagerNoScripts = $this->factoryAssetManager();
$assetsManagerWithScripts = $this->factoryAssetManager();
$assetsManagerWithScripts->register(
new Script('example-1', 'script1.js')
);

static::assertFalse($ignoreCacheHandler->run($assetsManagerNoScripts));
static::assertTrue($ignoreCacheHandler->run($assetsManagerWithScripts));
}

public function testExtractHandles(): void
{
$assetsManager = $this->factoryAssetManager();
Expand Down
6 changes: 3 additions & 3 deletions tests/phpunit/Unit/Caching/IgnoreSitegroundCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
use Inpsyde\Assets\Caching\IgnoreSitegroundCache;
use Inpsyde\Assets\Tests\Unit\AbstractTestCase;

use function PHPUnit\Framework\assertSame;

class IgnoreSitegroundCacheTest extends AbstractTestCase
{
public function testIsInstalled()
// phpcs:disable Squiz.PHP.Eval.Discouraged
// phpcs:disable Inpsyde.CodeQuality.VariablesName.SnakeCaseVar
public function testIsInstalled(): void
{
if (!class_exists('SiteGround_Optimizer\Loader\Loader')) {
eval('namespace SiteGround_Optimizer\Loader { class Loader {} }');
Expand Down
6 changes: 3 additions & 3 deletions tests/phpunit/Unit/Caching/IgnoreW3TotalCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@

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
{
// phpcs:disable Squiz.PHP.Eval.Discouraged
// phpcs:disable Inpsyde.CodeQuality.VariablesName.SnakeCaseVar
public function testIsInstalled(): void
{
if (!class_exists('W3TC\Root_Loader')) {
Expand All @@ -30,6 +29,7 @@ public function testIsInstalled(): void
$IgnoreW3TotalCache = new IgnoreW3TotalCache();
$this->assertTrue($IgnoreW3TotalCache->isInstalled());
}

public function testApply(): void
{

Expand Down

0 comments on commit ad7026e

Please sign in to comment.