diff options
Diffstat (limited to 'tests/ConstantTest.php')
| -rw-r--r-- | tests/ConstantTest.php | 70 |
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); + } +} |
