/
ResultSetTest.php
420 lines (372 loc) · 12.4 KB
/
ResultSetTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
<?php
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 3.0.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Test\TestCase\ORM;
use Cake\Core\Plugin;
use Cake\Database\Exception;
use Cake\Datasource\ConnectionManager;
use Cake\ORM\Entity;
use Cake\ORM\ResultSet;
use Cake\ORM\Table;
use Cake\TestSuite\TestCase;
/**
* ResultSet test case.
*/
class ResultSetTest extends TestCase
{
public $fixtures = ['core.articles', 'core.authors', 'core.comments'];
/**
* setup
*
* @return void
*/
public function setUp()
{
parent::setUp();
$this->connection = ConnectionManager::get('test');
$this->table = new Table([
'table' => 'articles',
'connection' => $this->connection,
]);
$this->fixtureData = [
['id' => 1, 'author_id' => 1, 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y'],
['id' => 2, 'author_id' => 3, 'title' => 'Second Article', 'body' => 'Second Article Body', 'published' => 'Y'],
['id' => 3, 'author_id' => 1, 'title' => 'Third Article', 'body' => 'Third Article Body', 'published' => 'Y']
];
}
/**
* Test that result sets can be rewound and re-used.
*
* @return void
*/
public function testRewind()
{
$query = $this->table->find('all');
$results = $query->all();
$first = $second = [];
foreach ($results as $result) {
$first[] = $result;
}
foreach ($results as $result) {
$second[] = $result;
}
$this->assertEquals($first, $second);
}
/**
* Test that streaming results cannot be rewound
*
* @return void
*/
public function testRewindStreaming()
{
$query = $this->table->find('all')->enableBufferedResults(false);
$results = $query->all();
$first = $second = [];
foreach ($results as $result) {
$first[] = $result;
}
$this->expectException(Exception::class);
foreach ($results as $result) {
$second[] = $result;
}
}
/**
* An integration test for testing serialize and unserialize features.
*
* Compare the results of a query with the results iterated, with
* those of a different query that have been serialized/unserialized.
*
* @return void
*/
public function testSerialization()
{
$query = $this->table->find('all');
$results = $query->all();
$expected = $results->toArray();
$query2 = $this->table->find('all');
$results2 = $query2->all();
$serialized = serialize($results2);
$outcome = unserialize($serialized);
$this->assertEquals($expected, $outcome->toArray());
}
/**
* Test iteration after serialization
*
* @return void
*/
public function testIteratorAfterSerializationNoHydration()
{
$query = $this->table->find('all')->enableHydration(false);
$results = unserialize(serialize($query->all()));
// Use a loop to test Iterator implementation
foreach ($results as $i => $row) {
$this->assertEquals($this->fixtureData[$i], $row, "Row $i does not match");
}
}
/**
* Test iteration after serialization
*
* @return void
*/
public function testIteratorAfterSerializationHydrated()
{
$query = $this->table->find('all');
$results = unserialize(serialize($query->all()));
// Use a loop to test Iterator implementation
foreach ($results as $i => $row) {
$expected = new Entity($this->fixtureData[$i]);
$expected->isNew(false);
$expected->setSource($this->table->getAlias());
$expected->clean();
$this->assertEquals($expected, $row, "Row $i does not match");
}
}
/**
* Test converting resultsets into json
*
* @return void
*/
public function testJsonSerialize()
{
$query = $this->table->find('all');
$results = $query->all();
$expected = json_encode($this->fixtureData);
$this->assertEquals($expected, json_encode($results));
}
/**
* Test first() method with a statement backed result set.
*
* @return void
*/
public function testFirst()
{
$query = $this->table->find('all');
$results = $query->enableHydration(false)->all();
$row = $results->first();
$this->assertEquals($this->fixtureData[0], $row);
$row = $results->first();
$this->assertEquals($this->fixtureData[0], $row);
}
/**
* Test first() method with a result set that has been unserialized
*
* @return void
*/
public function testFirstAfterSerialize()
{
$query = $this->table->find('all');
$results = $query->enableHydration(false)->all();
$results = unserialize(serialize($results));
$row = $results->first();
$this->assertEquals($this->fixtureData[0], $row);
$this->assertSame($row, $results->first());
$this->assertSame($row, $results->first());
}
/**
* Test the countable interface.
*
* @return void
*/
public function testCount()
{
$query = $this->table->find('all');
$results = $query->all();
$this->assertCount(3, $results, 'Should be countable and 3');
}
/**
* Test the countable interface after unserialize
*
* @return void
*/
public function testCountAfterSerialize()
{
$query = $this->table->find('all');
$results = $query->all();
$results = unserialize(serialize($results));
$this->assertCount(3, $results, 'Should be countable and 3');
}
/**
* Integration test to show methods from CollectionTrait work
*
* @return void
*/
public function testGroupBy()
{
$query = $this->table->find('all');
$results = $query->all()->groupBy('author_id')->toArray();
$options = [
'markNew' => false,
'markClean' => true,
'source' => $this->table->getAlias()
];
$expected = [
1 => [
new Entity($this->fixtureData[0], $options),
new Entity($this->fixtureData[2], $options)
],
3 => [
new Entity($this->fixtureData[1], $options),
]
];
$this->assertEquals($expected, $results);
}
/**
* Tests __debugInfo
*
* @return void
*/
public function testDebugInfo()
{
$query = $this->table->find('all');
$results = $query->all();
$expected = [
'items' => $results->toArray()
];
$this->assertSame($expected, $results->__debugInfo());
}
/**
* Test that eagerLoader leaves empty associations unpopulated.
*
* @return void
*/
public function testBelongsToEagerLoaderLeavesEmptyAssociation()
{
$comments = $this->getTableLocator()->get('Comments');
$comments->belongsTo('Articles');
// Clear the articles table so we can trigger an empty belongsTo
$this->table->deleteAll([]);
$comment = $comments->find()->where(['Comments.id' => 1])
->contain(['Articles'])
->enableHydration(false)
->first();
$this->assertEquals(1, $comment['id']);
$this->assertNotEmpty($comment['comment']);
$this->assertNull($comment['article']);
$comment = $comments->get(1, ['contain' => ['Articles']]);
$this->assertNull($comment->article);
$this->assertEquals(1, $comment->id);
$this->assertNotEmpty($comment->comment);
}
/**
* Test showing associated record is preserved when selecting only field with
* null value if auto fields is disabled.
*
* @return void
*/
public function testBelongsToEagerLoaderWithAutoFieldsFalse()
{
$authors = $this->getTableLocator()->get('Authors');
$author = $authors->newEntity(['name' => null]);
$authors->save($author);
$articles = $this->getTableLocator()->get('Articles');
$articles->belongsTo('Authors');
$article = $articles->newEntity([
'author_id' => $author->id,
'title' => 'article with author with null name'
]);
$articles->save($article);
$result = $articles->find()
->select(['Articles.id', 'Articles.title', 'Authors.name'])
->contain(['Authors'])
->where(['Articles.id' => $article->id])
->enableAutoFields(false)
->enableHydration(false)
->first();
$this->assertNotNull($result['author']);
}
/**
* Test that eagerLoader leaves empty associations unpopulated.
*
* @return void
*/
public function testHasOneEagerLoaderLeavesEmptyAssociation()
{
$this->table->hasOne('Comments');
// Clear the comments table so we can trigger an empty hasOne.
$comments = $this->getTableLocator()->get('Comments');
$comments->deleteAll([]);
$article = $this->table->get(1, ['contain' => ['Comments']]);
$this->assertNull($article->comment);
$this->assertEquals(1, $article->id);
$this->assertNotEmpty($article->title);
$article = $this->table->find()->where(['articles.id' => 1])
->contain(['Comments'])
->enableHydration(false)
->first();
$this->assertNull($article['comment']);
$this->assertEquals(1, $article['id']);
$this->assertNotEmpty($article['title']);
}
/**
* Test that fetching rows does not fail when no fields were selected
* on the default alias.
*
* @return void
*/
public function testFetchMissingDefaultAlias()
{
$comments = $this->getTableLocator()->get('Comments');
$query = $comments->find()->select(['Other__field' => 'test']);
$query->enableAutoFields(false);
$row = ['Other__field' => 'test'];
$statement = $this->getMockBuilder('Cake\Database\StatementInterface')->getMock();
$statement->method('fetch')
->will($this->onConsecutiveCalls($row, $row));
$statement->method('rowCount')
->will($this->returnValue(1));
$result = new ResultSet($query, $statement);
$result->valid();
$this->assertNotEmpty($result->current());
}
/**
* Test that associations have source() correctly set.
*
* @return void
*/
public function testSourceOnContainAssociations()
{
$this->loadPlugins('TestPlugin');
$comments = $this->getTableLocator()->get('TestPlugin.Comments');
$comments->belongsTo('Authors', [
'className' => 'TestPlugin.Authors',
'foreignKey' => 'user_id'
]);
$result = $comments->find()->contain(['Authors'])->first();
$this->assertEquals('TestPlugin.Comments', $result->getSource());
$this->assertEquals('TestPlugin.Authors', $result->author->getSource());
$result = $comments->find()->matching('Authors', function ($q) {
return $q->where(['Authors.id' => 1]);
})->first();
$this->assertEquals('TestPlugin.Comments', $result->getSource());
$this->assertEquals('TestPlugin.Authors', $result->_matchingData['Authors']->getSource());
Plugin::unload();
}
/**
* Ensure that isEmpty() on a ResultSet doesn't result in loss
* of records. This behavior is provided by CollectionTrait.
*
* @return void
*/
public function testIsEmptyDoesNotConsumeData()
{
$table = $this->getTableLocator()->get('Comments');
$query = $table->find()
->formatResults(function ($results) {
return $results;
});
$res = $query->all();
$res->isEmpty();
$this->assertCount(6, $res->toArray());
}
}