|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
| 5 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 6 | + */ |
| 7 | + |
| 8 | +namespace Test; |
| 9 | + |
| 10 | +use OC\SnowflakeId; |
| 11 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 12 | + |
| 13 | +/** |
| 14 | + * @package Test |
| 15 | + */ |
| 16 | +class SnowflakeIdTest extends TestCase { |
| 17 | + #[DataProvider('provideSnowflakeIds')] |
| 18 | + public function testDecode( |
| 19 | + int|float $snowflakeId, |
| 20 | + float $timestamp, |
| 21 | + int $serverId, |
| 22 | + int $sequenceId, |
| 23 | + bool $isCli, |
| 24 | + ): void { |
| 25 | + $snowflake = new SnowflakeId($snowflakeId); |
| 26 | + |
| 27 | + $this->assertEquals($snowflakeId, $snowflake->numeric()); |
| 28 | + $this->assertEquals($timestamp, $snowflake->createdAt()); |
| 29 | + $this->assertEquals($serverId, $snowflake->serverId()); |
| 30 | + $this->assertEquals($sequenceId, $snowflake->sequenceId()); |
| 31 | + $this->assertEquals($isCli, $snowflake->isCli()); |
| 32 | + } |
| 33 | + |
| 34 | + public static function provideSnowflakeIds(): array { |
| 35 | + return [ |
| 36 | + [4688076898113587, 1760368327.984, 392, 2099, true], |
| 37 | + // Max all (can't happen ms are up to 999) |
| 38 | + [0x7fffffffffffffff, 3906760448.023, 511, 4095, true], |
| 39 | + // Max all (real) |
| 40 | + [0x7ffffffff9ffffff, 3906760447.999, 511, 4095, true], |
| 41 | + // Max seconds |
| 42 | + [0x7fffffff00000000, 3906760447, 0, 0, false], |
| 43 | + // Max milliseconds |
| 44 | + [4190109696, 1759276800.999, 0, 0, false], |
| 45 | + // Max serverId |
| 46 | + [4186112, 1759276800.0, 511, 0, false], |
| 47 | + // Max sequenceId |
| 48 | + [4095, 1759276800.0, 0, 4095, false], |
| 49 | + // Max isCli |
| 50 | + [4096, 1759276800.0, 0, 0, true], |
| 51 | + // Min |
| 52 | + [0, 1759276800, 0, 0, false], |
| 53 | + ]; |
| 54 | + } |
| 55 | +} |
0 commit comments