Passed
Branch develop (bae466)
by Paul
06:12
created

StrTest   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 193
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 23
eloc 102
dl 0
loc 193
rs 10
c 0
b 0
f 0

23 Methods

Rating   Name   Duplication   Size   Complexity  
A test_starts_with() 0 7 1
A test_convert_name_to_path() 0 12 1
A test_restrict_to() 0 4 1
A test_replace_first() 0 5 1
A test_convert_path_to_name() 0 8 1
A test_random() 0 4 1
A test_dash_case() 0 4 1
A test_mask() 0 13 1
A test_hash() 0 8 1
A test_ends_with() 0 6 1
A test_replace_last() 0 5 1
A test_natural_join() 0 6 1
A test_prefix() 0 3 1
A test_convert_name_to_id() 0 12 1
A test_contains() 0 7 1
A test_snake_case() 0 5 1
A test_suffix() 0 6 1
A test_truncate() 0 6 1
A test_remove_prefix() 0 4 1
A test_wp_case() 0 6 1
A test_join() 0 6 1
A test_fallback() 0 6 1
A test_camel_case() 0 4 1
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Tests;
4
5
use GeminiLabs\SiteReviews\Helpers\Str;
6
use WP_UnitTestCase;
0 ignored issues
show
Bug introduced by
The type WP_UnitTestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
8
/**
9
 * Test case for the Plugin.
10
 * @group plugin
11
 */
