cache = new GeneralCache($inMemoryCache, $serializer); $this->logger = Mockery::mock(Logger::class); $this->logger->shouldReceive('info')->andReturnNull(); $this->logger->shouldReceive('debug')->andReturnNull(); $this->logger->shouldReceive('error')->andReturnNull(); // Use real CompiledRoutes instance for testing $this->compiledRoutes = new CompiledRoutes( staticRoutes: [ 'GET' => [ 'default' => [ '/home' => null, // Route objects not needed for cache warming test '/about' => null, ] ] ], dynamicPatterns: [ 'GET' => [ 'default' => null // CompiledPattern not needed for basic test ] ], namedRoutes: [] ); // Use real Environment instance for testing $this->environment = new Environment([ 'APP_ENV' => 'testing', 'APP_DEBUG' => 'true', ]); }); afterEach(function () { Mockery::close(); }); it('warms cache end-to-end', function () { $strategy = new CriticalPathWarmingStrategy( cache: $this->cache, compiledRoutes: $this->compiledRoutes, environment: $this->environment ); $service = new CacheWarmingService( logger: $this->logger, strategies: [$strategy] ); // Execute warmup $metrics = $service->warmAll(); expect($metrics->totalStrategiesExecuted)->toBe(1); expect($metrics->totalItemsWarmed)->toBeGreaterThan(0); expect($metrics->getOverallSuccessRate())->toBe(1.0); // 100% success // 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( logger: $this->logger, strategies: [$strategy] ); $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( logger: $this->logger, strategies: [$strategy] ); // 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( logger: $this->logger, strategies: [$strategy] ); // 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( logger: $this->logger, strategies: [$strategy] ); $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); }); });