diff options
| author | Zhineng Li <[email protected]> | 2026-01-05 16:26:11 +0800 |
|---|---|---|
| committer | Zhineng Li <[email protected]> | 2026-01-05 16:26:11 +0800 |
| commit | a49763dd739c3c68c4a8322896d594e926ac8e6b (patch) | |
| tree | ab96cf55ccf828ec3c61d7cf5c440cc8fe837a9b /tests/TimestampTest.php | |
first commit
Diffstat (limited to 'tests/TimestampTest.php')
| -rw-r--r-- | tests/TimestampTest.php | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/tests/TimestampTest.php b/tests/TimestampTest.php new file mode 100644 index 0000000..d157adf --- /dev/null +++ b/tests/TimestampTest.php @@ -0,0 +1,67 @@ +<?php + +declare(strict_types=1); + +namespace Zhineng\Snowflake\Tests; + +use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\TestCase; +use Zhineng\Snowflake\Timestamp; + +#[CoversClass(Timestamp::class)] +final class TimestampTest extends TestCase +{ + public function testTimestampCanBeInitialized(): void + { + $field = new Timestamp('timestamp', 41); + $this->assertSame('timestamp', $field->name); + $this->assertSame(41, $field->bits); + } + + public function testTimestampHasDefaultParameterValues(): void + { + $field = new Timestamp; + $this->assertSame('timestamp', $field->name); + $this->assertSame(41, $field->bits); + } + + public function testTimestampHasMakeFactoryMethod(): void + { + $field = Timestamp::make('timestamp', 41); + $this->assertSame('timestamp', $field->name); + $this->assertSame(41, $field->bits); + } + + public function testTimestampHasDynamicValue(): void + { + $field = new Timestamp; + $value1 = $field->value(); + usleep(1000); // Sleep for 1 millisecond + $value2 = $field->value(); + $this->assertGreaterThan($value1, $value2); + } + + public function testEpochCanBeCustomized(): void + { + $epoch = new \DateTime('2026-01-01 00:00:00'); + $field = new Timestamp('timestamp', 41, $epoch); + $now = (int) floor(microtime(as_float: true) * 1000); + $this->assertSame($now - $epoch->getTimestamp() * 1000, $field->value()); + } + + public function testEpochCanBeSetAsInteger(): void + { + $epoch = new \DateTime('2026-01-01 00:00:00'); + $timestampInMillis = $epoch->getTimestamp() * 1000; + $field = new Timestamp('timestamp', 41, $timestampInMillis); + $now = (int) floor(microtime(as_float: true) * 1000); + $this->assertSame($now - $timestampInMillis, $field->value()); + } + + public function testEpochMustBeNonNegative(): void + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('Epoch must be non-negative.'); + new Timestamp('timestamp', 41, -1); + } +} |
