2009-07-17 02:01:06 +02:00
|
|
|
<?php
|
2016-10-14 03:30:05 +02:00
|
|
|
|
|
|
|
namespace SilverStripe\ORM\Tests;
|
|
|
|
|
2023-08-03 05:33:13 +02:00
|
|
|
use PHPUnit\Framework\ExpectationFailedException;
|
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'
|
|
|
|
);
|
|
|
|
}
|
2023-08-03 05:33:13 +02:00
|
|
|
|
|
|
|
public function testShuffleAssociative()
|
|
|
|
{
|
|
|
|
$list = ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5, 'f' => 6];
|
|
|
|
$copy = $list;
|
|
|
|
// Try shuffling 3 times - it's technically possible the result of a shuffle could be
|
|
|
|
// the exact same order as the original list.
|
|
|
|
for ($attempts = 1; $attempts <= 3; $attempts++) {
|
|
|
|
ArrayLib::shuffleAssociative($copy);
|
|
|
|
// Check value/key association is retained
|
|
|
|
foreach ($list as $key => $value) {
|
|
|
|
$this->assertEquals($value, $copy[$key]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$failed = false;
|
|
|
|
try {
|
|
|
|
// Check the order is different
|
|
|
|
$this->assertNotSame($list, $copy);
|
|
|
|
} catch (ExpectationFailedException $e) {
|
|
|
|
$failed = true;
|
|
|
|
// Only fail the test if we've tried and failed 3 times.
|
|
|
|
if ($attempts === 3) {
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we've passed the shuffle test, don't retry.
|
|
|
|
if (!$failed) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-09-13 07:18:15 +02:00
|
|
|
|
|
|
|
public function provideInsertBefore(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'simple insertion' => [
|
|
|
|
'insert' => 'new',
|
|
|
|
'before' => 'def',
|
|
|
|
'strict' => true,
|
|
|
|
'splat' => false,
|
|
|
|
'expected' => ['abc', '', [1,2,3], 'new', 'def', '0', null, true, 0, 'last']
|
|
|
|
],
|
|
|
|
'insert before first' => [
|
|
|
|
'insert' => 'new',
|
|
|
|
'before' => 'abc',
|
|
|
|
'strict' => true,
|
|
|
|
'splat' => false,
|
|
|
|
'expected' => ['new', 'abc', '', [1,2,3], 'def', '0', null, true, 0, 'last']
|
|
|
|
],
|
|
|
|
'insert before last' => [
|
|
|
|
'insert' => 'new',
|
|
|
|
'before' => 'last',
|
|
|
|
'strict' => true,
|
|
|
|
'splat' => false,
|
|
|
|
'expected' => ['abc', '', [1,2,3], 'def', '0', null, true, 0, 'new', 'last']
|
|
|
|
],
|
|
|
|
'insert before missing' => [
|
|
|
|
'insert' => 'new',
|
|
|
|
'before' => 'this value isnt there',
|
|
|
|
'strict' => true,
|
|
|
|
'splat' => false,
|
|
|
|
'expected' => ['abc', '', [1,2,3], 'def', '0', null, true, 0, 'last', 'new']
|
|
|
|
],
|
|
|
|
'strict' => [
|
|
|
|
'insert' => 'new',
|
|
|
|
'before' => 0,
|
|
|
|
'strict' => true,
|
|
|
|
'splat' => false,
|
|
|
|
'expected' => ['abc', '', [1,2,3], 'def', '0', null, true, 'new', 0, 'last']
|
|
|
|
],
|
|
|
|
'not strict' => [
|
|
|
|
'insert' => 'new',
|
|
|
|
'before' => 0,
|
|
|
|
'strict' => false,
|
|
|
|
'splat' => false,
|
|
|
|
'expected' => ['abc', '', [1,2,3], 'def', 'new', '0', null, true, 0, 'last']
|
|
|
|
],
|
|
|
|
'before array' => [
|
|
|
|
'insert' => 'new',
|
|
|
|
'before' => [1,2,3],
|
|
|
|
'strict' => true,
|
|
|
|
'splat' => false,
|
|
|
|
'expected' => ['abc', '', 'new', [1,2,3], 'def', '0', null, true, 0, 'last']
|
|
|
|
],
|
|
|
|
'before missing array' => [
|
|
|
|
'insert' => 'new',
|
|
|
|
'before' => ['a', 'b', 'c'],
|
|
|
|
'strict' => true,
|
|
|
|
'splat' => false,
|
|
|
|
'expected' => ['abc', '', [1,2,3], 'def', '0', null, true, 0, 'last', 'new']
|
|
|
|
],
|
|
|
|
'splat array' => [
|
|
|
|
'insert' => ['a', 'b', 'c'],
|
|
|
|
'before' => 'def',
|
|
|
|
'strict' => true,
|
|
|
|
'splat' => true,
|
|
|
|
'expected' => ['abc', '', [1,2,3], 'a', 'b', 'c', 'def', '0', null, true, 0, 'last']
|
|
|
|
],
|
|
|
|
'no splat array' => [
|
|
|
|
'insert' => ['a', 'b', 'c'],
|
|
|
|
'before' => 'def',
|
|
|
|
'strict' => true,
|
|
|
|
'splat' => false,
|
|
|
|
'expected' => ['abc', '', [1,2,3], ['a', 'b', 'c'], 'def', '0', null, true, 0, 'last']
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideInsertBefore
|
|
|
|
*/
|
|
|
|
public function testInsertBefore(mixed $insert, mixed $before, bool $strict, bool $splat, array $expected): void
|
|
|
|
{
|
|
|
|
$array = ['abc', '', [1,2,3], 'def', '0', null, true, 0, 'last'];
|
|
|
|
$final = ArrayLib::insertBefore($array, $insert, $before, $strict, $splat);
|
|
|
|
$this->assertSame($expected, $final);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideInsertAfter(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'simple insertion' => [
|
|
|
|
'insert' => 'new',
|
|
|
|
'before' => 'def',
|
|
|
|
'strict' => true,
|
|
|
|
'splat' => false,
|
|
|
|
'expected' => ['abc', '', [1,2,3], 'def', 'new', '0', null, true, 0, 'last']
|
|
|
|
],
|
|
|
|
'insert after first' => [
|
|
|
|
'insert' => 'new',
|
|
|
|
'before' => 'abc',
|
|
|
|
'strict' => true,
|
|
|
|
'splat' => false,
|
|
|
|
'expected' => ['abc', 'new', '', [1,2,3], 'def', '0', null, true, 0, 'last']
|
|
|
|
],
|
|
|
|
'insert after last' => [
|
|
|
|
'insert' => 'new',
|
|
|
|
'before' => 'last',
|
|
|
|
'strict' => true,
|
|
|
|
'splat' => false,
|
|
|
|
'expected' => ['abc', '', [1,2,3], 'def', '0', null, true, 0, 'last', 'new']
|
|
|
|
],
|
|
|
|
'insert after missing' => [
|
|
|
|
'insert' => 'new',
|
|
|
|
'before' => 'this value isnt there',
|
|
|
|
'strict' => true,
|
|
|
|
'splat' => false,
|
|
|
|
'expected' => ['abc', '', [1,2,3], 'def', '0', null, true, 0, 'last', 'new']
|
|
|
|
],
|
|
|
|
'strict' => [
|
|
|
|
'insert' => 'new',
|
|
|
|
'before' => 0,
|
|
|
|
'strict' => true,
|
|
|
|
'splat' => false,
|
|
|
|
'expected' => ['abc', '', [1,2,3], 'def', '0', null, true, 0, 'new', 'last']
|
|
|
|
],
|
|
|
|
'not strict' => [
|
|
|
|
'insert' => 'new',
|
|
|
|
'before' => 0,
|
|
|
|
'strict' => false,
|
|
|
|
'splat' => false,
|
|
|
|
'expected' => ['abc', '', [1,2,3], 'def', '0', 'new', null, true, 0, 'last']
|
|
|
|
],
|
|
|
|
'after array' => [
|
|
|
|
'insert' => 'new',
|
|
|
|
'before' => [1,2,3],
|
|
|
|
'strict' => true,
|
|
|
|
'splat' => false,
|
|
|
|
'expected' => ['abc', '', [1,2,3], 'new', 'def', '0', null, true, 0, 'last']
|
|
|
|
],
|
|
|
|
'after missing array' => [
|
|
|
|
'insert' => 'new',
|
|
|
|
'before' => ['a', 'b', 'c'],
|
|
|
|
'strict' => true,
|
|
|
|
'splat' => false,
|
|
|
|
'expected' => ['abc', '', [1,2,3], 'def', '0', null, true, 0, 'last', 'new']
|
|
|
|
],
|
|
|
|
'splat array' => [
|
|
|
|
'insert' => ['a', 'b', 'c'],
|
|
|
|
'before' => 'def',
|
|
|
|
'strict' => true,
|
|
|
|
'splat' => true,
|
|
|
|
'expected' => ['abc', '', [1,2,3], 'def', 'a', 'b', 'c', '0', null, true, 0, 'last']
|
|
|
|
],
|
|
|
|
'no splat array' => [
|
|
|
|
'insert' => ['a', 'b', 'c'],
|
|
|
|
'before' => 'def',
|
|
|
|
'strict' => true,
|
|
|
|
'splat' => false,
|
|
|
|
'expected' => ['abc', '', [1,2,3], 'def', ['a', 'b', 'c'], '0', null, true, 0, 'last']
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideInsertAfter
|
|
|
|
*/
|
|
|
|
public function testInsertAfter(mixed $insert, mixed $after, bool $strict, bool $splat, array $expected): void
|
|
|
|
{
|
|
|
|
$array = ['abc', '', [1,2,3], 'def', '0', null, true, 0, 'last'];
|
|
|
|
$final = ArrayLib::insertAfter($array, $insert, $after, $strict, $splat);
|
|
|
|
$this->assertSame($expected, $final);
|
|
|
|
}
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|