1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TraderInteractive\Filter; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @coversDefaultClass \TraderInteractive\Filter\Strings |
10
|
|
|
*/ |
11
|
|
|
final class StringsTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Verify basic use of filter |
15
|
|
|
* |
16
|
|
|
* @test |
17
|
|
|
* @covers ::filter |
18
|
|
|
* @dataProvider filterData |
19
|
|
|
* |
20
|
|
|
* @param mixed $input The input. |
21
|
|
|
* @param mixed $expected The expected value(s). |
22
|
|
|
* |
23
|
|
|
* @return void |
24
|
|
|
* @throws Exception |
25
|
|
|
*/ |
26
|
|
|
public function filter($input, $expected) |
27
|
|
|
{ |
28
|
|
|
$this->assertSame($expected, Strings::filter($input)); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Data provider for basic filter tests |
33
|
|
|
* |
34
|
|
|
* @return array |
35
|
|
|
*/ |
36
|
|
|
public function filterData() |
37
|
|
|
{ |
38
|
|
|
return [ |
39
|
|
|
'string' => ['abc', 'abc'], |
40
|
|
|
'int' => [1, '1'], |
41
|
|
|
'float' => [1.1, '1.1'], |
42
|
|
|
'bool' => [true, '1'], |
43
|
|
|
'object' => [new \SplFileInfo(__FILE__), __FILE__], |
44
|
|
|
]; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @test |
49
|
|
|
* @covers ::filter |
50
|
|
|
*/ |
51
|
|
|
public function filterNullPass() |
52
|
|
|
{ |
53
|
|
|
$this->assertSame(null, Strings::filter(null, true)); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @test |
58
|
|
|
* @expectedException \TraderInteractive\Filter\Exception |
59
|
|
|
* @expectedExceptionMessage Value 'NULL' is not a string |
60
|
|
|
* @covers ::filter |
61
|
|
|
*/ |
62
|
|
|
public function filterNullFail() |
63
|
|
|
{ |
64
|
|
|
Strings::filter(null); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @test |
69
|
|
|
* @covers ::filter |
70
|
|
|
*/ |
71
|
|
|
public function filterMinLengthPass() |
72
|
|
|
{ |
73
|
|
|
$this->assertSame('a', Strings::filter('a')); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @test |
78
|
|
|
* @expectedException \TraderInteractive\Filter\Exception |
79
|
|
|
* @covers ::filter |
80
|
|
|
*/ |
81
|
|
|
public function filterMinLengthFail() |
82
|
|
|
{ |
83
|
|
|
Strings::filter(''); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @test |
88
|
|
|
* @covers ::filter |
89
|
|
|
*/ |
90
|
|
|
public function filterMaxLengthPass() |
91
|
|
|
{ |
92
|
|
|
$this->assertSame('a', Strings::filter('a', false, 0, 1)); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @test |
97
|
|
|
* @expectedException \TraderInteractive\Filter\Exception |
98
|
|
|
* @expectedExceptionMessage Value 'a' with length '1' is less than '0' or greater than '0' |
99
|
|
|
* @covers ::filter |
100
|
|
|
*/ |
101
|
|
|
public function filterMaxLengthFail() |
102
|
|
|
{ |
103
|
|
|
Strings::filter('a', false, 0, 0); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @test |
108
|
|
|
* @expectedException InvalidArgumentException |
109
|
|
|
* @expectedExceptionMessage $minLength was not a positive integer value |
110
|
|
|
* @covers ::filter |
111
|
|
|
*/ |
112
|
|
|
public function filterMinLengthNotInteger() |
113
|
|
|
{ |
114
|
|
|
Strings::filter('a', false, -1); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @test |
119
|
|
|
* @expectedException InvalidArgumentException |
120
|
|
|
* @expectedExceptionMessage $maxLength was not a positive integer value |
121
|
|
|
* @covers ::filter |
122
|
|
|
*/ |
123
|
|
|
public function filterMaxLengthNotInteger() |
124
|
|
|
{ |
125
|
|
|
Strings::filter('a', false, 1, -1); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @test |
130
|
|
|
* @expectedException InvalidArgumentException |
131
|
|
|
* @expectedExceptionMessage $minLength was not a positive integer value |
132
|
|
|
* @covers ::filter |
133
|
|
|
*/ |
134
|
|
|
public function filterMinLengthNegative() |
135
|
|
|
{ |
136
|
|
|
Strings::filter('a', false, -1); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @test |
141
|
|
|
* @expectedException InvalidArgumentException |
142
|
|
|
* @expectedExceptionMessage $maxLength was not a positive integer value |
143
|
|
|
* @covers ::filter |
144
|
|
|
*/ |
145
|
|
|
public function filterMaxLengthNegative() |
146
|
|
|
{ |
147
|
|
|
Strings::filter('a', false, 1, -1); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Verifies basic explode functionality. |
152
|
|
|
* |
153
|
|
|
* @test |
154
|
|
|
* @covers ::explode |
155
|
|
|
*/ |
156
|
|
|
public function explode() |
157
|
|
|
{ |
158
|
|
|
$this->assertSame(['a', 'bcd', 'e'], Strings::explode('a,bcd,e')); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Verifies explode with a custom delimiter. |
163
|
|
|
* |
164
|
|
|
* @test |
165
|
|
|
* @covers ::explode |
166
|
|
|
*/ |
167
|
|
|
public function explodeCustomDelimiter() |
168
|
|
|
{ |
169
|
|
|
$this->assertSame(['a', 'b', 'c', 'd,e'], Strings::explode('a b c d,e', ' ')); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Verifies explode filter with an empty delimiter. |
174
|
|
|
* |
175
|
|
|
* @test |
176
|
|
|
* @expectedException \InvalidArgumentException |
177
|
|
|
* @expectedExceptionMessage Delimiter '''' is not a non-empty string |
178
|
|
|
* @covers ::explode |
179
|
|
|
*/ |
180
|
|
|
public function explodeEmptyDelimiter() |
181
|
|
|
{ |
182
|
|
|
Strings::explode('test', ''); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|