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

tests/UniversalTimestampTests.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
4
use Litipk\Jiffy\UniversalTimestamp;
5
6
7
class UniversalTimestampTests extends \PHPUnit_Framework_TestCase
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 testFromMongoDate()
32
    {
33
        if (!extension_loaded('mongo')) return;
34
35
        $uTs_A = UniversalTimestamp::now();
36
        $uTs_B = UniversalTimestamp::fromMongoDate(new \MongoDate());
0 ignored issues
show
The method fromMongoDate() does not seem to exist on object<Litipk\Jiffy\UniversalTimestamp>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
37
        $uTs_C = UniversalTimestamp::now();
38
39
        $this->assertGreaterThanOrEqual(4, 5);
40
41
        $this->assertGreaterThanOrEqual($uTs_A->asMilliseconds(), $uTs_B->asMilliseconds());
42
        $this->assertGreaterThanOrEqual($uTs_B->asMilliseconds(), $uTs_C->asMilliseconds());
43
        $this->assertGreaterThanOrEqual($uTs_A->asMilliseconds(), $uTs_C->asMilliseconds());
44
    }
45
46
    public function testFromSeconds()
47
    {
48
        $classicTs_A = time();
49
        $uTs_A = UniversalTimestamp::fromSecondsTimestamp(time());
50
        $classicTs_B = time();
51
52
        $this->assertGreaterThanOrEqual($classicTs_A, $uTs_A->asSeconds());
53
        $this->assertGreaterThanOrEqual($uTs_A->asSeconds(), $classicTs_B);
54
    }
55
56
    public function testFromMilliseconds()
57
    {
58
        $classicTs_A = 1445817008;
59
        $uTs_A = UniversalTimestamp::fromMillisecondsTimestamp(1445817008639);
60
        $classicTs_B = 1445817009;
61
62
        $this->assertGreaterThanOrEqual($classicTs_A, $uTs_A->asSeconds());
63
        $this->assertGreaterThanOrEqual($uTs_A->asSeconds(), $classicTs_B);
64
    }
65
66
    /**
67
     * @expectedException \Litipk\Jiffy\JiffyException
68
     * @expectedExceptionMessage The number of milliseconds and microseconds must be positive
69
     */
70
    public function testFromMilliseconds_WithNegativeValues()
71
    {
72
        $uTs = UniversalTimestamp::fromMillisecondsTimestamp(-1445817008639);
73
    }
74
75
    public function testFromWhatever()
76
    {
77
        $ts1 = UniversalTimestamp::fromWhatever(1445817008639);
78
        $ts2 = UniversalTimestamp::fromWhatever(new \DateTime());
79
        $ts3 = UniversalTimestamp::fromWhatever(null);
80
81
        $this->assertTrue($ts1 instanceof UniversalTimestamp);
82
        $this->assertTrue($ts2 instanceof UniversalTimestamp);
83
        $this->assertTrue($ts3 instanceof UniversalTimestamp);
84
85
        if (extension_loaded('mongo')) {
86
            $ts4 = UniversalTimestamp::fromWhatever(new \MongoDate());
87
            $this->assertTrue($ts4 instanceof UniversalTimestamp);
88
        }
89
    }
90
91
    /**
92
     * @expectedException \Litipk\Jiffy\JiffyException
93
     * @expectedExceptionMessage The provided value cannot be interpreted as a timestamp
94
     */
95
    public function testFromWhatever_WithInvalidTimestamp()
96
    {
97
        $ts1 = UniversalTimestamp::fromWhatever("Hello");
98
    }
99
100
    public function testIsGreaterThan()
101
    {
102
        $ts1 = UniversalTimestamp::now();
103
        $ts2 = UniversalTimestamp::now();
104
105
        $this->assertTrue($ts2->isGreaterThan($ts1));
106
        $this->assertFalse($ts1->isGreaterThan($ts2));
107
    }
108
109
    public function testAddSeconds()
110
    {
111
        $ts1 = UniversalTimestamp::now();
112
        $ts2 = $ts1->addSeconds(1);
113
114
        $this->assertTrue($ts2->isGreaterThan($ts1));
115
        $this->assertFalse($ts1->isGreaterThan($ts2));
116
117
        $this->assertEquals($ts1->asSeconds()+1, $ts2->asSeconds());
118
    }
