generated from vormkracht10/laravel-package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
04df89b
commit 8907b1f
Showing
7 changed files
with
108 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
tests/Unit/CachedComponent/ScheduledAndQueuedCachedComponent.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Vormkracht10\PermanentCache\CachedComponent; | ||
use Vormkracht10\PermanentCache\Scheduled; | ||
|
||
class ScheduledAndQueuedCachedComponent extends CachedComponent implements Scheduled, ShouldQueue | ||
{ | ||
protected $store = 'file:unique-cache-key'; | ||
|
||
public function __construct(public string $value = '') | ||
{ | ||
} | ||
|
||
public function render(): string | ||
{ | ||
$value = $this->value ?? str_random(); | ||
|
||
return <<<'HTML' | ||
<div>This is a {{ $value ?? 'cached' }} component!</div> | ||
HTML; | ||
} | ||
|
||
public static function schedule($callback) | ||
{ | ||
return $callback->everyMinute(); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
tests/Unit/CachedComponent/ScheduledAndQueuedCachedComponentTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
use Illuminate\Console\Scheduling\Schedule; | ||
use Illuminate\Support\Facades\Queue; | ||
use Vormkracht10\PermanentCache\Facades\PermanentCache; | ||
|
||
require_once 'tests/Unit/CachedComponent/ScheduledAndQueuedCachedComponent.php'; | ||
|
||
beforeEach(function () { | ||
Cache::driver('file')->clear(); | ||
Queue::fake(); | ||
|
||
(fn () => $this->caches = new \SplObjectStorage)->call(app(\Vormkracht10\PermanentCache\PermanentCache::class)); | ||
}); | ||
|
||
test('test scheduled queued cached component gets scheduled', function () { | ||
PermanentCache::caches([ | ||
ScheduledAndQueuedCachedComponent::class, | ||
]); | ||
|
||
$events = collect(app(Schedule::class)->events()) | ||
->filter(fn ($schedule) => $schedule->description === 'ScheduledAndQueuedCachedComponent'); | ||
|
||
expect($events)->toHaveCount(1); | ||
expect($events->first()->expression)->toBe('* * * * *'); | ||
}); | ||
|
||
test('test scheduled queued cached component with parameters gets scheduled', function () { | ||
PermanentCache::caches([ | ||
ScheduledAndQueuedCachedComponent::class => ['parameter' => 'test cached'], | ||
]); | ||
|
||
$events = collect(app(Schedule::class)->events()) | ||
->filter(fn ($schedule) => $schedule->description === 'ScheduledAndQueuedCachedComponent'); | ||
|
||
expect($events)->toHaveCount(1); | ||
expect($events->first()->expression)->toBe('* * * * *'); | ||
}); | ||
|
||
test('test scheduled queued cached component gets executed', function () { | ||
PermanentCache::caches([ | ||
ScheduledAndQueuedCachedComponent::class => ['parameter' => 'test cached'], | ||
]); | ||
|
||
$events = collect(app(Schedule::class)->events()) | ||
->filter(fn ($schedule) => $schedule->description === 'ScheduledAndQueuedCachedComponent'); | ||
|
||
expect($events)->toHaveCount(1); | ||
expect($events->first()->expression)->toBe('* * * * *'); | ||
|
||
PermanentCache::update(); | ||
|
||
Queue::assertPushed(ScheduledAndQueuedCachedComponent::class); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters