ResultSetTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\ORM;
  16. use Cake\Core\Configure;
  17. use Cake\Datasource\ConnectionManager;
  18. use Cake\ORM\Entity;
  19. use Cake\ORM\Query;
  20. use Cake\ORM\ResultSet;
  21. use Cake\ORM\Table;
  22. use Cake\ORM\TableRegistry;
  23. use Cake\TestSuite\TestCase;
  24. /**
  25. * ResultSet test case.
  26. */
  27. class ResultSetTest extends TestCase {
  28. public $fixtures = ['core.article', 'core.comment'];
  29. /**
  30. * setup
  31. *
  32. * @return void
  33. */
  34. public function setUp() {
  35. parent::setUp();
  36. $this->connection = ConnectionManager::get('test');
  37. $this->table = new Table([
  38. 'table' => 'articles',
  39. 'connection' => $this->connection,
  40. ]);
  41. $this->fixtureData = [
  42. ['id' => 1, 'author_id' => 1, 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y'],
  43. ['id' => 2, 'author_id' => 3, 'title' => 'Second Article', 'body' => 'Second Article Body', 'published' => 'Y'],
  44. ['id' => 3, 'author_id' => 1, 'title' => 'Third Article', 'body' => 'Third Article Body', 'published' => 'Y']
  45. ];
  46. }
  47. /**
  48. * Test that result sets can be rewound and re-used.
  49. *
  50. * @return void
  51. */
  52. public function testRewind() {
  53. $query = $this->table->find('all');
  54. $results = $query->all();
  55. $first = $second = [];
  56. foreach ($results as $result) {
  57. $first[] = $result;
  58. }
  59. foreach ($results as $result) {
  60. $second[] = $result;
  61. }
  62. }
  63. /**
  64. * Test that streaming results cannot be rewound
  65. *
  66. * @return void
  67. */
  68. public function testRewindStreaming() {
  69. $query = $this->table->find('all')->bufferResults(false);
  70. $results = $query->all();
  71. $first = $second = [];
  72. foreach ($results as $result) {
  73. $first[] = $result;
  74. }
  75. $this->setExpectedException('Cake\Database\Exception');
  76. foreach ($results as $result) {
  77. $second[] = $result;
  78. }
  79. }
  80. /**
  81. * An integration test for testing serialize and unserialize features.
  82. *
  83. * Compare the results of a query with the results iterated, with
  84. * those of a different query that have been serialized/unserialized.
  85. *
  86. * @return void
  87. */
  88. public function testSerialization() {
  89. $query = $this->table->find('all');
  90. $results = $query->all();
  91. $expected = $results->toArray();
  92. $query2 = $this->table->find('all');
  93. $results2 = $query2->all();
  94. $serialized = serialize($results2);
  95. $outcome = unserialize($serialized);
  96. $this->assertEquals($expected, $outcome->toArray());
  97. }
  98. /**
  99. * Test iteration after serialization
  100. *
  101. * @return void
  102. */
  103. public function testIteratorAfterSerializationNoHydration() {
  104. $query = $this->table->find('all')->hydrate(false);
  105. $results = unserialize(serialize($query->all()));
  106. // Use a loop to test Iterator implementation
  107. foreach ($results as $i => $row) {
  108. $this->assertEquals($this->fixtureData[$i], $row, "Row $i does not match");
  109. }
  110. }
  111. /**
  112. * Test iteration after serialization
  113. *
  114. * @return void
  115. */
  116. public function testIteratorAfterSerializationHydrated() {
  117. $query = $this->table->find('all');
  118. $results = unserialize(serialize($query->all()));
  119. // Use a loop to test Iterator implementation
  120. foreach ($results as $i => $row) {
  121. $expected = new \Cake\ORM\Entity($this->fixtureData[$i]);
  122. $expected->isNew(false);
  123. $expected->source($this->table->alias());
  124. $expected->clean();
  125. $this->assertEquals($expected, $row, "Row $i does not match");
  126. }
  127. }
  128. /**
  129. * Test converting resultsets into json
  130. *
  131. * @return void
  132. */
  133. public function testJsonSerialize() {
  134. $query = $this->table->find('all');
  135. $results = $query->all();
  136. $expected = json_encode($this->fixtureData);
  137. $this->assertEquals($expected, json_encode($results));
  138. }
  139. /**
  140. * Test first() method with a statement backed result set.
  141. *
  142. * @return void
  143. */
  144. public function testFirst() {
  145. $query = $this->table->find('all');
  146. $results = $query->hydrate(false)->all();
  147. $row = $results->first();
  148. $this->assertEquals($this->fixtureData[0], $row);
  149. $row = $results->first();
  150. $this->assertEquals($this->fixtureData[0], $row);
  151. }
  152. /**
  153. * Test first() method with a result set that has been unserialized
  154. *
  155. * @return void
  156. */
  157. public function testFirstAfterSerialize() {
  158. $query = $this->table->find('all');
  159. $results = $query->hydrate(false)->all();
  160. $results = unserialize(serialize($results));
  161. $row = $results->first();
  162. $this->assertEquals($this->fixtureData[0], $row);
  163. $this->assertSame($row, $results->first());
  164. $this->assertSame($row, $results->first());
  165. }
  166. /**
  167. * Test the countable interface.
  168. *
  169. * @return void
  170. */
  171. public function testCount() {
  172. $query = $this->table->find('all');
  173. $results = $query->all();
  174. $this->assertCount(3, $results, 'Should be countable and 3');
  175. }
  176. /**
  177. * Test the countable interface after unserialize
  178. *
  179. * @return void
  180. */
  181. public function testCountAfterSerialize() {
  182. $query = $this->table->find('all');
  183. $results = $query->all();
  184. $results = unserialize(serialize($results));
  185. $this->assertCount(3, $results, 'Should be countable and 3');
  186. }
  187. /**
  188. * Integration test to show methods from CollectionTrait work
  189. *
  190. * @return void
  191. */
  192. public function testGroupBy() {
  193. $query = $this->table->find('all');
  194. $results = $query->all()->groupBy('author_id')->toArray();
  195. $options = [
  196. 'markNew' => false,
  197. 'markClean' => true,
  198. 'source' => $this->table->alias()
  199. ];
  200. $expected = [
  201. 1 => [
  202. new Entity($this->fixtureData[0], $options),
  203. new Entity($this->fixtureData[2], $options)
  204. ],
  205. 3 => [
  206. new Entity($this->fixtureData[1], $options),
  207. ]
  208. ];
  209. $this->assertEquals($expected, $results);
  210. }
  211. /**
  212. * Tests __debugInfo
  213. *
  214. * @return void
  215. */
  216. public function testDebugInfo() {
  217. $query = $this->table->find('all');
  218. $results = $query->all();
  219. $expected = [
  220. 'query' => $query,
  221. 'items' => $results->toArray()
  222. ];
  223. $this->assertSame($expected, $results->__debugInfo());
  224. }
  225. /**
  226. * Test that eagerLoader leaves empty associations unpopulated.
  227. *
  228. * @return void
  229. */
  230. public function testBelongsToEagerLoaderLeavesEmptyAssocation() {
  231. $comments = TableRegistry::get('Comments');
  232. $comments->belongsTo('Articles');
  233. // Clear the articles table so we can trigger an empty belongsTo
  234. $this->table->deleteAll([]);
  235. $comment = $comments->find()->where(['Comments.id' => 1])
  236. ->contain(['Articles'])
  237. ->hydrate(false)
  238. ->first();
  239. $this->assertEquals(1, $comment['id']);
  240. $this->assertNotEmpty($comment['comment']);
  241. $this->assertNull($comment['article']);
  242. $comment = $comments->get(1, ['contain' => ['Articles']]);
  243. $this->assertNull($comment->article);
  244. $this->assertEquals(1, $comment->id);
  245. $this->assertNotEmpty($comment->comment);
  246. }
  247. /**
  248. * Test that eagerLoader leaves empty associations unpopulated.
  249. *
  250. * @return void
  251. */
  252. public function testHasOneEagerLoaderLeavesEmptyAssocation() {
  253. $this->table->hasOne('Comments');
  254. // Clear the comments table so we can trigger an empty hasOne.
  255. $comments = TableRegistry::get('Comments');
  256. $comments->deleteAll([]);
  257. $article = $this->table->get(1, ['contain' => ['Comments']]);
  258. $this->assertNull($article->comment);
  259. $this->assertEquals(1, $article->id);
  260. $this->assertNotEmpty($article->title);
  261. $article = $this->table->find()->where(['articles.id' => 1])
  262. ->contain(['Comments'])
  263. ->hydrate(false)
  264. ->first();
  265. $this->assertNull($article['comment']);
  266. $this->assertEquals(1, $article['id']);
  267. $this->assertNotEmpty($article['title']);
  268. }
  269. }