File: TimeTest.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 (126 lines) | stat: -rw-r--r-- 3,470 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
<?php

declare(strict_types=1);

namespace Ramsey\Uuid\Test\Type;

use Ramsey\Uuid\Exception\UnsupportedOperationException;
use Ramsey\Uuid\Test\TestCase;
use Ramsey\Uuid\Type\Integer as IntegerObject;
use Ramsey\Uuid\Type\Time;

use function json_encode;
use function serialize;
use function unserialize;

class TimeTest extends TestCase
{
    /**
     * @param int|float|string|IntegerObject $seconds
     * @param int|float|string|IntegerObject|null $microseconds
     *
     * @dataProvider provideTimeValues
     */
    public function testTime($seconds, $microseconds): void
    {
        $params = [$seconds];
        $timeString = (string) $seconds;

        if ($microseconds !== null) {
            $params[] = $microseconds;
            $timeString .= sprintf('.%06s', (string) $microseconds);
        } else {
            $timeString .= '.000000';
        }

        $time = new Time(...$params);

        $this->assertSame((string) $seconds, $time->getSeconds()->toString());

        $this->assertSame(
            (string) $microseconds ?: '0',
            $time->getMicroseconds()->toString()
        );

        $this->assertSame($timeString, (string) $time);
    }

    /**
     * @return array<array{seconds: int|float|string|IntegerObject, microseconds: int|float|string|IntegerObject|null}>
     */
    public function provideTimeValues(): array
    {
        return [
            [
                'seconds' => 103072857659,
                'microseconds' => null,
            ],
            [
                'seconds' => -12219292800,
                'microseconds' => 1234,
            ],
        ];
    }

    /**
     * @param int|float|string|IntegerObject $seconds
     * @param int|float|string|IntegerObject|null $microseconds
     *
     * @dataProvider provideTimeValues
     */
    public function testSerializeUnserializeTime($seconds, $microseconds): void
    {
        $params = [$seconds];
        if ($microseconds !== null) {
            $params[] = $microseconds;
        }

        $time = new Time(...$params);
        $serializedTime = serialize($time);

        /** @var Time $unserializedTime */
        $unserializedTime = unserialize($serializedTime);

        $this->assertSame((string) $seconds, $unserializedTime->getSeconds()->toString());

        $this->assertSame(
            (string) $microseconds ?: '0',
            $unserializedTime->getMicroseconds()->toString()
        );
    }

    public function testUnserializeOfInvalidValueException(): void
    {
        $invalidSerialization = 'C:21:"Ramsey\\Uuid\\Type\\Time":13:{{"foo":"bar"}}';

        $this->expectException(UnsupportedOperationException::class);
        $this->expectExceptionMessage('Attempted to unserialize an invalid value');

        unserialize($invalidSerialization);
    }

    /**
     * @param int|float|string|IntegerObject $seconds
     * @param int|float|string|IntegerObject|null $microseconds
     *
     * @dataProvider provideTimeValues
     */
    public function testJsonSerialize($seconds, $microseconds): void
    {
        $time = [
            'seconds' => (string) $seconds,
            'microseconds' => (string) $microseconds ?: '0',
        ];

        $expectedJson = json_encode($time);

        $params = [$seconds];
        if ($microseconds !== null) {
            $params[] = $microseconds;
        }

        $time = new Time(...$params);

        $this->assertSame($expectedJson, json_encode($time));
    }
}