2009-07-17 02:01:06 +02:00
|
|
|
<?php
|
2016-10-14 03:30:05 +02:00
|
|
|
|
|
|
|
namespace SilverStripe\ORM\Tests;
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\ORM\ArrayLib;
|
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
class ArrayLibTest extends SapphireTest
|
|
|
|
{
|
|
|
|
|
|
|
|
public function testInvert()
|
|
|
|
{
|
2020-04-20 19:58:09 +02:00
|
|
|
$arr = [
|
|
|
|
'row1' => [
|
2016-12-16 05:34:21 +01:00
|
|
|
'col1' =>'val1',
|
|
|
|
'col2' => 'val2'
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
'row2' => [
|
2016-12-16 05:34:21 +01:00
|
|
|
'col1' => 'val3',
|
|
|
|
'col2' => 'val4'
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
ArrayLib::invert($arr),
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
'col1' => [
|
2016-12-16 05:34:21 +01:00
|
|
|
'row1' => 'val1',
|
|
|
|
'row2' => 'val3',
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
'col2' => [
|
2016-12-16 05:34:21 +01:00
|
|
|
'row1' => 'val2',
|
|
|
|
'row2' => 'val4',
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValuekey()
|
|
|
|
{
|
|
|
|
$this->assertEquals(
|
|
|
|
ArrayLib::valuekey(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'testkey1' => 'testvalue1',
|
|
|
|
'testkey2' => 'testvalue2'
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
),
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'testvalue1' => 'testvalue1',
|
|
|
|
'testvalue2' => 'testvalue2'
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testArrayMapRecursive()
|
|
|
|
{
|
2020-04-20 19:58:09 +02:00
|
|
|
$array = [
|
2016-12-16 05:34:21 +01:00
|
|
|
'a ',
|
2020-04-20 19:58:09 +02:00
|
|
|
[' b', 'c'],
|
|
|
|
];
|
|
|
|
$strtoupper = [
|
2016-12-16 05:34:21 +01:00
|
|
|
'A ',
|
2020-04-20 19:58:09 +02:00
|
|
|
[' B', 'C'],
|
|
|
|
];
|
|
|
|
$trim = [
|
2016-12-16 05:34:21 +01:00
|
|
|
'a',
|
2020-04-20 19:58:09 +02:00
|
|
|
['b', 'c'],
|
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(
|
|
|
|
$strtoupper,
|
|
|
|
ArrayLib::array_map_recursive('strtoupper', $array)
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
$trim,
|
|
|
|
ArrayLib::array_map_recursive('trim', $array)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testArrayMergeRecursive()
|
|
|
|
{
|
2020-04-20 19:58:09 +02:00
|
|
|
$first = [
|
2016-12-16 05:34:21 +01:00
|
|
|
'first' => 'a',
|
|
|
|
'second' => 'b',
|
2020-04-20 19:58:09 +02:00
|
|
|
];
|
|
|
|
$second = [
|
2016-12-16 05:34:21 +01:00
|
|
|
'third' => 'c',
|
|
|
|
'fourth' => 'd',
|
2020-04-20 19:58:09 +02:00
|
|
|
];
|
|
|
|
$expected = [
|
2016-12-16 05:34:21 +01:00
|
|
|
'first' => 'a',
|
|
|
|
'second' => 'b',
|
|
|
|
'third' => 'c',
|
|
|
|
'fourth' => 'd',
|
2020-04-20 19:58:09 +02:00
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(
|
|
|
|
$expected,
|
|
|
|
ArrayLib::array_merge_recursive($first, $second),
|
|
|
|
'First values should supplement second values'
|
|
|
|
);
|
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
$first = [
|
2016-12-16 05:34:21 +01:00
|
|
|
'first' => 'a',
|
|
|
|
'second' => 'b',
|
2020-04-20 19:58:09 +02:00
|
|
|
];
|
|
|
|
$second = [
|
2016-12-16 05:34:21 +01:00
|
|
|
'first' => 'c',
|
|
|
|
'third' => 'd',
|
2020-04-20 19:58:09 +02:00
|
|
|
];
|
|
|
|
$expected = [
|
2016-12-16 05:34:21 +01:00
|
|
|
'first' => 'c',
|
|
|
|
'second' => 'b',
|
|
|
|
'third' => 'd',
|
2020-04-20 19:58:09 +02:00
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(
|
|
|
|
$expected,
|
|
|
|
ArrayLib::array_merge_recursive($first, $second),
|
|
|
|
'Second values should override first values'
|
|
|
|
);
|
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
$first = [
|
|
|
|
'first' => [
|
2016-12-16 05:34:21 +01:00
|
|
|
'first' => 'a',
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
'second' => [
|
2016-12-16 05:34:21 +01:00
|
|
|
'second' => 'b',
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
];
|
|
|
|
$second = [
|
|
|
|
'first' => [
|
2016-12-16 05:34:21 +01:00
|
|
|
'first' => 'c',
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
'third' => [
|
2016-12-16 05:34:21 +01:00
|
|
|
'third' => 'd',
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
];
|
|
|
|
$expected = [
|
|
|
|
'first' => [
|
2016-12-16 05:34:21 +01:00
|
|
|
'first' => 'c',
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
'second' => [
|
2016-12-16 05:34:21 +01:00
|
|
|
'second' => 'b',
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
'third' => [
|
2016-12-16 05:34:21 +01:00
|
|
|
'third' => 'd',
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(
|
|
|
|
$expected,
|
|
|
|
ArrayLib::array_merge_recursive($first, $second),
|
|
|
|
'Nested second values should override first values'
|
|
|
|
);
|
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
$first = [
|
|
|
|
'first' => [
|
2016-12-16 05:34:21 +01:00
|
|
|
'first' => 'a',
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
'second' => [
|
2016-12-16 05:34:21 +01:00
|
|
|
'second' => 'b',
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
];
|
|
|
|
$second = [
|
|
|
|
'first' => [
|
2016-12-16 05:34:21 +01:00
|
|
|
'second' => 'c',
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
'third' => [
|
2016-12-16 05:34:21 +01:00
|
|
|
'third' => 'd',
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
];
|
|
|
|
$expected = [
|
|
|
|
'first' => [
|
2016-12-16 05:34:21 +01:00
|
|
|
'first' => 'a',
|
|
|
|
'second' => 'c',
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
'second' => [
|
2016-12-16 05:34:21 +01:00
|
|
|
'second' => 'b',
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
'third' => [
|
2016-12-16 05:34:21 +01:00
|
|
|
'third' => 'd',
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(
|
|
|
|
$expected,
|
|
|
|
ArrayLib::array_merge_recursive($first, $second),
|
|
|
|
'Nested first values should supplement second values'
|
|
|
|
);
|
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
$first = [
|
|
|
|
'first' => [
|
2016-12-16 05:34:21 +01:00
|
|
|
0 => 'a',
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
'second' => [
|
2016-12-16 05:34:21 +01:00
|
|
|
1 => 'b',
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
];
|
|
|
|
$second = [
|
|
|
|
'first' => [
|
2016-12-16 05:34:21 +01:00
|
|
|
0 => 'c',
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
'third' => [
|
2016-12-16 05:34:21 +01:00
|
|
|
2 => 'd',
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
];
|
|
|
|
$expected = [
|
|
|
|
'first' => [
|
2016-12-16 05:34:21 +01:00
|
|
|
0 => 'c',
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
'second' => [
|
2016-12-16 05:34:21 +01:00
|
|
|
1 => 'b',
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
'third' => [
|
2016-12-16 05:34:21 +01:00
|
|
|
2 => 'd',
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
$expected,
|
|
|
|
ArrayLib::array_merge_recursive($first, $second),
|
|
|
|
'Numeric keys should behave like string keys'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFlatten()
|
|
|
|
{
|
2020-04-20 19:58:09 +02:00
|
|
|
$options = [
|
2016-12-16 05:34:21 +01:00
|
|
|
'1' => 'one',
|
|
|
|
'2' => 'two'
|
2020-04-20 19:58:09 +02:00
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$expected = $options;
|
|
|
|
|
|
|
|
$this->assertEquals($expected, ArrayLib::flatten($options));
|
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
$options = [
|
|
|
|
'1' => [
|
2016-12-16 05:34:21 +01:00
|
|
|
'2' => 'two',
|
|
|
|
'3' => 'three'
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
'4' => 'four'
|
2020-04-20 19:58:09 +02:00
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
$expected = [
|
2016-12-16 05:34:21 +01:00
|
|
|
'2' => 'two',
|
|
|
|
'3' => 'three',
|
|
|
|
'4' => 'four'
|
2020-04-20 19:58:09 +02:00
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$this->assertEquals($expected, ArrayLib::flatten($options));
|
|
|
|
}
|
2017-11-01 01:08:37 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that items can be added during iteration
|
|
|
|
*/
|
|
|
|
public function testIterateVolatileAppended()
|
|
|
|
{
|
|
|
|
$initial = [
|
|
|
|
'one' => [ 'next' => 'two', 'prev' => null ],
|
|
|
|
'two' => [ 'next' => 'three', 'prev' => 'one' ],
|
|
|
|
'three' => [ 'next' => null, 'prev' => 'two' ],
|
|
|
|
];
|
|
|
|
|
|
|
|
// Test new items are iterated
|
|
|
|
$items = $initial;
|
|
|
|
$seen = [];
|
|
|
|
foreach (ArrayLib::iterateVolatile($items) as $key => $value) {
|
|
|
|
$seen[$key] = $value;
|
|
|
|
// Append four
|
|
|
|
if ($key === 'three') {
|
|
|
|
$items['three']['next'] = 'four';
|
|
|
|
$items['four'] = [ 'next' => null, 'prev' => 'three'];
|
|
|
|
}
|
|
|
|
// Prepend zero won't force it to be iterated next, but it will be iterated
|
|
|
|
if ($key === 'one') {
|
|
|
|
$items['one']['next'] = 'zero';
|
|
|
|
$items = array_merge(
|
|
|
|
['zero' => [ 'next' => 'one', 'prev' => 'three']],
|
|
|
|
$items
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$expected = [
|
|
|
|
'one' => [ 'next' => 'two', 'prev' => null ],
|
|
|
|
'two' => [ 'next' => 'three', 'prev' => 'one' ],
|
|
|
|
'three' => [ 'next' => null, 'prev' => 'two' ],
|
|
|
|
'zero' => [ 'next' => 'one', 'prev' => 'three'],
|
|
|
|
'four' => [ 'next' => null, 'prev' => 'three']
|
|
|
|
];
|
|
|
|
// All items are iterated (order not deterministic)
|
|
|
|
$this->assertEquals(
|
|
|
|
$expected,
|
|
|
|
$seen,
|
|
|
|
'New items are iterated over'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that items can be modified during iteration
|
|
|
|
*/
|
|
|
|
public function testIterateVolatileModified()
|
|
|
|
{
|
|
|
|
$initial = [
|
|
|
|
'one' => [ 'next' => 'two', 'prev' => null ],
|
|
|
|
'two' => [ 'next' => 'three', 'prev' => 'one' ],
|
|
|
|
'three' => [ 'next' => 'four', 'prev' => 'two' ],
|
|
|
|
'four' => [ 'next' => null, 'prev' => 'three' ],
|
|
|
|
];
|
|
|
|
|
|
|
|
// Test new items are iterated
|
|
|
|
$items = $initial;
|
|
|
|
$seen = [];
|
|
|
|
foreach (ArrayLib::iterateVolatile($items) as $key => $value) {
|
|
|
|
$seen[$key] = $value;
|
|
|
|
// One modifies two
|
|
|
|
if ($key === 'one') {
|
|
|
|
$items['two']['modifiedby'] = 'one';
|
|
|
|
}
|
|
|
|
// Two removes three, preventing it from being iterated next
|
|
|
|
if ($key === 'two') {
|
|
|
|
unset($items['three']);
|
|
|
|
}
|
|
|
|
// Four removes two, but since it's already been iterated by this point
|
|
|
|
// it's too late.
|
|
|
|
if ($key === 'four') {
|
|
|
|
unset($items['two']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$expected = [
|
|
|
|
'one' => [ 'next' => 'two', 'prev' => null ],
|
|
|
|
'two' => [ 'next' => 'three', 'prev' => 'one', 'modifiedby' => 'one' ],
|
|
|
|
'four' => [ 'next' => null, 'prev' => 'three' ],
|
|
|
|
];
|
|
|
|
// All items are iterated (order not deterministic)
|
|
|
|
$this->assertEquals(
|
|
|
|
ksort($expected),
|
|
|
|
ksort($seen),
|
|
|
|
'New items are iterated over'
|
|
|
|
);
|
|
|
|
}
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|