[Uid] [GenerateUuidCommand] Compute a new \DateTimeImmutable every loop

This commit is contained in:
Thomas Calvet 2021-03-19 17:26:02 +01:00
parent 6c0786b579
commit 2135fc3e71
3 changed files with 24 additions and 2 deletions

View File

@ -131,7 +131,7 @@ EOF
} }
try { try {
$time = new \DateTimeImmutable($time); new \DateTimeImmutable($time);
} catch (\Exception $e) { } catch (\Exception $e) {
$io->error(sprintf('Invalid timestamp "%s": %s', $time, str_replace('DateTimeImmutable::__construct(): ', '', $e->getMessage()))); $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 { $create = function () use ($node, $time): Uuid {
return $this->factory->timeBased($node)->create($time); return $this->factory->timeBased($node)->create(new \DateTimeImmutable($time));
}; };
break; break;

View File

@ -89,4 +89,15 @@ final class GenerateUlidCommandTest extends TestCase
Ulid::fromRfc4122(trim($commandTester->getDisplay())); 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]);
}
} }

View File

@ -209,4 +209,15 @@ final class GenerateUuidCommandTest extends TestCase
Uuid::fromBase32(trim($commandTester->getDisplay())); 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]);
}
} }