1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Uid\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Uid\MaxUlid;
use Symfony\Component\Uid\NilUlid;
use Symfony\Component\Uid\Tests\Fixtures\CustomUlid;
use Symfony\Component\Uid\Ulid;
use Symfony\Component\Uid\UuidV4;
class UlidTest extends TestCase
{
#[\PHPUnit\Framework\Attributes\Group('time-sensitive')]
#[\PHPUnit\Framework\Attributes\Group('nophpunit11')]
public function testGenerate()
{
$a = new Ulid();
$b = new Ulid();
usleep(-10000);
$c = new Ulid();
$this->assertSame(0, strncmp($a, $b, 20));
$this->assertSame(0, strncmp($a, $c, 20));
$a = base_convert(strtr(substr($a, -6), 'ABCDEFGHJKMNPQRSTVWXYZ', 'abcdefghijklmnopqrstuv'), 32, 10);
$b = base_convert(strtr(substr($b, -6), 'ABCDEFGHJKMNPQRSTVWXYZ', 'abcdefghijklmnopqrstuv'), 32, 10);
$c = base_convert(strtr(substr($c, -6), 'ABCDEFGHJKMNPQRSTVWXYZ', 'abcdefghijklmnopqrstuv'), 32, 10);
$this->assertSame(1, $b - $a);
$this->assertSame(1, $c - $b);
}
public function testWithInvalidUlid()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid ULID: "this is not a ulid".');
new Ulid('this is not a ulid');
}
public function testBinary()
{
$ulid = new Ulid('00000000000000000000000000');
$this->assertSame("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", $ulid->toBinary());
$ulid = new Ulid('3zzzzzzzzzzzzzzzzzzzzzzzzz');
$this->assertSame('7fffffffffffffffffffffffffffffff', bin2hex($ulid->toBinary()));
$this->assertTrue($ulid->equals(Ulid::fromString(hex2bin('7fffffffffffffffffffffffffffffff'))));
}
public function toHex()
{
$ulid = Ulid::fromString('1BVXue8CnY8ogucrHX3TeF');
$this->assertSame('0x0177058f4dacd0b2a990a49af02bc008', $ulid->toHex());
}
public function testFromUuid()
{
$uuid = new UuidV4();
$ulid = Ulid::fromString($uuid);
$this->assertSame($uuid->toBase32(), (string) $ulid);
$this->assertSame($ulid->toBase32(), (string) $ulid);
$this->assertSame((string) $uuid, $ulid->toRfc4122());
$this->assertTrue($ulid->equals(Ulid::fromString($uuid)));
}
public function testBase58()
{
$ulid = new Ulid('00000000000000000000000000');
$this->assertSame('1111111111111111111111', $ulid->toBase58());
$ulid = Ulid::fromString("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF");
$this->assertSame('YcVfxkQb6JRzqk5kF2tNLv', $ulid->toBase58());
$this->assertTrue($ulid->equals(Ulid::fromString('YcVfxkQb6JRzqk5kF2tNLv')));
}
#[\PHPUnit\Framework\Attributes\Group('time-sensitive')]
public function testGetDateTime()
{
$this->markTestSkipped('Test failing on current reproducible infrastructure');
$time = microtime(false);
$ulid = new Ulid();
$time = substr($time, 11).substr($time, 1, 4);
$this->assertEquals(\DateTimeImmutable::createFromFormat('U.u', $time), $ulid->getDateTime());
$this->assertEquals(new \DateTimeImmutable('@0'), (new Ulid('000000000079KA1307SR9X4MV3'))->getDateTime());
$this->assertEquals(\DateTimeImmutable::createFromFormat('U.u', '0.001'), (new Ulid('000000000179KA1307SR9X4MV3'))->getDateTime());
$this->assertEquals(\DateTimeImmutable::createFromFormat('U.u', '281474976710.654'), (new Ulid('7ZZZZZZZZY79KA1307SR9X4MV3'))->getDateTime());
$this->assertEquals(\DateTimeImmutable::createFromFormat('U.u', '281474976710.655'), (new Ulid('7ZZZZZZZZZ79KA1307SR9X4MV3'))->getDateTime());
}
public function testIsValid()
{
$this->assertFalse(Ulid::isValid('not a ulid'));
$this->assertTrue(Ulid::isValid('00000000000000000000000000'));
}
public function testEquals()
{
$a = new Ulid();
$b = new Ulid();
$this->assertTrue($a->equals($a));
$this->assertFalse($a->equals($b));
$this->assertFalse($a->equals((string) $a));
}
#[\PHPUnit\Framework\Attributes\Group('time-sensitive')]
public function testCompare()
{
$a = new Ulid();
$b = new Ulid();
$this->assertSame(0, $a->compare($a));
$this->assertLessThan(0, $a->compare($b));
$this->assertGreaterThan(0, $b->compare($a));
usleep(1001);
$c = new Ulid();
$this->assertLessThan(0, $b->compare($c));
$this->assertGreaterThan(0, $c->compare($b));
}
public function testFromBinary()
{
$this->assertEquals(
Ulid::fromString("\x01\x77\x05\x8F\x4D\xAC\xD0\xB2\xA9\x90\xA4\x9A\xF0\x2B\xC0\x08"),
Ulid::fromBinary("\x01\x77\x05\x8F\x4D\xAC\xD0\xB2\xA9\x90\xA4\x9A\xF0\x2B\xC0\x08")
);
}
#[\PHPUnit\Framework\Attributes\DataProvider('provideInvalidBinaryFormat')]
public function testFromBinaryInvalidFormat(string $ulid)
{
$this->expectException(\InvalidArgumentException::class);
Ulid::fromBinary($ulid);
}
public static function provideInvalidBinaryFormat(): array
{
return [
['01EW2RYKDCT2SAK454KBR2QG08'],
['1BVXue8CnY8ogucrHX3TeF'],
['0177058f-4dac-d0b2-a990-a49af02bc008'],
];
}
public function testFromBase58()
{
$this->assertEquals(
Ulid::fromString('1BVXue8CnY8ogucrHX3TeF'),
Ulid::fromBase58('1BVXue8CnY8ogucrHX3TeF')
);
}
#[\PHPUnit\Framework\Attributes\DataProvider('provideInvalidBase58Format')]
public function testFromBase58InvalidFormat(string $ulid)
{
$this->expectException(\InvalidArgumentException::class);
Ulid::fromBase58($ulid);
}
public static function provideInvalidBase58Format(): array
{
return [
["\x01\x77\x05\x8F\x4D\xAC\xD0\xB2\xA9\x90\xA4\x9A\xF0\x2B\xC0\x08"],
['01EW2RYKDCT2SAK454KBR2QG08'],
['0177058f-4dac-d0b2-a990-a49af02bc008'],
];
}
public function testFromBase32()
{
$this->assertEquals(
Ulid::fromString('01EW2RYKDCT2SAK454KBR2QG08'),
Ulid::fromBase32('01EW2RYKDCT2SAK454KBR2QG08')
);
}
#[\PHPUnit\Framework\Attributes\DataProvider('provideInvalidBase32Format')]
public function testFromBase32InvalidFormat(string $ulid)
{
$this->expectException(\InvalidArgumentException::class);
Ulid::fromBase32($ulid);
}
public static function provideInvalidBase32Format(): array
{
return [
["\x01\x77\x05\x8F\x4D\xAC\xD0\xB2\xA9\x90\xA4\x9A\xF0\x2B\xC0\x08"],
['1BVXue8CnY8ogucrHX3TeF'],
['0177058f-4dac-d0b2-a990-a49af02bc008'],
];
}
public function testFromRfc4122()
{
$this->assertEquals(
Ulid::fromString('0177058f-4dac-d0b2-a990-a49af02bc008'),
Ulid::fromRfc4122('0177058f-4dac-d0b2-a990-a49af02bc008')
);
}
#[\PHPUnit\Framework\Attributes\DataProvider('provideInvalidRfc4122Format')]
public function testFromRfc4122InvalidFormat(string $ulid)
{
$this->expectException(\InvalidArgumentException::class);
Ulid::fromRfc4122($ulid);
}
public static function provideInvalidRfc4122Format(): array
{
return [
["\x01\x77\x05\x8F\x4D\xAC\xD0\xB2\xA9\x90\xA4\x9A\xF0\x2B\xC0\x08"],
['01EW2RYKDCT2SAK454KBR2QG08'],
['1BVXue8CnY8ogucrHX3TeF'],
];
}
public function testFromStringOnExtendedClassReturnsStatic()
{
$this->assertInstanceOf(CustomUlid::class, CustomUlid::fromString((new CustomUlid())->toBinary()));
}
public function testFromStringBase58Padding()
{
$this->assertInstanceOf(Ulid::class, Ulid::fromString('111111111u9QRyVM94rdmZ'));
}
#[\PHPUnit\Framework\Attributes\TestWith(['00000000-0000-0000-0000-000000000000'])]
#[\PHPUnit\Framework\Attributes\TestWith(['1111111111111111111111'])]
#[\PHPUnit\Framework\Attributes\TestWith(['00000000000000000000000000'])]
public function testNilUlid(string $ulid)
{
$ulid = Ulid::fromString($ulid);
$this->assertInstanceOf(NilUlid::class, $ulid);
$this->assertSame('00000000000000000000000000', (string) $ulid);
}
public function testNewNilUlid()
{
$this->assertSame('00000000000000000000000000', (string) new NilUlid());
}
#[\PHPUnit\Framework\Attributes\TestWith(['ffffffff-ffff-ffff-ffff-ffffffffffff'])]
#[\PHPUnit\Framework\Attributes\TestWith(['7zzzzzzzzzzzzzzzzzzzzzzzzz'])]
public function testMaxUlid(string $ulid)
{
$ulid = Ulid::fromString($ulid);
$this->assertInstanceOf(MaxUlid::class, $ulid);
$this->assertSame('7ZZZZZZZZZZZZZZZZZZZZZZZZZ', (string) $ulid);
}
public function testNewMaxUlid()
{
$this->assertSame('7ZZZZZZZZZZZZZZZZZZZZZZZZZ', (string) new MaxUlid());
}
}
|