1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TraderInteractive\Filter; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @coversDefaultClass \TraderInteractive\Filter\Ints |
9
|
|
|
* @covers ::<private> |
10
|
|
|
*/ |
11
|
|
|
final class IntsTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @test |
15
|
|
|
* @covers ::filter |
16
|
|
|
*/ |
17
|
|
|
public function filterAllowNullIsTrueAndNullValue() |
18
|
|
|
{ |
19
|
|
|
$result = Ints::filter(null, true); |
20
|
|
|
$this->assertSame(null, $result); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @test |
25
|
|
|
* @covers ::filter |
26
|
|
|
*/ |
27
|
|
|
public function filterPositiveInt() |
28
|
|
|
{ |
29
|
|
|
$this->assertSame(123, Ints::filter(123)); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @test |
34
|
|
|
* @covers ::filter |
35
|
|
|
*/ |
36
|
|
|
public function filterNegativeInt() |
37
|
|
|
{ |
38
|
|
|
$this->assertSame(-123, Ints::filter(-123)); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @test |
43
|
|
|
* @covers ::filter |
44
|
|
|
*/ |
45
|
|
|
public function filterZeroInt() |
46
|
|
|
{ |
47
|
|
|
$positiveZero = + 0; |
48
|
|
|
$this->assertSame(0, Ints::filter($positiveZero)); |
49
|
|
|
$this->assertSame(0, Ints::filter(-0)); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @test |
54
|
|
|
* @covers ::filter |
55
|
|
|
*/ |
56
|
|
|
public function filterPositiveString() |
57
|
|
|
{ |
58
|
|
|
$this->assertSame(123, Ints::filter(' 123 ')); |
59
|
|
|
$this->assertSame(123, Ints::filter(' +123 ')); |
60
|
|
|
$this->assertSame(0, Ints::filter(' +0 ')); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @test |
65
|
|
|
* @covers ::filter |
66
|
|
|
*/ |
67
|
|
|
public function filterNegativeString() |
68
|
|
|
{ |
69
|
|
|
$this->assertSame(-123, Ints::filter(' -123 ')); |
70
|
|
|
$this->assertSame(0, Ints::filter(' -0 ')); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @test |
75
|
|
|
* @covers ::filter |
76
|
|
|
*/ |
77
|
|
|
public function filterNonStringOrInt() |
78
|
|
|
{ |
79
|
|
|
$this->expectException(\TraderInteractive\Exceptions\FilterException::class); |
80
|
|
|
$this->expectExceptionMessage('"true" $value is not a string'); |
81
|
|
|
Ints::filter(true); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @test |
86
|
|
|
* @covers ::filter |
87
|
|
|
*/ |
88
|
|
|
public function filterEmptyString() |
89
|
|
|
{ |
90
|
|
|
$this->expectException(\TraderInteractive\Exceptions\FilterException::class); |
91
|
|
|
$this->expectExceptionMessage('$value string length is zero'); |
92
|
|
|
Ints::filter(''); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @test |
97
|
|
|
* @covers ::filter |
98
|
|
|
*/ |
99
|
|
|
public function filterWhitespaceString() |
100
|
|
|
{ |
101
|
|
|
$this->expectException(\TraderInteractive\Exceptions\FilterException::class); |
102
|
|
|
$this->expectExceptionMessage('$value string length is zero'); |
103
|
|
|
Ints::filter(' '); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @test |
108
|
|
|
* @covers ::filter |
109
|
|
|
*/ |
110
|
|
|
public function nonDigitString() |
111
|
|
|
{ |
112
|
|
|
try { |
113
|
|
|
Ints::filter('123.4'); |
114
|
|
|
$this->fail("No exception thrown"); |
115
|
|
|
} catch (\Throwable $e) { |
116
|
|
|
$this->assertSame( |
117
|
|
|
"123.4 does not contain all digits, optionally prepended by a '+' or '-' and optionally surrounded by " |
118
|
|
|
. "whitespace", |
119
|
|
|
$e->getMessage() |
120
|
|
|
); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @test |
126
|
|
|
* @covers ::filter |
127
|
|
|
*/ |
128
|
|
|
public function filterGreaterThanPhpIntMax() |
129
|
|
|
{ |
130
|
|
|
$this->expectException(\TraderInteractive\Exceptions\FilterException::class); |
131
|
|
|
//32, 64 and 128 bit and their +1 's |
132
|
|
|
$maxes = [ |
133
|
|
|
'2147483647' => '2147483648', |
134
|
|
|
'9223372036854775807' => '9223372036854775808', |
135
|
|
|
'170141183460469231731687303715884105727' => '170141183460469231731687303715884105728', |
136
|
|
|
]; |
137
|
|
|
$oneOverMax = $maxes[(string)PHP_INT_MAX]; |
138
|
|
|
Ints::filter($oneOverMax); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @test |
143
|
|
|
* @covers ::filter |
144
|
|
|
*/ |
145
|
|
|
public function filterLessThanPhpIntMin() |
146
|
|
|
{ |
147
|
|
|
$this->expectException(\TraderInteractive\Exceptions\FilterException::class); |
148
|
|
|
//32, 64 and 128 bit and their -1 's |
149
|
|
|
$mins = [ |
150
|
|
|
'-2147483648' => '-2147483649', |
151
|
|
|
'-9223372036854775808' => '-9223372036854775809', |
152
|
|
|
'-170141183460469231731687303715884105728' => '-170141183460469231731687303715884105729', |
153
|
|
|
]; |
154
|
|
|
$oneUnderMin = $mins[(string)~PHP_INT_MAX]; |
155
|
|
|
Ints::filter($oneUnderMin); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @test |
160
|
|
|
* @covers ::filter |
161
|
|
|
*/ |
162
|
|
|
public function filterLessThanMin() |
163
|
|
|
{ |
164
|
|
|
$this->expectException(\TraderInteractive\Exceptions\FilterException::class); |
165
|
|
|
$this->expectExceptionMessage('-1 is less than 0'); |
166
|
|
|
Ints::filter(-1, false, 0); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @test |
171
|
|
|
* @covers ::filter |
172
|
|
|
*/ |
173
|
|
|
public function filterEqualToMin() |
174
|
|
|
{ |
175
|
|
|
$this->assertSame(0, Ints::filter(0, false, 0)); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @test |
180
|
|
|
* @covers ::filter |
181
|
|
|
*/ |
182
|
|
|
public function filterGreaterThanMax() |
183
|
|
|
{ |
184
|
|
|
$this->expectException(\TraderInteractive\Exceptions\FilterException::class); |
185
|
|
|
$this->expectExceptionMessage('1 is greater than 0'); |
186
|
|
|
Ints::filter(1, false, null, 0); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @test |
191
|
|
|
* @covers ::filter |
192
|
|
|
*/ |
193
|
|
|
public function filterEqualToMax() |
194
|
|
|
{ |
195
|
|
|
$this->assertSame(0, Ints::filter(0, false, null, 0)); |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|