cacheDir = sys_get_temp_dir() . '/cache_warming_test_' . uniqid(); mkdir($this->cacheDir, 0777, true); $this->cache = new FileCache(); $this->logger = Mockery::mock(Logger::class); $this->logger->shouldReceive('info')->andReturnNull(); $this->logger->shouldReceive('debug')->andReturnNull(); $this->logger->shouldReceive('error')->andReturnNull(); $this->compiledRoutes = Mockery::mock(CompiledRoutes::class); $this->compiledRoutes->shouldReceive('getStaticRoutes')->andReturn([ '/home' => 'HomeController', '/about' => 'AboutController', ]); $this->compiledRoutes->shouldReceive('getDynamicRoutes')->andReturn([ '/users/{id}' => 'UserController', ]); $this->environment = Mockery::mock(Environment::class); }); afterEach(function () { // Cleanup test cache directory if (is_dir($this->cacheDir)) { array_map('unlink', glob($this->cacheDir . '/*')); rmdir($this->cacheDir); } Mockery::close(); }); it('warms cache end-to-end', function () { $strategy = new CriticalPathWarmingStrategy( cache: $this->cache, compiledRoutes: $this->compiledRoutes, environment: $this->environment ); $service = new CacheWarmingService( strategies: [$strategy], logger: $this->logger ); // Execute warmup $metrics = $service->warmAll(); expect($metrics->totalStrategiesExecuted)->toBe(1); expect($metrics->totalItemsWarmed)->toBeGreaterThan(0); expect($metrics->isSuccess())->toBeTrue(); // Verify cache was populated $routesKey = CacheKey::fromString('routes_static'); $cachedRoutes = $this->cache->get($routesKey); expect($cachedRoutes)->not->toBeNull(); expect($cachedRoutes->value)->toBeArray(); }); it('integrates with ScheduledWarmupJob', function () { $strategy = new CriticalPathWarmingStrategy( cache: $this->cache, compiledRoutes: $this->compiledRoutes, environment: $this->environment ); $service = new CacheWarmingService( strategies: [$strategy], logger: $this->logger ); $scheduledJob = new ScheduledWarmupJob( warmingService: $service, logger: $this->logger ); // Execute scheduled warmup $result = $scheduledJob->warmCriticalPaths(); expect($result)->toBeArray(); expect($result['total_strategies_executed'])->toBe(1); expect($result['total_items_warmed'])->toBeGreaterThan(0); }); it('handles priority-based warmup', function () { $strategy = new CriticalPathWarmingStrategy( cache: $this->cache, compiledRoutes: $this->compiledRoutes, environment: $this->environment ); $service = new CacheWarmingService( strategies: [$strategy], logger: $this->logger ); // Warm only high priority $metrics = $service->warmByPriority(500); // HIGH priority threshold expect($metrics->totalStrategiesExecuted)->toBe(1); // Critical > High expect($metrics->totalItemsWarmed)->toBeGreaterThan(0); }); it('supports forced warmup', function () { $strategy = new CriticalPathWarmingStrategy( cache: $this->cache, compiledRoutes: $this->compiledRoutes, environment: $this->environment ); $service = new CacheWarmingService( strategies: [$strategy], logger: $this->logger ); // First warmup $service->warmAll(); // Second warmup (forced) should re-warm even if cache exists $metrics = $service->warmAll(force: true); expect($metrics->totalStrategiesExecuted)->toBe(1); expect($metrics->totalItemsWarmed)->toBeGreaterThan(0); }); it('provides accurate metrics', function () { $strategy = new CriticalPathWarmingStrategy( cache: $this->cache, compiledRoutes: $this->compiledRoutes, environment: $this->environment ); $service = new CacheWarmingService( strategies: [$strategy], logger: $this->logger ); $startTime = microtime(true); $metrics = $service->warmAll(); $actualDuration = microtime(true) - $startTime; expect($metrics->totalDurationSeconds)->toBeGreaterThan(0); expect($metrics->totalDurationSeconds)->toBeLessThan($actualDuration + 1); // Allow 1s margin expect($metrics->totalMemoryUsedBytes)->toBeGreaterThan(0); expect($metrics->getOverallSuccessRate())->toBeGreaterThan(0.5); }); it('handles multiple strategies correctly', function () { $strategy1 = new CriticalPathWarmingStrategy( cache: $this->cache, compiledRoutes: $this->compiledRoutes, environment: $this->environment ); // Create a second mock strategy $strategy2 = Mockery::mock(\App\Framework\Cache\Warming\WarmupStrategy::class); $strategy2->shouldReceive('getName')->andReturn('test_strategy'); $strategy2->shouldReceive('getPriority')->andReturn(100); $strategy2->shouldReceive('shouldRun')->andReturn(true); $strategy2->shouldReceive('getEstimatedDuration')->andReturn(1); $strategy2->shouldReceive('warmup')->andReturn( new \App\Framework\Cache\Warming\ValueObjects\WarmupResult( strategyName: 'test_strategy', itemsWarmed: 5, itemsFailed: 0, durationSeconds: 0.5, memoryUsedBytes: 512 ) ); $service = new CacheWarmingService( strategies: [$strategy1, $strategy2], logger: $this->logger ); $metrics = $service->warmAll(); expect($metrics->totalStrategiesExecuted)->toBe(2); expect($metrics->strategyResults)->toHaveCount(2); }); });