testAsFormattedString_WithSpecialSettings()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
c 0
b 0
f 0
rs 9.3142
cc 1
eloc 14
nc 1
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
    public function testFromStringTimestamp()
52
    {
53
        $classicTs_A = '2015-10-25 12:00:00';
54
        $uTs_A = UniversalTimestamp::fromStringTimestamp('2015-10-25 12:30:00');
55
        $classicTs_B = '2015-10-25 13:00:00';
56
57
        $this->assertGreaterThanOrEqual($classicTs_A, $uTs_A->asFormattedString('Y-m-d H:i:s'));
58
        $this->assertGreaterThanOrEqual($uTs_A->asFormattedString('Y-m-d H:i:s'), $classicTs_B);
59
    }
60
61
    /**
62
     * @expectedException \Litipk\Jiffy\JiffyException
63
     * @expectedExceptionMessage The number of milliseconds and microseconds must be positive
64
     */
65
    public function testFromMilliseconds_WithNegativeValues()
66
    {
67
        $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...
68
    }
69
70
    public function testFromWhatever()
71
    {
72
        $ts1 = UniversalTimestamp::fromWhatever(1445817008639);
73
        $ts2 = UniversalTimestamp::fromWhatever(new \DateTime());
74
        $ts3 = UniversalTimestamp::fromWhatever(null);
75
        $ts4 = UniversalTimestamp::fromWhatever("2015-10-25 00:00:00");
76
77
        $this->assertTrue($ts1 instanceof UniversalTimestamp);
78
        $this->assertTrue($ts2 instanceof UniversalTimestamp);
79
        $this->assertTrue($ts3 instanceof UniversalTimestamp);
80
        $this->assertTrue($ts4 instanceof UniversalTimestamp);
81
82
        if (extension_loaded('mongo')) {
83
            $ts5 = UniversalTimestamp::fromWhatever(new \MongoDate());
84
            $this->assertTrue($ts5 instanceof UniversalTimestamp);
85
        }
86
        if (extension_loaded('mongodb')) {
87
            $ts5 = UniversalTimestamp::fromWhatever(
88
                new \MongoDB\BSON\UTCDatetime(UniversalTimestamp::now()->asMilliseconds())
89
            );
90
            $this->assertTrue($ts5 instanceof UniversalTimestamp);
91
        }
92
    }
93
94
    /**
95
     * @expectedException \Litipk\Jiffy\JiffyException
96
     * @expectedExceptionMessage The provided value cannot be interpreted as a timestamp
97
     */
98
    public function testFromWhatever_WithInvalidTimestamp()
99
    {
100
        $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...
101
    }
102
103
    public function testIsGreaterThan()
104
    {
105
        $ts1 = UniversalTimestamp::now();
106
        $ts2 = UniversalTimestamp::now();
107
108
        $this->assertTrue($ts2->isGreaterThan($ts1));
109
        $this->assertFalse($ts1->isGreaterThan($ts2));
110
    }
111
112
    public function testAddSeconds()
113
    {
114
        $ts1 = UniversalTimestamp::now();
115
        $ts2 = $ts1->addSeconds(1);
116
117
        $this->assertTrue($ts2->isGreaterThan($ts1));
118
        $this->assertFalse($ts1->isGreaterThan($ts2));
119
120
        $this->assertEquals($ts1->asSeconds()+1, $ts2->asSeconds());
121
    }
122
123
    public function testAddMilliseconds()
124
    {
125
        $ts1 = UniversalTimestamp::now();
126
        $ts1Millis = $ts1->asMilliseconds();
127
128
        $ts2 = $ts1->addMilliseconds(37);
129
130
        $this->assertEquals($ts1Millis, $ts1->asMilliseconds()); // Checking immutability
131
132
        $this->assertTrue($ts2->isGreaterThan($ts1));
133
        $this->assertFalse($ts1->isGreaterThan($ts2));
134
135
        $this->assertEquals($ts1->asMilliseconds()+37, $ts2->asMilliseconds());
136
    }
137
138
    public function testAsMilliseconds()
139
    {
140
        $ts = UniversalTimestamp::now();
141
142
        $this->assertGreaterThanOrEqual($ts->asSeconds()*1000, $ts->asMilliseconds());
143
        $this->assertGreaterThan($ts->asMilliseconds(), ($ts->asSeconds()+1)*1000);
144
    }
145
146
    public function testGetRemainingMicroseconds()
147
    {
148
        $ts1 = UniversalTimestamp::now();
149
        $ts2 = UniversalTimestamp::now();
150
151
        $this->assertGreaterThanOrEqual(0, $ts1->getRemainingMicroseconds());
152
        $this->assertLessThan(1000, $ts1->getRemainingMicroseconds());
153
154
        if ($ts1->asMilliseconds() === $ts2->asMilliseconds()) {
155
            $this->assertTrue($ts2->getRemainingMicroseconds() > $ts1->getRemainingMicroseconds());
156
        }
157
    }
158
159
    public function testToString()
160
    {
161
        $ts1 = UniversalTimestamp::fromMillisecondsTimestamp(1445817646455, 378);
162
163
        $d1 = new \DateTime($ts1->asFormattedString());
164
        $d2 = new \DateTime((string)$ts1);
165
166
        $ts2 = UniversalTimestamp::fromDateTimeInterface($d1);
167
        $ts3 = UniversalTimestamp::fromDateTimeInterface($d2);
168
169
        $this->assertEquals($ts1->asSeconds(), $ts2->asSeconds());
170
        $this->assertEquals($ts1->asMilliseconds(), $ts2->asMilliseconds());
171
        $this->assertEquals($ts1->asSeconds(), $ts3->asSeconds());
172
        $this->assertEquals($ts1->asMilliseconds(), $ts3->asMilliseconds());
173
    }
174
175
    public function testAsFormattedString_WithSpecialSettings()
176
    {
177
        $ts1 = UniversalTimestamp::fromMillisecondsTimestamp(1445817646571, 473);
178
179
        $this->assertEquals(
180
            '2015-10-26T00:00:46.571+0000',
181
            $ts1->asFormattedString(UniversalTimestamp::ISO8601_WITH_MILLISECONDS, 'UTC')
182
        );
183
        $this->assertEquals(
184
            '2015-10-26T00:00:46.571',
185
            $ts1->asFormattedString(UniversalTimestamp::ISO8601_WITH_MILLISECONDS_WITHOUT_TZ, 'UTC')
186
        );
187
        $this->assertEquals(
188
            '2015-10-26T00:00:46.571473+0000',
189
            $ts1->asFormattedString(UniversalTimestamp::ISO8601_WITH_MICROSECONDS, 'UTC')
190
        );
191
        $this->assertEquals(
192
            '2015-10-26T00:00:46.571473',
193
            $ts1->asFormattedString(UniversalTimestamp::ISO8601_WITH_MICROSECONDS_WITHOUT_TZ, 'UTC')
194
        );
195
    }
196
}
197