Passed
Push — master ( c8b0fe...42afb4 )
by Aimeos
05:38
created

StrTest::testStrtime()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2020-2021
6
 */
7
8
9
namespace Aimeos\MW;
10
11
use Aimeos\MW\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 testEnds()
69
	{
70
		$this->assertFalse( Str::ends( 'abc', '' ) );
71
		$this->assertFalse( Str::ends( 'abc', 'a' ) );
72
		$this->assertFalse( Str::ends( 'abc', 'ab' ) );
73
74
		$this->assertFalse( Str::ends( 'abc', 'x' ) );
75
		$this->assertFalse( Str::ends( 'abc', ['x', 'y'] ) );
76
77
		$this->assertFalse( Str::ends( 'abc', null ) );
78
		$this->assertFalse( Str::ends( null, 'x' ) );
79
80
		$this->assertTrue( Str::ends( 123, 3 ) );
81
		$this->assertTrue( Str::ends( 123, 23 ) );
82
83
		$this->assertTrue( Str::ends( 'abc', ['c', 'x'] ) );
84
		$this->assertTrue( Str::ends( 'abc', 'bc' ) );
85
		$this->assertTrue( Str::ends( 'abc', 'c' ) );
86
87
		$this->assertTrue( Str::ends( 'はしこ', 'しこ' ) );
88
		$this->assertTrue( Str::ends( 'はしこ', 'こ' ) );
89
90
		$this->assertTrue( Str::ends( '他弛池', '弛池' ) );
91
		$this->assertTrue( Str::ends( '他弛池', '池' ) );
92
	}
93
94
95
	public function testHtml()
96
	{
97
		$this->assertEquals( '&lt;html&gt;', Str::html( '<html>' ) );
98
		$this->assertEquals( '123', Str::html( 123 ) );
99
		$this->assertEquals( '', Str::html( null ) );
100
	}
101
102
103
	public function testIn()
104
	{
105
		$this->assertFalse( Str::in( 'abc', '' ) );
106
		$this->assertFalse( Str::in( 'abc', 'ax' ) );
107
		$this->assertFalse( Str::in( 'abc', ['x'] ) );
108
109
		$this->assertFalse( Str::in( 'abc', null ) );
110
		$this->assertFalse( Str::in( null, 'x' ) );
111
112
		$this->assertTrue( Str::in( 123, 2 ) );
113
		$this->assertTrue( Str::in( 123, [1, 3] ) );
114
115
		$this->assertTrue( Str::in( 'abc', 'a' ) );
116
		$this->assertTrue( Str::in( 'abc', ['a', 'c'] ) );
117
	}
118
119
120
	public function testSlug()
121
	{
122
		$this->assertEquals( 'a_b_c', Str::slug( 'a/b&c', 'en', '_' ) );
123
		$this->assertEquals( 'Ae-oe-ue', Str::slug( 'Ä/ö&ü', 'de' ) );
124
		$this->assertEquals( 'a-o-u', Str::slug( 'ä/ö&ü' ) );
125
		$this->assertEquals( 'a-b-c', Str::slug( 'a/b&c' ) );
126
		$this->assertEquals( 'a~b', Str::slug( 'a~b' ) );
127
		$this->assertEquals( '123', Str::slug( 123 ) );
128
		$this->assertEquals( '', Str::slug( null ) );
129
	}
130
131
132
	public function testSlugCustom()
133
	{
134
		\Aimeos\MW\Str::macro( 'slug', function( $str, $lang, $sep ) {
135
			return strtolower( $str );
136
		} );
137
138
		$this->assertEquals( 'abc', Str::slug( 'ABC' ) );
139
140
		\Aimeos\MW\Str::macro( 'slug', function( $str, $lang, $sep ) {
141
			$str = \voku\helper\ASCII::to_ascii( (string) $str, $lang );
142
			return trim( preg_replace( '/[^A-Za-z0-9]+/', $sep, $str ), $sep );
143
		} );
144
	}
145
146
147
	public function testSome()
148
	{
149
		$this->assertFalse( Str::some( null, [''] ) );
150
		$this->assertFalse( Str::some( 'abc', [''] ) );
151
152
		$this->assertFalse( Str::some( 'abc', ['ax'] ) );
153
		$this->assertFalse( Str::some( 'abc', ['x', 'y'] ) );
154
155
		$this->assertTrue( Str::some( 123, [2] ) );
156
		$this->assertTrue( Str::some( 123, [1, 3] ) );
157
158
		$this->assertTrue( Str::some( 'abc', ['a'] ) );
159
		$this->assertTrue( Str::some( 'abc', ['a', 'c'] ) );
160
		$this->assertTrue( Str::some( 'abc', ['a', 'x'] ) );
161
	}
162
163
164
	public function testStarts()
165
	{
166
		$this->assertFalse( Str::starts( 'abc', '' ) );
167
		$this->assertFalse( Str::starts( 'abc', 'c' ) );
168
		$this->assertFalse( Str::starts( 'abc', 'bc' ) );
169
170
		$this->assertFalse( Str::starts( 'abc', 'x' ) );
171
		$this->assertFalse( Str::starts( 'abc', ['x', 'y'] ) );
172
173
		$this->assertFalse( Str::starts( 'abc', null ) );
174
		$this->assertFalse( Str::starts( null, 'c' ) );
175
176
		$this->assertTrue( Str::starts( 123, 12 ) );
177
		$this->assertTrue( Str::starts( 123, 1 ) );
178
179
		$this->assertTrue( Str::starts( 'abc', ['a', 'x'] ) );
180
		$this->assertTrue( Str::starts( 'abc', 'ab' ) );
181
		$this->assertTrue( Str::starts( 'abc', 'a' ) );
182
183
		$this->assertTrue( Str::starts( 'はしこ', 'はし' ) );
184
		$this->assertTrue( Str::starts( 'はしこ', 'は' ) );
185
186
		$this->assertTrue( Str::starts( '他弛池', '他弛' ) );
187
		$this->assertTrue( Str::starts( '他弛池', '他' ) );
188
	}
189
190
191
	public function testStrtime()
192
	{
193
		$this->assertEquals( './order_' . date( 'Y-m-d_H:i:s' ) . '.xml', Str::strtime( './order_%Y-%m-%d_%H:%i:%s.xml' ) );
194
	}
195
196
197
	public function testUid()
198
	{
199
		$r = Str::uid();
200
201
		$this->assertNotEquals( $r, Str::uid() );
202
		$this->assertEquals( 20, strlen( $r ) );
203
	}
204
}
205