UuidTest   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 8
Bugs 0 Features 1
Metric Value
wmc 2
eloc 36
c 8
b 0
f 1
dl 0
loc 67
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testRecordGetsUuid() 0 19 1
A testFormatting() 0 37 1
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
class UuidTest extends SapphireTest
14
{
15
    /**
16
     * @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...
17
     */
18
    protected static $extra_dataobjects = [
19
        Test_UuidModel::class
20
    ];
21
22
    public function testRecordGetsUuid(): void
23
    {
24
        $model = new Test_UuidModel();
25
26
        $modelTitle = 'test model';
27
        $model->Title = $modelTitle;
28
        // Uuid is set by extension
29
        $model->write();
30
31
        $this->assertEquals($modelTitle, $model->Title);
32
        $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...
33
34
        // check if we can fetch this record using our helper
35
        $fetchedModel = UuidExtension::getByUuid(Test_UuidModel::class, $model->Uuid);
36
        $this->assertEquals($model->ID, $fetchedModel->ID);
37
38
        // or with the trait
39
        $fetchedModel = Test_UuidModel::byUuid($model->Uuid);
40
        $this->assertEquals($model->ID, $fetchedModel->ID);
41
    }
42
43
    public function testFormatting(): void
44
    {
45
        $uuid = 'd84560c8-134f-11e6-a1e2-34363bd26dae';
46
        $base62 = '6a630O1jrtMjCrQDyG3D3O';
47
        $binary = Uuid::fromString($uuid)->getBytes();
48
49
        $model = new Test_UuidModel();
50
        // Manually assign a uuid
51
        $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...
52
        $model->write();
53
54
        /** @var DBUuid $dbUuid */
55
        $dbUuid = $model->dbObject('Uuid');
56
57
        $this->assertEquals($uuid, $dbUuid->Nice());
58
        $this->assertEquals($base62, $dbUuid->Base62());
59
        $this->assertEquals($base62, $model->UuidSegment());
0 ignored issues
show
Bug introduced by
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

59
        $this->assertEquals($base62, $model->/** @scrutinizer ignore-call */ UuidSegment());
Loading history...
60
        $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...
61
        $this->assertEquals($binary, $dbUuid->Bytes());
62
63
        $this->assertEquals(UuidExtension::UUID_BASE62_FORMAT, UuidExtension::getUuidFormat($base62));
64
        $this->assertEquals(UuidExtension::UUID_BINARY_FORMAT, UuidExtension::getUuidFormat($binary));
65
        $this->assertEquals(UuidExtension::UUID_STRING_FORMAT, UuidExtension::getUuidFormat($uuid));
66
67
        // A uuid that results to 21 chars
68
        $uuid = '037961c2-122d-431f-9b34-4f8506b55ce7';
69
        $base62 = '6YRobjF5RORzHeaX6fvCZ';
70
        $binary = Uuid::fromString($uuid)->getBytes();
71
72
        $model = new Test_UuidModel();
73
        // Manually assign a uuid
74
        $model->Uuid = $binary;
75
        $model->write();
76
77
        /** @var DBUuid $dbUuid */
78
        $dbUuid = $model->dbObject('Uuid');
79
        $this->assertEquals($base62, $dbUuid->Base62());
80
    }
81
}
82