File: FunctionsTest.php

package info (click to toggle)
php-ramsey-uuid 4.7.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,124 kB
  • sloc: php: 13,359; xml: 194; python: 54; makefile: 16
file content (137 lines) | stat: -rw-r--r-- 3,958 bytes parent folder | download | duplicates (2)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php

declare(strict_types=1);

namespace Ramsey\Uuid\Test;

use DateTimeImmutable;
use DateTimeInterface;
use Ramsey\Uuid\Rfc4122\FieldsInterface;
use Ramsey\Uuid\Rfc4122\UuidV7;
use Ramsey\Uuid\Rfc4122\UuidV8;
use Ramsey\Uuid\Type\Hexadecimal;
use Ramsey\Uuid\Type\Integer as IntegerObject;
use Ramsey\Uuid\Uuid;

use function Ramsey\Uuid\v1;
use function Ramsey\Uuid\v2;
use function Ramsey\Uuid\v3;
use function Ramsey\Uuid\v4;
use function Ramsey\Uuid\v5;
use function Ramsey\Uuid\v6;
use function Ramsey\Uuid\v7;
use function Ramsey\Uuid\v8;

class FunctionsTest extends TestCase
{
    public function testV1ReturnsVersion1UuidString(): void
    {
        $v1 = v1();

        $this->assertIsString($v1);
        $this->assertSame(Uuid::UUID_TYPE_TIME, Uuid::fromString($v1)->getVersion());
    }

    public function testV2ReturnsVersion2UuidString(): void
    {
        $v2 = v2(
            Uuid::DCE_DOMAIN_PERSON,
            new IntegerObject('1004'),
            new Hexadecimal('aabbccdd0011'),
            63
        );

        /** @var FieldsInterface $fields */
        $fields = Uuid::fromString($v2)->getFields();

        $this->assertIsString($v2);
        $this->assertSame(Uuid::UUID_TYPE_DCE_SECURITY, $fields->getVersion());
    }

    public function testV3ReturnsVersion3UuidString(): void
    {
        $ns = Uuid::fromString(Uuid::NAMESPACE_URL);
        $v3 = v3($ns, 'https://example.com/foo');

        $this->assertIsString($v3);
        $this->assertSame(Uuid::UUID_TYPE_HASH_MD5, Uuid::fromString($v3)->getVersion());
    }

    public function testV4ReturnsVersion4UuidString(): void
    {
        $v4 = v4();

        $this->assertIsString($v4);
        $this->assertSame(Uuid::UUID_TYPE_RANDOM, Uuid::fromString($v4)->getVersion());
    }

    public function testV5ReturnsVersion5UuidString(): void
    {
        $ns = Uuid::fromString(Uuid::NAMESPACE_URL);
        $v5 = v5($ns, 'https://example.com/foo');

        $this->assertIsString($v5);
        $this->assertSame(Uuid::UUID_TYPE_HASH_SHA1, Uuid::fromString($v5)->getVersion());
    }

    public function testV6ReturnsVersion6UuidString(): void
    {
        $v6 = v6(
            new Hexadecimal('aabbccdd0011'),
            1234
        );

        /** @var FieldsInterface $fields */
        $fields = Uuid::fromString($v6)->getFields();

        $this->assertIsString($v6);
        $this->assertSame(Uuid::UUID_TYPE_REORDERED_TIME, $fields->getVersion());
    }

    public function testV7ReturnsVersion7UuidString(): void
    {
        $v7 = v7();

        /** @var UuidV7 $uuid */
        $uuid = Uuid::fromString($v7);

        /** @var FieldsInterface $fields */
        $fields = $uuid->getFields();

        $this->assertIsString($v7);
        $this->assertSame(Uuid::UUID_TYPE_UNIX_TIME, $fields->getVersion());
        $this->assertInstanceOf(DateTimeInterface::class, $uuid->getDateTime());
    }

    public function testV7WithCustomDateTimeReturnsVersion7UuidString(): void
    {
        $dateTime = new DateTimeImmutable('2022-09-14T22:44:33+00:00');

        $v7 = v7($dateTime);

        /** @var UuidV7 $uuid */
        $uuid = Uuid::fromString($v7);

        /** @var FieldsInterface $fields */
        $fields = $uuid->getFields();

        $this->assertIsString($v7);
        $this->assertSame(Uuid::UUID_TYPE_UNIX_TIME, $fields->getVersion());
        $this->assertInstanceOf(DateTimeInterface::class, $uuid->getDateTime());
        $this->assertSame(1663195473, $uuid->getDateTime()->getTimestamp());
    }

    public function testV8ReturnsVersion8UuidString(): void
    {
        $v8 = v8("\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff");

        /** @var UuidV8 $uuid */
        $uuid = Uuid::fromString($v8);

        /** @var FieldsInterface $fields */
        $fields = $uuid->getFields();

        $this->assertIsString($v8);
        $this->assertSame(Uuid::UUID_TYPE_CUSTOM, $fields->getVersion());
    }
}