Commit cc5618c0 by Alexander Makarov

More tests for ArrayHelper and Inflector

parent 8f2ce5da
...@@ -24,6 +24,17 @@ class Post2 extends Object ...@@ -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 class ArrayHelperTest extends TestCase
{ {
public function testToArray() public function testToArray()
...@@ -57,6 +68,16 @@ class ArrayHelperTest extends TestCase ...@@ -57,6 +68,16 @@ class ArrayHelperTest extends TestCase
return strlen($post->content); 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() public function testRemove()
...@@ -66,6 +87,9 @@ class ArrayHelperTest extends TestCase ...@@ -66,6 +87,9 @@ class ArrayHelperTest extends TestCase
$this->assertEquals($name, 'b'); $this->assertEquals($name, 'b');
$this->assertEquals($array, array('age' => 3)); $this->assertEquals($array, array('age' => 3));
$default = ArrayHelper::remove($array, 'nonExisting', 'defaultValue');
$this->assertEquals('defaultValue', $default);
} }
...@@ -150,4 +174,123 @@ class ArrayHelperTest extends TestCase ...@@ -150,4 +174,123 @@ class ArrayHelperTest extends TestCase
$this->assertEquals(array('name' => 'a', 'age' => 1), $array[1]); $this->assertEquals(array('name' => 'a', 'age' => 1), $array[1]);
$this->assertEquals(array('name' => 'b', 'age' => 3), $array[2]); $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);
}
} }
...@@ -131,5 +131,11 @@ class InflectorTest extends TestCase ...@@ -131,5 +131,11 @@ class InflectorTest extends TestCase
public function testOrdinalize() public function testOrdinalize()
{ {
$this->assertEquals('21st', Inflector::ordinalize('21')); $this->assertEquals('21st', Inflector::ordinalize('21'));
$this->assertEquals('22nd', Inflector::ordinalize('22'));
$this->assertEquals('23rd', Inflector::ordinalize('23'));
$this->assertEquals('24th', Inflector::ordinalize('24'));
$this->assertEquals('25th', Inflector::ordinalize('25'));
$this->assertEquals('111th', Inflector::ordinalize('111'));
$this->assertEquals('113th', Inflector::ordinalize('113'));
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment