From 2135fc3e71dd230e54560d0cdc1837804dc85bd6 Mon Sep 17 00:00:00 2001 From: Thomas Calvet Date: Fri, 19 Mar 2021 17:26:02 +0100 Subject: [PATCH] [Uid] [GenerateUuidCommand] Compute a new \DateTimeImmutable every loop --- .../Component/Uid/Command/GenerateUuidCommand.php | 4 ++-- .../Uid/Tests/Command/GenerateUlidCommandTest.php | 11 +++++++++++ .../Uid/Tests/Command/GenerateUuidCommandTest.php | 11 +++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Uid/Command/GenerateUuidCommand.php b/src/Symfony/Component/Uid/Command/GenerateUuidCommand.php index 63cfb72367..aab44257be 100644 --- a/src/Symfony/Component/Uid/Command/GenerateUuidCommand.php +++ b/src/Symfony/Component/Uid/Command/GenerateUuidCommand.php @@ -131,7 +131,7 @@ EOF } try { - $time = new \DateTimeImmutable($time); + new \DateTimeImmutable($time); } catch (\Exception $e) { $io->error(sprintf('Invalid timestamp "%s": %s', $time, str_replace('DateTimeImmutable::__construct(): ', '', $e->getMessage()))); @@ -139,7 +139,7 @@ EOF } $create = function () use ($node, $time): Uuid { - return $this->factory->timeBased($node)->create($time); + return $this->factory->timeBased($node)->create(new \DateTimeImmutable($time)); }; break; diff --git a/src/Symfony/Component/Uid/Tests/Command/GenerateUlidCommandTest.php b/src/Symfony/Component/Uid/Tests/Command/GenerateUlidCommandTest.php index e41e6fc15c..5382dff2f5 100644 --- a/src/Symfony/Component/Uid/Tests/Command/GenerateUlidCommandTest.php +++ b/src/Symfony/Component/Uid/Tests/Command/GenerateUlidCommandTest.php @@ -89,4 +89,15 @@ final class GenerateUlidCommandTest extends TestCase Ulid::fromRfc4122(trim($commandTester->getDisplay())); } + + public function testTimestampIncrementWhenGeneratingSeveralTimeBasedUlids() + { + $commandTester = new CommandTester(new GenerateUlidCommand()); + + $this->assertSame(0, $commandTester->execute(['--time' => 'now', '--count' => '2'])); + + $ulids = explode("\n", trim($commandTester->getDisplay(true))); + + $this->assertNotSame($ulids[0], $ulids[1]); + } } diff --git a/src/Symfony/Component/Uid/Tests/Command/GenerateUuidCommandTest.php b/src/Symfony/Component/Uid/Tests/Command/GenerateUuidCommandTest.php index 27e829fc2b..ac69a5a6d4 100644 --- a/src/Symfony/Component/Uid/Tests/Command/GenerateUuidCommandTest.php +++ b/src/Symfony/Component/Uid/Tests/Command/GenerateUuidCommandTest.php @@ -209,4 +209,15 @@ final class GenerateUuidCommandTest extends TestCase Uuid::fromBase32(trim($commandTester->getDisplay())); } + + public function testTimestampIncrementWhenGeneratingSeveralTimeBasedUuids() + { + $commandTester = new CommandTester(new GenerateUuidCommand()); + + $this->assertSame(0, $commandTester->execute(['--time-based' => 'now', '--count' => '2'])); + + $uuids = explode("\n", trim($commandTester->getDisplay(true))); + + $this->assertNotSame($uuids[0], $uuids[1]); + } }