Completed
Push — master ( 2823b8...fb1f20 )
by Chad
19s
created

StringsTest.php$1 ➔ concatScalarValue()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
258
    }
259
260
    /**
261
     * @test
262
     * @covers ::stripTags
263
     */
264
    public function stripTagsRemoveHtmlFromString()
265
    {
266
        $actual = Strings::stripTags('A string with <p>paragraph</p> tags');
267
        $expected = 'A string with paragraph tags';
268
        $this->assertSame($expected, $actual);
269
    }
270
271
    /**
272
     * @test
273
     * @covers ::concat
274
     */
275
    public function concat()
276
    {
277
        $this->assertSame('prefixstringsuffix', Strings::concat('string', 'prefix', 'suffix'));
278
    }
279
280
    /**
281
     * Verify behavior of concat() when $value is not filterable
282
     *
283
     * @test
284
     * @covers ::concat
285
     * @expectedException \TraderInteractive\Exceptions\FilterException
286
     *
287
     * @return void
288
     */
289
    public function concatValueNotFilterable()
290
    {
291
        Strings::concat(new \StdClass(), 'prefix', 'suffix');
292
    }
293
294
    /**
295
     * @test
296
     * @covers ::concat
297
     */
298
    public function concatScalarValue()
299
    {
300
        $this->assertSame('prefix123suffix', Strings::concat(123, 'prefix', 'suffix'));
301
    }
302
303
    /**
304
     * @test
305
     * @covers ::concat
306
     */
307
    public function concatObjectValue()
308
    {
309
        $this->assertSame('prefix' . __FILE__ . 'suffix', Strings::concat(new \SplFileInfo(__FILE__), 'prefix', 'suffix'));
310
    }
311
}
312