diff --git a/src/Strategy/PrivateCacheStrategy.php b/src/Strategy/PrivateCacheStrategy.php index 23db04e..86d7947 100644 --- a/src/Strategy/PrivateCacheStrategy.php +++ b/src/Strategy/PrivateCacheStrategy.php @@ -83,7 +83,7 @@ protected function getCacheObject(RequestInterface $request, ResponseInterface $ if ($cacheControl->has('no-cache')) { // Stale response see RFC7234 section 5.2.1.4 - $entry = new CacheEntry($request, $response, new \DateTime('-1 seconds')); + $entry = new CacheEntry($request, $response, new \DateTime('@' . (time() - 1))); return $entry->hasValidationInformation() ? $entry : null; } @@ -109,7 +109,7 @@ protected function getCacheObject(RequestInterface $request, ResponseInterface $ } } - return new CacheEntry($request, $response, new \DateTime('-1 seconds')); + return new CacheEntry($request, $response, new \DateTime('@' . (time() - 1))); } /** diff --git a/tests/DstTransitionTest.php b/tests/DstTransitionTest.php new file mode 100644 index 0000000..7e6757e --- /dev/null +++ b/tests/DstTransitionTest.php @@ -0,0 +1,75 @@ +originalTimezone = date_default_timezone_get(); + } + + protected function tearDown(): void + { + // Restore original timezone + date_default_timezone_set($this->originalTimezone); + } + + /** + * Test CacheEntry behavior with UTC timestamp approach + * This ensures that cache entries marked for immediate expiry + * behave consistently across timezones. + */ + public function testCacheEntryWithUtcTimestampIsAlwaysStale() + { + $timezones = ['UTC', 'Europe/Berlin', 'America/New_York']; + + foreach ($timezones as $timezone) { + date_default_timezone_set($timezone); + + $request = new Request('GET', 'http://example.com'); + $response = new Response(200, [], 'test content'); + + // Create entry with UTC timestamp approach (the fix) + $entry = new CacheEntry($request, $response, new \DateTime('@' . (time() - 1))); + + // Should always be stale regardless of timezone + $this->assertTrue($entry->isStale(), "Entry should be stale in timezone: $timezone"); + $this->assertFalse($entry->isFresh(), "Entry should not be fresh in timezone: $timezone"); + $this->assertGreaterThan(0, $entry->getStaleAge(), "Stale age should be positive in timezone: $timezone"); + } + } + + /** + * Test that validates the TTL calculation is correct + */ + public function testCacheEntryTtlWithUtcTimestamp() + { + date_default_timezone_set('Europe/Berlin'); + + $request = new Request('GET', 'http://example.com'); + $response = new Response(200, [], 'test content'); + + // Create entry with UTC timestamp approach + $entry = new CacheEntry($request, $response, new \DateTime('@' . (time() - 1))); + + $ttl = $entry->getTTL(); + + // TTL should be -1 for expired entries without validation info + $this->assertEquals(-1, $ttl, "TTL should be -1 for expired entries without validation info"); + } +} \ No newline at end of file