From 48b02f14273284dc9599a842f4562da0139dda6a Mon Sep 17 00:00:00 2001 From: Jose Lorenzo Rodriguez Date: Sun, 19 Jan 2014 14:41:40 +0100 Subject: [PATCH] Added test for formatResults --- tests/TestCase/ORM/QueryTest.php | 65 ++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/tests/TestCase/ORM/QueryTest.php b/tests/TestCase/ORM/QueryTest.php index fa0f96e6d43..59ed27bd351 100644 --- a/tests/TestCase/ORM/QueryTest.php +++ b/tests/TestCase/ORM/QueryTest.php @@ -1724,4 +1724,69 @@ public function testContainWithClosure() { $this->assertEquals([1], array_unique($ids)); } +/** + * Tests the formatResults method + * + * @return void + */ + public function testFormatResults() { + $callback1 = function() { + }; + $callback2 = function() { + }; + $table = TableRegistry::get('authors'); + $query = new Query($this->connection, $table); + $this->assertSame($query, $query->formatResults($callback1)); + $this->assertSame([$callback1], $query->formatResults()); + $this->assertSame($query, $query->formatResults($callback2)); + $this->assertSame([$callback1, $callback2], $query->formatResults()); + $query->formatResults($callback2, true); + $this->assertSame([$callback2], $query->formatResults()); + $query->formatResults(null, true); + $this->assertSame([], $query->formatResults()); + } + +/** + * Test fetching results from a qurey with a custom formatter + * + * @return void + */ + public function testQueryWithFormatter() { + $table = TableRegistry::get('authors'); + $query = new Query($this->connection, $table); + $query->select()->formatResults(function($results, $q) use ($query) { + $this->assertSame($query, $q); + $this->assertInstanceOf('\Cake\ORM\ResultSet', $results); + return $results->indexBy('id'); + }); + $this->assertEquals([1, 2, 3, 4], array_keys($query->toArray())); + } + +/** + * Test fetching results from a qurey with a two custom formatters + * + * @return void + */ + public function testQueryWithStackedFormatters() { + $table = TableRegistry::get('authors'); + $query = new Query($this->connection, $table); + $query->select()->formatResults(function($results, $q) use ($query) { + $this->assertSame($query, $q); + $this->assertInstanceOf('\Cake\ORM\ResultSet', $results); + return $results->indexBy('id'); + }); + + $query->formatResults(function($results) { + return $results->extract('name'); + }); + + $expected = [ + 1 => 'mariano', + 2 => 'nate', + 3 => 'larry', + 4 => 'garrett' + ]; + $this->assertEquals($expected, $query->toArray()); + } + }