Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalidate entries instead of a collection #1

Merged
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
13 changes: 6 additions & 7 deletions resources/lang/en/command.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

return [

'collections' => [
'count' => 'Scheduled Cache Invalidator found :count Collections to invalidate:',
'invalidated' => 'Static Cache invalidated for ":collection"',
'none' => 'There is nothing for the Scheduled Caching Invalidator to do.',
'single' => 'Scheduled Cache Invalidator found 1 Collection to invalidate:',
],

'completed' => 'Scheduled Cache Invalidator has finished.',

'disabled' => 'Static Caching is disabled: there is nothing for the Scheduled Caching Invalidator to do.',

'entries' => [
'count' => 'Scheduled Cache Invalidator found :count Entries to invalidate:',
'invalidated' => 'Static Cache invalidated for Entry ":id"',
'none' => 'There is nothing for the Scheduled Caching Invalidator to do.',
'single' => 'Scheduled Cache Invalidator found 1 Entry to invalidate:',
],
];
22 changes: 11 additions & 11 deletions src/Console/Commands/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Illuminate\Console\Command;
use MityDigital\StatamicScheduledCacheInvalidator\Support\ScheduledCacheInvalidator;
use Statamic\Console\RunsInPlease;
use Statamic\Entries\Collection;
use Statamic\Contracts\Entries\Entry;
use Statamic\StaticCaching\Invalidator;

class RunCommand extends Command
Expand All @@ -31,34 +31,34 @@ public function handle(): void
$support = app(ScheduledCacheInvalidator::class);

// get some entries
$collections = $support->getCollections();
$entries = $support->getEntries();

