Skip to content

Commit

Permalink
Check is_callable after all other checks, tests refactoring, see #2
Browse files Browse the repository at this point in the history
  • Loading branch information
samokspv committed May 23, 2014
1 parent 9031970 commit b62f116
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 192 deletions.
342 changes: 153 additions & 189 deletions Test/Case/Utility/ArraySortTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
6 changes: 3 additions & 3 deletions Utility/ArraySort.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
}
Expand Down

0 comments on commit b62f116

Please sign in to comment.