2009-07-17 02:01:06 +02:00
|
|
|
<?php
|
2013-05-10 14:00:31 +02:00
|
|
|
|
2009-07-17 02:01:06 +02:00
|
|
|
/**
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2009-07-17 02:01:06 +02:00
|
|
|
* @subpackage tests
|
|
|
|
*/
|
|
|
|
class ArrayLibTest extends SapphireTest {
|
2013-05-10 14:00:31 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testInvert() {
|
2009-07-17 02:01:06 +02:00
|
|
|
$arr = array(
|
|
|
|
'row1' => array(
|
|
|
|
'col1' =>'val1',
|
|
|
|
'col2' => 'val2'
|
|
|
|
),
|
|
|
|
'row2' => array(
|
|
|
|
'col1' => 'val3',
|
|
|
|
'col2' => 'val4'
|
|
|
|
)
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-07-17 02:01:06 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
ArrayLib::invert($arr),
|
|
|
|
array(
|
|
|
|
'col1' => array(
|
|
|
|
'row1' => 'val1',
|
|
|
|
'row2' => 'val3',
|
|
|
|
),
|
|
|
|
'col2' => array(
|
|
|
|
'row1' => 'val2',
|
|
|
|
'row2' => 'val4',
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2010-10-15 05:24:32 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testValuekey() {
|
2010-10-15 05:24:32 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
ArrayLib::valuekey(
|
|
|
|
array(
|
|
|
|
'testkey1' => 'testvalue1',
|
|
|
|
'testkey2' => 'testvalue2'
|
|
|
|
)
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'testvalue1' => 'testvalue1',
|
|
|
|
'testvalue2' => 'testvalue2'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-09-21 14:30:12 +02:00
|
|
|
public function testArrayMapRecursive() {
|
|
|
|
$array = array(
|
|
|
|
'a ',
|
|
|
|
array(' b', 'c'),
|
|
|
|
);
|
|
|
|
$strtoupper = array(
|
|
|
|
'A ',
|
|
|
|
array(' B', 'C'),
|
|
|
|
);
|
|
|
|
$trim = array(
|
|
|
|
'a',
|
|
|
|
array('b', 'c'),
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
$strtoupper,
|
|
|
|
ArrayLib::array_map_recursive('strtoupper', $array)
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
$trim,
|
|
|
|
ArrayLib::array_map_recursive('trim', $array)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testArrayMergeRecursive() {
|
2011-12-22 23:04:44 +01:00
|
|
|
$first = array(
|
|
|
|
'first' => 'a',
|
|
|
|
'second' => 'b',
|
|
|
|
);
|
|
|
|
$second = array(
|
|
|
|
'third' => 'c',
|
|
|
|
'fourth' => 'd',
|
|
|
|
);
|
|
|
|
$expected = array(
|
|
|
|
'first' => 'a',
|
|
|
|
'second' => 'b',
|
|
|
|
'third' => 'c',
|
|
|
|
'fourth' => 'd',
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
$expected,
|
|
|
|
ArrayLib::array_merge_recursive($first, $second),
|
|
|
|
'First values should supplement second values'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-22 23:04:44 +01:00
|
|
|
$first = array(
|
|
|
|
'first' => 'a',
|
|
|
|
'second' => 'b',
|
|
|
|
);
|
|
|
|
$second = array(
|
|
|
|
'first' => 'c',
|
|
|
|
'third' => 'd',
|
|
|
|
);
|
|
|
|
$expected = array(
|
|
|
|
'first' => 'c',
|
|
|
|
'second' => 'b',
|
|
|
|
'third' => 'd',
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
$expected,
|
|
|
|
ArrayLib::array_merge_recursive($first, $second),
|
|
|
|
'Second values should override first values'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-22 23:04:44 +01:00
|
|
|
$first = array(
|
|
|
|
'first' => array(
|
|
|
|
'first' => 'a',
|
|
|
|
),
|
|
|
|
'second' => array(
|
|
|
|
'second' => 'b',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
$second = array(
|
|
|
|
'first' => array(
|
|
|
|
'first' => 'c',
|
|
|
|
),
|
|
|
|
'third' => array(
|
|
|
|
'third' => 'd',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
$expected = array(
|
|
|
|
'first' => array(
|
|
|
|
'first' => 'c',
|
|
|
|
),
|
|
|
|
'second' => array(
|
|
|
|
'second' => 'b',
|
|
|
|
),
|
|
|
|
'third' => array(
|
|
|
|
'third' => 'd',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
$expected,
|
|
|
|
ArrayLib::array_merge_recursive($first, $second),
|
|
|
|
'Nested second values should override first values'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-22 23:04:44 +01:00
|
|
|
$first = array(
|
|
|
|
'first' => array(
|
|
|
|
'first' => 'a',
|
|
|
|
),
|
|
|
|
'second' => array(
|
|
|
|
'second' => 'b',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
$second = array(
|
|
|
|
'first' => array(
|
|
|
|
'second' => 'c',
|
|
|
|
),
|
|
|
|
'third' => array(
|
|
|
|
'third' => 'd',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
$expected = array(
|
|
|
|
'first' => array(
|
|
|
|
'first' => 'a',
|
|
|
|
'second' => 'c',
|
|
|
|
),
|
|
|
|
'second' => array(
|
|
|
|
'second' => 'b',
|
|
|
|
),
|
|
|
|
'third' => array(
|
|
|
|
'third' => 'd',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
$expected,
|
|
|
|
ArrayLib::array_merge_recursive($first, $second),
|
|
|
|
'Nested first values should supplement second values'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-22 23:04:44 +01:00
|
|
|
$first = array(
|
|
|
|
'first' => array(
|
|
|
|
0 => 'a',
|
|
|
|
),
|
|
|
|
'second' => array(
|
|
|
|
1 => 'b',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
$second = array(
|
|
|
|
'first' => array(
|
|
|
|
0 => 'c',
|
|
|
|
),
|
|
|
|
'third' => array(
|
|
|
|
2 => 'd',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
$expected = array(
|
|
|
|
'first' => array(
|
|
|
|
0 => 'c',
|
|
|
|
),
|
|
|
|
'second' => array(
|
|
|
|
1 => 'b',
|
|
|
|
),
|
|
|
|
'third' => array(
|
|
|
|
2 => 'd',
|
|
|
|
),
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-22 23:04:44 +01:00
|
|
|
$this->assertEquals(
|
|
|
|
$expected,
|
|
|
|
ArrayLib::array_merge_recursive($first, $second),
|
|
|
|
'Numeric keys should behave like string keys'
|
|
|
|
);
|
|
|
|
}
|
2013-05-10 14:00:31 +02:00
|
|
|
|
|
|
|
public function testFlatten() {
|
|
|
|
$options = array(
|
|
|
|
'1' => 'one',
|
|
|
|
'2' => 'two'
|
|
|
|
);
|
|
|
|
|
|
|
|
$expected = $options;
|
|
|
|
|
|
|
|
$this->assertEquals($expected, ArrayLib::flatten($options));
|
|
|
|
|
|
|
|
$options = array(
|
|
|
|
'1' => array(
|
|
|
|
'2' => 'two',
|
|
|
|
'3' => 'three'
|
|
|
|
),
|
|
|
|
'4' => 'four'
|
|
|
|
);
|
|
|
|
|
|
|
|
$expected = array(
|
|
|
|
'2' => 'two',
|
|
|
|
'3' => 'three',
|
|
|
|
'4' => 'four'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals($expected, ArrayLib::flatten($options));
|
|
|
|
}
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|