summaryrefslogtreecommitdiff
path: root/tests/SequenceTest.php
blob: fc02d875687cdf59b4aeb986e74d19d80a300eec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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());
    }
}