<?php

it('caches the paginated order-bookings list response and invalidates it on booking changes', function (): void {
    $routeFile = app_path('routes/orderBookingRoute.php');
    $objectFile = app_path('objects/order_bookings_o.php');

    expect(is_file($routeFile))->toBeTrue();
    expect(is_file($objectFile))->toBeTrue();

    $routeCode = preg_replace('/\s+/', ' ', (string)file_get_contents($routeFile));
    $objectCode = preg_replace('/\s+/', ' ', (string)file_get_contents($objectFile));

    expect($routeCode)->toContain('use classes\order_bookings_list_cache;');
    expect($routeCode)->toContain('$cacheTtl = order_bookings_list_cache::getTtl();');
    expect($routeCode)->toContain('$cacheKey = $cacheTtl > 0 ? $this->getOrderBookingsListCacheKey($object, $forcedFilters) : null;');
    expect($routeCode)->toContain('$cachedPayload = order_bookings_list_cache::getPayload($cacheKey);');
    expect($routeCode)->toContain('$response->rawJson($cachedPayload);');
    expect($routeCode)->toContain('order_bookings_list_cache::storePayload($cacheKey, $payload, $cacheTtl);');
    expect($routeCode)->toContain('$response->rawJson($payload);');
    expect($routeCode)->toContain('private function getOrderBookingsListCacheKey(order_bookings_o $object, string $forcedFilters): string');

    expect($objectCode)->toContain('use classes\order_bookings_list_cache;');
    expect($objectCode)->toContain('order_bookings_list_cache::clearAll();');
});