119
120
    public function testAddMilliseconds()
121
    {
122
        $ts1 = UniversalTimestamp::now();
123
        $ts1Millis = $ts1->asMilliseconds();
124
125
        $ts2 = $ts1->addMilliseconds(37);
126
127
        $this->assertEquals($ts1Millis, $ts1->asMilliseconds()); // Checking immutability
128
129
        $this->assertTrue($ts2->isGreaterThan($ts1));
130
        $this->assertFalse($ts1->isGreaterThan($ts2));
131
132
        $this->assertEquals($ts1->asMilliseconds()+37, $ts2->asMilliseconds());
133
    }
134
135
    public function testAsMilliseconds()
136
    {
137
        $ts = UniversalTimestamp::now();
138
139
        $this->assertGreaterThanOrEqual($ts->asSeconds()*1000, $ts->asMilliseconds());
140
        $this->assertGreaterThan($ts->asMilliseconds(), ($ts->asSeconds()+1)*1000);
141
    }
142
143
    public function testGetRemainingMicroseconds()
144
    {
145
        $ts1 = UniversalTimestamp::now();
146
        $ts2 = UniversalTimestamp::now();
147
148
        $this->assertGreaterThanOrEqual(0, $ts1->getRemainingMicroseconds());
149
        $this->assertLessThan(1000, $ts1->getRemainingMicroseconds());
150
151
        if ($ts1->asMilliseconds() === $ts2->asMilliseconds()) {
152
            $this->assertTrue($ts2->getRemainingMicroseconds() > $ts1->getRemainingMicroseconds());
153
        }
154
    }
155
156
    public function testAsMongoDate()
157
    {
158
        if (!extension_loaded('mongo')) return;
159
160
        $ts1 = UniversalTimestamp::fromMongoDate(new \MongoDate());
0 ignored issues
show
The method fromMongoDate() does not seem to exist on object<Litipk\Jiffy\UniversalTimestamp>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
161
        $md1 = $ts1->asMongoDate();
162
163
        $this->assertTrue($md1 instanceof \MongoDate);
164
        $this->assertEquals($ts1->asSeconds(), $md1->sec);
165
        $this->assertEquals($ts1->asMilliseconds()%1000, $md1->usec/1000);
166
    }
167
168
    public function testToString()
169
    {
170
        $ts1 = UniversalTimestamp::fromMillisecondsTimestamp(1445817646455, 378);
171
172
        $d1 = new \DateTime($ts1->asFormattedString());
173
        $d2 = new \DateTime((string)$ts1);
174
175
        $ts2 = UniversalTimestamp::fromDateTimeInterface($d1);
176
        $ts3 = UniversalTimestamp::fromDateTimeInterface($d2);
177
178
        $this->assertEquals($ts1->asSeconds(), $ts2->asSeconds());
179
        $this->assertEquals($ts1->asMilliseconds(), $ts2->asMilliseconds());
180
        $this->assertEquals($ts1->asSeconds(), $ts3->asSeconds());
181
        $this->assertEquals($ts1->asMilliseconds(), $ts3->asMilliseconds());
182
    }
183
184
    public function testAsFormattedString_WithSpecialSettings()
185
    {
186
        $ts1 = UniversalTimestamp::fromMillisecondsTimestamp(1445817646571, 473);
187
188
        $this->assertEquals(
189
            '2015-10-26T00:00:46.571+0000',
190
            $ts1->asFormattedString(UniversalTimestamp::ISO8601_WITH_MILLISECONDS, 'UTC')
191
        );
192
        $this->assertEquals(
193
            '2015-10-26T00:00:46.571',
194
            $ts1->asFormattedString(UniversalTimestamp::ISO8601_WITH_MILLISECONDS_WITHOUT_TZ, 'UTC')
195
        );
196
        $this->assertEquals(
197
            '2015-10-26T00:00:46.571473+0000',
198
            $ts1->asFormattedString(UniversalTimestamp::ISO8601_WITH_MICROSECONDS, 'UTC')
199
        );
200
        $this->assertEquals(
201
            '2015-10-26T00:00:46.571473',
202
            $ts1->asFormattedString(UniversalTimestamp::ISO8601_WITH_MICROSECONDS_WITHOUT_TZ, 'UTC')
203
        );
204
    }
205
}
206