1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TraderInteractive\Filter; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @coversDefaultClass \TraderInteractive\Filter\Floats |
9
|
|
|
*/ |
10
|
|
|
final class FloatsTest extends TestCase |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @test |
14
|
|
|
* @covers ::filter |
15
|
|
|
* @expectedException \InvalidArgumentException |
16
|
|
|
* @expectedExceptionMessage "1" $allowNull was not a bool |
17
|
|
|
*/ |
18
|
|
|
public function filterAllowNullIsNotBool() |
19
|
|
|
{ |
20
|
|
|
Floats::filter('1', 1); |
|
|
|
|
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @test |
25
|
|
|
* @covers ::filter |
26
|
|
|
* @expectedException \InvalidArgumentException |
27
|
|
|
* @expectedExceptionMessage "'boo'" $minValue was not a float |
28
|
|
|
*/ |
29
|
|
|
public function filterMinValueNotFloat() |
30
|
|
|
{ |
31
|
|
|
Floats::filter('1', false, 'boo'); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @test |
36
|
|
|
* @covers ::filter |
37
|
|
|
* @expectedException \InvalidArgumentException |
38
|
|
|
* @expectedExceptionMessage "1" $maxValue was not a float |
39
|
|
|
*/ |
40
|
|
|
public function filterMaxValueNotFloat() |
41
|
|
|
{ |
42
|
|
|
Floats::filter('1', false, 1.0, 1); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @test |
47
|
|
|
* @covers ::filter |
48
|
|
|
*/ |
49
|
|
|
public function filterAllowNullIsTrueAndNullValue() |
50
|
|
|
{ |
51
|
|
|
$this->assertNull(Floats::filter(null, true)); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @test |
56
|
|
|
* @covers ::filter |
57
|
|
|
*/ |
58
|
|
|
public function filterPositiveFloat() |
59
|
|
|
{ |
60
|
|
|
$this->assertSame(123.0, Floats::filter(123.0)); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @test |
65
|
|
|
* @covers ::filter |
66
|
|
|
*/ |
67
|
|
|
public function filterNegativeFloat() |
68
|
|
|
{ |
69
|
|
|
$this->assertSame(-123.0, Floats::filter(-123.0)); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @test |
74
|
|
|
* @covers ::filter |
75
|
|
|
*/ |
76
|
|
|
public function filterZeroFloat() |
77
|
|
|
{ |
78
|
|
|
$positiveZero = + 0.0; |
79
|
|
|
$this->assertSame(0.0, Floats::filter($positiveZero)); |
80
|
|
|
$this->assertSame(-0.0, Floats::filter(-0.0)); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @test |
85
|
|
|
* @covers ::filter |
86
|
|
|
*/ |
87
|
|
|
public function filterPositiveString() |
88
|
|
|
{ |
89
|
|
|
$this->assertSame(123.0, Floats::filter(' 123 ')); |
90
|
|
|
$this->assertSame(123.0, Floats::filter(' +123 ')); |
91
|
|
|
$this->assertSame(123.0, Floats::filter(' 123.0 ')); |
92
|
|
|
$this->assertSame(123.0, Floats::filter(' 123E0 ')); |
93
|
|
|
$this->assertSame(123.0, Floats::filter(' +123e0 ')); |
94
|
|
|
$this->assertSame(123.0, Floats::filter(' +1230e-1 ')); |
95
|
|
|
$this->assertSame(1230.0, Floats::filter(' +123e+1 ')); |
96
|
|
|
$this->assertSame(0.0, Floats::filter(' +0 ')); |
97
|
|
|
$this->assertSame(0.0, Floats::filter(' +0.0 ')); |
98
|
|
|
$this->assertSame(0.0, Floats::filter(' 0E0 ')); |
99
|
|
|
$this->assertSame(0.0, Floats::filter(' 00e-1 ')); |
100
|
|
|
$this->assertSame(0.0, Floats::filter(' 00e+1 ')); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @test |
105
|
|
|
* @covers ::filter |
106
|
|
|
*/ |
107
|
|
|
public function filterNegativeString() |
108
|
|
|
{ |
109
|
|
|
$this->assertSame(-123.0, Floats::filter(' -123 ')); |
110
|
|
|
$this->assertSame(-123.0, Floats::filter(' -123.0 ')); |
111
|
|
|
$this->assertSame(-123.0, Floats::filter(' -123E0 ')); |
112
|
|
|
$this->assertSame(-123.0, Floats::filter(' -1230E-1 ')); |
113
|
|
|
$this->assertSame(-1230.0, Floats::filter(' -123e+1 ')); |
114
|
|
|
$this->assertSame(-0.0, Floats::filter(' -0 ')); |
115
|
|
|
$this->assertSame(-0.0, Floats::filter(' -0.0 ')); |
116
|
|
|
$this->assertSame(-0.0, Floats::filter(' -0e0 ')); |
117
|
|
|
$this->assertSame(-0.0, Floats::filter(' -00e-1 ')); |
118
|
|
|
$this->assertSame(-0.0, Floats::filter(' -0e+1 ')); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @test |
123
|
|
|
* @covers ::filter |
124
|
|
|
* @expectedException \TraderInteractive\Filter\Exception |
125
|
|
|
* @expectedExceptionMessage "true" $value is not a string |
126
|
|
|
*/ |
127
|
|
|
public function filterNonStringOrFloat() |
128
|
|
|
{ |
129
|
|
|
Floats::filter(true); |
|
|
|
|
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @test |
134
|
|
|
* @covers ::filter |
135
|
|
|
* @expectedException \TraderInteractive\Filter\Exception |
136
|
|
|
* @exceptedExceptionMessage does not pass is_numeric |
137
|
|
|
*/ |
138
|
|
|
public function filterEmptyString() |
139
|
|
|
{ |
140
|
|
|
Floats::filter(''); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @test |
145
|
|
|
* @covers ::filter |
146
|
|
|
* @expectedException \TraderInteractive\Filter\Exception |
147
|
|
|
* @expectedExceptionMessage does not pass is_numeric |
148
|
|
|
*/ |
149
|
|
|
public function filterWhitespaceString() |
150
|
|
|
{ |
151
|
|
|
Floats::filter(' '); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @test |
156
|
|
|
* @covers ::filter |
157
|
|
|
* @expectedException \TraderInteractive\Filter\Exception |
158
|
|
|
* @expectedExceptionMessage 123-4 does not pass is_numeric |
159
|
|
|
*/ |
160
|
|
|
public function filterNonDigitString() |
161
|
|
|
{ |
162
|
|
|
Floats::filter('123-4'); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @test |
167
|
|
|
* @covers ::filter |
168
|
|
|
* @expectedException \TraderInteractive\Filter\Exception |
169
|
|
|
*/ |
170
|
|
|
public function filterHexString() |
171
|
|
|
{ |
172
|
|
|
Floats::filter('0xFF'); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @test |
177
|
|
|
* @covers ::filter |
178
|
|
|
* @expectedException \TraderInteractive\Filter\Exception |
179
|
|
|
* @expectedExceptionMessage 1. 0 does not pass is_numeric |
180
|
|
|
*/ |
181
|
|
|
public function filterRogueSpaceStringAfterPeriod() |
182
|
|
|
{ |
183
|
|
|
Floats::filter('1. 0'); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @test |
188
|
|
|
* @covers ::filter |
189
|
|
|
* @expectedException \TraderInteractive\Filter\Exception |
190
|
|
|
* @expectedExceptionMessage 1 0 does not pass is_numeric |
191
|
|
|
*/ |
192
|
|
|
public function filterRogueSpaceStringBetweenDigits() |
193
|
|
|
{ |
194
|
|
|
Floats::filter('1 0'); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @test |
199
|
|
|
* @covers ::filter |
200
|
|
|
* @expectedException \TraderInteractive\Filter\Exception |
201
|
|
|
* @expectedExceptionMessage 1e999999999999 overflow |
202
|
|
|
*/ |
203
|
|
|
public function filterOverflow() |
204
|
|
|
{ |
205
|
|
|
Floats::filter('1e999999999999'); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @test |
210
|
|
|
* @covers ::filter |
211
|
|
|
* @expectedException \TraderInteractive\Filter\Exception |
212
|
|
|
* @expectedExceptionMessage -1e999999999999 overflow |
213
|
|
|
*/ |
214
|
|
|
public function filterUnderflow() |
215
|
|
|
{ |
216
|
|
|
Floats::filter('-1e999999999999'); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @test |
221
|
|
|
* @covers ::filter |
222
|
|
|
* @expectedException \TraderInteractive\Filter\Exception |
223
|
|
|
* @expectedExceptionMessage -1 is less than 0 |
224
|
|
|
*/ |
225
|
|
|
public function filterLessThanMin() |
226
|
|
|
{ |
227
|
|
|
Floats::filter(-1.0, false, 0.0); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @test |
232
|
|
|
* @covers ::filter |
233
|
|
|
*/ |
234
|
|
|
public function filterEqualToMin() |
235
|
|
|
{ |
236
|
|
|
$this->assertSame(0.0, Floats::filter(0.0, false, 0.0)); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* @test |
241
|
|
|
* @covers ::filter |
242
|
|
|
* @expectedException \TraderInteractive\Filter\Exception |
243
|
|
|
* @expectedExceptionMessage 1 is greater than 0 |
244
|
|
|
*/ |
245
|
|
|
public function filterGreaterThanMax() |
246
|
|
|
{ |
247
|
|
|
Floats::filter(1.0, false, null, 0.0); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @test |
252
|
|
|
* @covers ::filter |
253
|
|
|
*/ |
254
|
|
|
public function filterEqualToMax() |
255
|
|
|
{ |
256
|
|
|
$this->assertSame(0.0, Floats::filter(0.0, false, null, 0.0)); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* @test |
261
|
|
|
* @covers ::filter |
262
|
|
|
*/ |
263
|
|
|
public function filterCastInts() |
264
|
|
|
{ |
265
|
|
|
$this->assertSame(1.0, Floats::filter(1, false, null, null, true)); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* @test |
270
|
|
|
* @covers ::filter |
271
|
|
|
* @expectedException \TraderInteractive\Filter\Exception |
272
|
|
|
* @expectedExceptionMessage "1" $value is not a string |
273
|
|
|
*/ |
274
|
|
|
public function filterCastIntsIsFalse() |
275
|
|
|
{ |
276
|
|
|
Floats::filter(1, false, null, null, false); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* @test |
281
|
|
|
* @covers ::filter |
282
|
|
|
* @expectedException \InvalidArgumentException |
283
|
|
|
* @expectedExceptionMessage "1" $castInts was not a bool |
284
|
|
|
*/ |
285
|
|
|
public function filterCastIntsIsNotBool() |
286
|
|
|
{ |
287
|
|
|
Floats::filter('1', false, null, null, 1); |
|
|
|
|
288
|
|
|
} |
289
|
|
|
} |
290
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: