From 4167a2953cadd4e05a62fb3fc4dadf048a64e036 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 15 Sep 2011 22:27:29 -0500 Subject: [PATCH] added tests for arr::first. --- laravel/arr.php | 26 ++++++-------------------- tests/ArrTest.php | 20 ++++++++++++++++++++ 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/laravel/arr.php b/laravel/arr.php index 522ba8ff..35c7afc0 100644 --- a/laravel/arr.php +++ b/laravel/arr.php @@ -1,5 +1,7 @@ - * // Get the "name" item from the array - * $name = Arr::get(array('name' => 'Fred'), 'name'); - * - * // Get the "age" item from the array, or return 25 if it doesn't exist - * $name = Arr::get(array('name' => 'Fred'), 'age', 25); - * - * * @param array $array * @param string $key * @param mixed $default @@ -30,7 +24,7 @@ public static function get($array, $key, $default = null) { if ( ! is_array($array) or ! array_key_exists($segment, $array)) { - return ($default instanceof \Closure) ? call_user_func($default) : $default; + return ($default instanceof Closure) ? call_user_func($default) : $default; } $array = $array[$segment]; @@ -46,11 +40,6 @@ public static function get($array, $key, $default = null) * a variable depth, such as configuration arrays. Like the Arr::get * method, JavaScript "dot" syntax is supported. * - * - * // Set an array's "name" item to "Fred" - * Arr::set($array, 'name', 'Fred'); - * - * * @param array $array * @param string $key * @param mixed $value @@ -80,21 +69,18 @@ public static function set(&$array, $key, $value) /** * Return the first element in an array which passes a given truth test. * - * - * // Get the first element in an array that is less than 2 - * $value = Arr::first(array(4, 3, 2, 1), function($key, $value) {return $value < 2;}); - * - * * @param array $array * @param Closure $callback * @return mixed */ - public static function first($array, $callback) + public static function first($array, $callback, $default = null) { foreach ($array as $key => $value) { if (call_user_func($callback, $key, $value)) return $value; } + + return ($default instanceof Closure) ? call_user_func($default) : $default; } } \ No newline at end of file diff --git a/tests/ArrTest.php b/tests/ArrTest.php index 7db6b07f..7e1c8a6f 100644 --- a/tests/ArrTest.php +++ b/tests/ArrTest.php @@ -36,6 +36,26 @@ public function testSetMethodSetsItemsInArray($array) } + /** + * @dataProvider getArray + */ + public function testFirstMethodReturnsFirstItemPassingTruthTest($array) + { + $array['email2'] = 'taylor@hotmail.com'; + + $this->assertEquals('taylorotwell@gmail.com', Arr::first($array, function($k, $v) {return substr($v, 0, 3) == 'tay';})); + } + + /** + * @dataProvider getArray + */ + public function testFirstMethodReturnsDefaultWhenNoItemExists($array) + { + $this->assertNull(Arr::first($array, function($k, $v) {return $v === 'something';})); + $this->assertEquals('default', Arr::first($array, function($k, $v) {return $v === 'something';}, 'default')); + $this->assertEquals('default', Arr::first($array, function($k, $v) {return $v === 'something';}, function() {return 'default';})); + } + public function getArray() { return array(array(