12
class StrTest extends WP_UnitTestCase
13
{
14
    public function test_camel_case()
15
    {
16
        $this->assertEquals(Str::camelCase('a-b_cDE'), 'ABCDE');
17
        $this->assertEquals(Str::camelCase('aaa-bbb_cde'), 'AaaBbbCde');
18
    }
19
20
    public function test_contains()
21
    {
22
        $this->assertTrue(Str::contains('abcdef', ['cd']));
23
        $this->assertTrue(Str::contains('abcdef', ['abcdef']));
24
        $this->assertFalse(Str::contains('abcdef', ['']));
25
        $this->assertFalse(Str::contains('abcdef', ['z']));
26
        $this->assertFalse(Str::contains('abcdef', []));
27
    }
28
29
    public function test_convert_name_to_id()
30
    {
31
        $this->assertEquals(Str::convertNameToId('a--b'), 'a-b');
32
        $this->assertEquals(Str::convertNameToId('a_b_c'), 'a_b_c');
33
        $this->assertEquals(Str::convertNameToId('a.b.c'), 'a-b-c');
34
        $this->assertEquals(Str::convertNameToId('a[b][c]'), 'a-b-c');
35
        $this->assertEquals(Str::convertNameToId('a[b][c][]'), 'a-b-c');
36
        $this->assertEquals(Str::convertNameToId('b.c', 'a'), 'a-b-c');
37
        $this->assertEquals(Str::convertNameToId('b[c]', 'a'), 'a-b-c');
38
        $this->assertEquals(Str::convertNameToId('b[c][]', 'a'), 'a-b-c');
39
        $this->assertEquals(Str::convertNameToId('[b][c][]', 'a'), 'a-b-c');
40
        $this->assertEquals(Str::convertNameToId('[b][c-d]', 'a'), 'a-b-c-d');
41
    }
42
43
    public function test_convert_name_to_path()
44
    {
45
        $this->assertEquals(Str::convertNameToPath('a-b'), 'a-b');
46
        $this->assertEquals(Str::convertNameToPath('a_b_c'), 'a_b_c');
47
        $this->assertEquals(Str::convertNameToPath('a.b.c'), 'a.b.c');
48
        $this->assertEquals(Str::convertNameToPath('a[b][c]'), 'a.b.c');
49
        $this->assertEquals(Str::convertNameToPath('a[b][c][]'), 'a.b.c');
50
        $this->assertEquals(Str::convertNameToPath('b.c'), 'b.c');
51
        $this->assertEquals(Str::convertNameToPath('b[c]'), 'b.c');
52
        $this->assertEquals(Str::convertNameToPath('b[c][]'), 'b.c');
53
        $this->assertEquals(Str::convertNameToPath('[b][c][]'), 'b.c');
54
        $this->assertEquals(Str::convertNameToPath('[b][c-d]'), 'b.c-d');
55
    }
56
57
    public function test_convert_path_to_name()
58
    {
59
        $this->assertEquals(Str::convertPathToName('a'), 'a');
60
        $this->assertEquals(Str::convertPathToName('a.b.c'), 'a[b][c]');
61
        $this->assertEquals(Str::convertPathToName('b.c', 'a'), 'a[b][c]');
62
        $this->assertEquals(Str::convertPathToName('b.c.', 'a'), 'a[b][c]');
63
        $this->assertEquals(Str::convertPathToName('.b.c', 'a'), 'a[b][c]');
64
        $this->assertEquals(Str::convertPathToName('.b.c.', 'a'), 'a[b][c]');
65
    }
66
67
    public function test_dash_case()
68
    {
69
        $this->assertEquals(Str::dashCase('a-b_cDE'), 'a-b-c-d-e');
70
        $this->assertEquals(Str::dashCase('GeminiLabs\SiteReviews\Helpers\Str'), 'gemini-labs\site-reviews\helpers\str');
71
    }
72
73
    public function test_ends_with()
74
    {
75
        $this->assertTrue(Str::endsWith('abcdefg', ['efg']));
76
        $this->assertFalse(Str::endsWith('ABCDEFG', ['efg']));
77
        $this->assertFalse(Str::endsWith('ABCDEFG', ['']));
78
        $this->assertFalse(Str::endsWith('ABCDEFG', []));
79
    }
80
81
    public function test_fallback()
82
    {
83
        $this->assertEquals(Str::fallback('1', '2'), '1');
84
        $this->assertEquals(Str::fallback('', '2'), '2');
85
        $this->assertEquals(Str::fallback(1, '2'), '1');
86
        $this->assertEquals(Str::fallback([], '2'), '2');
87
    }
88
89
    public function test_hash()
90
    {
91
        require_once ABSPATH.WPINC.'/pluggable.php';
92
        $hash = wp_hash('123', 'nonce');
93
        $this->assertEquals(Str::hash('123'), $hash);
94
        $this->assertEquals(Str::hash('123', 0), substr($hash, 0, 8));
95
        $this->assertEquals(Str::hash('123', 8), substr($hash, 0, 8));
96
        $this->assertEquals(Str::hash('123', 12), substr($hash, 0, 12));
97
    }
98
99
    public function test_join()
100
    {
101
        $this->assertEquals(Str::join(['1']), '1');
102
        $this->assertEquals(Str::join(['1'], true), "'1'");
103
        $this->assertEquals(Str::join(['1', '2']), '1, 2');
104
        $this->assertEquals(Str::join(['1', '2'], true), "'1','2'");
105
    }
106
107
    public function test_mask()
108
    {
109
        $string = 'abcdefghijklmnopqrstuvwxyz';
110
        $this->assertEquals(Str::mask($string), '*************');
111
        $this->assertEquals(Str::mask($string, 4), 'abcd*********');
112
        $this->assertEquals(Str::mask($string, 0, 4), '*********wxyz');
113
        $this->assertEquals(Str::mask($string, 4, 4), 'abcd*****wxyz');
114
        $this->assertEquals(Str::mask($string, 4, 4, 20), 'abcd************wxyz');
115
        $this->assertEquals(Str::mask($string, 4, 4, 2), $string);
116
        $this->assertEquals(Str::mask($string, 20, 20), $string);
117
        $this->assertEquals(Str::mask($string, 40, 0), $string);
118
        $this->assertEquals(Str::mask($string, 0, 40), $string);
119
        $this->assertEquals(Str::mask($string, -10, 40), $string);
120
    }
121
122
    public function test_natural_join()
123
    {
124
        $this->assertEquals(Str::naturalJoin(['1']), '1');
125
        $this->assertEquals(Str::naturalJoin(['1', '2']), '1 and 2');
126
        $this->assertEquals(Str::naturalJoin(['1', '2', '3']), '1, 2 and 3');
127
        $this->assertEquals(Str::naturalJoin(['1', '2', '3', '4']), '1, 2, 3 and 4');
128
    }
129
130
    public function test_prefix()
131
    {
132
        $this->assertEquals(Str::prefix(' bob ', 'hello_'), 'hello_bob');
133
    }
134
135
    public function test_random()
136
    {
137
        $this->assertEquals(strlen(Str::random(4)), 4);
138
        $this->assertEquals(strlen(Str::random(8)), 8);
139
    }
140
141
    public function test_remove_prefix()
142
    {
143
        $this->assertEquals(Str::removePrefix('_abc', '_'), 'abc');
144
        $this->assertEquals(Str::removePrefix('_abc', ''), '_abc');
145
    }
146
147
    public function test_replace_first()
148
    {
149
        $this->assertEquals(Str::replaceFirst('', 'xyz', 'abcabc'), 'abcabc');
150
        $this->assertEquals(Str::replaceFirst('zzx', 'xyz', 'abcabc'), 'abcabc');
151
        $this->assertEquals(Str::replaceFirst('abc', 'xyz', 'abcabc'), 'xyzabc');
152
    }
153
154
    public function test_replace_last()
155
    {
156
        $this->assertEquals(Str::replaceLast('', 'xyz', 'abcabc'), 'abcabc');
157
        $this->assertEquals(Str::replaceLast('abc', 'xyz', 'abcabc'), 'abcxyz');
158
        $this->assertEquals(Str::replaceLast('zzz', 'xyz', 'abcabc'), 'abcabc');
159
    }
160
161
    public function test_restrict_to()
162
    {
163
        $this->assertEquals(Str::restrictTo('asc,desc,', 'ASC', 'DESC'), 'ASC');
164
        $this->assertEquals(Str::restrictTo('asc,desc,', 'ASC', 'DESC', true), 'DESC');
165
    }
166
167
    public function test_snake_case()
168
    {
169
        $this->assertEquals(Str::snakeCase('a-b_cDE'), 'a_b_c_d_e');
170
        $this->assertEquals(Str::snakeCase('GeminiLabs\SiteReviews\Helpers\Str'), 'gemini_labs\site_reviews\helpers\str');
171
        $this->assertEquals(Str::snakeCase('_GeminiLabs\_SiteReviews'), '_gemini_labs\_site_reviews');
172
    }
173
174
    public function test_starts_with()
175
    {
176
        $this->assertTrue(Str::startsWith('abcdefg', ['abc']));
177
        $this->assertTrue(Str::startsWith('defg', ['abc', 'def']));
178
        $this->assertFalse(Str::startsWith('ABCDEFG', ['abc']));
179
        $this->assertFalse(Str::startsWith('ABCDEFG', ['']));
180
        $this->assertFalse(Str::startsWith('ABCDEFG', []));
181
    }
182
183
    public function test_suffix()
184
    {
185
        $this->assertEquals(Str::suffix('bob', '_goodbye'), 'bob_goodbye');
186
        $this->assertEquals(Str::suffix('bob_goodbye', '_goodbye'), 'bob_goodbye');
187
        $this->assertEquals(Str::suffix(' bob ', '_goodbye'), ' bob _goodbye');
188
        $this->assertEquals(Str::suffix(' bob ', ' _goodbye '), ' bob  _goodbye ');
189
    }
190
191
    public function test_truncate()
192
    {
193
        $this->assertEquals(Str::truncate('abc', 1), 'a');
194
        $this->assertEquals(Str::truncate('abc', 2), 'ab');
195
        $this->assertEquals(Str::truncate('abc', 3), 'abc');
196
        $this->assertEquals(Str::truncate('abc', 4), 'abc');
197
    }
198
199
    public function test_wp_case()
200
    {
201
        $this->assertEquals(Str::wpCase('a-b_cde'), 'A_B_Cde');
202
        $this->assertEquals(Str::wpCase('a-b_cDE'), 'A_B_C_D_E');
203
        $this->assertEquals(Str::wpCase('a-b_cdE'), 'A_B_Cd_E');
204
        $this->assertEquals(Str::wpCase('aaa-bbb_cde'), 'Aaa_Bbb_Cde');
205
    }
206
}
207