Skip to content

Array unit tests #873

Merged
merged 8 commits into from Mar 22, 2012
View
5 classes/arr.php
@@ -481,6 +481,11 @@ public static function sort($array, $key, $order = 'asc', $sort_flags = SORT_REG
throw new \InvalidArgumentException('Arr::sort() - $array must be an array.');
}
+ if (empty($array))
+ {
+ return $array;
+ }
+
foreach ($array as $k=>$v)
{
$b[$k] = static::get($v, $key);
View
5 classes/format.php
@@ -102,6 +102,11 @@ public function to_array($data = null)
$data = get_object_vars($data);
}
+ if (empty($data))
+ {
+ return array();
+ }
+
foreach ($data as $key => $value)
{
if (is_object($value) or is_array($value))
View
7 tests/arr.php
@@ -492,6 +492,13 @@ public function test_sort_invalid_direction($data, $expected)
$this->assertEquals(Arr::sort($data, 'info.pet.type', 'downer'), $expected);
}
+ public function test_sort_empty()
+ {
+ $expected = array();
+ $output = Arr::sort(array(), 'test', 'test');
+ $this->assertEquals($expected, $output);
+ }
+
/**
* Tests Arr::filter_keys()
*
View
7 tests/format.php
@@ -151,4 +151,11 @@ public function test__from_xml()
$this->assertEquals(Format::forge($expected)->to_php(), Format::forge($xml, 'xml')->to_php());
}
+
+ function test_to_array_empty()
+ {
+ $array = null;
+ $expected = array();
+ $this->assertEquals($expected, Format::forge($array)->to_array());
+ }
}
Something went wrong with that request. Please try again.