1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Test; |
4
|
|
|
|
5
|
|
|
use const Spatie\CASE_LOWER; |
6
|
|
|
use const Spatie\CASE_UPPER; |
7
|
|
|
use const Spatie\CASE_SNAKE; |
8
|
|
|
use const Spatie\CASE_TITLE; |
9
|
|
|
use const Spatie\CASE_CAMEL; |
10
|
|
|
use const Spatie\CASE_PASCAL; |
11
|
|
|
use const Spatie\CASE_LISP; |
12
|
|
|
use function Spatie\array_change_key_case; |
13
|
|
|
use PHPUnit\Framework\TestCase; |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
class ArrayChangeKeyCaseTest extends TestCase |
17
|
|
|
{ |
18
|
|
|
const SAMPLE_ARRAY = [ |
19
|
|
|
'oneKeyUsingCamelCase' => 1, |
20
|
|
|
'OneKeyUsingPascalCase' => 1, |
21
|
|
|
'one key with spaces' => 1, |
22
|
|
|
'one-key-with-hyphens' => 1, |
23
|
|
|
'and_one_with_underscores' => 1, |
24
|
|
|
'what About_Mixed-separators!?!_and-numb333r5?' => 1, |
25
|
|
|
'one' => 1, |
26
|
|
|
'onewithsymbol!' => 1, |
27
|
|
|
'~~ ~~' => 1, |
28
|
|
|
]; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @test |
32
|
|
|
*/ |
33
|
|
|
public function it_change_nothing_for_non_associative_arrays() |
34
|
|
|
{ |
35
|
|
|
$this->assertEquals( |
36
|
|
|
array_values(self::SAMPLE_ARRAY), |
37
|
|
|
array_change_key_case(array_values(self::SAMPLE_ARRAY), CASE_LISP) |
38
|
|
|
); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @test |
43
|
|
|
*/ |
44
|
|
|
public function it_change_nothing_for_invalid_case_flag() |
45
|
|
|
{ |
46
|
|
|
$this->assertEquals( |
47
|
|
|
array_values(self::SAMPLE_ARRAY), |
48
|
|
|
array_change_key_case(array_values(self::SAMPLE_ARRAY), 1234) |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @test |
54
|
|
|
*/ |
55
|
|
|
public function it_works_with_php_core_upper_case() |
56
|
|
|
{ |
57
|
|
|
$this->assertEquals( |
58
|
|
|
\array_change_key_case(self::SAMPLE_ARRAY, \CASE_UPPER), |
59
|
|
|
array_change_key_case(self::SAMPLE_ARRAY, \CASE_UPPER) |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @test |
65
|
|
|
*/ |
66
|
|
|
public function it_works_with_php_core_lower_case() |
67
|
|
|
{ |
68
|
|
|
$this->assertEquals( |
69
|
|
|
\array_change_key_case(self::SAMPLE_ARRAY, \CASE_LOWER), |
70
|
|
|
array_change_key_case(self::SAMPLE_ARRAY, \CASE_LOWER) |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @test |
76
|
|
|
*/ |
77
|
|
|
public function it_works_with_spatie_upper_case() |
78
|
|
|
{ |
79
|
|
|
$this->assertEquals( |
80
|
|
|
\array_change_key_case(self::SAMPLE_ARRAY, \CASE_UPPER), |
81
|
|
|
array_change_key_case(self::SAMPLE_ARRAY, CASE_UPPER) |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @test |
87
|
|
|
*/ |
88
|
|
|
public function it_works_with_spatie_lower_case() |
89
|
|
|
{ |
90
|
|
|
$this->assertEquals( |
91
|
|
|
\array_change_key_case(self::SAMPLE_ARRAY, \CASE_LOWER), |
92
|
|
|
array_change_key_case(self::SAMPLE_ARRAY, CASE_LOWER) |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @test |
98
|
|
|
*/ |
99
|
|
|
public function it_works_with_snake_case() |
100
|
|
|
{ |
101
|
|
|
$this->assertEquals( |
102
|
|
|
[ |
103
|
|
|
'one_key_using_camel_case' => 1, |
104
|
|
|
'one_key_using_pascal_case' => 1, |
105
|
|
|
'one_key_with_spaces' => 1, |
106
|
|
|
'one_key_with_hyphens' => 1, |
107
|
|
|
'and_one_with_underscores' => 1, |
108
|
|
|
'what_about_mixed_separators!?!_and_numb_333_r_5?' => 1, |
109
|
|
|
'one' => 1, |
110
|
|
|
'onewithsymbol!' => 1, |
111
|
|
|
'~~_~~' => 1, |
112
|
|
|
], |
113
|
|
|
array_change_key_case(self::SAMPLE_ARRAY, CASE_SNAKE) |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @test |
119
|
|
|
*/ |
120
|
|
|
public function it_works_with_title_case() |
121
|
|
|
{ |
122
|
|
|
$this->assertEquals( |
123
|
|
|
[ |
124
|
|
|
'One key using camel case' => 1, |
125
|
|
|
'One key using pascal case' => 1, |
126
|
|
|
'One key with spaces' => 1, |
127
|
|
|
'One key with hyphens' => 1, |
128
|
|
|
'And one with underscores' => 1, |
129
|
|
|
'What about mixed separators!?! and numb 333 r 5?' => 1, |
130
|
|
|
'One' => 1, |
131
|
|
|
'Onewithsymbol!' => 1, |
132
|
|
|
'~~ ~~' => 1, |
133
|
|
|
], |
134
|
|
|
array_change_key_case(self::SAMPLE_ARRAY, CASE_TITLE) |
135
|
|
|
); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @test |
140
|
|
|
*/ |
141
|
|
View Code Duplication |
public function it_works_with_camel_case() |
|
|
|
|
142
|
|
|
{ |
143
|
|
|
$this->assertEquals( |
144
|
|
|
[ |
145
|
|
|
'oneKeyUsingCamelCase' => 1, |
146
|
|
|
'oneKeyUsingPascalCase' => 1, |
147
|
|
|
'oneKeyWithSpaces' => 1, |
148
|
|
|
'oneKeyWithHyphens' => 1, |
149
|
|
|
'andOneWithUnderscores' => 1, |
150
|
|
|
'whatAboutMixedSeparators!?!AndNumb333r5?' => 1, |
151
|
|
|
'one' => 1, |
152
|
|
|
'onewithsymbol!' => 1, |
153
|
|
|
'~~~~' => 1, |
154
|
|
|
], |
155
|
|
|
array_change_key_case(self::SAMPLE_ARRAY, CASE_CAMEL) |
156
|
|
|
); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @test |
161
|
|
|
*/ |
162
|
|
View Code Duplication |
public function it_works_with_pascal_case() |
|
|
|
|
163
|
|
|
{ |
164
|
|
|
$this->assertEquals( |
165
|
|
|
[ |
166
|
|
|
'OneKeyUsingCamelCase' => 1, |
167
|
|
|
'OneKeyUsingPascalCase' => 1, |
168
|
|
|
'OneKeyWithSpaces' => 1, |
169
|
|
|
'OneKeyWithHyphens' => 1, |
170
|
|
|
'AndOneWithUnderscores' => 1, |
171
|
|
|
'WhatAboutMixedSeparators!?!AndNumb333r5?' => 1, |
172
|
|
|
'One' => 1, |
173
|
|
|
'Onewithsymbol!' => 1, |
174
|
|
|
'~~~~' => 1, |
175
|
|
|
], |
176
|
|
|
array_change_key_case(self::SAMPLE_ARRAY, CASE_PASCAL) |
177
|
|
|
); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @test |
182
|
|
|
*/ |
183
|
|
|
public function it_works_with_lisp_case() |
184
|
|
|
{ |
185
|
|
|
$this->assertEquals( |
186
|
|
|
[ |
187
|
|
|
'one-key-using-camel-case' => 1, |
188
|
|
|
'one-key-using-pascal-case' => 1, |
189
|
|
|
'one-key-with-spaces' => 1, |
190
|
|
|
'one-key-with-hyphens' => 1, |
191
|
|
|
'and-one-with-underscores' => 1, |
192
|
|
|
'what-about-mixed-separators!?!-and-numb-333-r-5?' => 1, |
193
|
|
|
'one' => 1, |
194
|
|
|
'onewithsymbol!' => 1, |
195
|
|
|
'~~-~~' => 1, |
196
|
|
|
], |
197
|
|
|
array_change_key_case(self::SAMPLE_ARRAY, CASE_LISP) |
198
|
|
|
); |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
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.