Passed
Pull Request — master (#1312)
by Michael
08:36 queued 02:22
created

UuidTest::testInvalidUnpack2()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Xmf\Test;
6
7
use PHPUnit\Framework\TestCase;
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\TestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
9
use Xmf\Uuid;
10
11
class UuidTest extends TestCase
12
{
13
    /**
14
     * @var Random
15
     */
16
    protected $object;
17
    protected $myClass = Uuid::class;
18
19
    /**
20
     * Sets up the fixture, for example, opens a network connection.
21
     * This method is called before a test is executed.
22
     */
23
    protected function setUp(): void
24
    {
25
    }
26
27
    /**
28
     * Tears down the fixture, for example, closes a network connection.
29
     * This method is called after a test is executed.
30
     */
31
    protected function tearDown(): void
32
    {
33
    }
34
35
    public function testGenerate()
36
    {
37
        // match spec for version 4 UUID as per rfc4122
38
        $uuidMatch = '/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/';
39
40
        try {
41
            $result = Uuid::generate();
42
        } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
43
        }
44
        $this->assertMatchesRegularExpression($uuidMatch, $result);
45
46
        try {
47
            $anotherResult = Uuid::generate();
48
        } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
49
        }
50
        $this->assertMatchesRegularExpression($uuidMatch, $anotherResult);
51
52
        $this->assertNotEquals($result, $anotherResult);
53
    }
54
55
    public function testPackUnpack()
56
    {
57
        try {
58
            $uuid = Uuid::generate();
59
        } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
60
        }
61
        $binUuid = Uuid::packAsBinary($uuid);
62
        $strUuid = Uuid::unpackBinary($binUuid);
63
        $this->assertEquals($uuid, $strUuid);
64
    }
65
66
    public function testInvalidPack()
67
    {
68
        $this->expectException(\InvalidArgumentException::class);
69
        $binUuid = Uuid::packAsBinary('garbage-data');
0 ignored issues
show
Unused Code introduced by
The assignment to $binUuid is dead and can be removed.
Loading history...
70
    }
71
72
    public function testInvalidUnpack()
73
    {
74
        $this->expectException(\InvalidArgumentException::class);
75
        $binUuid = Uuid::unpackBinary('123456789012345');
0 ignored issues
show
Unused Code introduced by
The assignment to $binUuid is dead and can be removed.
Loading history...
76
    }
77
78
    public function testInvalidUnpack2()
79
    {
80
        $this->expectException(\UnexpectedValueException::class);
81
        $binUuid = Uuid::unpackBinary('0000000000000000');
0 ignored issues
show
Unused Code introduced by
The assignment to $binUuid is dead and can be removed.
Loading history...
82
    }
83
84
    /* verify natural sort order is the same for readable and binary formats */
85
    public function testSortOrder()
86
    {
87
        $auuid = [];
88
        $buuid = [];
89
        for ($i = 1; $i < 10; ++$i) {
90
            try {
91
                $uuid = Uuid::generate();
92
            } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
93
            }
94
            $auuid[] = $uuid;
95
            $buuid[] = Uuid::packAsBinary($uuid);
96
        }
97
        sort($auuid);
98
        sort($buuid);
99
        foreach ($auuid as $key => $uuid) {
100
            $this->assertEquals($uuid, Uuid::unpackBinary($buuid[$key]));
101
        }
102
    }
103
}
104
105