Browse Source

Adding a shortcut collection method

Jose Lorenzo Rodriguez 11 years ago
parent
commit
2dc378289d
2 changed files with 28 additions and 0 deletions
  1. 15 0
      src/basics.php
  2. 13 0
      tests/TestCase/BasicsTest.php

+ 15 - 0
src/basics.php

@@ -16,6 +16,7 @@ use Cake\Cache\Cache;
 use Cake\Core\Configure;
 use Cake\I18n\I18n;
 use Cake\Utility\Debugger;
+use Cake\Collection\Collection;
 
 /**
  * Basic defines for timing functions.
@@ -461,3 +462,17 @@ if (!function_exists('__x')) {
 	}
 
 }
+
+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 - 0
tests/TestCase/BasicsTest.php

@@ -513,4 +513,17 @@ EXPECTED;
 		$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());
+	}
+
 }