Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
markvaneijk committed May 9, 2024
1 parent 04df89b commit 8907b1f
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 6 deletions.
8 changes: 7 additions & 1 deletion tests/Unit/CachedComponent/CachedComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@ class CachedComponent extends \Vormkracht10\PermanentCache\CachedComponent
{
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 cached component: {{ str_random() }}</div>
<div>This is a {{ $value ?? 'cached' }} component!</div>
HTML;
}
}
14 changes: 13 additions & 1 deletion tests/Unit/CachedComponent/CachedComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

beforeEach(function () {
Cache::driver('file')->clear();
(fn () => $this->caches = new \SplObjectStorage)->call(app(\Vormkracht10\PermanentCache\PermanentCache::class));
(fn () => $this->cachers = new \SplObjectStorage)->call(app(\Vormkracht10\PermanentCache\PermanentCache::class));
});

test('test cached component is cached second time', function () {
Expand All @@ -15,3 +15,15 @@

$this->assertEquals($firstRunOutput, $secondRunOutput);
});

test('test cached component with parameters is cached correctly', function () {
$randomString = str_random();

$cachedComponentWithParameters = new CachedComponent(value: $randomString);

$firstRunOutput = Blade::renderComponent($cachedComponentWithParameters);
$secondRunOutput = Blade::renderComponent($cachedComponentWithParameters);

$this->assertEquals($firstRunOutput, '<div>This is a '.$randomString.' component!</div>');
$this->assertEquals($secondRunOutput, '<div>This is a '.$randomString.' component!</div>');
});
28 changes: 28 additions & 0 deletions tests/Unit/CachedComponent/ScheduledAndQueuedCachedComponent.php
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();
}
}
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);
});
4 changes: 2 additions & 2 deletions tests/Unit/CachedComponent/ScheduledCachedComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ class ScheduledCachedComponent extends \Vormkracht10\PermanentCache\CachedCompon
{
protected $store = 'file:unique-cache-key';

public function __construct(public string $parameter = '')
public function __construct(public string $value = '')
{
}

public function render(): string
{
$value = $this->parameter;
$value = $this->value;

return <<<'HTML'
<div>This is a {{ $value ?? 'cached' }} component!</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
beforeEach(function () {
Cache::driver('file')->clear();

(fn () => $this->caches = new \SplObjectStorage)->call(app(\Vormkracht10\PermanentCache\PermanentCache::class));
(fn () => $this->cachers = new \SplObjectStorage)->call(app(\Vormkracht10\PermanentCache\PermanentCache::class));
});

test('test scheduled cached component gets scheduled', function () {
Expand Down
4 changes: 3 additions & 1 deletion tests/Unit/Events/PermanentCacheEventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

beforeEach(function () {
Cache::driver('file')->clear();
(fn () => $this->caches = new \SplObjectStorage)->call(app(\Vormkracht10\PermanentCache\PermanentCache::class));
(fn () => $this->cachers = new \SplObjectStorage)->call(app(\Vormkracht10\PermanentCache\PermanentCache::class));
});

test('caches listeners registers when using the PermanentCache facade', function () {
Expand Down Expand Up @@ -52,7 +52,9 @@ public function run(TestEvent $_)
test('cache will dispatch the updating and updated events when it gets invoked', function () {
Event::fakeExcept(TestEvent::class);
Permanentcache::caches(TestPermanentCache::class);

event(new TestEvent);

Event::assertDispatchedTimes(PermanentCacheUpdating::class, times: 1);
Event::assertDispatchedTimes(PermanentCacheUpdated::class, times : 1);
});

0 comments on commit 8907b1f

Please sign in to comment.