summaryrefslogtreecommitdiff
path: root/tests/ConstantTest.php
diff options
context:
space:
mode:
authorZhineng Li <[email protected]>2026-01-05 16:26:11 +0800
committerZhineng Li <[email protected]>2026-01-05 16:26:11 +0800
commita49763dd739c3c68c4a8322896d594e926ac8e6b (patch)
treeab96cf55ccf828ec3c61d7cf5c440cc8fe837a9b /tests/ConstantTest.php
first commit
Diffstat (limited to 'tests/ConstantTest.php')
-rw-r--r--tests/ConstantTest.php70
1 files changed, 70 insertions, 0 deletions
diff --git a/tests/ConstantTest.php b/tests/ConstantTest.php
new file mode 100644
index 0000000..78d1be3
--- /dev/null
+++ b/tests/ConstantTest.php
@@ -0,0 +1,70 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Zhineng\Snowflake\Tests;
+
+use PHPUnit\Framework\Attributes\CoversClass;
+use PHPUnit\Framework\TestCase;
+use Zhineng\Snowflake\Constant;
+
+#[CoversClass(Constant::class)]
+final class ConstantTest extends TestCase
+{
+ public function testConstantCanBeInitialized(): void
+ {
+ $field = new Constant('machine_id', 10);
+ $this->assertSame('machine_id', $field->name);
+ $this->assertSame(10, $field->bits);
+ $this->assertSame(0, $field->value);
+ }
+
+ public function testMaxValueShouldBeCalculatedCorrectly(): void
+ {
+ $field = new Constant('machine_id', 10);
+ $maxValue = (1 << 10) - 1;
+ $this->assertSame($maxValue, $field->maxValue());
+ }
+
+ public function testInitialValueCanBeSet(): void
+ {
+ $field = new Constant('machine_id', 10, 2);
+ $this->assertSame(2, $field->value);
+ }
+
+ public function testInitialValueMustBePositive(): void
+ {
+ $this->expectException(\InvalidArgumentException::class);
+ $this->expectExceptionMessage('Field value must be non-negative.');
+ new Constant('machine_id', 10, -1);
+ }
+
+ public function testInitialValueMustNotExceedMaxValue(): void
+ {
+ $this->expectException(\InvalidArgumentException::class);
+ $this->expectExceptionMessage('Field value 1024 exceeds maximum 1023 for 10 bits.');
+ new Constant('machine_id', 10, 1024);
+ }
+
+ public function testConstantHasMakeFactoryMethod(): void
+ {
+ $field = Constant::make('machine_id', 10, 5);
+ $this->assertSame('machine_id', $field->name);
+ $this->assertSame(10, $field->bits);
+ $this->assertSame(5, $field->value);
+ }
+
+ public function testBitsMustBeAtLeastOne(): void
+ {
+ $this->expectException(\InvalidArgumentException::class);
+ $this->expectExceptionMessage('Bits must be between 1 and 63.');
+ new Constant('machine_id', 0);
+ }
+
+ public function testBitsMustNotExceed63(): void
+ {
+ $this->expectException(\InvalidArgumentException::class);
+ $this->expectExceptionMessage('Bits must be between 1 and 63.');
+ new Constant('machine_id', 64);
+ }
+}