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