ResultSetTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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\TestSuite\TestCase;
  23. /**
  24. * ResultSet test case.
  25. */
  26. class ResultSetTest extends TestCase {
  27. public $fixtures = ['core.article'];
  28. /**
  29. * setup
  30. *
  31. * @return void
  32. */
  33. public function setUp() {
  34. parent::setUp();
  35. $this->connection = ConnectionManager::get('test');
  36. $this->table = new Table([
  37. 'table' => 'articles',
  38. 'connection' => $this->connection,
  39. ]);
  40. $this->fixtureData = [
  41. ['id' => 1, 'author_id' => 1, 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y'],
  42. ['id' => 2, 'author_id' => 3, 'title' => 'Second Article', 'body' => 'Second Article Body', 'published' => 'Y'],
  43. ['id' => 3, 'author_id' => 1, 'title' => 'Third Article', 'body' => 'Third Article Body', 'published' => 'Y']
  44. ];
  45. }
  46. /**
  47. * Test that result sets can be rewound and re-used.
  48. *
  49. * @return void
  50. */
  51. public function testRewind() {
  52. $query = $this->table->find('all');
  53. $results = $query->all();
  54. $first = $second = [];
  55. foreach ($results as $result) {
  56. $first[] = $result;
  57. }
  58. foreach ($results as $result) {
  59. $second[] = $result;
  60. }
  61. }
  62. /**
  63. * Test that streaming results cannot be rewound
  64. *
  65. * @return void
  66. */
  67. public function testRewindStreaming() {
  68. $query = $this->table->find('all')->bufferResults(false);
  69. $results = $query->all();
  70. $first = $second = [];
  71. foreach ($results as $result) {
  72. $first[] = $result;
  73. }
  74. $this->setExpectedException('Cake\Database\Exception');
  75. foreach ($results as $result) {
  76. $second[] = $result;
  77. }
  78. }
  79. /**
  80. * An integration test for testing serialize and unserialize features.
  81. *
  82. * Compare the results of a query with the results iterated, with
  83. * those of a different query that have been serialized/unserialized.
  84. *
  85. * @return void
  86. */
  87. public function testSerialization() {
  88. $query = $this->table->find('all');
  89. $results = $query->all();
  90. $expected = $results->toArray();
  91. $query2 = $this->table->find('all');
  92. $results2 = $query2->all();
  93. $serialized = serialize($results2);
  94. $outcome = unserialize($serialized);
  95. $this->assertEquals($expected, $outcome->toArray());
  96. }
  97. /**
  98. * Test iteration after serialization
  99. *
  100. * @return void
  101. */
  102. public function testIteratorAfterSerializationNoHydration() {
  103. $query = $this->table->find('all')->hydrate(false);
  104. $results = unserialize(serialize($query->all()));
  105. // Use a loop to test Iterator implementation
  106. foreach ($results as $i => $row) {
  107. $this->assertEquals($this->fixtureData[$i], $row, "Row $i does not match");
  108. }
  109. }
  110. /**
  111. * Test iteration after serialization
  112. *
  113. * @return void
  114. */
  115. public function testIteratorAfterSerializationHydrated() {
  116. $query = $this->table->find('all');
  117. $results = unserialize(serialize($query->all()));
  118. // Use a loop to test Iterator implementation
  119. foreach ($results as $i => $row) {
  120. $expected = new \Cake\ORM\Entity($this->fixtureData[$i]);
  121. $expected->isNew(false);
  122. $expected->source($this->table->alias());
  123. $expected->clean();
  124. $this->assertEquals($expected, $row, "Row $i does not match");
  125. }
  126. }
  127. /**
  128. * Test converting resultsets into json
  129. *
  130. * @return void
  131. */
  132. public function testJsonSerialize() {
  133. $query = $this->table->find('all');
  134. $results = $query->all();
  135. $expected = json_encode($this->fixtureData);
  136. $this->assertEquals($expected, json_encode($results));
  137. }
  138. /**
  139. * Test first() method with a statement backed result set.
  140. *
  141. * @return void
  142. */
  143. public function testFirst() {
  144. $query = $this->table->find('all');
  145. $results = $query->hydrate(false)->all();
  146. $row = $results->first();
  147. $this->assertEquals($this->fixtureData[0], $row);
  148. $row = $results->first();
  149. $this->assertEquals($this->fixtureData[0], $row);
  150. }
  151. /**
  152. * Test first() method with a result set that has been unserialized
  153. *
  154. * @return void
  155. */
  156. public function testFirstAfterSerialize() {
  157. $query = $this->table->find('all');
  158. $results = $query->hydrate(false)->all();
  159. $results = unserialize(serialize($results));
  160. $row = $results->first();
  161. $this->assertEquals($this->fixtureData[0], $row);
  162. $this->assertSame($row, $results->first());
  163. $this->assertSame($row, $results->first());
  164. }
  165. /**
  166. * Test the countable interface.
  167. *
  168. * @return void
  169. */
  170. public function testCount() {
  171. $query = $this->table->find('all');
  172. $results = $query->all();
  173. $this->assertCount(3, $results, 'Should be countable and 3');
  174. }
  175. /**
  176. * Test the countable interface after unserialize
  177. *
  178. * @return void
  179. */
  180. public function testCountAfterSerialize() {
  181. $query = $this->table->find('all');
  182. $results = $query->all();
  183. $results = unserialize(serialize($results));
  184. $this->assertCount(3, $results, 'Should be countable and 3');
  185. }
  186. /**
  187. * Integration test to show methods from CollectionTrait work
  188. *
  189. * @return void
  190. */
  191. public function testGroupBy() {
  192. $query = $this->table->find('all');
  193. $results = $query->all()->groupBy('author_id')->toArray();
  194. $options = [
  195. 'markNew' => false,
  196. 'markClean' => true,
  197. 'source' => $this->table->alias()
  198. ];
  199. $expected = [
  200. 1 => [
  201. new Entity($this->fixtureData[0], $options),
  202. new Entity($this->fixtureData[2], $options)
  203. ],
  204. 3 => [
  205. new Entity($this->fixtureData[1], $options),
  206. ]
  207. ];
  208. $this->assertEquals($expected, $results);
  209. }
  210. /**
  211. * Tests __debugInfo
  212. *
  213. * @return void
  214. */
  215. public function testDebugInfo() {
  216. $query = $this->table->find('all');
  217. $results = $query->all();
  218. $expected = [
  219. 'query' => $query,
  220. 'items' => $results->toArray()
  221. ];
  222. $this->assertSame($expected, $results->__debugInfo());
  223. }
  224. }