StringsTest::formatKeyOrderDoesNotMatter()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace TraderInteractive\Util;
4
5
use TraderInteractive\Util\Strings as S;
6
use PHPUnit\Framework\TestCase;
7
8
/**
9
 * @coversDefaultClass \TraderInteractive\Util\Strings
10
 * @covers ::<private>
11
 */
12
final class StringsTest extends TestCase
13
{
14
    /**
15
     * Verify bahavior of format() with argument cannot be casted to a string.
16
     *
17
     * @test
18
     * @covers ::format
19
     *
20
     * @return void
21
     */
22
    public function formatNonStringCastableObject()
23
    {
24
        $this->expectException(\TypeError::class);
25
        S::format('{0} and {1}', new \StdClass(), 'Jill');
0 ignored issues
show
Bug introduced by
new StdClass() of type StdClass is incompatible with the type string expected by parameter $arguments of TraderInteractive\Util\Strings::format(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

25
        S::format('{0} and {1}', /** @scrutinizer ignore-type */ new \StdClass(), 'Jill');
Loading history...
26
    }
27
28
    /**
29
     * Verify bahavior of format() with object argument casted to a string.
30
     *
31
     * @test
32
     * @covers ::format
33
     *
34
     * @return void
35
     */
36
    public function formatStringCastableObject()
37
    {
38
        $e = new \Exception();
39
        $this->assertSame(
40
            "Exception {$e} was thrown",
41
            S::format('Exception {0} was thrown', $e)
42
        );
43
    }
44
45
    /**
46
     * Verify bahavior of format() with repeated key.
47
     *
48
     * @test
49
     * @covers ::format
50
     *
51
     * @return void
52
     */
53
    public function formatKeysAreRepeatable()
54
    {
55
        $this->assertSame('AAA', S::format('{0}{0}{0}', 'A'));
56
    }
57
58
    /**
59
     * Verify bahavior of format() with repeated unordered keys.
60
     *
61
     * @test
62
     * @covers ::format
63
     *
64
     * @return void
65
     */
66
    public function formatKeyOrderDoesNotMatter()
67
    {
68
        $this->assertSame('ABC', S::format('{2}{1}{0}', 'C', 'B', 'A'));
69
    }
70
71
    /**
72
     * Verify bahavior of format() with non-string $format.
73
     *
74
     * @test
75
     * @covers ::format
76
     *
77
     * @return void
78
     */
79
    public function formatNonStringFormat()
80
    {
81
        $this->expectException(\TypeError::class);
82
        S::format([], 'C', 'B', 'A');
0 ignored issues
show
Bug introduced by
array() of type array is incompatible with the type string expected by parameter $format of TraderInteractive\Util\Strings::format(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

82
        S::format(/** @scrutinizer ignore-type */ [], 'C', 'B', 'A');
Loading history...
83
    }
84
85
    /**
86
     * @test
87
     * @covers ::endsWith
88
     *
89
     * @return void
90
     */
91
    public function endsWithEmptyString()
92
    {
93
        $nonSuffix = null;
94
        $this->assertFalse(S::endsWith('', 'suffix', $nonSuffix));
95
        $this->assertSame('', $nonSuffix);
96
    }
97
98
    /**
99
     * Verify matching bahavior of endsWith().
100
     *
101
     * @test
102
     * @covers ::endsWith
103
     *
104
     * @return void
105
     */
106
    public function endsWithMatches()
107
    {
108
        $nonSuffix = null;
109
        $this->assertTrue(S::endsWith('bah', 'h', $nonSuffix));
110
        $this->assertSame('ba', $nonSuffix);
111
    }
112
113
    /**
114
     * Verify non-matching bahavior of endsWith().
115
     *
116
     * @test
117
     * @covers ::endsWith
118
     *
119
     * @return void
120
     */
121
    public function endsWithNoMatches()
122
    {
123
        $nonSuffix = null;
124
        $this->assertFalse(S::endsWith('bah', 'z', $nonSuffix));
125
        $this->assertSame('bah', $nonSuffix);
126
    }
127
128
    /**
129
     * Verify non-matching bahavior of endsWith().
130
     *
131
     * @test
132
     * @covers ::endsWith
133
     *
134
     * @return void
135
     */
136
    public function endsWithBadTypeForSubject()
137
    {
138
        $this->expectException(\TypeError::class);
139
        S::endsWith(new \StdClass(), '');
0 ignored issues
show
Bug introduced by
new StdClass() of type StdClass is incompatible with the type string expected by parameter $string of TraderInteractive\Util\Strings::endsWith(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

139
        S::endsWith(/** @scrutinizer ignore-type */ new \StdClass(), '');
Loading history...
140
    }
141
142
    /**
143
     * Verify behavior of endsWith() with non-string $suffix.
144
     *
145
     * @test
146
     * @covers ::endsWith
147
     *
148
     * @return void
149
     */
150
    public function endsWithBadTypeForSuffix()
151
    {
152
        $this->expectException(\TypeError::class);
153
        S::endsWith('', new \StdClass());
0 ignored issues
show
Bug introduced by
new StdClass() of type StdClass is incompatible with the type string expected by parameter $suffix of TraderInteractive\Util\Strings::endsWith(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

153
        S::endsWith('', /** @scrutinizer ignore-type */ new \StdClass());
Loading history...
154
    }
155
156
    /**
157
     * Verify behavior of endsWith() with all arguments as empty strings.
158
     *
159
     * @test
160
     * @covers ::endsWith
161
     *
162
     * @return void
163
     */
164
    public function endsWithEmptyBoth()
165
    {
166
        $nonSuffix = null;
167
        $this->assertTrue(S::endsWith('', '', $nonSuffix));
168
        $this->assertSame('', $nonSuffix);
169
    }
170
171
    /**
172
     * Verify behavior of endsWith() with empty string $suffix.
173
     *
174
     * @test
175
     * @covers ::endsWith
176
     *
177
     * @return void
178
     */
179
    public function endsWithEmptySuffix()
180
    {
181
        $nonSuffix = null;
182
        $this->assertTrue(S::endsWith('a', '', $nonSuffix));
183
        $this->assertSame('a', $nonSuffix);
184
    }
185
186
    /**
187
     * Verify behavior of endsWith() with empty string $subject.
188
     *
189
     * @test
190
     * @covers ::endsWith
191
     *
192
     * @return void
193
     */
194
    public function endsWithEmptySubject()
195
    {
196
        $nonSuffix = null;
197
        $this->assertFalse(S::endsWith('', 'b', $nonSuffix));
198
        $this->assertSame('', $nonSuffix);
199
    }
200
201
    /**
202
     * Verify basic behavior of ellipsize().
203
     *
204
     * @test
205
     * @covers ::ellipsize
206
     *
207
     * @return void
208
     */
209
    public function ellipsize()
210
    {
211
        $input = 'Short text is an arbitrary thing.';
212
        $this->assertSame('', S::ellipsize($input, 0));
213
        $this->assertSame('.', S::ellipsize($input, 1));
214
        $this->assertSame('...', S::ellipsize($input, 3));
215
        $this->assertSame('S...', S::ellipsize($input, 4));
216
        $this->assertSame('Short text...', S::ellipsize($input, 13));
217
        $this->assertSame('Short text is an arbitrary th...', S::ellipsize($input, 32));
218
        $this->assertSame($input, S::ellipsize($input, 33));
219
        $this->assertSame($input, S::ellipsize($input, 34));
220
        $this->assertSame($input, S::ellipsize($input, 35));
221
        $this->assertSame($input, S::ellipsize($input, 50));
222
    }
223
224
    /**
225
     * Verify behavior of ellipsize() with negative max length.
226
     *
227
     * @test
228
     * @covers ::ellipsize
229
     *
230
     * @return void
231
     */
232
    public function ellipsizeNegativeMaxLength()
233
    {
234
        $this->expectException(\InvalidArgumentException::class);
235
        $this->expectExceptionMessage('$maxLength is negative');
236
        S::ellipsize('foo', -1);
237
    }
238
239
    /**
240
     * Tests that ellipsize works with a custom suffix.
241
     *
242
     * @test
243
     * @covers ::ellipsize
244
     *
245
     * @return void
246
     */
247
    public function ellipsizeCustomSuffix()
248
    {
249
        $this->assertSame('Test!', S::ellipsize('Testing', 5, '!'));
250
    }
251
252
    /**
253
     * Tests that ellipsize fails with an integer instead of a string.
254
     *
255
     * @test
256
     * @covers ::ellipsize
257
     *
258
     * @return void
259
     */
260
    public function ellipsizeIntegerInsteadOfString()
261
    {
262
        $this->expectException(\TypeError::class);
263
        S::ellipsize(null, 10);
0 ignored issues
show
Bug introduced by
null of type null is incompatible with the type string expected by parameter $string of TraderInteractive\Util\Strings::ellipsize(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

263
        S::ellipsize(/** @scrutinizer ignore-type */ null, 10);
Loading history...
264
    }
265
266
    /**
267
     * Tests that ellipsize fails with a string for $maxLength.
268
     *
269
     * @test
270
     * @covers ::ellipsize
271
     *
272
     * @return void
273
     */
274
    public function ellipsizeStringMaxLength()
275
    {
276
        $this->expectException(\TypeError::class);
277
        S::ellipsize('test', 'a');
0 ignored issues
show
Bug introduced by
'a' of type string is incompatible with the type integer expected by parameter $maxLength of TraderInteractive\Util\Strings::ellipsize(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

277
        S::ellipsize('test', /** @scrutinizer ignore-type */ 'a');
Loading history...
278
    }
279
280
    /**
281
     * @test
282
     * @covers ::ellipsize
283
     *
284
     * @return void
285
     */
286
    public function ellipsizeNonStringSuffix()
287
    {
288
        $this->expectException(\TypeError::class);
289
        S::ellipsize('test', 10, new \StdClass());
0 ignored issues
show
Bug introduced by
new StdClass() of type StdClass is incompatible with the type string expected by parameter $suffix of TraderInteractive\Util\Strings::ellipsize(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

289
        S::ellipsize('test', 10, /** @scrutinizer ignore-type */ new \StdClass());
Loading history...
290
    }
291
}
292