Issues (16)

tests/UuidTest.php (5 issues)

1
<?php
2
3
namespace LeKoala\Uuid\Tests;
4
5
use Ramsey\Uuid\Uuid;
6
use LeKoala\Uuid\DBUuid;
7
use LeKoala\Uuid\UuidExtension;
8
use SilverStripe\Dev\SapphireTest;
9
10
/**
11
 * Test for Uuid
12
 *
13
 * @group Uuid
14
 */
15
class UuidTest extends SapphireTest
16
{
17
    /**
18
     * @var array<class-string>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<class-string> at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in array<class-string>.
Loading history...
19
     */
20
    protected static $extra_dataobjects = [
21
        Test_UuidModel::class
22
    ];
23
24
    public function testRecordGetsUuid(): void
25
    {
26
        $model = new Test_UuidModel;
27
28
        $modelTitle = 'test model';
29
        $model->Title = $modelTitle;
30
        // Uuid is set by extension
31
        $model->write();
32
33
        $this->assertEquals($modelTitle, $model->Title);
34
        $this->assertNotEmpty($model->Uuid);
0 ignored issues
show
Bug Best Practice introduced by
The property Uuid does not exist on LeKoala\Uuid\Tests\Test_UuidModel. Since you implemented __get, consider adding a @property annotation.
Loading history...
35
36
        // check if we can fetch this record using our helper
37
        $fetchedModel = UuidExtension::getByUuid(Test_UuidModel::class, $model->Uuid);
38
        $this->assertEquals($model->ID, $fetchedModel->ID);
39
40
        // or with the trait
41
        $fetchedModel = Test_UuidModel::byUuid($model->Uuid);
42
        $this->assertEquals($model->ID, $fetchedModel->ID);
43
    }
44
45
    public function testFormatting(): void
46
    {
47
        $uuid = 'd84560c8-134f-11e6-a1e2-34363bd26dae';
48
        $base62 = '6a630O1jrtMjCrQDyG3D3O';
49
        $binary = Uuid::fromString($uuid)->getBytes();
50
51
        $model = new Test_UuidModel;
52
        // Manually assign a uuid
53
        $model->Uuid = $binary;
0 ignored issues
show
Bug Best Practice introduced by
The property Uuid does not exist on LeKoala\Uuid\Tests\Test_UuidModel. Since you implemented __set, consider adding a @property annotation.
Loading history...
54
        $model->write();
55
56
        /** @var DBUuid $dbUuid */
57
        $dbUuid = $model->dbObject('Uuid');
58
59
        $this->assertEquals($uuid, $dbUuid->Nice());
60
        $this->assertEquals($base62, $dbUuid->Base62());
61
        $this->assertEquals($base62, $model->UuidSegment());
0 ignored issues
show
The method UuidSegment() does not exist on LeKoala\Uuid\Tests\Test_UuidModel. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

61
        $this->assertEquals($base62, $model->/** @scrutinizer ignore-call */ UuidSegment());
Loading history...
62
        $this->assertEquals($binary, $model->Uuid);
0 ignored issues
show
Bug Best Practice introduced by
The property Uuid does not exist on LeKoala\Uuid\Tests\Test_UuidModel. Since you implemented __get, consider adding a @property annotation.
Loading history...
63
        $this->assertEquals($binary, $dbUuid->Bytes());
64
65
        $this->assertEquals(UuidExtension::UUID_BASE62_FORMAT, UuidExtension::getUuidFormat($base62));
66
        $this->assertEquals(UuidExtension::UUID_BINARY_FORMAT, UuidExtension::getUuidFormat($binary));
67
        $this->assertEquals(UuidExtension::UUID_STRING_FORMAT, UuidExtension::getUuidFormat($uuid));
68
69
        // A uuid that results to 21 chars
70
        $uuid = '037961c2-122d-431f-9b34-4f8506b55ce7';
71
        $base62 = '6YRobjF5RORzHeaX6fvCZ';
72
        $binary = Uuid::fromString($uuid)->getBytes();
73
74
        $model = new Test_UuidModel;
75
        // Manually assign a uuid
76
        $model->Uuid = $binary;
77
        $model->write();
78
79
        /** @var DBUuid $dbUuid */
80
        $dbUuid = $model->dbObject('Uuid');
81
        $this->assertEquals($base62, $dbUuid->Base62());
82
    }
83
}
84