1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\Stdlib\Test\Unit;
use Magento\Framework\Stdlib\ArrayUtils;
/**
* Test for ArrayUtils.
*
* @see ArrayUtils
*/
class ArrayUtilsTest extends \PHPUnit\Framework\TestCase
{
/**
* @var ArrayUtils
*/
protected $_arrayUtils;
/**
* @inheritdoc
*/
protected function setUp()
{
$this->_arrayUtils = new ArrayUtils();
}
/**
* Tests ksort multibyte.
*
* @param array $input
* @param string $locale
* @dataProvider ksortMultibyteDataProvider
*/
public function testKsortMultibyte($input, $locale)
{
$this->_arrayUtils->ksortMultibyte($input, $locale);
$iterator = 0;
foreach ($input as $value) {
$iterator++;
$this->assertEquals($iterator, $value);
}
}
/**
* Data provider for ksortMultibyteDataProvider
* @todo implement provider with values which different depends on locale
*/
public function ksortMultibyteDataProvider()
{
return [[['б' => 2, 'в' => 3, 'а' => 1], 'ru_RU']];
}
public function testDecorateArray()
{
$original = [['value' => 1], ['value' => 2], ['value' => 3]];
$decorated = [
['value' => 1, 'is_first' => true, 'is_odd' => true],
['value' => 2, 'is_even' => true],
['value' => 3, 'is_last' => true, 'is_odd' => true],
];
// arrays
$this->assertEquals($decorated, $this->_arrayUtils->decorateArray($original, ''));
// \Magento\Framework\DataObject
$sample = [
new \Magento\Framework\DataObject($original[0]),
new \Magento\Framework\DataObject($original[1]),
new \Magento\Framework\DataObject($original[2]),
];
$decoratedVo = [
new \Magento\Framework\DataObject($decorated[0]),
new \Magento\Framework\DataObject($decorated[1]),
new \Magento\Framework\DataObject($decorated[2]),
];
$this->assertEquals($decoratedVo, $this->_arrayUtils->decorateArray($sample, ''));
}
/**
* Test flattening of array.
*
* @param array $data
* @param array $expected
* @param string $path
* @param string $separator
* @dataProvider flattenDataProvider
*/
public function testFlatten(array $data, array $expected, $path, $separator)
{
$this->assertSame($expected, $this->_arrayUtils->flatten($data, $path, $separator));
}
/**
* @return array
*/
public function flattenDataProvider()
{
return [
[
[
'default' => ['web' => ['unsecure' => ['base_url' => 'http://magento2.local/']]],
'websites' => ['base' => ['web' => ['unsecure' => ['base_url' => 'http://magento2.local/']]]],
],
[
'default/web/unsecure/base_url' => 'http://magento2.local/',
'websites/base/web/unsecure/base_url' => 'http://magento2.local/',
],
'',
'/'
],
[
[
'default' => ['web' => ['unsecure' => ['base_url' => 'http://magento2.local/']]],
],
[
'default+web+unsecure+base_url' => 'http://magento2.local/',
],
'',
'+',
],
[
[
'default' => ['web' => ['unsecure' => ['base_url' => 'http://magento2.local/']]],
],
[
'test+default+web+unsecure+base_url' => 'http://magento2.local/',
],
'test',
'+',
],
[
[
'default' => ['unsecure' => 'http://magento2.local/'],
],
[
'test/default/unsecure' => 'http://magento2.local/',
],
'test',
'/',
],
[
[
'unsecure' => 'http://magento2.local/',
],
[
'unsecure' => 'http://magento2.local/',
],
'',
'/',
],
[
[],
[],
'',
'/',
]
];
}
/**
* Tests recursive diff between arrays.
*
* @param array $originalArray
* @param array $newArray
* @param $expected
* @dataProvider recursiveDiffDataProvider
*/
public function testRecursiveDiff(array $originalArray, array $newArray, $expected)
{
$this->assertSame($expected, $this->_arrayUtils->recursiveDiff($originalArray, $newArray));
}
/**
* @return array
*/
public function recursiveDiffDataProvider()
{
return [
[
[
'test' => ['test2' => 2]
],
[],
[
'test' => ['test2' => 2]
]
],
[
[
'test' => ['test2' => 2]
],
[
'test' => ['test2' => 2]
],
[]
],
[
[
'test' => ['test2' => ['test3' => 3, 'test4' => 4]]
],
[
'test' => ['test3' => 3]
],
[
'test' => ['test2' => ['test3' => 3, 'test4' => 4]]
]
]
];
}
}