Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/Tags/CacheTracker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Thoughtco\StatamicCacheTracker\Tags;

use Statamic\Tags\Tags;
use Thoughtco\StatamicCacheTracker\Events\TrackContentTags;

class CacheTracker extends Tags
{
/**
* {{ cache_tracker tag="hero" }}
*/
public function index(): void
{
$this->track($this->params->get('tag'));
}

/**
* {{ cache_tracker:hero }}
*/
public function wildcard(string $method): void
{
$this->track($method);
}

private function track(?string $tag): void
{
if (! is_null($tag)) {
TrackContentTags::dispatch([$tag]);
}
}
}
47 changes: 47 additions & 0 deletions tests/Unit/CacheTrackerTagTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Thoughtco\StatamicCacheTracker\Tests\Unit;

use Illuminate\Support\Facades\Event;
use PHPUnit\Framework\Attributes\Test;
use Statamic\Facades\Antlers;
use Thoughtco\StatamicCacheTracker\Events\TrackContentTags;
use Thoughtco\StatamicCacheTracker\Tests\TestCase;

class CacheTrackerTagTest extends TestCase
{
#[Test]
public function it_tracks_a_tag_via_the_tag_parameter()
{
Event::fake([TrackContentTags::class]);

Antlers::parse('{{ cache_tracker tag="custom:dependency" }}', [], true);

$this->assertTracked(['custom:dependency']);
}

#[Test]
public function it_tracks_a_tag_via_the_wildcard_method()
{
Event::fake([TrackContentTags::class]);

Antlers::parse('{{ cache_tracker:custom:dependency }}', [], true);

$this->assertTracked(['custom:dependency']);
}

#[Test]
public function it_does_not_track_when_no_tag_is_given()
{
Event::fake([TrackContentTags::class]);

Antlers::parse('{{ cache_tracker }}', [], true);

Event::assertNotDispatched(TrackContentTags::class);
}

private function assertTracked(array $tags): void
{
Event::assertDispatched(TrackContentTags::class, fn (TrackContentTags $event) => $event->tags === $tags);
}
}