From b62f1169bbb72cc820acf6f5ebc70772bb91023f Mon Sep 17 00:00:00 2001 From: samokspv Date: Fri, 23 May 2014 15:04:39 +0300 Subject: [PATCH] Check is_callable after all other checks, tests refactoring, see #2 --- Test/Case/Utility/ArraySortTest.php | 342 +++++++++++++--------------- Utility/ArraySort.php | 6 +- 2 files changed, 156 insertions(+), 192 deletions(-) diff --git a/Test/Case/Utility/ArraySortTest.php b/Test/Case/Utility/ArraySortTest.php index 38f199a..9de61ae 100644 --- a/Test/Case/Utility/ArraySortTest.php +++ b/Test/Case/Utility/ArraySortTest.php @@ -14,202 +14,166 @@ public function setUp() { parent::setUp(); } - public function testMultisort() { - $expected = array( - 'item1' => array( - 'weight' => 4, - 'diff' => array( - 1 => 10, - 7 => -5, - 30 => 0 - ) - ), - 'item2' => array( - 'weight' => 3, - 'diff' => array( - 1 => 10, - 7 => -5, - 30 => 0 - ) - ), - 'item3' => array( - 'weight' => 3, - 'diff' => array( - 1 => 8, - 7 => -5, - 30 => 0 - ) - ), - 'item4' => array( - 'weight' => 3, - 'diff' => array( - 1 => 8, - 7 => -10, - 30 => 0 - ) - ), - 'item5' => array( - 'weight' => 3, - 'diff' => array( - 1 => 8, - 7 => -10, - 30 => -1 - ) - ), - 'item6' => array( - 'weight' => 2, - 'diff' => array( - 1 => 3, - 7 => 4, - 30 => 5 - ) - ), - 'item7' => array( - 'weight' => 1, - 'diff' => array( - 1 => 30, - 7 => 40, - 30 => 50 - ) - ), - 'item8' => array( - 'weight' => 1, - 'diff' => array( - 1 => false, - 7 => false, - 30 => false - ) - ) - ); - - $test = $expected; - - $ashuffle = function (&$array) { - $keys = array_keys($array); - shuffle($keys); - $array = array_merge(array_flip($keys), $array); - return true; - }; - - $ashuffle($test); - - $params = array( - 'weight' => 'desc', - 'diff.1' => 'desc', - 'diff.7' => 'desc', - 'diff.30' => 'desc' - ); - $this->assertNotSame($expected, $test); - $this->assertSame($expected, ArraySort::multisort($test, $params)); - - $expected = array(1, 2, 3, 4, 5, 6, 7, 8, 9); - $test = $expected; - shuffle($test); - $params = 'asc'; - $this->assertNotSame($expected, $test); - $this->assertSame($expected, ArraySort::multisort($test, $params)); - - $expected = array( - 'item1' => 1, - 'item2' => 2, - 'item3' => 3, - 'item4' => 4, - 'item5' => 5 - ); - $test = $expected; - $ashuffle($test); - if ($test === $expected) { - $ashuffle($test); - } - $params = 'asc'; - $this->assertNotSame($expected, $test); - $this->assertSame($expected, ArraySort::multisort($test, $params)); - } - - /** - * Test multisort for objects - */ - public function testMultisortObjectByField() { - $Object1 = (object)array( - 'weight' => 1 - ); - $Object2 = (object)array( - 'weight' => 2 - ); - $array = array( - $Object1, - $Object2 - ); - - $result = array( - $Object2, - $Object1 - ); - - $this->assertSame($result, ArraySort::multisort($array, array('weight' => 'DESC'))); - } - /** - * Test multisort for objects + * Test multisort + * + * @param string $testable + * @param mixed $expected + * @dataProvider testMultisortProvider */ - public function testMultisortObjectByMethod() { - $Object1 = new _ArraySortObject(1); - $Object2 = new _ArraySortObject(2); - $array = array( - $Object1, - $Object2 - ); - - $result = array( - $Object2, - $Object1 - ); + public function testMultisort($testable, $expected, $sort) { + if (empty($testable)) { + $testable = $expected; + if (is_string(key($testable)[0])) { + $ashuffle = function (&$array) { + $keys = array_keys($array); + shuffle($keys); + $array = array_merge(array_flip($keys), $array); + return true; + }; + $ashuffle($testable); + } else { + shuffle($testable); + } + } - $this->assertSame($result, ArraySort::multisort($array, array('getWeight' => 'DESC'))); + $this->assertNotSame($expected, $testable); + $this->assertSame($expected, ArraySort::multisort($testable, $sort)); } /** - * Test multisort for objects + * data provider for testMultisortProvider + * + * @return array */ - public function testMultisortObjectByCallable() { - $Object1 = new _ArraySortObject(1); - $Object2 = new _ArraySortObject(2); - $array = array( - $Object1, - $Object2 - ); - - $result = array( - $Object2, - $Object1 - ); - - $this->assertSame($result, ArraySort::multisort($array, array( + public static function testMultisortProvider() { + return array( + // set #0 + array( + array( + array( + 'trend' => 'France', + 'count' => (int) 181 + ), array( - 'field' => function($Object) { - return $Object->getWeight(); - }, - 'direction' => 'desc' + 'trend' => 'Brazil', + 'count' => (int) 121 + ), + array( + 'trend' => 'India', + 'count' => (int) 601 ) - ))); - } - -} - -//@codingStandardsIgnoreStart -if (!class_exists('_ArraySortObject')) { - - class _ArraySortObject { - - function __construct($weight) { - $this->weight = $weight; - } - - function getWeight() { - return $this->weight; - } - + ), + array( + array( + 'trend' => 'India', + 'count' => (int) 601 + ), + array( + 'trend' => 'France', + 'count' => (int) 181 + ), + array( + 'trend' => 'Brazil', + 'count' => (int) 121 + ) + ), + array('count' => 'desc') + ), + // set #1 + array( + array(), + array( + 'item1' => 1, + 'item2' => 2, + 'item3' => 3, + 'item4' => 4, + 'item5' => 5 + ), + array('sort' => 'asc') + ), + // set #2 + array( + array(), + array(1, 2, 3, 4, 5, 6, 7, 8, 9), + array('sort' => 'asc') + ), + // set #3 + array( + array(), + array( + 'item1' => array( + 'weight' => 4, + 'diff' => array( + 1 => 10, + 7 => -5, + 30 => 0 + ) + ), + 'item2' => array( + 'weight' => 3, + 'diff' => array( + 1 => 10, + 7 => -5, + 30 => 0 + ) + ), + 'item3' => array( + 'weight' => 3, + 'diff' => array( + 1 => 8, + 7 => -5, + 30 => 0 + ) + ), + 'item4' => array( + 'weight' => 3, + 'diff' => array( + 1 => 8, + 7 => -10, + 30 => 0 + ) + ), + 'item5' => array( + 'weight' => 3, + 'diff' => array( + 1 => 8, + 7 => -10, + 30 => -1 + ) + ), + 'item6' => array( + 'weight' => 2, + 'diff' => array( + 1 => 3, + 7 => 4, + 30 => 5 + ) + ), + 'item7' => array( + 'weight' => 1, + 'diff' => array( + 1 => 30, + 7 => 40, + 30 => 50 + ) + ), + 'item8' => array( + 'weight' => 1, + 'diff' => array( + 1 => false, + 7 => false, + 30 => false + ) + ) + ), + array( + 'weight' => 'desc', + 'diff.1' => 'desc', + 'diff.7' => 'desc', + 'diff.30' => 'desc' + ) + ) + ); } - -} -//@codingStandardsIgnoreEnd +} \ No newline at end of file diff --git a/Utility/ArraySort.php b/Utility/ArraySort.php index e11ddd6..9f22d8a 100644 --- a/Utility/ArraySort.php +++ b/Utility/ArraySort.php @@ -63,9 +63,6 @@ public static function multisort($array, $params) { protected static function _getValue($from, $subject) { $value = null; switch (true) { - case is_callable($from): - $value = call_user_func($from, $subject); - break; case is_array($subject): $value = Set::get($subject, $from); break; @@ -81,6 +78,9 @@ protected static function _getValue($from, $subject) { case is_numeric($subject) || is_string($subject): $value = $subject; break; + case is_callable($from): + $value = call_user_func($from, $subject); + break; default: throw new InvalidArgumentException('Wrong type: ' . gettype($subject)); }