HEX
Server: LiteSpeed
System: Linux s1049.use1.mysecurecloudhost.com 4.18.0-477.27.2.lve.el8.x86_64 #1 SMP Wed Oct 11 12:32:56 UTC 2023 x86_64
User: xedaptot (3356)
PHP: 8.3.31
Disabled: NONE
Upload Files
File: /home/xedaptot/be.naniguide.com/tests/Feature/AdminDashboardCacheFeatureTest.php
<?php

use App\Library\Cache\AppCache;
use App\Services\Admin\AdminDashboardCache;
use Illuminate\Support\Facades\Cache;

uses(Tests\TestCase::class);

beforeEach(function () {
    Cache::flush();
    AppCache::reset();
});

test('AdminDashboardCache implements Cacheable with expected identity', function () {
    $cache = AdminDashboardCache::instance();

    expect($cache->cacheKey())->toBe('admin.dashboard');
    expect($cache->cacheManifest())->toHaveKeys(['SentByDay_7', 'SentByDay_30', 'SentByDay_90']);
});

test('Singleton returns same instance every call', function () {
    $a = AdminDashboardCache::instance();
    $b = AdminDashboardCache::instance();

    expect($a)->toBe($b);
});

test('manifest declares explicit ttl for both windows', function () {
    $manifest = AdminDashboardCache::instance()->cacheManifest();

    foreach (['SentByDay_7', 'SentByDay_30', 'SentByDay_90'] as $key) {
        expect($manifest[$key])->toBeArray();
        expect($manifest[$key]['compute'])->toBeInstanceOf(\Closure::class);
        expect($manifest[$key]['ttl'])->toBeInt()->toBeGreaterThan(0);
    }
});

test('SentByDay_7 read returns default on miss before warming', function () {
    $result = AppCache::for(AdminDashboardCache::instance())
        ->read('SentByDay_7', []);

    expect($result)->toBe([]);
});

test('refresh populates both windows and lastUpdatedAt is set', function () {
    AppCache::for(AdminDashboardCache::instance())->refresh();

    $seven = AppCache::for(AdminDashboardCache::instance())->read('SentByDay_7');
    $thirty = AppCache::for(AdminDashboardCache::instance())->read('SentByDay_30');

    expect($seven)->toBeArray();
    expect($thirty)->toBeArray();

    $stamp = AppCache::for(AdminDashboardCache::instance())->lastUpdatedAt('SentByDay_7');
    expect($stamp)->toBeInstanceOf(\Carbon\Carbon::class);
    expect($stamp->diffInSeconds(now()))->toBeLessThan(10);
});