summaryrefslogtreecommitdiff
path: root/tests/TimestampTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'tests/TimestampTest.php')
-rw-r--r--tests/TimestampTest.php67
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);
+ }
+}