diff --git a/Cake/Collection/CollectionTrait.php b/Cake/Collection/CollectionTrait.php index 7947946ad3e..26819480b3b 100644 --- a/Cake/Collection/CollectionTrait.php +++ b/Cake/Collection/CollectionTrait.php @@ -603,6 +603,17 @@ public function firstMatch(array $conditions) { return null; } +/** + * Returns the first result in this collection + * + * @return mixed The first value in the collection will be returned. + */ + public function first() { + foreach ($this->take(1) as $result) { + return $result; + } + } + /** * Returns a new collection as the result of concatenating the list of elements * in this collection with the passed list of elements diff --git a/Cake/ORM/Query.php b/Cake/ORM/Query.php index 7915fbc746d..2a9ae11dae6 100644 --- a/Cake/ORM/Query.php +++ b/Cake/ORM/Query.php @@ -638,7 +638,7 @@ public function first() { } $this->bufferResults(); $this->_results = $this->execute(); - return $this->_results->one(); + return $this->_results->first(); } /** diff --git a/Cake/ORM/ResultSet.php b/Cake/ORM/ResultSet.php index cae14f43370..3497d103338 100644 --- a/Cake/ORM/ResultSet.php +++ b/Cake/ORM/ResultSet.php @@ -213,7 +213,7 @@ public function valid() { * * @return array|object */ - public function one() { + public function first() { if ($this->valid()) { if ($this->_statement) { $this->_statement->closeCursor(); diff --git a/Cake/ORM/ResultSetDecorator.php b/Cake/ORM/ResultSetDecorator.php index 3d76fdeafcf..786b51df18a 100644 --- a/Cake/ORM/ResultSetDecorator.php +++ b/Cake/ORM/ResultSetDecorator.php @@ -16,12 +16,13 @@ */ namespace Cake\ORM; -use \ArrayIterator; -use \Countable; -use \IteratorAggregate; -use \JsonSerializable; -use \Serializable; -use \Traversable; +use ArrayIterator; +use Cake\Collection\Collection; +use Countable; +use IteratorIterator; +use JsonSerializable; +use Serializable; +use Traversable; /** * Generic ResultSet decorator. This will make any traversable object appear to @@ -29,70 +30,42 @@ * * @return void */ -class ResultSetDecorator implements Countable, IteratorAggregate, Serializable, JsonSerializable { - - use ResultCollectionTrait; - -/** - * Holds the records after an instance of this object has been unserialized - * - * @var array - */ - protected $_results; +class ResultSetDecorator extends Collection implements Countable, Serializable, JsonSerializable { /** - * Constructor + * Make this object countable. * - * @param Traversable $results - */ - public function __construct(Traversable $results) { - $this->_results = $results; - } - -/** - * Returns the inner iterator this decorator is wrapping + * Part of the Countable interface. Calling this method + * will convert the underlying traversable object into an array and + * get the count of the underlying data. * - * @return \Iterator + * @return integer. */ - public function getIterator() { - if (is_array($this->_results)) { - $this->_results = new ArrayIterator($this->_results); - } - return $this->_results; + public function count() { + return count(iterator_to_array($this)); } /** - * Get a single result from the results. + * Serialize a resultset. * - * Calling this method will convert the underlying data into - * an array and will return the first result in the data set. + * Part of Serializable interface. * - * @return mixed The first value in the results will be returned. + * @return string Serialized object */ - public function one() { - if (!is_array($this->_results)) { - $this->_results = iterator_to_array($this->_results); - } - if (count($this->_results) < 1) { - return false; - } - return current($this->_results); + public function serialize() { + return serialize($this->toArray()); } /** - * Make this object countable. + * Unserialize a resultset. * - * Part of the Countable interface. Calling this method - * will convert the underlying traversable object into an array and - * get the count of the underlying data. + * Part of Serializable interface. * - * @return integer. + * @param string Serialized object + * @return ResultSet The hydrated result set. */ - public function count() { - if (!is_array($this->_results)) { - $this->_results = iterator_to_array($this->_results); - } - return count($this->_results); + public function unserialize($serialized) { + parent::__construct(unserialize($serialized)); } } diff --git a/Cake/Test/TestCase/ORM/QueryTest.php b/Cake/Test/TestCase/ORM/QueryTest.php index 58685fc7804..d3af2dd3045 100644 --- a/Cake/Test/TestCase/ORM/QueryTest.php +++ b/Cake/Test/TestCase/ORM/QueryTest.php @@ -867,23 +867,6 @@ public function testSetResult() { $this->assertSame($results, $query->execute()); } -/** - * Test enabling buffering of results. - * - * @return void - */ - public function testBufferResults() { - $this->markTestIncomplete(); - $table = TableRegistry::get('articles', ['table' => 'articles']); - $query = new Query($this->connection, $table); - - $result = $query->select()->bufferResults(); - $this->assertSame($query, $result, 'Query should be the same'); - $result = $query->execute(); - $this->assertInstanceOf('Cake\ORM\BufferedResultSet', $result); - $result->toArray(); - } - /** * Tests that applying array options to a query will convert them * to equivalent function calls with the correspondent array values @@ -1089,7 +1072,6 @@ function($k, $v, $mr) { * @return void */ public function testFirstDirtyQuery() { - $this->markTestIncomplete(); $table = TableRegistry::get('articles', ['table' => 'articles']); $query = new Query($this->connection, $table); $result = $query->select(['id'])->hydrate(false)->first(); @@ -1404,7 +1386,6 @@ public function testHydrateBelongsToCustomEntity() { * @return void */ public function testCount() { - $this->markTestIncomplete(); $table = TableRegistry::get('articles'); $result = $table->find('all')->count(); $this->assertEquals(3, $result); @@ -1415,7 +1396,7 @@ public function testCount() { $this->assertEquals(2, $result); $result = $query->execute(); - $this->assertEquals(['count' => 2], $result->one()); + $this->assertEquals(['count' => 2], $result->first()); } /** diff --git a/Cake/Test/TestCase/ORM/ResultSetDecoratorTest.php b/Cake/Test/TestCase/ORM/ResultSetDecoratorTest.php index cfaa3816f2c..f36e1eda298 100644 --- a/Cake/Test/TestCase/ORM/ResultSetDecoratorTest.php +++ b/Cake/Test/TestCase/ORM/ResultSetDecoratorTest.php @@ -71,16 +71,16 @@ public function testSerialization() { } /** - * Test the one() method which is part of the ResultSet duck type. + * Test the first() method which is part of the ResultSet duck type. * * @return void */ - public function testOne() { + public function testFirst() { $data = new \ArrayIterator([1, 2, 3]); $decorator = new ResultSetDecorator($data); - $this->assertEquals(1, $decorator->one()); - $this->assertEquals(1, $decorator->one()); + $this->assertEquals(1, $decorator->first()); + $this->assertEquals(1, $decorator->first()); } /** diff --git a/Cake/Test/TestCase/ORM/ResultSetTest.php b/Cake/Test/TestCase/ORM/ResultSetTest.php index 3df184aefd4..0b0923b6524 100644 --- a/Cake/Test/TestCase/ORM/ResultSetTest.php +++ b/Cake/Test/TestCase/ORM/ResultSetTest.php @@ -154,36 +154,36 @@ public function testJsonSerialize() { } /** - * Test one() method with a statement backed result set. + * Test first() method with a statement backed result set. * * @return void */ - public function testOne() { + public function testFirst() { $query = $this->table->find('all'); $results = $query->hydrate(false)->execute(); - $row = $results->one(); + $row = $results->first(); $this->assertEquals($this->fixtureData[0], $row); - $row = $results->one(); + $row = $results->first(); $this->assertEquals($this->fixtureData[0], $row); } /** - * Test one() method with a result set that has been unserialized + * Test first() method with a result set that has been unserialized * * @return void */ - public function testOneAfterSerialize() { + public function testFirstAfterSerialize() { $query = $this->table->find('all'); $results = $query->hydrate(false)->execute(); $results = unserialize(serialize($results)); - $row = $results->one(); + $row = $results->first(); $this->assertEquals($this->fixtureData[0], $row); - $this->assertNull($results->one(), 'No more rows.'); - $this->assertNull($results->one(), 'No more rows.'); + $this->assertNull($results->first(), 'No more rows.'); + $this->assertNull($results->first(), 'No more rows.'); } /** diff --git a/Cake/Test/TestCase/ORM/TableTest.php b/Cake/Test/TestCase/ORM/TableTest.php index b45f00920df..0e932321af5 100644 --- a/Cake/Test/TestCase/ORM/TableTest.php +++ b/Cake/Test/TestCase/ORM/TableTest.php @@ -1648,7 +1648,7 @@ public function testDeleteDependent() { 'author_id' => $entity->id ] ]); - $this->assertNull($query->execute()->one(), 'Should not find any rows.'); + $this->assertNull($query->execute()->first(), 'Should not find any rows.'); } /** @@ -1689,7 +1689,7 @@ public function testDeleteBelongsToMany() { $junction = $table->association('tags')->junction(); $query = $junction->find('all')->where(['article_id' => 1]); - $this->assertNull($query->execute()->one(), 'Should not find any rows.'); + $this->assertNull($query->execute()->first(), 'Should not find any rows.'); } /**