1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Tests\Unit\Support; |
6
|
|
|
|
7
|
|
|
use Longman\LaravelLodash\Support\Str; |
8
|
|
|
use stdClass; |
9
|
|
|
use Tests\Unit\TestCase; |
10
|
|
|
|
11
|
|
|
use function mb_strlen; |
12
|
|
|
|
13
|
|
|
class StrTest extends TestCase |
|
|
|
|
14
|
|
|
{ |
15
|
|
|
/** @test */ |
16
|
|
|
public function add_zeros(): void |
17
|
|
|
{ |
18
|
|
|
$string = '12345678'; |
19
|
|
|
|
20
|
|
|
$this->assertSame('0012345678', Str::addZeros($string, 10, 'left')); |
21
|
|
|
$this->assertSame('1234567800', Str::addZeros($string, 10, 'right')); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** @test */ |
25
|
|
|
public function format_balance(): void |
26
|
|
|
{ |
27
|
|
|
$int = 12345; |
28
|
|
|
|
29
|
|
|
$this->assertSame('123.45', Str::formatBalance($int, 2)); |
30
|
|
|
$this->assertSame('123.450', Str::formatBalance($int, 3)); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** @test */ |
34
|
|
|
public function snake_case_to_camel_case(): void |
35
|
|
|
{ |
36
|
|
|
$string = 'Lorem_ipsum_dolores'; |
37
|
|
|
$this->assertSame('LoremIpsumDolores', Str::snakeCaseToCamelCase($string)); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** @test */ |
41
|
|
|
public function camel_case_to_snake_case(): void |
42
|
|
|
{ |
43
|
|
|
$string = 'LoremIpsumDolores'; |
44
|
|
|
$this->assertSame('lorem_ipsum_dolores', Str::camelCaseToSnakeCase($string)); |
45
|
|
|
|
46
|
|
|
$string = 'არჩევანისგარემოსუზრუნველყოფისსისტემა'; |
47
|
|
|
$this->assertSame('არჩევანისგარემოსუზრუნველყოფისსისტემა', Str::camelCaseToSnakeCase($string)); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** @test */ |
51
|
|
|
public function convert_spaces_to_dashes(): void |
52
|
|
|
{ |
53
|
|
|
$string = 'Lorem Ipsum Dolores'; |
54
|
|
|
$this->assertSame('Lorem-Ipsum-Dolores', Str::convertSpacesToDashes($string)); |
55
|
|
|
|
56
|
|
|
$string = 'არჩევანის გარემოს უზრუნველყოფის სისტემა'; |
57
|
|
|
$this->assertSame('არჩევანის-გარემოს-უზრუნველყოფის-სისტემა', Str::convertSpacesToDashes($string)); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** @test */ |
61
|
|
|
public function limit_middle(): void |
62
|
|
|
{ |
63
|
|
|
$string = 'არჩევანის გარემოს უზრუნველყოფის სისტემა'; |
64
|
|
|
|
65
|
|
|
$this->assertSame('არჩევანის ...ის სისტემა', Str::limitMiddle($string, 20, '...')); |
66
|
|
|
$this->assertSame(23, mb_strlen(Str::limitMiddle($string, 20, '...'))); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** @test */ |
70
|
|
|
public function hash(): void |
71
|
|
|
{ |
72
|
|
|
$data = ['aa' => 1, 'bb' => 2, 'cc' => 3]; |
73
|
|
|
$this->assertSame('899a999da95e9f021fc63c6af006933fd4dc3aa1', Str::hash($data)); |
74
|
|
|
|
75
|
|
|
$data = new stdClass(); |
76
|
|
|
$data->aaa = 1; |
77
|
|
|
$data->bbb = 2; |
78
|
|
|
$data->ccc = 3; |
79
|
|
|
$this->assertSame('41d162b72eab4e7cfb6bb853d651fbaa2ae0573b', Str::hash($data)); |
80
|
|
|
|
81
|
|
|
$data = null; |
82
|
|
|
$this->assertSame('eef19c54306daa69eda49c0272623bdb5e2b341f', Str::hash($data)); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** @test */ |
86
|
|
View Code Duplication |
public function to_dot_notation(): void |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
$string = 'data[first][]'; |
89
|
|
|
$this->assertSame('data.first[]', Str::toDotNotation($string)); |
90
|
|
|
|
91
|
|
|
$string = 'data[first][second]'; |
92
|
|
|
$this->assertSame('data.first.second', Str::toDotNotation($string)); |
93
|
|
|
|
94
|
|
|
$string = 'data[first][second]third'; |
95
|
|
|
$this->assertSame('data.first.secondthird', Str::toDotNotation($string)); |
96
|
|
|
|
97
|
|
|
$string = 'data[first][second][0]'; |
98
|
|
|
$this->assertSame('data.first.second.0', Str::toDotNotation($string)); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** @test */ |
102
|
|
View Code Duplication |
public function convert_to_utf8(): void |
|
|
|
|
103
|
|
|
{ |
104
|
|
|
$data = 'hello žš, გამარჯობა'; |
105
|
|
|
$this->assertSame('hello žš, გამარჯობა', Str::convertToUtf8($data)); |
106
|
|
|
|
107
|
|
|
$data = 'Hírek'; |
108
|
|
|
$this->assertSame('Hírek', Str::convertToUtf8($data)); |
109
|
|
|
|
110
|
|
|
$data = 'H�rek'; |
111
|
|
|
$this->assertSame('H�rek', Str::convertToUtf8($data)); |
112
|
|
|
|
113
|
|
|
$data = "Fédération Camerounaise de Football\n"; |
114
|
|
|
$this->assertSame('Fédération Camerounaise de Football', Str::convertToUtf8($data)); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|