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
|
|
|
* @param mixed $value The value to be filtered. |
|
|
|
|
50
|
|
|
* |
51
|
|
|
* @test |
52
|
|
|
* @covers ::filter |
53
|
|
|
* @expectedException \TraderInteractive\Filter\Exception |
54
|
|
|
* @expectedExceptionMessage $value is not a non-empty string |
55
|
|
|
* |
56
|
|
|
* @return void |
57
|
|
|
*/ |
58
|
|
|
public function filterEmptyValue() |
59
|
|
|
{ |
60
|
|
|
DateTime::filter("\t \n"); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Verify behavior of filter() when $value is not a string or integer. |
65
|
|
|
* |
66
|
|
|
* @param mixed $value The value to be filtered. |
|
|
|
|
67
|
|
|
* |
68
|
|
|
* @test |
69
|
|
|
* @covers ::filter |
70
|
|
|
* @expectedException \TraderInteractive\Filter\Exception |
71
|
|
|
* @expectedExceptionMessage $value is not a non-empty string |
72
|
|
|
* |
73
|
|
|
* @return void |
74
|
|
|
*/ |
75
|
|
|
public function filterInvalidValue() |
76
|
|
|
{ |
77
|
|
|
DateTime::filter(true); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Verify behavior of filter() when $allowNull is not a boolean. |
82
|
|
|
* |
83
|
|
|
* @test |
84
|
|
|
* @expectedException \InvalidArgumentException |
85
|
|
|
* @expectedExceptionMessage $allowNull was not a boolean value |
86
|
|
|
* @covers ::filter |
87
|
|
|
*/ |
88
|
|
|
public function filterAllowNullNotBoolean() |
89
|
|
|
{ |
90
|
|
|
DateTime::filter('n/a', 5); |
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Verify behavior of filter() when null is given for $value and $allowNull is true. |
95
|
|
|
* |
96
|
|
|
* @test |
97
|
|
|
* @covers ::filter |
98
|
|
|
*/ |
99
|
|
|
public function filterNullAllowed() |
100
|
|
|
{ |
101
|
|
|
$this->assertNull(DateTime::filter(null, true)); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Verify behavior of filter() when null is given for $value and $allowNull is true. |
106
|
|
|
* |
107
|
|
|
* @test |
108
|
|
|
* @covers ::filter |
109
|
|
|
* @expectedException \TraderInteractive\Filter\Exception |
110
|
|
|
* @expectedExceptionMessage $value is not a non-empty string |
111
|
|
|
*/ |
112
|
|
|
public function filterNullNotAllowed() |
113
|
|
|
{ |
114
|
|
|
DateTime::filter(null, false); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Verify behavior of filter() when $value is a \DateTime object. |
119
|
|
|
* |
120
|
|
|
* @test |
121
|
|
|
* @covers ::filter |
122
|
|
|
*/ |
123
|
|
|
public function filterDateTimePass() |
124
|
|
|
{ |
125
|
|
|
$dateTime = new \DateTime('now'); |
126
|
|
|
$this->assertSame($dateTime, DateTime::filter($dateTime)); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Verify behavior of filter() when $timezone is given. |
131
|
|
|
* |
132
|
|
|
* @test |
133
|
|
|
* @covers ::filter |
134
|
|
|
*/ |
135
|
|
View Code Duplication |
public function filterWithTimeZone() |
|
|
|
|
136
|
|
|
{ |
137
|
|
|
$timezone = new \DateTimeZone('Pacific/Honolulu'); |
138
|
|
|
$dateTime = DateTime::filter('now', false, $timezone); |
139
|
|
|
$this->assertSame($timezone->getName(), $dateTime->getTimeZone()->getName()); |
140
|
|
|
$this->assertSame(-36000, $dateTime->getOffset()); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Verify behavior of filter() when $value is an integer. |
145
|
|
|
* |
146
|
|
|
* @test |
147
|
|
|
* @covers ::filter |
148
|
|
|
*/ |
149
|
|
|
public function filterWithIntegerValue() |
150
|
|
|
{ |
151
|
|
|
$now = time(); |
152
|
|
|
$dateTime = DateTime::filter($now); |
153
|
|
|
$this->assertSame($now, $dateTime->getTimestamp()); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Verify behavior of format() when $format is not a string |
158
|
|
|
* |
159
|
|
|
* @test |
160
|
|
|
* @expectedException \InvalidArgumentException |
161
|
|
|
* @expectedExceptionMessage $format is not a non-empty string |
162
|
|
|
* @covers ::format |
163
|
|
|
*/ |
164
|
|
|
public function formatNonStringFormat() |
165
|
|
|
{ |
166
|
|
|
DateTime::format(new \DateTime(), true); |
|
|
|
|
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Verify basic behavior of format(). |
171
|
|
|
* |
172
|
|
|
* @test |
173
|
|
|
* @covers ::format |
174
|
|
|
*/ |
175
|
|
|
public function format() |
176
|
|
|
{ |
177
|
|
|
$now = new \DateTime(); |
178
|
|
|
$this->assertSame($now->format('Y-m-d H:i:s'), DateTime::format($now, 'Y-m-d H:i:s')); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.