|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Signify\SecurityChecker\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
|
6
|
|
|
use Signify\SecurityChecker\StringUtil; |
|
7
|
|
|
|
|
8
|
|
|
final class StringUtilTest extends TestCase |
|
9
|
|
|
{ |
|
10
|
|
|
public const TEST_STRING = 'this is a string to test'; |
|
11
|
|
|
|
|
12
|
|
|
public function testStartsWith(): void |
|
13
|
|
|
{ |
|
14
|
|
|
// Single string comparison. |
|
15
|
|
|
$result = StringUtil::startsWith(self::TEST_STRING, 'this '); |
|
16
|
|
|
$this->assertTrue($result); |
|
17
|
|
|
|
|
18
|
|
|
// Compare with two strings that match. |
|
19
|
|
|
$result = StringUtil::startsWith(self::TEST_STRING, ['this ', 'this is']); |
|
20
|
|
|
$this->assertTrue($result); |
|
21
|
|
|
|
|
22
|
|
|
// Compare with one string that matches, and one that doesn't. |
|
23
|
|
|
$result = StringUtil::startsWith(self::TEST_STRING, ['not start', 'thi']); |
|
24
|
|
|
$this->assertTrue($result); |
|
25
|
|
|
|
|
26
|
|
|
// Same as above but checking that order doesn't matter. |
|
27
|
|
|
$result = StringUtil::startsWith(self::TEST_STRING, ['this is', 'not start']); |
|
28
|
|
|
$this->assertTrue($result); |
|
29
|
|
|
|
|
30
|
|
|
// Test empty comparator. |
|
31
|
|
|
$result = StringUtil::startsWith(self::TEST_STRING, ''); |
|
32
|
|
|
$this->assertTrue($result); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function testDoesntStartWith(): void |
|
36
|
|
|
{ |
|
37
|
|
|
// Single string comparison. |
|
38
|
|
|
$result = StringUtil::startsWith(self::TEST_STRING, 'not the start'); |
|
39
|
|
|
$this->assertFalse($result); |
|
40
|
|
|
|
|
41
|
|
|
// Check case sensitivity. |
|
42
|
|
|
$result = StringUtil::startsWith(self::TEST_STRING, 'This'); |
|
43
|
|
|
$this->assertFalse($result); |
|
44
|
|
|
|
|
45
|
|
|
// Compare with multiple strings that don't match. |
|
46
|
|
|
$result = StringUtil::startsWith(self::TEST_STRING, ['This', 'something else']); |
|
47
|
|
|
$this->assertFalse($result); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function testEndsWith(): void |
|
51
|
|
|
{ |
|
52
|
|
|
// Single string comparison. |
|
53
|
|
|
$result = StringUtil::endsWith(self::TEST_STRING, 'test'); |
|
54
|
|
|
$this->assertTrue($result); |
|
55
|
|
|
|
|
56
|
|
|
// Compare with two strings that match. |
|
57
|
|
|
$result = StringUtil::endsWith(self::TEST_STRING, ['test', 'g to test']); |
|
58
|
|
|
$this->assertTrue($result); |
|
59
|
|
|
|
|
60
|
|
|
// Compare with one string that matches, and one that doesn't. |
|
61
|
|
|
$result = StringUtil::endsWith(self::TEST_STRING, ['not end', 'est']); |
|
62
|
|
|
$this->assertTrue($result); |
|
63
|
|
|
|
|
64
|
|
|
// Same as above but checking that order doesn't matter. |
|
65
|
|
|
$result = StringUtil::endsWith(self::TEST_STRING, ['to test', 'not end']); |
|
66
|
|
|
$this->assertTrue($result); |
|
67
|
|
|
|
|
68
|
|
|
// Test empty comparator. |
|
69
|
|
|
$result = StringUtil::endsWith(self::TEST_STRING, ''); |
|
70
|
|
|
$this->assertTrue($result); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function testDoesntEndWith(): void |
|
74
|
|
|
{ |
|
75
|
|
|
// Single string comparison. |
|
76
|
|
|
$result = StringUtil::endsWith(self::TEST_STRING, 'not the end'); |
|
77
|
|
|
$this->assertFalse($result); |
|
78
|
|
|
|
|
79
|
|
|
// Check case sensitivity. |
|
80
|
|
|
$result = StringUtil::endsWith(self::TEST_STRING, 'Test'); |
|
81
|
|
|
$this->assertFalse($result); |
|
82
|
|
|
|
|
83
|
|
|
// Compare with multiple strings that don't match. |
|
84
|
|
|
$result = StringUtil::endsWith(self::TEST_STRING, ['Test', 'something else']); |
|
85
|
|
|
$this->assertFalse($result); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function testRemoveFromStart(): void |
|
89
|
|
|
{ |
|
90
|
|
|
// Remove a single string. |
|
91
|
|
|
$result = StringUtil::removeFromStart(self::TEST_STRING, 'this is'); |
|
92
|
|
|
$this->assertSame(' a string to test', $result); |
|
93
|
|
|
|
|
94
|
|
|
// Remove multiple strings in succession. |
|
95
|
|
|
$result = StringUtil::removeFromStart(self::TEST_STRING, ['this is', ' a string ']); |
|
96
|
|
|
$this->assertSame('to test', $result); |
|
97
|
|
|
|
|
98
|
|
|
// Check that the strings are removed in order. |
|
99
|
|
|
$result = StringUtil::removeFromStart(self::TEST_STRING, ['this is', 'this is a ']); |
|
100
|
|
|
$this->assertSame(' a string to test', $result); |
|
101
|
|
|
|
|
102
|
|
|
// Check that only strings that fully match are removed (no removal by strlen if no match). |
|
103
|
|
|
$result = StringUtil::removeFromStart(self::TEST_STRING, ['this match', 'this ']); |
|
104
|
|
|
$this->assertSame('is a string to test', $result); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function testDontRemoveFromStart(): void |
|
108
|
|
|
{ |
|
109
|
|
|
// Don't remove a single string when it doesn't match. |
|
110
|
|
|
$result = StringUtil::removeFromStart(self::TEST_STRING, 'to test'); |
|
111
|
|
|
$this->assertSame(self::TEST_STRING, $result); |
|
112
|
|
|
|
|
113
|
|
|
// Don't remove any strings when they don't match. |
|
114
|
|
|
$result = StringUtil::removeFromStart(self::TEST_STRING, ['to test', ' a string ']); |
|
115
|
|
|
$this->assertSame(self::TEST_STRING, $result); |
|
116
|
|
|
|
|
117
|
|
|
// Make sure nothing weird happens with empty string comparators. |
|
118
|
|
|
$result = StringUtil::removeFromStart(self::TEST_STRING, ''); |
|
119
|
|
|
$this->assertSame(self::TEST_STRING, $result); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function testRemoveFromEnd(): void |
|
123
|
|
|
{ |
|
124
|
|
|
// Remove a single string. |
|
125
|
|
|
$result = StringUtil::removeFromEnd(self::TEST_STRING, 'to test'); |
|
126
|
|
|
$this->assertSame('this is a string ', $result); |
|
127
|
|
|
|
|
128
|
|
|
// Remove multiple strings in succession. |
|
129
|
|
|
$result = StringUtil::removeFromEnd(self::TEST_STRING, ['to test', ' a string ']); |
|
130
|
|
|
$this->assertSame('this is', $result); |
|
131
|
|
|
|
|
132
|
|
|
// Check that the strings are removed in order. |
|
133
|
|
|
$result = StringUtil::removeFromEnd(self::TEST_STRING, ['to test', 'string to test']); |
|
134
|
|
|
$this->assertSame('this is a string ', $result); |
|
135
|
|
|
|
|
136
|
|
|
// Check that only strings that fully match are removed (no removal by strlen if no match). |
|
137
|
|
|
$result = StringUtil::removeFromEnd(self::TEST_STRING, ['match test', ' test']); |
|
138
|
|
|
$this->assertSame('this is a string to', $result); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
public function testDontRemoveFromEnd(): void |
|
142
|
|
|
{ |
|
143
|
|
|
// Don't remove a single string when it doesn't match. |
|
144
|
|
|
$result = StringUtil::removeFromEnd(self::TEST_STRING, 'this is'); |
|
145
|
|
|
$this->assertSame(self::TEST_STRING, $result); |
|
146
|
|
|
|
|
147
|
|
|
// Don't remove any strings when they don't match. |
|
148
|
|
|
$result = StringUtil::removeFromEnd(self::TEST_STRING, ['this is', ' a string ']); |
|
149
|
|
|
$this->assertSame(self::TEST_STRING, $result); |
|
150
|
|
|
|
|
151
|
|
|
// Make sure nothing weird happens with empty string comparators. |
|
152
|
|
|
$result = StringUtil::removeFromEnd(self::TEST_STRING, ''); |
|
153
|
|
|
$this->assertSame(self::TEST_STRING, $result); |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|