Skip to content

Commit

Permalink
Fixing regression in SortByIterator, closes #6349
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Apr 16, 2015
1 parent 3a3de18 commit c296a3b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/Collection/Iterator/SortIterator.php
Expand Up @@ -59,10 +59,11 @@ class SortIterator extends Collection
*/
public function __construct($items, $callback, $dir = SORT_DESC, $type = SORT_NUMERIC)
{
if ($items instanceof CollectionInterface) {
$items = $items->toList();
if (is_array($items)) {
$items = new Collection($items);
}

$items = iterator_to_array($items, false);
$callback = $this->_propertyExtractor($callback);
$results = [];
foreach ($items as $key => $value) {
Expand Down
21 changes: 21 additions & 0 deletions tests/TestCase/Collection/CollectionTest.php
Expand Up @@ -1164,6 +1164,27 @@ public function testThroughReturnArray()
$this->assertEquals([1, 2, 3, 1, 2, 3], $collection->toList());
}

/**
* Tests that the sortBy method does not die when something that is not a
* collection is passed
*
* @return void
*/
public function testComplexSortBy()
{
$results = collection([3, 7])
->unfold(function ($value) {
return [
['sorting' => $value * 2],
['sorting' => $value * 2]
];
})
->sortBy('sorting')
->extract('sorting')
->toList();
$this->assertEquals([14, 14, 6, 6], $results);
}

/**
* Tests __debugInfo() or debug() usage
*
Expand Down

0 comments on commit c296a3b

Please sign in to comment.