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