Files
famous-ly4-ev/app-modules/routing/tests/Feature/RoutesCachingTest.php
T
Nyan Lin Paing 4da9ecfe7d Add Routing module: EvRoute, pricing, time slots, read API, caching (T3.1-T3.7)
Implements Phase 3 in full: EvRoute model with company/destination relations
and a from/to-must-differ guard; the ev_route_time_slots pivot; RoutePricing
with a per-route is_blocked flag (every route auto-manages exactly one price
row per vehicle option via a fixed-row Filament repeater on both create and
edit); PricingService::quote() with a shared VehicleOption enum; RoutingPlugin
with the EvRouteResource admin UI (activation gated on non-blocked options
being priced); the /api/v1/routes read API (list/show/pricing/time-slots,
AI-agent-friendly nested shape); and Redis-tag-based response caching
invalidated via EvRoute/RoutePricing observers.
2026-08-06 23:49:42 +07:00

83 lines
2.9 KiB
PHP

<?php
use App\Models\User;
use Illuminate\Support\Facades\Cache;
use Modules\Routing\Models\EvRoute;
use Modules\Routing\Models\RoutePricing;
use Modules\Shared\Enums\VehicleOption;
beforeEach(function () {
$this->token = User::factory()->create()->createToken('test-token')->plainTextToken;
});
test('the pricing endpoint serves a cached response until invalidated', function () {
$route = EvRoute::factory()->create(['is_active' => true]);
$pricing = RoutePricing::factory()->create([
'ev_route_id' => $route->id,
'vehicle_option' => VehicleOption::FrontSeat,
'price' => 10000,
]);
$this->withHeader('Authorization', "Bearer {$this->token}")
->getJson("/api/v1/routes/{$route->id}/pricing")
->assertJsonFragment(['price' => '10000.00']);
// Bypass Eloquent (no 'saved' event) so a stale cached response proves caching is active.
RoutePricing::query()->where('id', $pricing->id)->update(['price' => 99999]);
$this->withHeader('Authorization', "Bearer {$this->token}")
->getJson("/api/v1/routes/{$route->id}/pricing")
->assertJsonFragment(['price' => '10000.00']);
// A genuine Eloquent save fires the observer and flushes the 'routes' tag.
$pricing->refresh()->save();
$this->withHeader('Authorization', "Bearer {$this->token}")
->getJson("/api/v1/routes/{$route->id}/pricing")
->assertJsonFragment(['price' => '99999.00']);
});
test('saving an ev route invalidates the routes cache tag', function () {
$route = EvRoute::factory()->create(['is_active' => true]);
$this->withHeader('Authorization', "Bearer {$this->token}")
->getJson('/api/v1/routes')
->assertJsonCount(1, 'data');
// Bypass Eloquent so the change wouldn't be visible without invalidation.
EvRoute::query()->where('id', $route->id)->update(['is_active' => false]);
$this->withHeader('Authorization', "Bearer {$this->token}")
->getJson('/api/v1/routes')
->assertJsonCount(1, 'data');
$route->refresh()->save();
$this->withHeader('Authorization', "Bearer {$this->token}")
->getJson('/api/v1/routes')
->assertJsonCount(0, 'data');
});
test('deleting an ev route invalidates the routes cache tag', function () {
$route = EvRoute::factory()->create(['is_active' => true]);
$this->withHeader('Authorization', "Bearer {$this->token}")
->getJson('/api/v1/routes')
->assertJsonCount(1, 'data');
$route->delete();
$this->withHeader('Authorization', "Bearer {$this->token}")
->getJson('/api/v1/routes')
->assertJsonCount(0, 'data');
});
test('flushing the routes cache tag does not affect other cached data', function () {
Cache::put('unrelated-key', 'still here', now()->addMinutes(5));
$route = EvRoute::factory()->create(['is_active' => true]);
$route->update(['is_round_trip' => true]);
expect(Cache::get('unrelated-key'))->toBe('still here');
});