// do we have entries?
if ($collections->count()) {
if ($entries->count()) {
// let's make the invalidator
$invalidator = app(Invalidator::class);

// let's communicate - single or plural
$this->info(__($collections->count() === 1 ? 'statamic-scheduled-cache-invalidator::command.collections.single' : 'statamic-scheduled-cache-invalidator::command.collections.count',
$this->info(__($entries->count() === 1 ? 'statamic-scheduled-cache-invalidator::command.entries.single' : 'statamic-scheduled-cache-invalidator::command.entries.count',
[
'count' => $collections->count(),
'count' => $entries->count(),
]));

// let's clear the cache for each Collection's mount
$collections->each(function (Collection $collection, $index) use ($invalidator) {
// let's clear the cache for each Entry
$entries->each(function (Entry $entry, $index) use ($invalidator) {
// invalidate magic!
$invalidator->invalidate($collection);
$invalidator->invalidate($entry);

// progress output
$this->line(__('statamic-scheduled-cache-invalidator::command.collections.invalidated', [
'collection' => $collection->title(),
$this->line(__('statamic-scheduled-cache-invalidator::command.entries.invalidated', [
'id' => $entry->id(),
]));
});

$this->info(__('statamic-scheduled-cache-invalidator::command.completed'));
} else {
// nothing to do, let's finish this up
$this->info(__('statamic-scheduled-cache-invalidator::command.collections.none'));
$this->info(__('statamic-scheduled-cache-invalidator::command.entries.none'));
}
}
}
26 changes: 18 additions & 8 deletions src/Support/ScheduledCacheInvalidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,29 @@

class ScheduledCacheInvalidator
{
public function getCollections(): \Illuminate\Support\Collection
public function getEntries(): \Illuminate\Support\Collection
{
// what is "now"? how existential...
$now = Carbon::now()->format('Y-m-d H:i');

// get the collections that have a "date" (or whatever the collection is configured for) where an entry
// is due to be published this minute
// get the entries inside a dated collection
// that have a "date" (or whatever the collection is configured for)
// due to be published this minute
return CollectionFacade::all()
->filter(fn (Collection $collection) => $collection->dated() && Entry::query()
->where('collection', $collection->handle())
->where('published', true)
->whereTime($collection->sortField() ?? 'date', $now)
->count())
->filter(fn (Collection $collection) => $collection->dated())
->map(function (Collection $collection) use ($now) {
$entries = Entry::query()
->where('collection', $collection->handle())
->where('published', true)
->whereTime($collection->sortField() ?? 'date', $now)
->get();

// this triggers any publish status changes in the stache
$entries->each->saveQuietly();

return $entries;
})
->filter()
->flatten();
}
}
42 changes: 21 additions & 21 deletions tests/Commands/RunCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use MityDigital\StatamicScheduledCacheInvalidator\Console\Commands\RunCommand;
use MityDigital\StatamicScheduledCacheInvalidator\Support\ScheduledCacheInvalidator;
use Mockery\MockInterface;
use Statamic\Facades\Collection;
use Statamic\Facades\Entry;
use Statamic\StaticCaching\Invalidator;

beforeEach(function () {
Expand All @@ -28,7 +28,7 @@

// return nothing
$this->partialMock(ScheduledCacheInvalidator::class, function (MockInterface $mock) {
$mock->shouldReceive('getCollections')
$mock->shouldReceive('getEntries')
->andReturn(collect([]));
});

Expand All @@ -47,18 +47,18 @@
->assertExitCode(0);
});

it('invalidates the cache for a single collection', function () {
it('invalidates the cache for a single entry', function () {
// enable caching
config(['statamic.static_caching.strategy' => 'full']);

// get a single collection
$collection = Collection::findByHandle('dated');
// get a single entry
$entry = Entry::make()->id('test');

$this->partialMock(ScheduledCacheInvalidator::class, function (MockInterface $mock) use ($collection) {
$mock->shouldReceive('getCollections')
$this->partialMock(ScheduledCacheInvalidator::class, function (MockInterface $mock) use ($entry) {
$mock->shouldReceive('getEntries')
->once()
->andReturn(collect([
$collection,
$entry,
]));
});

Expand All @@ -67,27 +67,27 @@
});

$this->artisan('statamic:scheduled-cache-invalidator:run')
->expectsOutput('Scheduled Cache Invalidator found 1 Collection to invalidate:')
->expectsOutput('Static Cache invalidated for "'.$collection->title().'"')
->expectsOutput('Scheduled Cache Invalidator found 1 Entry to invalidate:')
->expectsOutput('Static Cache invalidated for Entry "'.$entry->id().'"')
->expectsOutput('Scheduled Cache Invalidator has finished.')
->assertExitCode(0);
});

it('invalidates the cache for multiple collection', function () {
it('invalidates the cache for multiple entries', function () {
// enable caching
config(['statamic.static_caching.strategy' => 'full']);

// get two collections
$collection = Collection::findByHandle('dated');
$collection2 = Collection::findByHandle('dated_and_timed');
// get two entries
$entry1 = Entry::make()->id('test1');
$entry2 = Entry::make()->id('test2');

$this->partialMock(ScheduledCacheInvalidator::class,
function (MockInterface $mock) use ($collection, $collection2) {
$mock->shouldReceive('getCollections')
function (MockInterface $mock) use ($entry1, $entry2) {
$mock->shouldReceive('getEntries')
->once()
->andReturn(collect([
$collection,
$collection2,
$entry1,
$entry2,
]));
});

Expand All @@ -96,9 +96,9 @@ function (MockInterface $mock) use ($collection, $collection2) {
});

$this->artisan('statamic:scheduled-cache-invalidator:run')
->expectsOutput('Scheduled Cache Invalidator found 2 Collections to invalidate:')
->expectsOutput('Static Cache invalidated for "'.$collection->title().'"')
->expectsOutput('Static Cache invalidated for "'.$collection2->title().'"')
->expectsOutput('Scheduled Cache Invalidator found 2 Entries to invalidate:')
->expectsOutput('Static Cache invalidated for Entry "'.$entry1->id().'"')
->expectsOutput('Static Cache invalidated for Entry "'.$entry2->id().'"')
->expectsOutput('Scheduled Cache Invalidator has finished.')
->assertExitCode(0);
});
28 changes: 14 additions & 14 deletions tests/Support/ScheduledCacheInvalidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,61 +4,61 @@

use function Spatie\PestPluginTestTime\testTime;

it('correctly gets a collection when time is disabled for the collection', function () {
it('correctly gets an entry when time is disabled for the collection', function () {
// get the support
$support = app(ScheduledCacheInvalidator::class);

// freeze time to be BEFORE publish
testTime()->freeze('2023-12-14 23:59:00');

expect($support->getCollections())->toHaveCount(0);
expect($support->getEntries())->toHaveCount(0);

// freeze time to be ON publish
testTime()->freeze('2023-12-15 00:00:00');

$collections = $support->getCollections();
$entries = $support->getEntries();

// should have 1, and the "dated_and_timed" collection
expect($collections)->toHaveCount(1)
->and($collections->first()->handle())->toBe('dated');
expect($entries)->toHaveCount(1)
->and($entries->first()->collection()->handle())->toBe('dated');

// freeze time to be AFTER publish
testTime()->freeze('2023-12-15 00:01:00');

expect($support->getCollections())->toHaveCount(0);
expect($support->getEntries())->toHaveCount(0);
});

it('correctly gets a collection when time is enabled for the collection', function () {
it('correctly gets an entry when time is enabled for the collection', function () {
// get the support
$support = app(ScheduledCacheInvalidator::class);

// freeze time to be BEFORE publish
testTime()->freeze('2023-12-15 11:55:00');

expect($support->getCollections())->toHaveCount(0);
expect($support->getEntries())->toHaveCount(0);

// freeze time to be ON publish
testTime()->freeze('2023-12-15 11:56:00');

$collections = $support->getCollections();
$entries = $support->getEntries();

// should have 1, and the "dated_and_timed" collection
expect($collections)->toHaveCount(1)
->and($collections->first()->handle())->toBe('dated_and_timed');
expect($entries)->toHaveCount(1)
->and($entries->first()->collection()->handle())->toBe('dated_and_timed');

// freeze time to be AFTER publish
testTime()->freeze('2023-12-15 11:57:00');

expect($support->getCollections())->toHaveCount(0);
expect($support->getEntries())->toHaveCount(0);
});

it('does not return an undated collection', function () {
it('does not return entried from an undated collection', function () {
// get the support
$support = app(ScheduledCacheInvalidator::class);

// freeze time to be ON publish - this is when the undated entry has a "date" param
testTime()->freeze('2023-12-07 00:00:00');

// should have nothing returned - it's not dated
expect($support->getCollections())->toHaveCount(0);
expect($support->getEntries())->toHaveCount(0);
});