|
|
|
@ -24,6 +24,17 @@ class Post2 extends Object
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
class Post3 extends Object |
|
|
|
|
{ |
|
|
|
|
public $id = 33; |
|
|
|
|
public $subObject; |
|
|
|
|
|
|
|
|
|
public function init() |
|
|
|
|
{ |
|
|
|
|
$this->subObject = new Post2(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
class ArrayHelperTest extends TestCase |
|
|
|
|
{ |
|
|
|
|
public function testToArray() |
|
|
|
@ -57,6 +68,16 @@ class ArrayHelperTest extends TestCase
|
|
|
|
|
return strlen($post->content); |
|
|
|
|
} |
|
|
|
|
)))); |
|
|
|
|
|
|
|
|
|
$object = new Post3(); |
|
|
|
|
$this->assertEquals(get_object_vars($object), ArrayHelper::toArray($object, array(), false)); |
|
|
|
|
$this->assertEquals(array( |
|
|
|
|
'id' => 33, |
|
|
|
|
'subObject' => array( |
|
|
|
|
'id' => 123, |
|
|
|
|
'content' => 'test', |
|
|
|
|
), |
|
|
|
|
), ArrayHelper::toArray($object)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public function testRemove() |
|
|
|
@ -66,6 +87,9 @@ class ArrayHelperTest extends TestCase
|
|
|
|
|
|
|
|
|
|
$this->assertEquals($name, 'b'); |
|
|
|
|
$this->assertEquals($array, array('age' => 3)); |
|
|
|
|
|
|
|
|
|
$default = ArrayHelper::remove($array, 'nonExisting', 'defaultValue'); |
|
|
|
|
$this->assertEquals('defaultValue', $default); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -150,4 +174,123 @@ class ArrayHelperTest extends TestCase
|
|
|
|
|
$this->assertEquals(array('name' => 'a', 'age' => 1), $array[1]); |
|
|
|
|
$this->assertEquals(array('name' => 'b', 'age' => 3), $array[2]); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public function testMerge() |
|
|
|
|
{ |
|
|
|
|
$a = array( |
|
|
|
|
'name' => 'Yii', |
|
|
|
|
'version' => '1.0', |
|
|
|
|
'options' => array( |
|
|
|
|
'namespace' => false, |
|
|
|
|
'unittest' => false, |
|
|
|
|
), |
|
|
|
|
'features' => array( |
|
|
|
|
'mvc', |
|
|
|
|
), |
|
|
|
|
); |
|
|
|
|
$b = array( |
|
|
|
|
'version' => '1.1', |
|
|
|
|
'options' => array( |
|
|
|
|
'unittest' => true, |
|
|
|
|
), |
|
|
|
|
'features' => array( |
|
|
|
|
'gii', |
|
|
|
|
), |
|
|
|
|
); |
|
|
|
|
$c = array( |
|
|
|
|
'version' => '2.0', |
|
|
|
|
'options' => array( |
|
|
|
|
'namespace' => true, |
|
|
|
|
), |
|
|
|
|
'features' => array( |
|
|
|
|
'debug', |
|
|
|
|
), |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
$result = ArrayHelper::merge($a, $b, $c); |
|
|
|
|
$expected = array( |
|
|
|
|
'name' => 'Yii', |
|
|
|
|
'version' => '2.0', |
|
|
|
|
'options' => array( |
|
|
|
|
'namespace' => true, |
|
|
|
|
'unittest' => true, |
|
|
|
|
), |
|
|
|
|
'features' => array( |
|
|
|
|
'mvc', |
|
|
|
|
'gii', |
|
|
|
|
'debug', |
|
|
|
|
), |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $result); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public function testIndex() |
|
|
|
|
{ |
|
|
|
|
$array = array( |
|
|
|
|
array('id' => '123', 'data' => 'abc'), |
|
|
|
|
array('id' => '345', 'data' => 'def'), |
|
|
|
|
); |
|
|
|
|
$result = ArrayHelper::index($array, 'id'); |
|
|
|
|
$this->assertEquals(array( |
|
|
|
|
'123' => array('id' => '123', 'data' => 'abc'), |
|
|
|
|
'345' => array('id' => '345', 'data' => 'def'), |
|
|
|
|
), $result); |
|
|
|
|
|
|
|
|
|
$result = ArrayHelper::index($array, function ($element) { |
|
|
|
|
return $element['data']; |
|
|
|
|
}); |
|
|
|
|
$this->assertEquals(array( |
|
|
|
|
'abc' => array('id' => '123', 'data' => 'abc'), |
|
|
|
|
'def' => array('id' => '345', 'data' => 'def'), |
|
|
|
|
), $result); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public function testGetColumn() |
|
|
|
|
{ |
|
|
|
|
$array = array( |
|
|
|
|
'a' => array('id' => '123', 'data' => 'abc'), |
|
|
|
|
'b' => array('id' => '345', 'data' => 'def'), |
|
|
|
|
); |
|
|
|
|
$result = ArrayHelper::getColumn($array, 'id'); |
|
|
|
|
$this->assertEquals(array('a' => '123', 'b' => '345'), $result); |
|
|
|
|
$result = ArrayHelper::getColumn($array, 'id', false); |
|
|
|
|
$this->assertEquals(array('123', '345'), $result); |
|
|
|
|
|
|
|
|
|
$result = ArrayHelper::getColumn($array, function ($element) { |
|
|
|
|
return $element['data']; |
|
|
|
|
}); |
|
|
|
|
$this->assertEquals(array('a' => 'abc', 'b' => 'def'), $result); |
|
|
|
|
$result = ArrayHelper::getColumn($array, function ($element) { |
|
|
|
|
return $element['data']; |
|
|
|
|
}, false); |
|
|
|
|
$this->assertEquals(array('abc', 'def'), $result); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public function testMap() |
|
|
|
|
{ |
|
|
|
|
$array = array( |
|
|
|
|
array('id' => '123', 'name' => 'aaa', 'class' => 'x'), |
|
|
|
|
array('id' => '124', 'name' => 'bbb', 'class' => 'x'), |
|
|
|
|
array('id' => '345', 'name' => 'ccc', 'class' => 'y'), |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
$result = ArrayHelper::map($array, 'id', 'name'); |
|
|
|
|
$this->assertEquals(array( |
|
|
|
|
'123' => 'aaa', |
|
|
|
|
'124' => 'bbb', |
|
|
|
|
'345' => 'ccc', |
|
|
|
|
), $result); |
|
|
|
|
|
|
|
|
|
$result = ArrayHelper::map($array, 'id', 'name', 'class'); |
|
|
|
|
$this->assertEquals(array( |
|
|
|
|
'x' => array( |
|
|
|
|
'123' => 'aaa', |
|
|
|
|
'124' => 'bbb', |
|
|
|
|
), |
|
|
|
|
'y' => array( |
|
|
|
|
'345' => 'ccc', |
|
|
|
|
), |
|
|
|
|
), $result); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|