1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* @copyright (c) 2020 Mendel <[email protected]> |
4
|
|
|
* @license see license.txt |
5
|
|
|
*/ |
6
|
|
|
namespace drycart\data\tests; |
7
|
|
|
use drycart\data\StrHelper; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @author mendel |
11
|
|
|
*/ |
12
|
|
|
class StrHelperTest extends \PHPUnit\Framework\TestCase |
13
|
|
|
{ |
14
|
|
View Code Duplication |
public function testContain() |
|
|
|
|
15
|
|
|
{ |
16
|
|
|
$this->assertTrue(StrHelper::contain('qwertyuiop', 'rty')); |
17
|
|
|
$this->assertFalse(StrHelper::contain('qwertyuiop', '123')); |
18
|
|
|
// |
19
|
|
|
$this->assertTrue(StrHelper::contain('qwerTyuiop', 'rty', false)); |
20
|
|
|
$this->assertFalse(StrHelper::contain('qwerTyuiop', 'rty')); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
View Code Duplication |
public function testStart() |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
$this->assertTrue(StrHelper::start('qwertyuiop', 'qwe')); |
26
|
|
|
$this->assertFalse(StrHelper::start('qwertyuiop', '123')); |
27
|
|
|
// |
28
|
|
|
$this->assertTrue(StrHelper::start('qWertyuiop', 'qwe', false)); |
29
|
|
|
$this->assertFalse(StrHelper::start('qWertyuiop', 'qwe')); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
View Code Duplication |
public function testEnd() |
|
|
|
|
33
|
|
|
{ |
34
|
|
|
$this->assertTrue(StrHelper::end('qwertyuiop', 'iop')); |
35
|
|
|
$this->assertFalse(StrHelper::end('qwertyuiop', '123')); |
36
|
|
|
// |
37
|
|
|
$this->assertTrue(StrHelper::end('qwertyuioP', 'iop', false)); |
38
|
|
|
$this->assertFalse(StrHelper::end('qwertyuioP', 'iop')); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
View Code Duplication |
public function testLike() |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
$this->assertTrue(StrHelper::like('qwertyuiop', '%iop')); |
44
|
|
|
$this->assertFalse(StrHelper::like('qwertyuiop', '%123%')); |
45
|
|
|
// |
46
|
|
|
$this->assertTrue(StrHelper::like('qwertyuioP', '%iop', false)); |
47
|
|
|
$this->assertFalse(StrHelper::like('qwertyuioP', '%iop')); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testRemovePrefix() |
51
|
|
|
{ |
52
|
|
|
$this->assertEquals(StrHelper::removePrefix('qwertyuiop', 'qwerty'), 'uiop'); |
53
|
|
|
$this->assertEquals(StrHelper::removePrefix('qwertyuiop', 'uiop'), 'qwertyuiop'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function testFindPrefix() |
57
|
|
|
{ |
58
|
|
|
$this->assertEquals( |
59
|
|
|
StrHelper::findPrefix('qwertyuiop', ['qwerty', 'uiop', '123']), |
60
|
|
|
['qwerty', 'uiop'] |
61
|
|
|
); |
62
|
|
|
$this->assertEquals( |
63
|
|
|
StrHelper::findPrefix('Xsqwertyuiop', ['qwerty', 'uiop', '123']), |
64
|
|
|
[null, 'Xsqwertyuiop'] |
65
|
|
|
); |
66
|
|
|
$this->assertEquals( |
67
|
|
|
StrHelper::findPrefix('Xsqwertyuiop', ['qwerty', 'uiop', '123'], 'default'), |
68
|
|
|
['default', 'Xsqwertyuiop'] |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testStr2int() |
73
|
|
|
{ |
74
|
|
|
$this->assertEquals(StrHelper::str2int('qwertyuiop'), 2731992073887769); |
75
|
|
|
$this->assertNull(StrHelper::str2int('qwertyuiopasdfghjklzxcvbnm')); |
76
|
|
|
$this->assertNull(StrHelper::str2int('qwertyuiop@')); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testInt2str() |
80
|
|
|
{ |
81
|
|
|
$this->assertEquals(StrHelper::int2str(2731992073887769), 'qwertyuiop'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testCamelCase2underscore() |
85
|
|
|
{ |
86
|
|
|
$this->assertEquals(StrHelper::camelCase2underscore('camelCaseKey'), 'camel_case_key'); |
87
|
|
|
$this->assertEquals(StrHelper::camelCase2underscore('CamelCaseKey'), 'camel_case_key'); |
88
|
|
|
$this->assertEquals(StrHelper::camelCase2underscore('underscoupe_string'), 'underscoupe_string'); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testUnderscore2camelCase() |
92
|
|
|
{ |
93
|
|
|
$this->assertEquals(StrHelper::underscore2camelCase('underscoupe_string'), 'underscoupeString'); |
94
|
|
|
$this->assertEquals(StrHelper::underscore2camelCase('camelCaseKey'), 'camelCaseKey'); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function testKey2Title() |
98
|
|
|
{ |
99
|
|
|
$this->assertEquals(StrHelper::key2Label('forum.admins.count()'), 'Forum admins count'); |
100
|
|
|
$this->assertEquals(StrHelper::key2Label('forum_admins.Count()'), 'Forum admins count'); |
101
|
|
|
$this->assertEquals(StrHelper::key2Label('ForumAdminsCount'), 'Forum admins count'); |
102
|
|
|
$this->assertEquals(StrHelper::key2Label('Some_strangeDataKey'), 'Some strange data key'); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function testParseDocComment() |
106
|
|
|
{ |
107
|
|
|
$doc = implode("\n", [ |
108
|
|
|
'/**', |
109
|
|
|
' * Some text', |
110
|
|
|
' * @return int', |
111
|
|
|
'*/' |
112
|
|
|
]); |
113
|
|
|
$this->assertEquals(StrHelper::parseDocComment($doc), ['Some text','@return int']); |
114
|
|
|
$this->assertEquals(StrHelper::parseDocComment('* qwerty'), []); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function testToString() |
118
|
|
|
{ |
119
|
|
|
$this->assertEquals( |
120
|
|
|
StrHelper::templateToString('/article/{type}/{slug}',['type'=>'news','slug'=>'test-template']), |
121
|
|
|
'/article/news/test-template' |
122
|
|
|
); |
123
|
|
|
$this->assertEquals( |
124
|
|
|
StrHelper::templateToString('/article/{article.type}/{article.slug}',['article'=>['type'=>'news','slug'=>'test-template']]), |
125
|
|
|
'/article/news/test-template' |
126
|
|
|
); |
127
|
|
|
$this->assertEquals( |
128
|
|
|
StrHelper::templateToString('/article/{article.type}/{article.slug}',(object)['article'=>['type'=>'news','slug'=>'test-template']]), |
129
|
|
|
'/article/news/test-template' |
130
|
|
|
); |
131
|
|
|
} |
132
|
|
|
public function testFromString() |
133
|
|
|
{ |
134
|
|
|
$this->assertEquals( |
135
|
|
|
StrHelper::templateFromString('/article/{type}/{slug}','/article/news/test-template'), |
136
|
|
|
['type'=>'news','slug'=>'test-template'] |
137
|
|
|
); |
138
|
|
|
$this->assertNull( |
139
|
|
|
StrHelper::templateFromString('/article/{type}/{slug}','/admin/news/test-template') |
140
|
|
|
); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
} |
144
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.