1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license LGPLv3, https://opensource.org/licenses/LGPL-3.0 |
5
|
|
|
* @copyright Aimeos (aimeos.org), 2020-2025 |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
namespace Aimeos\Base; |
10
|
|
|
|
11
|
|
|
use Aimeos\Base\Str; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
class StrTest extends \PHPUnit\Framework\TestCase |
15
|
|
|
{ |
16
|
|
|
public function testAfter() |
17
|
|
|
{ |
18
|
|
|
$this->assertNull( Str::after( 'abc', '' ) ); |
19
|
|
|
$this->assertNull( Str::after( 'abc', 'x' ) ); |
20
|
|
|
|
21
|
|
|
$this->assertNull( Str::after( 'abc', null ) ); |
22
|
|
|
$this->assertNull( Str::after( null, 'x' ) ); |
23
|
|
|
|
24
|
|
|
$this->assertEquals( 3, Str::after( 123, 2 ) ); |
25
|
|
|
$this->assertEquals( 3, Str::after( 123, 12 ) ); |
26
|
|
|
|
27
|
|
|
$this->assertEquals( 'c', Str::after( 'abc', 'b' ) ); |
28
|
|
|
$this->assertEquals( 'c', Str::after( 'abc', 'ab' ) ); |
29
|
|
|
|
30
|
|
|
$this->assertEquals( 'こ', Str::after( 'はしこ', 'し' ) ); |
31
|
|
|
$this->assertEquals( 'こ', Str::after( 'はしこ', 'はし' ) ); |
32
|
|
|
|
33
|
|
|
$this->assertEquals( '池', Str::after( '他弛池', '弛' ) ); |
34
|
|
|
$this->assertEquals( '池', Str::after( '他弛池', '他弛' ) ); |
35
|
|
|
|
36
|
|
|
$this->assertEquals( 'c', Str::after( 'abc', ['b', 'c'] ) ); |
37
|
|
|
$this->assertEquals( '', Str::after( 'abc', ['c', 'b'] ) ); |
38
|
|
|
$this->assertNull( Str::after( 'abc', ['x'] ) ); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
public function testBefore() |
43
|
|
|
{ |
44
|
|
|
$this->assertNull( Str::before( 'abc', '' ) ); |
45
|
|
|
$this->assertNull( Str::before( 'abc', 'x' ) ); |
46
|
|
|
|
47
|
|
|
$this->assertNull( Str::before( 'abc', null ) ); |
48
|
|
|
$this->assertNull( Str::before( null, 'x' ) ); |
49
|
|
|
|
50
|
|
|
$this->assertEquals( 1, Str::before( 123, 2 ) ); |
51
|
|
|
$this->assertEquals( 1, Str::before( 123, 23 ) ); |
52
|
|
|
|
53
|
|
|
$this->assertEquals( 'a', Str::before( 'abc', 'b' ) ); |
54
|
|
|
$this->assertEquals( 'a', Str::before( 'abc', 'bc' ) ); |
55
|
|
|
|
56
|
|
|
$this->assertEquals( 'は', Str::before( 'はしこ', 'し' ) ); |
57
|
|
|
$this->assertEquals( 'は', Str::before( 'はしこ', 'しこ' ) ); |
58
|
|
|
|
59
|
|
|
$this->assertEquals( '他', Str::before( '他弛池', '弛' ) ); |
60
|
|
|
$this->assertEquals( '他', Str::before( '他弛池', '弛池' ) ); |
61
|
|
|
|
62
|
|
|
$this->assertEquals( 'a', Str::before( 'abc', ['b', 'c'] ) ); |
63
|
|
|
$this->assertEquals( 'ab', Str::before( 'abc', ['c', 'b'] ) ); |
64
|
|
|
$this->assertNull( Str::before( 'abc', ['x'] ) ); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
public function testDecode() |
69
|
|
|
{ |
70
|
|
|
$str = '<html onclick="alert(`xss`, 'yes')">'; |
71
|
|
|
$this->assertEquals( '<html onclick="alert(`xss`, \'yes\')">', Str::decode( $str ) ); |
72
|
|
|
$this->assertEquals( '123', Str::decode( 123 ) ); |
73
|
|
|
$this->assertEquals( '', Str::decode( null ) ); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
public function testEnds() |
78
|
|
|
{ |
79
|
|
|
$this->assertFalse( Str::ends( 'abc', '' ) ); |
80
|
|
|
$this->assertFalse( Str::ends( 'abc', 'a' ) ); |
81
|
|
|
$this->assertFalse( Str::ends( 'abc', 'ab' ) ); |
82
|
|
|
|
83
|
|
|
$this->assertFalse( Str::ends( 'abc', 'x' ) ); |
84
|
|
|
$this->assertFalse( Str::ends( 'abc', ['x', 'y'] ) ); |
85
|
|
|
|
86
|
|
|
$this->assertFalse( Str::ends( 'abc', null ) ); |
87
|
|
|
$this->assertFalse( Str::ends( null, 'x' ) ); |
88
|
|
|
|
89
|
|
|
$this->assertTrue( Str::ends( 123, 3 ) ); |
90
|
|
|
$this->assertTrue( Str::ends( 123, 23 ) ); |
91
|
|
|
|
92
|
|
|
$this->assertTrue( Str::ends( 'abc', ['c', 'x'] ) ); |
93
|
|
|
$this->assertTrue( Str::ends( 'abc', 'bc' ) ); |
94
|
|
|
$this->assertTrue( Str::ends( 'abc', 'c' ) ); |
95
|
|
|
|
96
|
|
|
$this->assertTrue( Str::ends( 'はしこ', 'しこ' ) ); |
97
|
|
|
$this->assertTrue( Str::ends( 'はしこ', 'こ' ) ); |
98
|
|
|
|
99
|
|
|
$this->assertTrue( Str::ends( '他弛池', '弛池' ) ); |
100
|
|
|
$this->assertTrue( Str::ends( '他弛池', '池' ) ); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
public function testHtml() |
105
|
|
|
{ |
106
|
|
|
$this->assertEquals( '<html>', Str::html( '<html>' ) ); |
107
|
|
|
$this->assertEquals( '123', Str::html( 123 ) ); |
108
|
|
|
$this->assertEquals( '', Str::html( null ) ); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
|
112
|
|
|
public function testIn() |
113
|
|
|
{ |
114
|
|
|
$this->assertFalse( Str::in( 'abc', '' ) ); |
115
|
|
|
$this->assertFalse( Str::in( 'abc', 'ax' ) ); |
116
|
|
|
$this->assertFalse( Str::in( 'abc', ['x'] ) ); |
117
|
|
|
|
118
|
|
|
$this->assertFalse( Str::in( 'abc', null ) ); |
119
|
|
|
$this->assertFalse( Str::in( null, 'x' ) ); |
120
|
|
|
|
121
|
|
|
$this->assertTrue( Str::in( 123, 2 ) ); |
122
|
|
|
$this->assertTrue( Str::in( 123, [1, 3] ) ); |
123
|
|
|
|
124
|
|
|
$this->assertTrue( Str::in( 'abc', 'a' ) ); |
125
|
|
|
$this->assertTrue( Str::in( 'abc', ['a', 'c'] ) ); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
|
129
|
|
|
public function testSlug() |
130
|
|
|
{ |
131
|
|
|
$this->assertEquals( 'a_b_c', Str::slug( 'a/b&c', 'en', '_' ) ); |
132
|
|
|
$this->assertEquals( 'ae-oe-ue_ss', Str::slug( 'Ä/ö&ü_ß', 'de' ) ); |
133
|
|
|
$this->assertEquals( 'a-o-u', Str::slug( 'ä/ö&ü' ) ); |
134
|
|
|
$this->assertEquals( 'a-b-c', Str::slug( 'A/b&c' ) ); |
135
|
|
|
$this->assertEquals( 'a-c', Str::slug( 'a-&-c' ) ); |
136
|
|
|
$this->assertEquals( 'a~b', Str::slug( 'a~b' ) ); |
137
|
|
|
$this->assertEquals( '123', Str::slug( 123 ) ); |
138
|
|
|
$this->assertEquals( '', Str::slug( null ) ); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
|
142
|
|
|
public function testSlugCustom() |
143
|
|
|
{ |
144
|
|
|
\Aimeos\Base\Str::macro( 'slug', function( $str, $lang, $sep ) { |
145
|
|
|
return strtolower( $str ); |
146
|
|
|
} ); |
147
|
|
|
|
148
|
|
|
$this->assertEquals( 'abc', Str::slug( 'ABC' ) ); |
149
|
|
|
|
150
|
|
|
\Aimeos\Base\Str::macro( 'slug', function( $str, $lang, $sep ) { |
151
|
|
|
$str = \voku\helper\ASCII::to_ascii( (string) $str, $lang ); |
152
|
|
|
return trim( preg_replace( '/[^A-Za-z0-9_]+/', $sep, $str ), $sep ); |
153
|
|
|
} ); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
|
157
|
|
|
public function testSome() |
158
|
|
|
{ |
159
|
|
|
$this->assertFalse( Str::some( null, [''] ) ); |
160
|
|
|
$this->assertFalse( Str::some( 'abc', [''] ) ); |
161
|
|
|
|
162
|
|
|
$this->assertFalse( Str::some( 'abc', ['ax'] ) ); |
163
|
|
|
$this->assertFalse( Str::some( 'abc', ['x', 'y'] ) ); |
164
|
|
|
|
165
|
|
|
$this->assertTrue( Str::some( 123, [2] ) ); |
166
|
|
|
$this->assertTrue( Str::some( 123, [1, 3] ) ); |
167
|
|
|
|
168
|
|
|
$this->assertTrue( Str::some( 'abc', ['a'] ) ); |
169
|
|
|
$this->assertTrue( Str::some( 'abc', ['a', 'c'] ) ); |
170
|
|
|
$this->assertTrue( Str::some( 'abc', ['a', 'x'] ) ); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
|
174
|
|
|
public function testStarts() |
175
|
|
|
{ |
176
|
|
|
$this->assertFalse( Str::starts( 'abc', '' ) ); |
177
|
|
|
$this->assertFalse( Str::starts( 'abc', 'c' ) ); |
178
|
|
|
$this->assertFalse( Str::starts( 'abc', 'bc' ) ); |
179
|
|
|
|
180
|
|
|
$this->assertFalse( Str::starts( 'abc', 'x' ) ); |
181
|
|
|
$this->assertFalse( Str::starts( 'abc', ['x', 'y'] ) ); |
182
|
|
|
|
183
|
|
|
$this->assertFalse( Str::starts( 'abc', null ) ); |
184
|
|
|
$this->assertFalse( Str::starts( null, 'c' ) ); |
185
|
|
|
|
186
|
|
|
$this->assertTrue( Str::starts( 123, 12 ) ); |
187
|
|
|
$this->assertTrue( Str::starts( 123, 1 ) ); |
188
|
|
|
|
189
|
|
|
$this->assertTrue( Str::starts( 'abc', ['a', 'x'] ) ); |
190
|
|
|
$this->assertTrue( Str::starts( 'abc', 'ab' ) ); |
191
|
|
|
$this->assertTrue( Str::starts( 'abc', 'a' ) ); |
192
|
|
|
|
193
|
|
|
$this->assertTrue( Str::starts( 'はしこ', 'はし' ) ); |
194
|
|
|
$this->assertTrue( Str::starts( 'はしこ', 'は' ) ); |
195
|
|
|
|
196
|
|
|
$this->assertTrue( Str::starts( '他弛池', '他弛' ) ); |
197
|
|
|
$this->assertTrue( Str::starts( '他弛池', '他' ) ); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
|
201
|
|
|
public function testStrtime() |
202
|
|
|
{ |
203
|
|
|
$this->assertEquals( './order_' . date( 'Y-m-d_H:i:s' ) . '.xml', Str::strtime( './order_%Y-%m-%d_%H:%M:%S.xml' ) ); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
|
207
|
|
|
public function testUid() |
208
|
|
|
{ |
209
|
|
|
$r = Str::uid(); |
210
|
|
|
|
211
|
|
|
$this->assertNotEquals( $r, Str::uid() ); |
212
|
|
|
$this->assertEquals( 20, strlen( $r ) ); |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|