1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TraderInteractive\Filter; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
use TraderInteractive\Exceptions\FilterException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @coversDefaultClass \TraderInteractive\Filter\Strings |
11
|
|
|
*/ |
12
|
|
|
final class StringsTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Verify basic use of filter |
16
|
|
|
* |
17
|
|
|
* @test |
18
|
|
|
* @covers ::filter |
19
|
|
|
* @dataProvider filterData |
20
|
|
|
* |
21
|
|
|
* @param mixed $input The input. |
22
|
|
|
* @param mixed $expected The expected value(s). |
23
|
|
|
* |
24
|
|
|
* @return void |
25
|
|
|
* @throws FilterException |
26
|
|
|
*/ |
27
|
|
|
public function filter($input, $expected) |
28
|
|
|
{ |
29
|
|
|
$this->assertSame($expected, Strings::filter($input)); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Data provider for basic filter tests |
34
|
|
|
* |
35
|
|
|
* @return array |
36
|
|
|
*/ |
37
|
|
|
public function filterData() |
38
|
|
|
{ |
39
|
|
|
return [ |
40
|
|
|
'string' => ['abc', 'abc'], |
41
|
|
|
'int' => [1, '1'], |
42
|
|
|
'float' => [1.1, '1.1'], |
43
|
|
|
'bool' => [true, '1'], |
44
|
|
|
'object' => [new \SplFileInfo(__FILE__), __FILE__], |
45
|
|
|
]; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @test |
50
|
|
|
* @covers ::filter |
51
|
|
|
*/ |
52
|
|
|
public function filterNullPass() |
53
|
|
|
{ |
54
|
|
|
$this->assertNull(Strings::filter(null, true)); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @test |
59
|
|
|
* @expectedException \TraderInteractive\Exceptions\FilterException |
60
|
|
|
* @expectedExceptionMessage Value failed filtering, $allowNull is set to false |
61
|
|
|
* @covers ::filter |
62
|
|
|
*/ |
63
|
|
|
public function filterNullFail() |
64
|
|
|
{ |
65
|
|
|
Strings::filter(null); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @test |
70
|
|
|
* @covers ::filter |
71
|
|
|
*/ |
72
|
|
|
public function filterMinLengthPass() |
73
|
|
|
{ |
74
|
|
|
$this->assertSame('a', Strings::filter('a')); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @test |
79
|
|
|
* @expectedException \TraderInteractive\Exceptions\FilterException |
80
|
|
|
* @covers ::filter |
81
|
|
|
*/ |
82
|
|
|
public function filterMinLengthFail() |
83
|
|
|
{ |
84
|
|
|
Strings::filter(''); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @test |
89
|
|
|
* @covers ::filter |
90
|
|
|
*/ |
91
|
|
|
public function filterMaxLengthPass() |
92
|
|
|
{ |
93
|
|
|
$this->assertSame('a', Strings::filter('a', false, 0, 1)); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @test |
98
|
|
|
* @expectedException \TraderInteractive\Exceptions\FilterException |
99
|
|
|
* @expectedExceptionMessage Value 'a' with length '1' is less than '0' or greater than '0' |
100
|
|
|
* @covers ::filter |
101
|
|
|
*/ |
102
|
|
|
public function filterMaxLengthFail() |
103
|
|
|
{ |
104
|
|
|
Strings::filter('a', false, 0, 0); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @test |
109
|
|
|
* @expectedException InvalidArgumentException |
110
|
|
|
* @expectedExceptionMessage $minLength was not a positive integer value |
111
|
|
|
* @covers ::filter |
112
|
|
|
*/ |
113
|
|
|
public function filterMinLengthNotInteger() |
114
|
|
|
{ |
115
|
|
|
Strings::filter('a', false, -1); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @test |
120
|
|
|
* @expectedException InvalidArgumentException |
121
|
|
|
* @expectedExceptionMessage $maxLength was not a positive integer value |
122
|
|
|
* @covers ::filter |
123
|
|
|
*/ |
124
|
|
|
public function filterMaxLengthNotInteger() |
125
|
|
|
{ |
126
|
|
|
Strings::filter('a', false, 1, -1); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @test |
131
|
|
|
* @expectedException InvalidArgumentException |
132
|
|
|
* @expectedExceptionMessage $minLength was not a positive integer value |
133
|
|
|
* @covers ::filter |
134
|
|
|
*/ |
135
|
|
|
public function filterMinLengthNegative() |
136
|
|
|
{ |
137
|
|
|
Strings::filter('a', false, -1); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @test |
142
|
|
|
* @expectedException InvalidArgumentException |
143
|
|
|
* @expectedExceptionMessage $maxLength was not a positive integer value |
144
|
|
|
* @covers ::filter |
145
|
|
|
*/ |
146
|
|
|
public function filterMaxLengthNegative() |
147
|
|
|
{ |
148
|
|
|
Strings::filter('a', false, 1, -1); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @test |
153
|
|
|
* @covers ::filter |
154
|
|
|
*/ |
155
|
|
|
public function filterWithScalar() |
156
|
|
|
{ |
157
|
|
|
$this->assertSame('24141', Strings::filter(24141)); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @test |
162
|
|
|
* @covers ::filter |
163
|
|
|
*/ |
164
|
|
|
public function filterWithObject() |
165
|
|
|
{ |
166
|
|
|
$testObject = new class() { |
167
|
|
|
private $data; |
168
|
|
|
|
169
|
|
|
public function __construct() |
170
|
|
|
{ |
171
|
|
|
$this->data = [1,2,3,4,5]; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
public function __toString() |
175
|
|
|
{ |
176
|
|
|
return implode(',', $this->data); |
177
|
|
|
} |
178
|
|
|
}; |
179
|
|
|
|
180
|
|
|
$this->assertSame('1,2,3,4,5', Strings::filter(new $testObject)); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @test |
185
|
|
|
* @covers ::filter |
186
|
|
|
* |
187
|
|
|
* @expectedException \TraderInteractive\Exceptions\FilterException |
188
|
|
|
* @expectedExceptionMessage Value 'class@anonymous |
189
|
|
|
*/ |
190
|
|
|
public function filterWithObjectNoToStringMethod() |
191
|
|
|
{ |
192
|
|
|
$testObject = new class() { |
193
|
|
|
private $data; |
194
|
|
|
|
195
|
|
|
public function __construct() |
196
|
|
|
{ |
197
|
|
|
$this->data = [1, 2, 3, 4, 5]; |
198
|
|
|
} |
199
|
|
|
}; |
200
|
|
|
|
201
|
|
|
Strings::filter(new $testObject); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Verifies basic explode functionality. |
206
|
|
|
* |
207
|
|
|
* @test |
208
|
|
|
* @covers ::explode |
209
|
|
|
*/ |
210
|
|
|
public function explode() |
211
|
|
|
{ |
212
|
|
|
$this->assertSame(['a', 'bcd', 'e'], Strings::explode('a,bcd,e')); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Verifies explode with a custom delimiter. |
217
|
|
|
* |
218
|
|
|
* @test |
219
|
|
|
* @covers ::explode |
220
|
|
|
*/ |
221
|
|
|
public function explodeCustomDelimiter() |
222
|
|
|
{ |
223
|
|
|
$this->assertSame(['a', 'b', 'c', 'd,e'], Strings::explode('a b c d,e', ' ')); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @test |
228
|
|
|
* @expectedException \TraderInteractive\Exceptions\FilterException |
229
|
|
|
* @expectedExceptionMessage Value '1234' is not a string |
230
|
|
|
* @covers ::explode |
231
|
|
|
*/ |
232
|
|
|
public function explodeNonString() |
233
|
|
|
{ |
234
|
|
|
Strings::explode(1234, ''); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Verifies explode filter with an empty delimiter. |
239
|
|
|
* |
240
|
|
|
* @test |
241
|
|
|
* @expectedException \InvalidArgumentException |
242
|
|
|
* @expectedExceptionMessage Delimiter '''' is not a non-empty string |
243
|
|
|
* @covers ::explode |
244
|
|
|
*/ |
245
|
|
|
public function explodeEmptyDelimiter() |
246
|
|
|
{ |
247
|
|
|
Strings::explode('test', ''); |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
|