1 | <?php |
||
2 | namespace tinymeng\tools; |
||
3 | |||
4 | use PHPUnit\Framework\TestCase; |
||
5 | |||
6 | class ArrayToolTest extends TestCase |
||
7 | { |
||
8 | public function testArraySort_AscendingOrder_ReturnsSortedArray() |
||
9 | { |
||
10 | $arr = [ |
||
11 | ['id' => 3, 'name' => 'Charlie'], |
||
12 | ['id' => 1, 'name' => 'Alice'], |
||
13 | ['id' => 2, 'name' => 'Bob'], |
||
14 | ]; |
||
15 | $expected = [ |
||
16 | ['id' => 1, 'name' => 'Alice'], |
||
17 | ['id' => 2, 'name' => 'Bob'], |
||
18 | ['id' => 3, 'name' => 'Charlie'], |
||
19 | ]; |
||
20 | $this->assertEquals($expected, ArrayTool::arraySort($arr, 'id', 'asc')); |
||
21 | } |
||
22 | |||
23 | public function testArraySort_DescendingOrder_ReturnsSortedArray() |
||
24 | { |
||
25 | $arr = [ |
||
26 | ['id' => 3, 'name' => 'Charlie'], |
||
27 | ['id' => 1, 'name' => 'Alice'], |
||
28 | ['id' => 2, 'name' => 'Bob'], |
||
29 | ]; |
||
30 | $expected = [ |
||
31 | ['id' => 3, 'name' => 'Charlie'], |
||
32 | ['id' => 2, 'name' => 'Bob'], |
||
33 | ['id' => 1, 'name' => 'Alice'], |
||
34 | ]; |
||
35 | $this->assertEquals($expected, ArrayTool::arraySort($arr, 'id', 'desc')); |
||
36 | } |
||
37 | |||
38 | public function testArraySort_EmptyArray_ReturnsEmptyArray() |
||
39 | { |
||
40 | $arr = []; |
||
41 | $expected = []; |
||
42 | $this->assertEquals($expected, ArrayTool::arraySort($arr, 'id', 'asc')); |
||
43 | } |
||
44 | |||
45 | public function testArraySort_InvalidKey_ReturnsOriginalArray() |
||
46 | { |
||
47 | $arr = [ |
||
48 | ['id' => 3, 'name' => 'Charlie'], |
||
49 | ['id' => 1, 'name' => 'Alice'], |
||
50 | ['id' => 2, 'name' => 'Bob'], |
||
51 | ]; |
||
52 | $expected = $arr; |
||
53 | $this->assertEquals($expected, ArrayTool::arraySort($arr, 'invalid_key', 'asc')); |
||
54 | } |
||
55 | |||
56 | public function testGetTreeStructure_NormalTree_ReturnsTreeStructure() |
||
57 | { |
||
58 | $list = [ |
||
59 | ['id' => 1, 'pid' => 0, 'name' => 'Root'], |
||
60 | ['id' => 2, 'pid' => 1, 'name' => 'Child 1'], |
||
61 | ['id' => 3, 'pid' => 1, 'name' => 'Child 2'], |
||
62 | ['id' => 4, 'pid' => 2, 'name' => 'Grandchild 1'], |
||
63 | ]; |
||
64 | $expected = [ |
||
65 | [ |
||
66 | 'id' => 1, |
||
67 | 'pid' => 0, |
||
68 | 'name' => 'Root', |
||
69 | 'child' => [ |
||
70 | [ |
||
71 | 'id' => 2, |
||
72 | 'pid' => 1, |
||
73 | 'name' => 'Child 1', |
||
74 | 'child' => [ |
||
75 | [ |
||
76 | 'id' => 4, |
||
77 | 'pid' => 2, |
||
78 | 'name' => 'Grandchild 1', |
||
79 | ], |
||
80 | ], |
||
81 | ], |
||
82 | [ |
||
83 | 'id' => 3, |
||
84 | 'pid' => 1, |
||
85 | 'name' => 'Child 2', |
||
86 | ], |
||
87 | ], |
||
88 | ], |
||
89 | ]; |
||
90 | $this->assertEquals($expected, ArrayTool::getTreeStructure($list)); |
||
91 | } |
||
92 | |||
93 | public function testGetTreeStructure_EmptyArray_ReturnsEmptyArray() |
||
94 | { |
||
95 | $list = []; |
||
96 | $expected = []; |
||
97 | $this->assertEquals($expected, ArrayTool::getTreeStructure($list)); |
||
98 | } |
||
99 | |||
100 | public function testGetTreeStructure_NoChildren_ReturnsTreeStructureWithoutChildren() |
||
101 | { |
||
102 | $list = [ |
||
103 | ['id' => 1, 'pid' => 0, 'name' => 'Root'], |
||
104 | ]; |
||
105 | $expected = [ |
||
106 | [ |
||
107 | 'id' => 1, |
||
108 | 'pid' => 0, |
||
109 | 'name' => 'Root', |
||
110 | ], |
||
111 | ]; |
||
112 | $this->assertEquals($expected, ArrayTool::getTreeStructure($list)); |
||
113 | } |
||
114 | |||
115 | public function testGetTreeStructure_SingleNode_ReturnsSingleNode() |
||
116 | { |
||
117 | $list = [ |
||
118 | ['id' => 1, 'pid' => 0, 'name' => 'Root'], |
||
119 | ]; |
||
120 | $expected = [ |
||
121 | [ |
||
122 | 'id' => 1, |
||
123 | 'pid' => 0, |
||
124 | 'name' => 'Root', |
||
125 | ], |
||
126 | ]; |
||
127 | $this->assertEquals($expected, ArrayTool::getTreeStructure($list)); |
||
128 | } |
||
129 | |||
130 | public function testObjectToArray_Object_ReturnsArray() |
||
131 | { |
||
132 | $obj = (object) [ |
||
133 | 'id' => 1, |
||
134 | 'name' => 'Alice', |
||
135 | 'details' => (object) [ |
||
136 | 'age' => 30, |
||
137 | 'city' => 'Wonderland', |
||
138 | ], |
||
139 | ]; |
||
140 | $expected = [ |
||
141 | 'id' => 1, |
||
142 | 'name' => 'Alice', |
||
143 | 'details' => [ |
||
144 | 'age' => 30, |
||
145 | 'city' => 'Wonderland', |
||
146 | ], |
||
147 | ]; |
||
148 | $this->assertEquals($expected, ArrayTool::objectToArray($obj)); |
||
149 | } |
||
150 | |||
151 | public function testObjectToArray_NestedObject_ReturnsNestedArray() |
||
152 | { |
||
153 | $obj = (object) [ |
||
154 | 'id' => 1, |
||
155 | 'name' => 'Alice', |
||
156 | 'details' => (object) [ |
||
157 | 'age' => 30, |
||
158 | 'city' => 'Wonderland', |
||
159 | 'contact' => (object) [ |
||
160 | 'email' => '[email protected]', |
||
161 | 'phone' => '1234567890', |
||
162 | ], |
||
163 | ], |
||
164 | ]; |
||
165 | $expected = [ |
||
166 | 'id' => 1, |
||
167 | 'name' => 'Alice', |
||
168 | 'details' => [ |
||
169 | 'age' => 30, |
||
170 | 'city' => 'Wonderland', |
||
171 | 'contact' => [ |
||
172 | 'email' => '[email protected]', |
||
173 | 'phone' => '1234567890', |
||
174 | ], |
||
175 | ], |
||
176 | ]; |
||
177 | $this->assertEquals($expected, ArrayTool::objectToArray($obj)); |
||
178 | } |
||
179 | |||
180 | public function testObjectToArray_NonObject_ReturnsOriginalInput() |
||
181 | { |
||
182 | $input = 'not an object'; |
||
183 | $expected = 'not an object'; |
||
184 | $this->assertEquals($expected, ArrayTool::objectToArray($input)); |
||
185 | } |
||
186 | |||
187 | public function testNullArrayToObject_EmptyArray_ReturnsObject() |
||
188 | { |
||
189 | $data = []; |
||
190 | ArrayTool::nullArrayToObject($data); |
||
191 | $this->assertIsObject($data); |
||
0 ignored issues
–
show
|
|||
192 | } |
||
193 | |||
194 | public function testNullArrayToObject_NestedEmptyArray_ReturnsNestedObject() |
||
195 | { |
||
196 | $data = [ |
||
197 | 'key1' => [], |
||
198 | 'key2' => [ |
||
199 | 'key3' => [], |
||
200 | ], |
||
201 | ]; |
||
202 | ArrayTool::nullArrayToObject($data); |
||
203 | $this->assertIsObject($data['key1']); |
||
204 | $this->assertIsObject($data['key2']['key3']); |
||
205 | } |
||
206 | |||
207 | public function testNullArrayToObject_NonArray_ReturnsOriginalInput() |
||
208 | { |
||
209 | $data = 'not an array'; |
||
210 | ArrayTool::nullArrayToObject($data); |
||
211 | $this->assertEquals('not an array', $data); |
||
212 | } |
||
213 | } |
||
214 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.