1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TraderInteractive\Filter; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Unit tests for the \TraderInteractive\Filter\DateTime class. |
9
|
|
|
* |
10
|
|
|
* @coversDefaultClass \TraderInteractive\Filter\DateTime |
11
|
|
|
* @covers ::<private> |
12
|
|
|
*/ |
13
|
|
|
final class DateTimeTest extends TestCase |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Verify basic usage of filter(). |
17
|
|
|
* |
18
|
|
|
* @test |
19
|
|
|
* @covers ::filter |
20
|
|
|
* |
21
|
|
|
* @return void |
22
|
|
|
*/ |
23
|
|
|
public function filter() |
24
|
|
|
{ |
25
|
|
|
$string = '2014-02-04T11:55:00-0500'; |
26
|
|
|
$dateTime = DateTime::filter($string); |
27
|
|
|
|
28
|
|
|
$this->assertSame(strtotime($string), $dateTime->getTimestamp()); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Verify behavior of filter() when $value is an integer. |
33
|
|
|
* |
34
|
|
|
* @test |
35
|
|
|
* @covers ::filter |
36
|
|
|
* |
37
|
|
|
* @return void |
38
|
|
|
*/ |
39
|
|
|
public function filterTimestamp() |
40
|
|
|
{ |
41
|
|
|
$now = time(); |
42
|
|
|
$dateTime = DateTime::filter("@{$now}"); |
43
|
|
|
|
44
|
|
|
$this->assertSame($now, $dateTime->getTimestamp()); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Verify behavior of filter() when $value is a string with only whitespace. |
49
|
|
|
* |
50
|
|
|
* @test |
51
|
|
|
* @covers ::filter |
52
|
|
|
* |
53
|
|
|
* @return void |
54
|
|
|
*/ |
55
|
|
|
public function filterEmptyValue() |
56
|
|
|
{ |
57
|
|
|
$this->expectException(\TraderInteractive\Exceptions\FilterException::class); |
58
|
|
|
$this->expectExceptionMessage('$value is not a non-empty string'); |
59
|
|
|
DateTime::filter("\t \n"); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Verify behavior of filter() when $value is not a string or integer. |
64
|
|
|
* |
65
|
|
|
* @test |
66
|
|
|
* @covers ::filter |
67
|
|
|
* |
68
|
|
|
* @return void |
69
|
|
|
*/ |
70
|
|
|
public function filterInvalidValue() |
71
|
|
|
{ |
72
|
|
|
$this->expectException(\TraderInteractive\Exceptions\FilterException::class); |
73
|
|
|
$this->expectExceptionMessage('$value is not a non-empty string'); |
74
|
|
|
DateTime::filter(true); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Verify behavior of filter() when null is given for $value and $allowNull is true. |
79
|
|
|
* |
80
|
|
|
* @test |
81
|
|
|
* @covers ::filter |
82
|
|
|
*/ |
83
|
|
|
public function filterNullAllowed() |
84
|
|
|
{ |
85
|
|
|
$this->assertNull(DateTime::filter(null, true)); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Verify behavior of filter() when null is given for $value and $allowNull is true. |
90
|
|
|
* |
91
|
|
|
* @test |
92
|
|
|
* @covers ::filter |
93
|
|
|
*/ |
94
|
|
|
public function filterNullNotAllowed() |
95
|
|
|
{ |
96
|
|
|
$this->expectException(\TraderInteractive\Exceptions\FilterException::class); |
97
|
|
|
$this->expectExceptionMessage('Value failed filtering, $allowNull is set to false'); |
98
|
|
|
DateTime::filter(null, false); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Verify behavior of filter() when $value is a \DateTime object. |
103
|
|
|
* |
104
|
|
|
* @test |
105
|
|
|
* @covers ::filter |
106
|
|
|
*/ |
107
|
|
|
public function filterDateTimePass() |
108
|
|
|
{ |
109
|
|
|
$dateTime = new \DateTime('now'); |
110
|
|
|
$this->assertSame($dateTime, DateTime::filter($dateTime)); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Verify behavior of filter() when $timezone is given. |
115
|
|
|
* |
116
|
|
|
* @test |
117
|
|
|
* @covers ::filter |
118
|
|
|
*/ |
119
|
|
|
public function filterWithTimeZone() |
120
|
|
|
{ |
121
|
|
|
$timezone = new \DateTimeZone('Pacific/Honolulu'); |
122
|
|
|
$dateTime = DateTime::filter('now', false, $timezone); |
123
|
|
|
$this->assertSame($timezone->getName(), $dateTime->getTimeZone()->getName()); |
124
|
|
|
$this->assertSame(-36000, $dateTime->getOffset()); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Verify behavior of filter() when $value is an integer. |
129
|
|
|
* |
130
|
|
|
* @test |
131
|
|
|
* @covers ::filter |
132
|
|
|
*/ |
133
|
|
|
public function filterWithIntegerValue() |
134
|
|
|
{ |
135
|
|
|
$now = time(); |
136
|
|
|
$dateTime = DateTime::filter($now); |
137
|
|
|
$this->assertSame($now, $dateTime->getTimestamp()); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Verify basic behavior of format(). |
142
|
|
|
* |
143
|
|
|
* @test |
144
|
|
|
* @covers ::format |
145
|
|
|
*/ |
146
|
|
|
public function format() |
147
|
|
|
{ |
148
|
|
|
$now = new \DateTime(); |
149
|
|
|
$this->assertSame($now->format('Y-m-d H:i:s'), DateTime::format($now, 'Y-m-d H:i:s')); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Verify basic behavior of format(). |
154
|
|
|
* |
155
|
|
|
* @test |
156
|
|
|
* @covers ::format |
157
|
|
|
*/ |
158
|
|
|
public function formatWithEmptyFormat() |
159
|
|
|
{ |
160
|
|
|
$this->expectException(\InvalidArgumentException::class); |
161
|
|
|
$this->expectExceptionMessage('$format is not a non-empty string'); |
162
|
|
|
$now = new \DateTime(); |
163
|
|
|
$this->assertSame($now->format('Y-m-d H:i:s'), DateTime::format($now, ' ')); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|