ResultSetTest.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. {
  29. public $fixtures = ['core.articles', 'core.comments'];
  30. /**
  31. * setup
  32. *
  33. * @return void
  34. */
  35. public function setUp()
  36. {
  37. parent::setUp();
  38. $this->connection = ConnectionManager::get('test');
  39. $this->table = new Table([
  40. 'table' => 'articles',
  41. 'connection' => $this->connection,
  42. ]);
  43. $this->fixtureData = [
  44. ['id' => 1, 'author_id' => 1, 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y'],
  45. ['id' => 2, 'author_id' => 3, 'title' => 'Second Article', 'body' => 'Second Article Body', 'published' => 'Y'],
  46. ['id' => 3, 'author_id' => 1, 'title' => 'Third Article', 'body' => 'Third Article Body', 'published' => 'Y']
  47. ];
  48. }
  49. /**
  50. * Test that result sets can be rewound and re-used.
  51. *
  52. * @return void
  53. */
  54. public function testRewind()
  55. {
  56. $query = $this->table->find('all');
  57. $results = $query->all();
  58. $first = $second = [];
  59. foreach ($results as $result) {
  60. $first[] = $result;
  61. }
  62. foreach ($results as $result) {
  63. $second[] = $result;
  64. }
  65. $this->assertEquals($first, $second);
  66. }
  67. /**
  68. * Test that streaming results cannot be rewound
  69. *
  70. * @return void
  71. */
  72. public function testRewindStreaming()
  73. {
  74. $query = $this->table->find('all')->bufferResults(false);
  75. $results = $query->all();
  76. $first = $second = [];
  77. foreach ($results as $result) {
  78. $first[] = $result;
  79. }
  80. $this->setExpectedException('Cake\Database\Exception');
  81. foreach ($results as $result) {
  82. $second[] = $result;
  83. }
  84. }
  85. /**
  86. * An integration test for testing serialize and unserialize features.
  87. *
  88. * Compare the results of a query with the results iterated, with
  89. * those of a different query that have been serialized/unserialized.
  90. *
  91. * @return void
  92. */
  93. public function testSerialization()
  94. {
  95. $query = $this->table->find('all');
  96. $results = $query->all();
  97. $expected = $results->toArray();
  98. $query2 = $this->table->find('all');
  99. $results2 = $query2->all();
  100. $serialized = serialize($results2);
  101. $outcome = unserialize($serialized);
  102. $this->assertEquals($expected, $outcome->toArray());
  103. }
  104. /**
  105. * Test iteration after serialization
  106. *
  107. * @return void
  108. */
  109. public function testIteratorAfterSerializationNoHydration()
  110. {
  111. $query = $this->table->find('all')->hydrate(false);
  112. $results = unserialize(serialize($query->all()));
  113. // Use a loop to test Iterator implementation
  114. foreach ($results as $i => $row) {
  115. $this->assertEquals($this->fixtureData[$i], $row, "Row $i does not match");
  116. }
  117. }
  118. /**
  119. * Test iteration after serialization
  120. *
  121. * @return void
  122. */
  123. public function testIteratorAfterSerializationHydrated()
  124. {
  125. $query = $this->table->find('all');
  126. $results = unserialize(serialize($query->all()));
  127. // Use a loop to test Iterator implementation
  128. foreach ($results as $i => $row) {
  129. $expected = new \Cake\ORM\Entity($this->fixtureData[$i]);
  130. $expected->isNew(false);
  131. $expected->source($this->table->alias());
  132. $expected->clean();
  133. $this->assertEquals($expected, $row, "Row $i does not match");
  134. }
  135. }
  136. /**
  137. * Test converting resultsets into json
  138. *
  139. * @return void
  140. */
  141. public function testJsonSerialize()
  142. {
  143. $query = $this->table->find('all');
  144. $results = $query->all();
  145. $expected = json_encode($this->fixtureData);
  146. $this->assertEquals($expected, json_encode($results));
  147. }
  148. /**
  149. * Test first() method with a statement backed result set.
  150. *
  151. * @return void
  152. */
  153. public function testFirst()
  154. {
  155. $query = $this->table->find('all');
  156. $results = $query->hydrate(false)->all();
  157. $row = $results->first();
  158. $this->assertEquals($this->fixtureData[0], $row);
  159. $row = $results->first();
  160. $this->assertEquals($this->fixtureData[0], $row);
  161. }
  162. /**
  163. * Test first() method with a result set that has been unserialized
  164. *
  165. * @return void
  166. */
  167. public function testFirstAfterSerialize()
  168. {
  169. $query = $this->table->find('all');
  170. $results = $query->hydrate(false)->all();
  171. $results = unserialize(serialize($results));
  172. $row = $results->first();
  173. $this->assertEquals($this->fixtureData[0], $row);
  174. $this->assertSame($row, $results->first());
  175. $this->assertSame($row, $results->first());
  176. }
  177. /**
  178. * Test the countable interface.
  179. *
  180. * @return void
  181. */
  182. public function testCount()
  183. {
  184. $query = $this->table->find('all');
  185. $results = $query->all();
  186. $this->assertCount(3, $results, 'Should be countable and 3');
  187. }
  188. /**
  189. * Test the countable interface after unserialize
  190. *
  191. * @return void
  192. */
  193. public function testCountAfterSerialize()
  194. {
  195. $query = $this->table->find('all');
  196. $results = $query->all();
  197. $results = unserialize(serialize($results));
  198. $this->assertCount(3, $results, 'Should be countable and 3');
  199. }
  200. /**
  201. * Integration test to show methods from CollectionTrait work
  202. *
  203. * @return void
  204. */
  205. public function testGroupBy()
  206. {
  207. $query = $this->table->find('all');
  208. $results = $query->all()->groupBy('author_id')->toArray();
  209. $options = [
  210. 'markNew' => false,
  211. 'markClean' => true,
  212. 'source' => $this->table->alias()
  213. ];
  214. $expected = [
  215. 1 => [
  216. new Entity($this->fixtureData[0], $options),
  217. new Entity($this->fixtureData[2], $options)
  218. ],
  219. 3 => [
  220. new Entity($this->fixtureData[1], $options),
  221. ]
  222. ];
  223. $this->assertEquals($expected, $results);
  224. }
  225. /**
  226. * Tests __debugInfo
  227. *
  228. * @return void
  229. */
  230. public function testDebugInfo()
  231. {
  232. $query = $this->table->find('all');
  233. $results = $query->all();
  234. $expected = [
  235. 'query' => $query,
  236. 'items' => $results->toArray()
  237. ];
  238. $this->assertSame($expected, $results->__debugInfo());
  239. }
  240. /**
  241. * Test that eagerLoader leaves empty associations unpopulated.
  242. *
  243. * @return void
  244. */
  245. public function testBelongsToEagerLoaderLeavesEmptyAssocation()
  246. {
  247. $comments = TableRegistry::get('Comments');
  248. $comments->belongsTo('Articles');
  249. // Clear the articles table so we can trigger an empty belongsTo
  250. $this->table->deleteAll([]);
  251. $comment = $comments->find()->where(['Comments.id' => 1])
  252. ->contain(['Articles'])
  253. ->hydrate(false)
  254. ->first();
  255. $this->assertEquals(1, $comment['id']);
  256. $this->assertNotEmpty($comment['comment']);
  257. $this->assertNull($comment['article']);
  258. $comment = $comments->get(1, ['contain' => ['Articles']]);
  259. $this->assertNull($comment->article);
  260. $this->assertEquals(1, $comment->id);
  261. $this->assertNotEmpty($comment->comment);
  262. }
  263. /**
  264. * Test that eagerLoader leaves empty associations unpopulated.
  265. *
  266. * @return void
  267. */
  268. public function testHasOneEagerLoaderLeavesEmptyAssocation()
  269. {
  270. $this->table->hasOne('Comments');
  271. // Clear the comments table so we can trigger an empty hasOne.
  272. $comments = TableRegistry::get('Comments');
  273. $comments->deleteAll([]);
  274. $article = $this->table->get(1, ['contain' => ['Comments']]);
  275. $this->assertNull($article->comment);
  276. $this->assertEquals(1, $article->id);
  277. $this->assertNotEmpty($article->title);
  278. $article = $this->table->find()->where(['articles.id' => 1])
  279. ->contain(['Comments'])
  280. ->hydrate(false)
  281. ->first();
  282. $this->assertNull($article['comment']);
  283. $this->assertEquals(1, $article['id']);
  284. $this->assertNotEmpty($article['title']);
  285. }
  286. /**
  287. * Test that fetching rows does not fail when no fields were selected
  288. * on the default alias.
  289. *
  290. * @return void
  291. */
  292. public function testFetchMissingDefaultAlias()
  293. {
  294. $comments = TableRegistry::get('Comments');
  295. $query = $comments->find()->select(['Other__field' => 'test']);
  296. $query->autoFields(false);
  297. $row = ['Other__field' => 'test'];
  298. $statement = $this->getMock('Cake\Database\StatementInterface');
  299. $statement->method('fetch')
  300. ->will($this->onConsecutiveCalls($row, $row));
  301. $statement->method('rowCount')
  302. ->will($this->returnValue(1));
  303. $result = new ResultSet($query, $statement);
  304. $result->valid();
  305. $data = $result->current();
  306. }
  307. }