Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ArrayManagerTest.php
Go to the documentation of this file.
1 <?php
7 
10 
11 class ArrayManagerTest extends \PHPUnit\Framework\TestCase
12 {
16  protected $arrayManager;
17 
22 
23  protected function setUp()
24  {
25  $this->objectManagerHelper = new ObjectManagerHelper($this);
26  $this->arrayManager = $this->objectManagerHelper->getObject(ArrayManager::class);
27  }
28 
35  public function testExists($path, $data, $result)
36  {
37  $this->assertSame($result, $this->arrayManager->exists($path, $data));
38  }
39 
43  public function existsDataProvider()
44  {
45  return [
46  0 => [
47  'path' => 'some/path',
48  'data' => ['some' => ['path' => null]],
49  'result' => true
50  ],
51  1 => [
52  'path' => '0/0/test',
53  'data' => [[['test' => false]]],
54  'result' => true
55  ],
56  2 => [
57  'path' => 'invalid/path',
58  'data' => ['valid' => ['path' => 0]],
59  'result' => false
60  ]
61  ];
62  }
63 
64  public function testExistsCustomDelimiter()
65  {
66  $data = ['custom' => ['delimiter' => null]];
67 
68  $this->assertFalse($this->arrayManager->exists('custom/delimiter', $data, '~'));
69  $this->assertTrue($this->arrayManager->exists('custom~delimiter', $data, '~'));
70  }
71 
78  public function testGet($path, $data, $result)
79  {
80  $this->assertSame($result, $this->arrayManager->get($path, $data));
81  }
82 
86  public function getDataProvider()
87  {
88  return [
89  0 => [
90  'path' => 'nested/path/0',
91  'data' => ['nested' => ['path' => ['value1']]],
92  'result' => 'value1'
93  ],
94  1 => [
95  'path' => '0',
96  'data' => [false],
97  'result' => false
98  ],
99  2 => [
100  'path' => 'invalid/path/0',
101  'data' => [],
102  'result' => null
103  ]
104  ];
105  }
106 
114  public function testSet($path, $data, $value, $result)
115  {
116  $this->assertSame($result, $this->arrayManager->set($path, $data, $value));
117  }
118 
122  public function setDataProvider()
123  {
124  return [
125  0 => [
126  'path' => '0/1',
127  'data' => [[false, false]],
128  'value' => true,
129  'result' => [[false, true]]
130  ],
131  1 => [
132  'path' => 'test',
133  'data' => ['test' => ['lost data']],
134  'value' => 'found data',
135  'result' => ['test' => 'found data']
136  ],
137  2 => [
138  'path' => 'new/path/2',
139  'data' => ['existing' => ['path' => 1]],
140  'value' => 'valuable data',
141  'result' => ['existing' => ['path' => 1], 'new' => ['path' => [2 => 'valuable data']]]
142  ],
143  3 => [
144  'path' => ['new', 'path/2'],
145  'data' => ['existing' => ['path' => 1]],
146  'value' => 'valuable data',
147  'result' => ['existing' => ['path' => 1], 'new' => ['path' => [2 => 'valuable data']]]
148  ]
149  ];
150  }
151 
159  public function testReplace($path, $data, $value, $result)
160  {
161  $this->assertSame($result, $this->arrayManager->set($path, $data, $value));
162  }
163 
167  public function setReplaceProvider()
168  {
169  return [
170  0 => [
171  'path' => '0/1',
172  'data' => [[false, false]],
173  'value' => true,
174  'result' => [[false, true]]
175  ],
176  1 => [
177  'path' => 'test',
178  'data' => ['test' => ['lost data']],
179  'value' => 'found data',
180  'result' => ['test' => 'found data']
181  ],
182  2 => [
183  'path' => 'new/path/2',
184  'data' => ['existing' => ['path' => 1]],
185  'value' => 'valuable data',
186  'result' => ['existing' => ['path' => 1]]
187  ],
188  3 => [
189  'path' => ['new', 'path', '2'],
190  'data' => ['existing' => ['path' => 1]],
191  'value' => 'valuable data',
192  'result' => ['existing' => ['path' => 1]]
193  ]
194  ];
195  }
196 
205  public function testMove($path, $targetPath, array $data, $overwrite, array $result)
206  {
207  $this->assertSame($result, $this->arrayManager->move($path, $targetPath, $data, $overwrite));
208  }
209 
213  public function moveDataProvider()
214  {
215  return [
216  0 => [
217  'path' => 'not/valid/path',
218  'targetPath' => 'target/path',
219  'data' => ['valid' => ['path' => 'value']],
220  'overwrite' => false,
221  'result' => ['valid' => ['path' => 'value']]
222  ],
223  1 => [
224  'path' => 'valid/path',
225  'targetPath' => 'target/path',
226  'data' => ['valid' => ['path' => 'value']],
227  'overwrite' => false,
228  'result' => ['valid' => [], 'target' => ['path' => 'value']]
229  ],
230  2 => [
231  'path' => 'valid/path',
232  'targetPath' => 'target/path',
233  'data' => ['valid' => ['path' => 'value'], 'target' => ['path' => 'exists']],
234  'overwrite' => false,
235  'result' => ['valid' => ['path' => 'value'], 'target' => ['path' => 'exists']]
236  ],
237  3 => [
238  'path' => 'valid/path',
239  'targetPath' => 'target/path',
240  'data' => ['valid' => ['path' => 'value'], 'target' => ['path' => 'exists']],
241  'overwrite' => true,
242  'result' => ['valid' => [], 'target' => ['path' => 'value']]
243  ],
244  4 => [
245  'path' => ['valid', 'path'],
246  'targetPath' => 'target/path',
247  'data' => ['valid' => ['path' => 'value'], 'target' => ['path' => 'exists']],
248  'overwrite' => true,
249  'result' => ['valid' => [], 'target' => ['path' => 'value']]
250  ]
251  ];
252  }
253 
261  public function testMerge($path, $data, $value, $result)
262  {
263  $this->assertSame($result, $this->arrayManager->merge($path, $data, $value));
264  }
265 
269  public function mergeDataProvider()
270  {
271  return [
272  0 => [
273  'path' => '0/path/1',
274  'data' => [['path' => [false, ['value' => false]]]],
275  'value' => ['value' => true, 'new_value' => false],
276  'result' => [['path' => [false, ['value' => true, 'new_value' => false]]]]
277  ],
278  1 => [
279  'path' => 0,
280  'data' => [['nested' => ['test' => 2, 'test2' => 1]]],
281  'value' => ['nested' => ['test' => 3], 'more' => 4],
282  'result' => [['nested' => ['test' => 3, 'test2' => 1], 'more' => 4]]
283  ],
284  2 => [
285  'path' => 'invalid/path',
286  'data' => [],
287  'value' => [true],
288  'result' => []
289  ],
290  3 => [
291  'path' => ['0', 'path/1'],
292  'data' => [['path' => [false, ['value' => false]]]],
293  'value' => ['value' => true, 'new_value' => false],
294  'result' => [['path' => [false, ['value' => true, 'new_value' => false]]]]
295  ],
296  ];
297  }
298 
305  public function testPopulate($path, $data, $result)
306  {
307  $this->assertSame($result, $this->arrayManager->populate($path, $data));
308  }
309 
313  public function populateDataProvider()
314  {
315  return [
316  0 => [
317  'path' => 'some/is/not/array',
318  'data' => ['some' => true],
319  'result' => ['some' => true]
320  ],
321  1 => [
322  'path' => 0,
323  'data' => [],
324  'result' => [[]]
325  ],
326  2 => [
327  'path' => 'nested/1/array',
328  'data' => ['nested' => [true]],
329  'result' => ['nested' => [true, ['array' => []]]]
330  ]
331  ];
332  }
333 
340  public function testRemove($path, $data, $result)
341  {
342  $this->assertSame($result, $this->arrayManager->remove($path, $data));
343  }
344 
348  public function removeDataProvider()
349  {
350  return [
351  0 => [
352  'path' => '0/0/0/0',
353  'data' => [[[[null]]]],
354  'result' => [[[[]]]]
355  ],
356  1 => [
357  'path' => 'simple',
358  'data' => ['simple' => true, 'complex' => false],
359  'result' => ['complex' => false]
360  ],
361  2 => [
362  'path' => 'invalid',
363  'data' => [true],
364  'result' => [true]
365  ],
366  3 => [
367  'path' => ['simple'],
368  'data' => ['simple' => true, 'complex' => false],
369  'result' => ['complex' => false]
370  ],
371  ];
372  }
373 
382  public function testFindPaths($indexes, array $data, $startPath, $internalPath, $result)
383  {
384  $this->assertSame($result, $this->arrayManager->findPaths($indexes, $data, $startPath, $internalPath));
385  }
386 
390  public function findPathsDataProvider()
391  {
392  $data = [
393  'element1' => [
394  'children' => [
395  'element11' => [
396  'children' => [true, true]
397  ],
398  'element12' => [
399  'config' => [
400  'argument' => [
401  'data' => true
402  ]
403  ]
404  ]
405  ]
406  ],
407  'element2' => [
408  'children' => [true, true, true]
409  ],
410  '' => [
411  [[[[]]]]
412  ]
413  ];
414 
415  return [
416  0 => [
417  'indexes' => [0, 2],
418  'data' => $data,
419  'startPath' => 'element2',
420  'internalPath' => null,
421  'result' => ['element2/children/0', 'element2/children/2']
422  ],
423  1 => [
424  'indexes' => 0,
425  'data' => $data,
426  'startPath' => ['', '0'],
427  'internalPath' => '0',
428  'result' => ['/0/0', '/0/0/0/0']
429  ],
430  2 => [
431  'indexes' => 0,
432  'data' => $data,
433  'startPath' => '',
434  'internalPath' => ['0', '0'],
435  'result' => ['/0', '/0/0/0/0']
436  ],
437  3 => [
438  'indexes' => 'data',
439  'data' => $data,
440  'startPath' => 'element1/children',
441  'internalPath' => 'config/argument',
442  'result' => ['element1/children/element12/config/argument/data']
443  ],
444  4 => [
445  'indexes' => 1,
446  'data' => $data,
447  'startPath' => null,
448  'internalPath' => 'elements',
449  'result' => []
450  ]
451  ];
452  }
453 
462  public function testFindPath($indexes, array $data, $startPath, $internalPath, $result)
463  {
464  $this->assertSame($result, $this->arrayManager->findPath($indexes, $data, $startPath, $internalPath));
465  }
466 
470  public function findPathDataProvider()
471  {
472  $data = [
473  'element1' => [
474  'children' => [
475  'element11' => [
476  'children' => [true, true]
477  ],
478  'element12' => [
479  'config' => [
480  'argument' => [
481  'data' => true
482  ]
483  ]
484  ]
485  ]
486  ],
487  'element2' => [
488  'children' => [true, true, true]
489  ],
490  '' => [
491  [[[[]]]]
492  ]
493  ];
494 
495  return [
496  0 => [
497  'indexes' => [0, 2],
498  'data' => $data,
499  'startPath' => 'element2',
500  'internalPath' => null,
501  'result' => 'element2/children/0'
502  ],
503  1 => [
504  'indexes' => 0,
505  'data' => $data,
506  'startPath' => ['', '0'],
507  'internalPath' => '0',
508  'result' => '/0/0'
509  ],
510  2 => [
511  'indexes' => 0,
512  'data' => $data,
513  'startPath' => '',
514  'internalPath' => ['0', '0'],
515  'result' => '/0'
516  ],
517  3 => [
518  'indexes' => 'data',
519  'data' => $data,
520  'startPath' => 'element1/children',
521  'internalPath' => 'config/argument',
522  'result' => 'element1/children/element12/config/argument/data'
523  ],
524  4 => [
525  'indexes' => 1,
526  'data' => $data,
527  'startPath' => null,
528  'internalPath' => 'elements',
529  'result' => null
530  ]
531  ];
532  }
533 
541  public function testSlicePath($path, $offset, $length, $result)
542  {
543  $this->assertSame($result, $this->arrayManager->slicePath($path, $offset, $length));
544  }
545 
549  public function slicePathDataProvider()
550  {
551  $path = 'some/very/very/long/path/0/goes/1/3/here';
552 
553  return [
554  0 => [
555  'path' => $path,
556  'offset' => 3,
557  'length' => null,
558  'result' => 'long/path/0/goes/1/3/here'
559  ],
560  1 => [
561  'path' => $path,
562  'offset' => -3,
563  'length' => null,
564  'result' => '1/3/here'
565  ],
566  2 => [
567  'path' => $path,
568  'offset' => 500,
569  'length' => null,
570  'result' => ''
571  ],
572  3 => [
573  'path' => $path,
574  'offset' => 2,
575  'length' => 2,
576  'result' => 'very/long'
577  ],
578  4 => [
579  'path' => $path,
580  'offset' => -6,
581  'length' => 3,
582  'result' => 'path/0/goes'
583  ],
584  ];
585  }
586 
588  {
589  $path = 'my~custom~path';
590 
591  $this->assertSame('custom', $this->arrayManager->slicePath($path, 1, 1, '~'));
592  $this->assertSame('', $this->arrayManager->slicePath($path, 1, 1));
593  }
594 }
testFindPath($indexes, array $data, $startPath, $internalPath, $result)
return false
Definition: gallery.phtml:36
$value
Definition: gender.phtml:16
testFindPaths($indexes, array $data, $startPath, $internalPath, $result)
testMove($path, $targetPath, array $data, $overwrite, array $result)