diff --git a/src/Illuminate/Cache/Repository.php b/src/Illuminate/Cache/Repository.php index 553474e7d7f3..a52f8fbfa624 100755 --- a/src/Illuminate/Cache/Repository.php +++ b/src/Illuminate/Cache/Repository.php @@ -625,6 +625,8 @@ protected function getSeconds($ttl) if ($duration instanceof DateTimeInterface) { $duration = Carbon::now()->diffInSeconds($duration, false); + + $duration = (int) ceil($duration); } return (int) ($duration > 0 ? $duration : 0); diff --git a/tests/Integration/Cache/RepositoryTest.php b/tests/Integration/Cache/RepositoryTest.php index da168b85ed6a..b0d48e2024fe 100644 --- a/tests/Integration/Cache/RepositoryTest.php +++ b/tests/Integration/Cache/RepositoryTest.php @@ -2,9 +2,11 @@ namespace Illuminate\Tests\Integration\Cache; +use Illuminate\Cache\Events\KeyWritten; use Illuminate\Foundation\Testing\LazilyRefreshDatabase; use Illuminate\Support\Carbon; use Illuminate\Support\Facades\Cache; +use Illuminate\Support\Facades\Event; use Orchestra\Testbench\Attributes\WithMigration; use Orchestra\Testbench\TestCase; @@ -235,4 +237,21 @@ public function testItImplicitlyClearsTtlKeysFromFileDriver() $this->assertFalse($cache->getFilesystem()->exists($cache->path('illuminate:cache:flexible:created:count'))); $this->assertTrue($cache->missing('illuminate:cache:flexible:created:count')); } + + public function testItRoundsDateTimeValuesToAccountForTimePassedDuringScriptExecution() + { + // do not freeze time as this test depends on time progressing duration execution. + $cache = Cache::driver('array'); + $events = []; + Event::listen(function (KeyWritten $event) use (&$events) { + $events[] = $event; + }); + + $result = $cache->put('foo', 'bar', now()->addSecond()); + + $this->assertTrue($result); + $this->assertCount(1, $events); + $this->assertSame('foo', $events[0]->key); + $this->assertSame(1, $events[0]->seconds); + } }