Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ArrayUtilsTest.php
Go to the documentation of this file.
1 <?php
7 
9 
15 class ArrayUtilsTest extends \PHPUnit\Framework\TestCase
16 {
20  protected $_arrayUtils;
21 
25  protected function setUp()
26  {
27  $this->_arrayUtils = new ArrayUtils();
28  }
29 
37  public function testKsortMultibyte($input, $locale)
38  {
39  $this->_arrayUtils->ksortMultibyte($input, $locale);
40 
41  $iterator = 0;
42  foreach ($input as $value) {
43  $iterator++;
44  $this->assertEquals($iterator, $value);
45  }
46  }
47 
52  public function ksortMultibyteDataProvider()
53  {
54  return [[['б' => 2, 'в' => 3, 'а' => 1], 'ru_RU']];
55  }
56 
57  public function testDecorateArray()
58  {
59  $original = [['value' => 1], ['value' => 2], ['value' => 3]];
60  $decorated = [
61  ['value' => 1, 'is_first' => true, 'is_odd' => true],
62  ['value' => 2, 'is_even' => true],
63  ['value' => 3, 'is_last' => true, 'is_odd' => true],
64  ];
65 
66  // arrays
67  $this->assertEquals($decorated, $this->_arrayUtils->decorateArray($original, ''));
68 
69  // \Magento\Framework\DataObject
70  $sample = [
71  new \Magento\Framework\DataObject($original[0]),
72  new \Magento\Framework\DataObject($original[1]),
73  new \Magento\Framework\DataObject($original[2]),
74  ];
75  $decoratedVo = [
76  new \Magento\Framework\DataObject($decorated[0]),
77  new \Magento\Framework\DataObject($decorated[1]),
78  new \Magento\Framework\DataObject($decorated[2]),
79  ];
80  $this->assertEquals($decoratedVo, $this->_arrayUtils->decorateArray($sample, ''));
81  }
82 
92  public function testFlatten(array $data, array $expected, $path, $separator)
93  {
94  $this->assertSame($expected, $this->_arrayUtils->flatten($data, $path, $separator));
95  }
96 
100  public function flattenDataProvider()
101  {
102  return [
103  [
104  [
105  'default' => ['web' => ['unsecure' => ['base_url' => 'http://magento2.local/']]],
106  'websites' => ['base' => ['web' => ['unsecure' => ['base_url' => 'http://magento2.local/']]]],
107  ],
108  [
109  'default/web/unsecure/base_url' => 'http://magento2.local/',
110  'websites/base/web/unsecure/base_url' => 'http://magento2.local/',
111  ],
112  '',
113  '/'
114  ],
115  [
116  [
117  'default' => ['web' => ['unsecure' => ['base_url' => 'http://magento2.local/']]],
118  ],
119  [
120  'default+web+unsecure+base_url' => 'http://magento2.local/',
121  ],
122  '',
123  '+',
124  ],
125  [
126  [
127  'default' => ['web' => ['unsecure' => ['base_url' => 'http://magento2.local/']]],
128  ],
129  [
130  'test+default+web+unsecure+base_url' => 'http://magento2.local/',
131  ],
132  'test',
133  '+',
134  ],
135  [
136  [
137  'default' => ['unsecure' => 'http://magento2.local/'],
138  ],
139  [
140  'test/default/unsecure' => 'http://magento2.local/',
141  ],
142  'test',
143  '/',
144  ],
145  [
146  [
147  'unsecure' => 'http://magento2.local/',
148  ],
149  [
150  'unsecure' => 'http://magento2.local/',
151  ],
152  '',
153  '/',
154  ],
155  [
156  [],
157  [],
158  '',
159  '/',
160  ]
161  ];
162  }
163 
172  public function testRecursiveDiff(array $originalArray, array $newArray, $expected)
173  {
174  $this->assertSame($expected, $this->_arrayUtils->recursiveDiff($originalArray, $newArray));
175  }
176 
180  public function recursiveDiffDataProvider()
181  {
182  return [
183  [
184  [
185  'test' => ['test2' => 2]
186  ],
187  [],
188  [
189  'test' => ['test2' => 2]
190  ]
191  ],
192  [
193  [
194  'test' => ['test2' => 2]
195  ],
196  [
197  'test' => ['test2' => 2]
198  ],
199  []
200  ],
201  [
202  [
203  'test' => ['test2' => ['test3' => 3, 'test4' => 4]]
204  ],
205  [
206  'test' => ['test3' => 3]
207  ],
208  [
209  'test' => ['test2' => ['test3' => 3, 'test4' => 4]]
210  ]
211  ]
212  ];
213  }
214 }
testFlatten(array $data, array $expected, $path, $separator)
testRecursiveDiff(array $originalArray, array $newArray, $expected)
$value
Definition: gender.phtml:16