Skip to content

Commit

Permalink
Formatting and readonly properties
Browse files Browse the repository at this point in the history
  • Loading branch information
coreyworrell committed Dec 13, 2023
1 parent 6748561 commit 147f33e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/Admin/RemoveDashboardWidgets.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

namespace Fire\Admin;

class RemoveDashboardWidgets
final class RemoveDashboardWidgets
{
protected array $ids = [];
/** @var string[] */
protected readonly array $ids;

public function __construct(string ...$ids)
{
Expand All @@ -16,6 +17,7 @@ public function __construct(string ...$ids)
public function register(): self
{
add_action('wp_dashboard_setup', [$this, 'remove']);

return $this;
}

Expand Down
7 changes: 5 additions & 2 deletions src/Core/CacheBustScripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

use InvalidArgumentException;

class CacheBustScripts
final class CacheBustScripts
{
protected int $hashLength = 10;

/** @var string[] */
protected array $validHosts;
protected readonly array $validHosts;

public function __construct(string ...$validHosts)
{
Expand All @@ -30,6 +30,7 @@ public function register(): self

add_filter('script_loader_src', [$this, 'src']);
add_filter('style_loader_src', [$this, 'src']);

return $this;
}

Expand Down Expand Up @@ -57,12 +58,14 @@ public function src(string $src): string
// Remove version and append to filename
$src = remove_query_arg('ver', $src);
$ver = substr(sha1($ver), 0, $this->hashLength);

return preg_replace('/\.(js|css)(\?.*)?$/', ".$ver.\$1\$2", $src);
}

public function setHashLength(int $length): self
{
$this->hashLength = $length;

return $this;
}

Expand Down
1 change: 1 addition & 0 deletions tests/Admin/RemoveDashboardWidgetsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ final class RemoveDashboardWidgetsTest extends TestCase
public function testActionsAdded(): void
{
$instance = (new RemoveDashboardWidgets())->register();

$this->assertIsInt(has_action('wp_dashboard_setup', [$instance, 'remove']));
}

Expand Down
14 changes: 14 additions & 0 deletions tests/Core/CacheBustScriptsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,41 @@ final class CacheBustScriptsTest extends TestCase
public function testFiltersAdded(): void
{
when('is_admin')->justReturn(false);

$instance = (new CacheBustScripts($this->current))->register();

$this->assertIsInt(has_filter('script_loader_src', [$instance, 'src']));
$this->assertIsInt(has_filter('style_loader_src', [$instance, 'src']));
}

public function testFiltersNotAddedForAdmin(): void
{
when('is_admin')->justReturn(true);

$instance = (new CacheBustScripts($this->current))->register();

$this->assertFalse(has_filter('script_loader_src', [$instance, 'src']));
$this->assertFalse(has_filter('style_loader_src', [$instance, 'src']));
}

public function testNoValidHosts(): void
{
$this->expectException(InvalidArgumentException::class);

new CacheBustScripts('//');
}

public function testFallbackHosts(): void
{
when('home_url')->justReturn($this->current);

$this->assertSame(parse_hosts($this->current), (new CacheBustScripts())->validHosts());
}

public function testLocal(): void
{
$this->functions();

$bust = new CacheBustScripts($this->current);

$this->assertSame(
Expand Down Expand Up @@ -75,20 +82,25 @@ public function testLocal(): void
public function testLocalWithoutVersion(): void
{
$this->functions();

$bust = new CacheBustScripts($this->current);

$this->assertSame($src = 'https://domain.com/file.js?a=b&c=d', $bust->src($src));
}

public function testExternal(): void
{
$this->functions();

$bust = new CacheBustScripts($this->current);

$this->assertSame($src = 'https://google.com/fonts.css', $bust->src($src));
}

public function testLength(): void
{
$this->functions();

$bust = (new CacheBustScripts($this->current))->setHashLength(5);

$this->assertSame(
Expand All @@ -100,7 +112,9 @@ public function testLength(): void
public function testBadSrc(): void
{
$this->functions();

$bust = new CacheBustScripts($this->current);

$this->assertSame($src = '//', $bust->src($src));
}

Expand Down

0 comments on commit 147f33e

Please sign in to comment.