summaryrefslogtreecommitdiff
path: root/tests/SequenceTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'tests/SequenceTest.php')
-rw-r--r--tests/SequenceTest.php53
1 files changed, 53 insertions, 0 deletions
diff --git a/tests/SequenceTest.php b/tests/SequenceTest.php
new file mode 100644
index 0000000..fc02d87
--- /dev/null
+++ b/tests/SequenceTest.php
@@ -0,0 +1,53 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Zhineng\Snowflake\Tests;
+
+use PHPUnit\Framework\Attributes\CoversClass;
+use PHPUnit\Framework\TestCase;
+use Zhineng\Snowflake\Sequence;
+
+#[CoversClass(Sequence::class)]
+final class SequenceTest extends TestCase
+{
+ public function testSequenceCanBeInitialized(): void
+ {
+ $seq = new Sequence('sequence', 12);
+ $this->assertSame('sequence', $seq->name);
+ $this->assertSame(12, $seq->bits);
+ }
+
+ public function testSequenceHasMakeFactoryMethod(): void
+ {
+ $seq = Sequence::make('sequence', 12);
+ $this->assertSame('sequence', $seq->name);
+ $this->assertSame(12, $seq->bits);
+ }
+
+ public function testNextValueResolution(): void
+ {
+ $seq = new Sequence('sequence', 12);
+ $this->assertSame(0, $seq->next());
+ $this->assertSame(1, $seq->next());
+ $this->assertSame(2, $seq->next());
+ }
+
+ public function testExceptionShouldBeThrownWhenMaxValueExceeded(): void
+ {
+ $seq = new Sequence('sequence', 1); // Max value is 1
+ $this->assertSame(0, $seq->next());
+ $this->assertSame(1, $seq->next());
+ $this->expectException(\OverflowException::class);
+ $this->expectExceptionMessage('Sequence "sequence" exceeded its maximum value of 1.');
+ $seq->next();
+ }
+
+ public function testSequenceCanBeReset(): void
+ {
+ $seq = new Sequence('sequence', 12);
+ $this->assertSame(0, $seq->next());
+ $this->assertSame(1, $seq->next());
+ $this->assertSame(0, $seq->reset()->next());
+ }
+}