Skip to content

Commit

Permalink
Adding a shortcut collection method
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Aug 13, 2014
1 parent 55a9bbc commit 2dc3782
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/basics.php
Expand Up @@ -16,6 +16,7 @@
use Cake\Core\Configure;
use Cake\I18n\I18n;
use Cake\Utility\Debugger;
use Cake\Collection\Collection;

/**
* Basic defines for timing functions.
Expand Down Expand Up @@ -461,3 +462,17 @@ function __x($context, $singular, $args = null) {
}

}

if (!function_exists('collection')) {

/**
* Returns a new Cake\Collection\Collection object wrapping the passed argument
*
* @param \Traversable|array $items
* @return \Cake\Collection\Collection
*/
function collection($items) {
return new Collection($items);
}

}
13 changes: 13 additions & 0 deletions tests/TestCase/BasicsTest.php
Expand Up @@ -513,4 +513,17 @@ public function testStackTrace() {
$result = ob_get_clean();
$this->assertEquals($expected, $result);
}

/**
* Tests that the collection() method is a shortcut for new Collection
*
* @return void
*/
public function testCollection() {
$items = [1, 2, 3];
$collection = collection($items);
$this->assertInstanceOf('Cake\Collection\Collection', $collection);
$this->assertSame($items, $collection->toArray());
}

}

0 comments on commit 2dc3782

Please sign in to comment.