Issues (17)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

tests/UniversalTimestampTests.php (3 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
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
$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
$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