ArrayUtilsTest.php 5.7 KB

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