2009-07-17 02:01:06 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2009-07-17 02:01:06 +02:00
|
|
|
* @subpackage tests
|
|
|
|
*/
|
|
|
|
class ArrayLibTest extends SapphireTest {
|
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'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$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'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2011-12-22 23:04:44 +01:00
|
|
|
|
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'
|
|
|
|
);
|
|
|
|
|
|
|
|
$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'
|
|
|
|
);
|
|
|
|
|
|
|
|
$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'
|
|
|
|
);
|
|
|
|
|
|
|
|
$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'
|
|
|
|
);
|
|
|
|
|
|
|
|
$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',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
$expected,
|
|
|
|
ArrayLib::array_merge_recursive($first, $second),
|
|
|
|
'Numeric keys should behave like string keys'
|
|
|
|
);
|
|
|
|
}
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|