Completed
Push — master ( 58c1c3...bed08d )
by Andreu
04:34
created

UniversalTimestampTests::testFromMongoDate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 14
rs 9.4285
cc 2
eloc 9
nc 2
nop 0
1
<?php
2
3
4
use Litipk\Jiffy\UniversalTimestamp;
5
6
7
class UniversalTimestampTests extends \PHPUnit_Framework_TestCase
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
8
{
9
    public function testNow()
10
    {
11
        $classicTs_A = time();
12
        $uTs_A = UniversalTimestamp::now();
13
        $classicTs_B = time();
14
15
        $this->assertGreaterThanOrEqual($classicTs_A, $uTs_A->asSeconds());
16
17
        $this->assertGreaterThanOrEqual($uTs_A->asSeconds(), $classicTs_B);
18
19
    }
20
21
    public function testFromDateTimeInterface()
22
    {
23
        $classicTs_A = time();
24
        $uTs_A = UniversalTimestamp::fromDateTimeInterface(new \DateTime());
25
        $classicTs_B = time();
26
27
        $this->assertGreaterThanOrEqual($classicTs_A, $uTs_A->asSeconds());
28
        $this->assertGreaterThanOrEqual($uTs_A->asSeconds(), $classicTs_B);
29
    }
30
31
    public function testFromSeconds()
32
    {
33
        $classicTs_A = time();
34
        $uTs_A = UniversalTimestamp::fromSecondsTimestamp(time());
35
        $classicTs_B = time();
36
37
        $this->assertGreaterThanOrEqual($classicTs_A, $uTs_A->asSeconds());
38
        $this->assertGreaterThanOrEqual($uTs_A->asSeconds(), $classicTs_B);
39
    }
40
41
    public function testFromMilliseconds()
42
    {
43
        $classicTs_A = 1445817008;
44
        $uTs_A = UniversalTimestamp::fromMillisecondsTimestamp(1445817008639);
45
        $classicTs_B = 1445817009;
46
47
        $this->assertGreaterThanOrEqual($classicTs_A, $uTs_A->asSeconds());
48
        $this->assertGreaterThanOrEqual($uTs_A->asSeconds(), $classicTs_B);
49
    }
50
51
    /**
52
     * @expectedException \Litipk\Jiffy\JiffyException
53
     * @expectedExceptionMessage The number of milliseconds and microseconds must be positive
54
     */
55
    public function testFromMilliseconds_WithNegativeValues()
56
    {
57
        $uTs = UniversalTimestamp::fromMillisecondsTimestamp(-1445817008639);
0 ignored issues
show
Unused Code introduced by
$uTs is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
58
    }
59
60
    public function testFromWhatever()
61
    {
62
        $ts1 = UniversalTimestamp::fromWhatever(1445817008639);
63
        $ts2 = UniversalTimestamp::fromWhatever(new \DateTime());
64
        $ts3 = UniversalTimestamp::fromWhatever(null);
65
66
        $this->assertTrue($ts1 instanceof UniversalTimestamp);
67
        $this->assertTrue($ts2 instanceof UniversalTimestamp);
68
        $this->assertTrue($ts3 instanceof UniversalTimestamp);
69
70
        if (extension_loaded('mongo')) {
71
            $ts4 = UniversalTimestamp::fromWhatever(new \MongoDate());
72
            $this->assertTrue($ts4 instanceof UniversalTimestamp);
73
        }
74
    }
75
76
    /**
77
     * @expectedException \Litipk\Jiffy\JiffyException
78
     * @expectedExceptionMessage The provided value cannot be interpreted as a timestamp
79
     */
80
    public function testFromWhatever_WithInvalidTimestamp()
81
    {
82
        $ts1 = UniversalTimestamp::fromWhatever("Hello");
0 ignored issues
show
Unused Code introduced by
$ts1 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
83
    }
84
85
    public function testIsGreaterThan()
86
    {
87
        $ts1 = UniversalTimestamp::now();
88
        $ts2 = UniversalTimestamp::now();
89
90
        $this->assertTrue($ts2->isGreaterThan($ts1));
91
        $this->assertFalse($ts1->isGreaterThan($ts2));
92
    }
93
94
    public function testAddSeconds()
95
    {
96
        $ts1 = UniversalTimestamp::now();
97
        $ts2 = $ts1->addSeconds(1);
98
99
        $this->assertTrue($ts2->isGreaterThan($ts1));
100
        $this->assertFalse($ts1->isGreaterThan($ts2));
101
102
        $this->assertEquals($ts1->asSeconds()+1, $ts2->asSeconds());
103
    }
104
105
    public function testAddMilliseconds()
106
    {
107
        $ts1 = UniversalTimestamp::now();
108
        $ts1Millis = $ts1->asMilliseconds();
109
110
        $ts2 = $ts1->addMilliseconds(37);
111
112
        $this->assertEquals($ts1Millis, $ts1->asMilliseconds()); // Checking immutability
113
114
        $this->assertTrue($ts2->isGreaterThan($ts1));
115
        $this->assertFalse($ts1->isGreaterThan($ts2));
116
117
        $this->assertEquals($ts1->asMilliseconds()+37, $ts2->asMilliseconds());
118
    }
119
120
    public function testAsMilliseconds()
121
    {
122
        $ts = UniversalTimestamp::now();
123
124
        $this->assertGreaterThanOrEqual($ts->asSeconds()*1000, $ts->asMilliseconds());
125
        $this->assertGreaterThan($ts->asMilliseconds(), ($ts->asSeconds()+1)*1000);
126
    }
127
128
    public function testGetRemainingMicroseconds()
129
    {
130
        $ts1 = UniversalTimestamp::now();
131
        $ts2 = UniversalTimestamp::now();
132
133
        $this->assertGreaterThanOrEqual(0, $ts1->getRemainingMicroseconds());
134
        $this->assertLessThan(1000, $ts1->getRemainingMicroseconds());
135
136
        if ($ts1->asMilliseconds() === $ts2->asMilliseconds()) {
137
            $this->assertTrue($ts2->getRemainingMicroseconds() > $ts1->getRemainingMicroseconds());
138
        }
139
    }
140
141
    public function testToString()
142
    {
143
        $ts1 = UniversalTimestamp::fromMillisecondsTimestamp(1445817646455, 378);
144
145
        $d1 = new \DateTime($ts1->asFormattedString());
146
        $d2 = new \DateTime((string)$ts1);
147
148
        $ts2 = UniversalTimestamp::fromDateTimeInterface($d1);
149
        $ts3 = UniversalTimestamp::fromDateTimeInterface($d2);
150
151
        $this->assertEquals($ts1->asSeconds(), $ts2->asSeconds());
152
        $this->assertEquals($ts1->asMilliseconds(), $ts2->asMilliseconds());
153
        $this->assertEquals($ts1->asSeconds(), $ts3->asSeconds());
154
        $this->assertEquals($ts1->asMilliseconds(), $ts3->asMilliseconds());
155
    }
156
157
    public function testAsFormattedString_WithSpecialSettings()
158
    {
159
        $ts1 = UniversalTimestamp::fromMillisecondsTimestamp(1445817646571, 473);
160
161
        $this->assertEquals(
162
            '2015-10-26T00:00:46.571+0000',
163
            $ts1->asFormattedString(UniversalTimestamp::ISO8601_WITH_MILLISECONDS, 'UTC')
164
        );
165
        $this->assertEquals(
166
            '2015-10-26T00:00:46.571',
167
            $ts1->asFormattedString(UniversalTimestamp::ISO8601_WITH_MILLISECONDS_WITHOUT_TZ, 'UTC')
168
        );
169
        $this->assertEquals(
170
            '2015-10-26T00:00:46.571473+0000',
171
            $ts1->asFormattedString(UniversalTimestamp::ISO8601_WITH_MICROSECONDS, 'UTC')
172
        );
173
        $this->assertEquals(
174
            '2015-10-26T00:00:46.571473',
175
            $ts1->asFormattedString(UniversalTimestamp::ISO8601_WITH_MICROSECONDS_WITHOUT_TZ, 'UTC')
176
        );
177
    }
178
}
179