Permalink
Browse files

fixes #75 and fixes #76. Errors introduced by new Collection::sort()…

… method.
  • Loading branch information...
gwoo committed Aug 16, 2011
1 parent d6a230a commit ee2dffd7e4e340e8679d6406382bffbd93b13d1a
Showing with 8 additions and 8 deletions.
  1. +7 −6 tests/cases/util/CollectionTest.php
  2. +1 −2 util/Collection.php
@@ -265,7 +265,7 @@ function($i) { return is_array($i) ? join(',', $i) : $i; }, $collection->to('arr
$expected = 'hello,goodbye,bar,dib';
$this->assertEqual($expected, $result);
}
-
+
/**
* Tests that the Collection::sort method works appropriately.
*
@@ -277,28 +277,29 @@ public function testCollectionSort() {
$collection->sort();
$expected = array(1,2,3,4,5);
$this->assertEqual($expected, $collection->to('array'));
-
+
// String sort using "sort"
$collection = new Collection(array('data' => array('alan','dave','betsy','carl')));
$collection->sort();
$expected = array('alan','betsy','carl','dave');
$this->assertEqual($expected, $collection->to('array'));
-
+
// String sort using strcasecmp
$collection = new Collection(array('data' => array('Alan','Dave','betsy','carl')));
$collection->sort('strcasecmp');
$expected = array('Alan','betsy','carl','Dave');
$this->assertEqual($expected, $collection->to('array'));
-
+
// Numeric sort using custom function
$collection = new Collection(array('data' => array(5,3,4,1,2)));
$collection->sort(function ($a,$b) { if ($a == $b) { return 0; } return ($b > $a ? 1 : -1); });
$expected = array(5,4,3,2,1);
$this->assertEqual($expected, $collection->to('array'));
-
+
// Test fail
$collection = new Collection(array('data' => array(5,3,4,1,2)));
- $this->assertFalse($collection->sort('blahgah'));
+ $result = $collection->sort('blahgah');
+ $this->assertEqual($collection->to('array'), $result->to('array'));
}
}
View
@@ -364,11 +364,10 @@ public function map($filter, array $options = array()) {
*/
public function sort($sorter = 'sort', array $options = array()) {
if (is_string($sorter) && strpos($sorter, 'sort') !== false && is_callable($sorter)) {
- call_user_func($sorter, &$this->_data);
+ call_user_func_array($sorter, array(&$this->_data));
} else if (is_callable($sorter)) {
usort($this->_data, $sorter);
}
-
return $this;
}

0 comments on commit ee2dffd

Please sign in to comment.