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
434cb09
commit c4c891d
Showing
7 changed files
with
117 additions
and
43 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 was deleted.
Oops, something went wrong.
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,19 @@ | ||
<?php | ||
|
||
use Vormkracht10\PermanentCache\Scheduled; | ||
|
||
class CachedComponent extends \Vormkracht10\PermanentCache\CachedComponent | ||
{ | ||
protected $store = 'file:unique-cache-key'; | ||
|
||
public function render(): string | ||
{ | ||
sleep(3); | ||
|
||
return <<<HTML | ||
<div class="alert alert-danger"> | ||
This is a cached component! | ||
</div> | ||
HTML; | ||
} | ||
} |
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,25 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Facades\Blade; | ||
|
||
require_once 'tests/Unit/CachedComponent/CachedComponent.php'; | ||
require_once 'tests/Unit/CachedComponent/ScheduledCachedComponent.php'; | ||
|
||
beforeEach(function () { | ||
Cache::driver('file')->clear(); | ||
(fn () => $this->cachers = new \SplObjectStorage)->call(app(\Vormkracht10\PermanentCache\PermanentCache::class)); | ||
}); | ||
|
||
test('test cached component is cached second time', function () { | ||
$time = microtime(true); | ||
|
||
Blade::renderComponent(new CachedComponent); | ||
|
||
$this->assertGreaterThanOrEqual(3, microtime(true) - $time); | ||
|
||
$time = microtime(true); | ||
|
||
Blade::renderComponent(new CachedComponent); | ||
|
||
$this->assertLessThan(3, microtime(true) - $time); | ||
}); |
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,24 @@ | ||
<?php | ||
|
||
use Vormkracht10\PermanentCache\Scheduled; | ||
|
||
class ScheduledCachedComponent extends \Vormkracht10\PermanentCache\CachedComponent implements Scheduled | ||
{ | ||
protected $store = 'file:unique-cache-key'; | ||
|
||
public function render(): string | ||
{ | ||
sleep(10); | ||
|
||
return <<<HTML | ||
<div class="alert alert-danger"> | ||
This is a cached component! | ||
</div> | ||
HTML; | ||
} | ||
|
||
public static function schedule($callback) | ||
{ | ||
return $callback->everyMinute(); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
tests/Unit/CachedComponent/ScheduledCachedComponentTest.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,24 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Facades\Blade; | ||
use Vormkracht10\PermanentCache\Facades\PermanentCache; | ||
|
||
require_once 'tests/Unit/CachedComponent/CachedComponent.php'; | ||
require_once 'tests/Unit/CachedComponent/ScheduledCachedComponent.php'; | ||
|
||
beforeEach(function () { | ||
Cache::driver('file')->clear(); | ||
(fn () => $this->cachers = new \SplObjectStorage)->call(app(\Vormkracht10\PermanentCache\PermanentCache::class)); | ||
}); | ||
|
||
test('test scheduled cached component gets scheduled', function () { | ||
PermanentCache::caches([ | ||
ScheduledCachedComponent::class | ||
]); | ||
|
||
$events = collect(app(\Illuminate\Console\Scheduling\Schedule::class)->events()) | ||
->filter(fn ($schedule) => $schedule->description === 'ScheduledCachedComponent'); | ||
|
||
expect($events)->toHaveCount(1); | ||
expect($events->first()->expression)->toBe('* * * * *'); | ||
}); |
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