ObjectHydratorTest.php 28.6 KB
Newer Older
1 2
<?php

3 4 5
namespace Doctrine\Tests\ORM\Hydration;

use Doctrine\Tests\Mocks\HydratorMockStatement;
6
use Doctrine\ORM\Query\ResultSetMapping;
7 8
use Doctrine\ORM\Proxy\ProxyFactory;
use Doctrine\ORM\Mapping\AssociationMapping;
9
use Doctrine\ORM\Query;
10

11
require_once __DIR__ . '/../../TestInit.php';
12

romanb's avatar
romanb committed
13
class ObjectHydratorTest extends HydrationTestCase
14 15
{
    /**
16
     * Select u.id, u.name from \Doctrine\Tests\Models\CMS\CmsUser u
17
     */
romanb's avatar
romanb committed
18
    public function testSimpleEntityQuery()
19
    {
20
        $rsm = new ResultSetMapping;
21
        $rsm->addEntityResult('Doctrine\Tests\Models\CMS\CmsUser', 'u');
22 23
        $rsm->addFieldResult('u', 'u__id', 'id');
        $rsm->addFieldResult('u', 'u__name', 'name');
24 25 26 27 28 29 30 31 32 33 34 35 36 37

        // Faked result set
        $resultSet = array(
            array(
                'u__id' => '1',
                'u__name' => 'romanb'
                ),
            array(
                'u__id' => '2',
                'u__name' => 'jwage'
                )
            );


38 39
        $stmt = new HydratorMockStatement($resultSet);
        $hydrator = new \Doctrine\ORM\Internal\Hydration\ObjectHydrator($this->_em);
40

41
        $result = $hydrator->hydrateAll($stmt, $rsm, array(Query::HINT_FORCE_PARTIAL_LOAD => true));
42 43

        $this->assertEquals(2, count($result));
44 45
        $this->assertTrue($result[0] instanceof \Doctrine\Tests\Models\CMS\CmsUser);
        $this->assertTrue($result[1] instanceof \Doctrine\Tests\Models\CMS\CmsUser);
46 47 48 49 50 51
        $this->assertEquals(1, $result[0]->id);
        $this->assertEquals('romanb', $result[0]->name);
        $this->assertEquals(2, $result[1]->id);
        $this->assertEquals('jwage', $result[1]->name);
    }

52 53 54
    /**
     * Select u.id, u.name from \Doctrine\Tests\Models\CMS\CmsUser u
     */
romanb's avatar
romanb committed
55
    public function testSimpleMultipleRootEntityQuery()
56 57
    {
        $rsm = new ResultSetMapping;
58 59
        $rsm->addEntityResult('Doctrine\Tests\Models\CMS\CmsUser', 'u');
        $rsm->addEntityResult('Doctrine\Tests\Models\CMS\CmsArticle', 'a');
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
        $rsm->addFieldResult('u', 'u__id', 'id');
        $rsm->addFieldResult('u', 'u__name', 'name');
        $rsm->addFieldResult('a', 'a__id', 'id');
        $rsm->addFieldResult('a', 'a__topic', 'topic');

        // Faked result set
        $resultSet = array(
            array(
                'u__id' => '1',
                'u__name' => 'romanb',
                'a__id' => '1',
                'a__topic' => 'Cool things.'
                ),
            array(
                'u__id' => '2',
                'u__name' => 'jwage',
                'a__id' => '2',
                'a__topic' => 'Cool things II.'
                )
            );


        $stmt = new HydratorMockStatement($resultSet);
        $hydrator = new \Doctrine\ORM\Internal\Hydration\ObjectHydrator($this->_em);

85
        $result = $hydrator->hydrateAll($stmt, $rsm, array(Query::HINT_FORCE_PARTIAL_LOAD => true));
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103

        $this->assertEquals(4, count($result));
        
        $this->assertTrue($result[0] instanceof \Doctrine\Tests\Models\CMS\CmsUser);
        $this->assertTrue($result[1] instanceof \Doctrine\Tests\Models\CMS\CmsArticle);
        $this->assertTrue($result[2] instanceof \Doctrine\Tests\Models\CMS\CmsUser);
        $this->assertTrue($result[3] instanceof \Doctrine\Tests\Models\CMS\CmsArticle);

        $this->assertEquals(1, $result[0]->id);
        $this->assertEquals('romanb', $result[0]->name);
        $this->assertEquals(1, $result[1]->id);
        $this->assertEquals('Cool things.', $result[1]->topic);
        $this->assertEquals(2, $result[2]->id);
        $this->assertEquals('jwage', $result[2]->name);
        $this->assertEquals(2, $result[3]->id);
        $this->assertEquals('Cool things II.', $result[3]->topic);
    }

104 105 106 107 108 109 110 111 112
    /**
     * Select p from \Doctrine\Tests\Models\ECommerce\ECommerceProduct p
     */
    public function testCreatesProxyForLazyLoadingWithForeignKeys()
    {
        $rsm = new ResultSetMapping;
        $rsm->addEntityResult('Doctrine\Tests\Models\ECommerce\ECommerceProduct', 'p');
        $rsm->addFieldResult('p', 'p__id', 'id');
        $rsm->addFieldResult('p', 'p__name', 'name');
113
        $rsm->addMetaResult('p', 'p__shipping_id', 'shipping_id');
114 115 116 117 118 119 120 121 122 123 124

        // Faked result set
        $resultSet = array(
            array(
                'p__id' => '1',
                'p__name' => 'Doctrine Book',
                'p__shipping_id' => 42
                )
            );
        
        // mocking the proxy factory
125
        $proxyFactory = $this->getMock('Doctrine\ORM\Proxy\ProxyFactory', array('getProxy'), array(), '', false, false, false);
126
        $proxyFactory->expects($this->once())
127 128 129 130
                     ->method('getProxy')
                     ->with($this->equalTo('Doctrine\Tests\Models\ECommerce\ECommerceShipping'),
                            array('id' => 42))
                     ->will($this->returnValue(new \stdClass));
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146

        $this->_em->setProxyFactory($proxyFactory);

        // configuring lazy loading
        $metadata = $this->_em->getClassMetadata('Doctrine\Tests\Models\ECommerce\ECommerceProduct');
        $metadata->getAssociationMapping('shipping')->fetchMode = AssociationMapping::FETCH_LAZY;

        $stmt = new HydratorMockStatement($resultSet);
        $hydrator = new \Doctrine\ORM\Internal\Hydration\ObjectHydrator($this->_em);

        $result = $hydrator->hydrateAll($stmt, $rsm);

        $this->assertEquals(1, count($result));
        $this->assertTrue($result[0] instanceof \Doctrine\Tests\Models\ECommerce\ECommerceProduct);
    }

147 148 149 150 151 152 153
    /**
     * select u.id, u.status, p.phonenumber, upper(u.name) nameUpper from User u
     * join u.phonenumbers p
     * =
     * select u.id, u.status, p.phonenumber, upper(u.name) as u__0 from USERS u
     * INNER JOIN PHONENUMBERS p ON u.id = p.user_id
     */
romanb's avatar
romanb committed
154
    public function testMixedQueryFetchJoin()
155
    {
156
        $rsm = new ResultSetMapping;
157
        $rsm->addEntityResult('Doctrine\Tests\Models\CMS\CmsUser', 'u');
158
        $rsm->addJoinedEntityResult(
159
                'Doctrine\Tests\Models\CMS\CmsPhonenumber',
160 161
                'p',
                'u',
162
                'phonenumbers'
163 164 165 166 167
        );
        $rsm->addFieldResult('u', 'u__id', 'id');
        $rsm->addFieldResult('u', 'u__status', 'status');
        $rsm->addScalarResult('sclr0', 'nameUpper');
        $rsm->addFieldResult('p', 'p__phonenumber', 'phonenumber');
168 169 170 171 172 173 174

        // Faked result set
        $resultSet = array(
            //row1
            array(
                'u__id' => '1',
                'u__status' => 'developer',
175
                'sclr0' => 'ROMANB',
176 177 178 179 180
                'p__phonenumber' => '42',
                ),
            array(
                'u__id' => '1',
                'u__status' => 'developer',
181
                'sclr0' => 'ROMANB',
182 183 184 185 186
                'p__phonenumber' => '43',
                ),
            array(
                'u__id' => '2',
                'u__status' => 'developer',
187
                'sclr0' => 'JWAGE',
188 189 190 191
                'p__phonenumber' => '91'
                )
            );

192 193
        $stmt = new HydratorMockStatement($resultSet);
        $hydrator = new \Doctrine\ORM\Internal\Hydration\ObjectHydrator($this->_em);
194

195
        $result = $hydrator->hydrateAll($stmt, $rsm, array(Query::HINT_FORCE_PARTIAL_LOAD => true));
196 197 198 199 200 201

        $this->assertEquals(2, count($result));
        $this->assertTrue(is_array($result));
        $this->assertTrue(is_array($result[0]));
        $this->assertTrue(is_array($result[1]));

202
        $this->assertTrue($result[0][0] instanceof \Doctrine\Tests\Models\CMS\CmsUser);
203
        $this->assertTrue($result[0][0]->phonenumbers instanceof \Doctrine\ORM\PersistentCollection);
204 205 206
        $this->assertTrue($result[0][0]->phonenumbers[0] instanceof \Doctrine\Tests\Models\CMS\CmsPhonenumber);
        $this->assertTrue($result[0][0]->phonenumbers[1] instanceof \Doctrine\Tests\Models\CMS\CmsPhonenumber);
        $this->assertTrue($result[1][0] instanceof \Doctrine\Tests\Models\CMS\CmsUser);
207
        $this->assertTrue($result[1][0]->phonenumbers instanceof \Doctrine\ORM\PersistentCollection);
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227

        // first user => 2 phonenumbers
        $this->assertEquals(2, count($result[0][0]->phonenumbers));
        $this->assertEquals('ROMANB', $result[0]['nameUpper']);
        // second user => 1 phonenumber
        $this->assertEquals(1, count($result[1][0]->phonenumbers));
        $this->assertEquals('JWAGE', $result[1]['nameUpper']);

        $this->assertEquals(42, $result[0][0]->phonenumbers[0]->phonenumber);
        $this->assertEquals(43, $result[0][0]->phonenumbers[1]->phonenumber);
        $this->assertEquals(91, $result[1][0]->phonenumbers[0]->phonenumber);
    }

    /**
     * select u.id, u.status, count(p.phonenumber) numPhones from User u
     * join u.phonenumbers p group by u.status, u.id
     * =
     * select u.id, u.status, count(p.phonenumber) as p__0 from USERS u
     * INNER JOIN PHONENUMBERS p ON u.id = p.user_id group by u.id, u.status
     */
romanb's avatar
romanb committed
228
    public function testMixedQueryNormalJoin()
229
    {
230
        $rsm = new ResultSetMapping;
231
        $rsm->addEntityResult('Doctrine\Tests\Models\CMS\CmsUser', 'u');
232 233 234
        $rsm->addFieldResult('u', 'u__id', 'id');
        $rsm->addFieldResult('u', 'u__status', 'status');
        $rsm->addScalarResult('sclr0', 'numPhones');
235 236 237 238 239 240 241

        // Faked result set
        $resultSet = array(
            //row1
            array(
                'u__id' => '1',
                'u__status' => 'developer',
242
                'sclr0' => '2',
243 244 245 246
                ),
            array(
                'u__id' => '2',
                'u__status' => 'developer',
247
                'sclr0' => '1',
248 249 250
                )
            );

251 252
        $stmt = new HydratorMockStatement($resultSet);
        $hydrator = new \Doctrine\ORM\Internal\Hydration\ObjectHydrator($this->_em);
253

254
        $result = $hydrator->hydrateAll($stmt, $rsm, array(Query::HINT_FORCE_PARTIAL_LOAD => true));
255 256 257 258 259 260 261 262 263

        $this->assertEquals(2, count($result));
        $this->assertTrue(is_array($result));
        $this->assertTrue(is_array($result[0]));
        $this->assertTrue(is_array($result[1]));
        // first user => 2 phonenumbers
        $this->assertEquals(2, $result[0]['numPhones']);
        // second user => 1 phonenumber
        $this->assertEquals(1, $result[1]['numPhones']);
264 265
        $this->assertTrue($result[0][0] instanceof \Doctrine\Tests\Models\CMS\CmsUser);
        $this->assertTrue($result[1][0] instanceof \Doctrine\Tests\Models\CMS\CmsUser);
266 267 268 269 270 271 272 273 274
    }

    /**
     * select u.id, u.status, upper(u.name) nameUpper from User u index by u.id
     * join u.phonenumbers p indexby p.phonenumber
     * =
     * select u.id, u.status, upper(u.name) as p__0 from USERS u
     * INNER JOIN PHONENUMBERS p ON u.id = p.user_id
     */
romanb's avatar
romanb committed
275
    public function testMixedQueryFetchJoinCustomIndex()
276
    {
277
        $rsm = new ResultSetMapping;
278
        $rsm->addEntityResult('Doctrine\Tests\Models\CMS\CmsUser', 'u');
279
        $rsm->addJoinedEntityResult(
280
                'Doctrine\Tests\Models\CMS\CmsPhonenumber',
281 282
                'p',
                'u',
283
                'phonenumbers'
284 285 286 287 288 289 290
        );
        $rsm->addFieldResult('u', 'u__id', 'id');
        $rsm->addFieldResult('u', 'u__status', 'status');
        $rsm->addScalarResult('sclr0', 'nameUpper');
        $rsm->addFieldResult('p', 'p__phonenumber', 'phonenumber');
        $rsm->addIndexBy('u', 'id');
        $rsm->addIndexBy('p', 'phonenumber');
291 292 293 294 295 296 297

        // Faked result set
        $resultSet = array(
            //row1
            array(
                'u__id' => '1',
                'u__status' => 'developer',
298
                'sclr0' => 'ROMANB',
299 300 301 302 303
                'p__phonenumber' => '42',
                ),
            array(
                'u__id' => '1',
                'u__status' => 'developer',
304
                'sclr0' => 'ROMANB',
305 306 307 308 309
                'p__phonenumber' => '43',
                ),
            array(
                'u__id' => '2',
                'u__status' => 'developer',
310
                'sclr0' => 'JWAGE',
311 312 313 314 315
                'p__phonenumber' => '91'
                )
            );


316 317
        $stmt = new HydratorMockStatement($resultSet);
        $hydrator = new \Doctrine\ORM\Internal\Hydration\ObjectHydrator($this->_em);
318

319
        $result = $hydrator->hydrateAll($stmt, $rsm, array(Query::HINT_FORCE_PARTIAL_LOAD => true));
320 321 322 323 324 325 326 327 328 329

        $this->assertEquals(2, count($result));
        $this->assertTrue(is_array($result));
        $this->assertTrue(is_array($result[0]));
        $this->assertTrue(is_array($result[1]));

        // test the scalar values
        $this->assertEquals('ROMANB', $result[0]['nameUpper']);
        $this->assertEquals('JWAGE', $result[1]['nameUpper']);

330 331
        $this->assertTrue($result[0]['1'] instanceof \Doctrine\Tests\Models\CMS\CmsUser);
        $this->assertTrue($result[1]['2'] instanceof \Doctrine\Tests\Models\CMS\CmsUser);
332
        $this->assertTrue($result[0]['1']->phonenumbers instanceof \Doctrine\ORM\PersistentCollection);
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
        // first user => 2 phonenumbers. notice the custom indexing by user id
        $this->assertEquals(2, count($result[0]['1']->phonenumbers));
        // second user => 1 phonenumber. notice the custom indexing by user id
        $this->assertEquals(1, count($result[1]['2']->phonenumbers));
        // test the custom indexing of the phonenumbers
        $this->assertTrue(isset($result[0]['1']->phonenumbers['42']));
        $this->assertTrue(isset($result[0]['1']->phonenumbers['43']));
        $this->assertTrue(isset($result[1]['2']->phonenumbers['91']));
    }

    /**
     * select u.id, u.status, p.phonenumber, upper(u.name) nameUpper, a.id, a.topic
     * from User u
     * join u.phonenumbers p
     * join u.articles a
     * =
     * select u.id, u.status, p.phonenumber, upper(u.name) as u__0, a.id, a.topic
     * from USERS u
     * inner join PHONENUMBERS p ON u.id = p.user_id
     * inner join ARTICLES a ON u.id = a.user_id
     */
romanb's avatar
romanb committed
354
    public function testMixedQueryMultipleFetchJoin()
355
    {
356
        $rsm = new ResultSetMapping;
357
        $rsm->addEntityResult('Doctrine\Tests\Models\CMS\CmsUser', 'u');
358
        $rsm->addJoinedEntityResult(
359
                'Doctrine\Tests\Models\CMS\CmsPhonenumber',
360 361
                'p',
                'u',
362
                'phonenumbers'
363 364
        );
        $rsm->addJoinedEntityResult(
365
                'Doctrine\Tests\Models\CMS\CmsArticle',
366 367
                'a',
                'u',
368
                'articles'
369 370 371 372 373 374 375
        );
        $rsm->addFieldResult('u', 'u__id', 'id');
        $rsm->addFieldResult('u', 'u__status', 'status');
        $rsm->addScalarResult('sclr0', 'nameUpper');
        $rsm->addFieldResult('p', 'p__phonenumber', 'phonenumber');
        $rsm->addFieldResult('a', 'a__id', 'id');
        $rsm->addFieldResult('a', 'a__topic', 'topic');
376 377 378 379 380 381 382

        // Faked result set
        $resultSet = array(
            //row1
            array(
                'u__id' => '1',
                'u__status' => 'developer',
383
                'sclr0' => 'ROMANB',
384 385 386 387 388 389 390
                'p__phonenumber' => '42',
                'a__id' => '1',
                'a__topic' => 'Getting things done!'
                ),
           array(
                'u__id' => '1',
                'u__status' => 'developer',
391
                'sclr0' => 'ROMANB',
392 393 394 395 396 397 398
                'p__phonenumber' => '43',
                'a__id' => '1',
                'a__topic' => 'Getting things done!'
                ),
            array(
                'u__id' => '1',
                'u__status' => 'developer',
399
                'sclr0' => 'ROMANB',
400 401 402 403 404 405 406
                'p__phonenumber' => '42',
                'a__id' => '2',
                'a__topic' => 'ZendCon'
                ),
           array(
                'u__id' => '1',
                'u__status' => 'developer',
407
                'sclr0' => 'ROMANB',
408 409 410 411 412 413 414
                'p__phonenumber' => '43',
                'a__id' => '2',
                'a__topic' => 'ZendCon'
                ),
            array(
                'u__id' => '2',
                'u__status' => 'developer',
415
                'sclr0' => 'JWAGE',
416 417 418 419 420 421 422
                'p__phonenumber' => '91',
                'a__id' => '3',
                'a__topic' => 'LINQ'
                ),
           array(
                'u__id' => '2',
                'u__status' => 'developer',
423
                'sclr0' => 'JWAGE',
424 425 426 427 428 429
                'p__phonenumber' => '91',
                'a__id' => '4',
                'a__topic' => 'PHP6'
                ),
            );

430 431
        $stmt = new HydratorMockStatement($resultSet);
        $hydrator = new \Doctrine\ORM\Internal\Hydration\ObjectHydrator($this->_em);
432

433
        $result = $hydrator->hydrateAll($stmt, $rsm, array(Query::HINT_FORCE_PARTIAL_LOAD => true));
434 435 436 437 438 439

        $this->assertEquals(2, count($result));
        $this->assertTrue(is_array($result));
        $this->assertTrue(is_array($result[0]));
        $this->assertTrue(is_array($result[1]));

440
        $this->assertTrue($result[0][0] instanceof \Doctrine\Tests\Models\CMS\CmsUser);
441
        $this->assertTrue($result[0][0]->phonenumbers instanceof \Doctrine\ORM\PersistentCollection);
442 443
        $this->assertTrue($result[0][0]->phonenumbers[0] instanceof \Doctrine\Tests\Models\CMS\CmsPhonenumber);
        $this->assertTrue($result[0][0]->phonenumbers[1] instanceof \Doctrine\Tests\Models\CMS\CmsPhonenumber);
444
        $this->assertTrue($result[0][0]->articles instanceof \Doctrine\ORM\PersistentCollection);
445 446 447
        $this->assertTrue($result[0][0]->articles[0] instanceof \Doctrine\Tests\Models\CMS\CmsArticle);
        $this->assertTrue($result[0][0]->articles[1] instanceof \Doctrine\Tests\Models\CMS\CmsArticle);
        $this->assertTrue($result[1][0] instanceof \Doctrine\Tests\Models\CMS\CmsUser);
448
        $this->assertTrue($result[1][0]->phonenumbers instanceof \Doctrine\ORM\PersistentCollection);
449 450 451
        $this->assertTrue($result[1][0]->phonenumbers[0] instanceof \Doctrine\Tests\Models\CMS\CmsPhonenumber);
        $this->assertTrue($result[1][0]->articles[0] instanceof \Doctrine\Tests\Models\CMS\CmsArticle);
        $this->assertTrue($result[1][0]->articles[1] instanceof \Doctrine\Tests\Models\CMS\CmsArticle);
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
    }

    /**
     * select u.id, u.status, p.phonenumber, upper(u.name) nameUpper, a.id, a.topic,
     * c.id, c.topic
     * from User u
     * join u.phonenumbers p
     * join u.articles a
     * left join a.comments c
     * =
     * select u.id, u.status, p.phonenumber, upper(u.name) as u__0, a.id, a.topic,
     * c.id, c.topic
     * from USERS u
     * inner join PHONENUMBERS p ON u.id = p.user_id
     * inner join ARTICLES a ON u.id = a.user_id
     * left outer join COMMENTS c ON a.id = c.article_id
     */
romanb's avatar
romanb committed
469
    public function testMixedQueryMultipleDeepMixedFetchJoin()
470
    {
471
        $rsm = new ResultSetMapping;
472
        $rsm->addEntityResult('Doctrine\Tests\Models\CMS\CmsUser', 'u');
473
        $rsm->addJoinedEntityResult(
474
                'Doctrine\Tests\Models\CMS\CmsPhonenumber',
475 476
                'p',
                'u',
477
                'phonenumbers'
478 479
        );
        $rsm->addJoinedEntityResult(
480
                'Doctrine\Tests\Models\CMS\CmsArticle',
481 482
                'a',
                'u',
483
                'articles'
484 485
        );
        $rsm->addJoinedEntityResult(
486
                'Doctrine\Tests\Models\CMS\CmsComment',
487 488
                'c',
                'a',
489
                'comments'
490 491 492 493 494 495 496 497 498
        );
        $rsm->addFieldResult('u', 'u__id', 'id');
        $rsm->addFieldResult('u', 'u__status', 'status');
        $rsm->addScalarResult('sclr0', 'nameUpper');
        $rsm->addFieldResult('p', 'p__phonenumber', 'phonenumber');
        $rsm->addFieldResult('a', 'a__id', 'id');
        $rsm->addFieldResult('a', 'a__topic', 'topic');
        $rsm->addFieldResult('c', 'c__id', 'id');
        $rsm->addFieldResult('c', 'c__topic', 'topic');
499 500 501 502 503 504 505

        // Faked result set
        $resultSet = array(
            //row1
            array(
                'u__id' => '1',
                'u__status' => 'developer',
506
                'sclr0' => 'ROMANB',
507 508 509 510 511 512 513 514 515
                'p__phonenumber' => '42',
                'a__id' => '1',
                'a__topic' => 'Getting things done!',
                'c__id' => '1',
                'c__topic' => 'First!'
                ),
           array(
                'u__id' => '1',
                'u__status' => 'developer',
516
                'sclr0' => 'ROMANB',
517 518 519 520 521 522 523 524 525
                'p__phonenumber' => '43',
                'a__id' => '1',
                'a__topic' => 'Getting things done!',
                'c__id' => '1',
                'c__topic' => 'First!'
                ),
            array(
                'u__id' => '1',
                'u__status' => 'developer',
526
                'sclr0' => 'ROMANB',
527 528 529 530 531 532 533 534 535
                'p__phonenumber' => '42',
                'a__id' => '2',
                'a__topic' => 'ZendCon',
                'c__id' => null,
                'c__topic' => null
                ),
           array(
                'u__id' => '1',
                'u__status' => 'developer',
536
                'sclr0' => 'ROMANB',
537 538 539 540 541 542 543 544 545
                'p__phonenumber' => '43',
                'a__id' => '2',
                'a__topic' => 'ZendCon',
                'c__id' => null,
                'c__topic' => null
                ),
            array(
                'u__id' => '2',
                'u__status' => 'developer',
546
                'sclr0' => 'JWAGE',
547 548 549 550 551 552 553 554 555
                'p__phonenumber' => '91',
                'a__id' => '3',
                'a__topic' => 'LINQ',
                'c__id' => null,
                'c__topic' => null
                ),
           array(
                'u__id' => '2',
                'u__status' => 'developer',
556
                'sclr0' => 'JWAGE',
557 558 559 560 561 562 563 564
                'p__phonenumber' => '91',
                'a__id' => '4',
                'a__topic' => 'PHP6',
                'c__id' => null,
                'c__topic' => null
                ),
            );

565 566
        $stmt = new HydratorMockStatement($resultSet);
        $hydrator = new \Doctrine\ORM\Internal\Hydration\ObjectHydrator($this->_em);
567

568
        $result = $hydrator->hydrateAll($stmt, $rsm, array(Query::HINT_FORCE_PARTIAL_LOAD => true));
569 570 571 572 573 574

        $this->assertEquals(2, count($result));
        $this->assertTrue(is_array($result));
        $this->assertTrue(is_array($result[0]));
        $this->assertTrue(is_array($result[1]));

575 576
        $this->assertTrue($result[0][0] instanceof \Doctrine\Tests\Models\CMS\CmsUser);
        $this->assertTrue($result[1][0] instanceof \Doctrine\Tests\Models\CMS\CmsUser);
577
        // phonenumbers
578
        $this->assertTrue($result[0][0]->phonenumbers instanceof \Doctrine\ORM\PersistentCollection);
579 580
        $this->assertTrue($result[0][0]->phonenumbers[0] instanceof \Doctrine\Tests\Models\CMS\CmsPhonenumber);
        $this->assertTrue($result[0][0]->phonenumbers[1] instanceof \Doctrine\Tests\Models\CMS\CmsPhonenumber);
581
        $this->assertTrue($result[1][0]->phonenumbers instanceof \Doctrine\ORM\PersistentCollection);
582
        $this->assertTrue($result[1][0]->phonenumbers[0] instanceof \Doctrine\Tests\Models\CMS\CmsPhonenumber);
583
        // articles
584
        $this->assertTrue($result[0][0]->articles instanceof \Doctrine\ORM\PersistentCollection);
585 586 587 588
        $this->assertTrue($result[0][0]->articles[0] instanceof \Doctrine\Tests\Models\CMS\CmsArticle);
        $this->assertTrue($result[0][0]->articles[1] instanceof \Doctrine\Tests\Models\CMS\CmsArticle);
        $this->assertTrue($result[1][0]->articles[0] instanceof \Doctrine\Tests\Models\CMS\CmsArticle);
        $this->assertTrue($result[1][0]->articles[1] instanceof \Doctrine\Tests\Models\CMS\CmsArticle);
589
        // article comments
590
        $this->assertTrue($result[0][0]->articles[0]->comments instanceof \Doctrine\ORM\PersistentCollection);
591
        $this->assertTrue($result[0][0]->articles[0]->comments[0] instanceof \Doctrine\Tests\Models\CMS\CmsComment);
592
        // empty comment collections
593
        $this->assertTrue($result[0][0]->articles[1]->comments instanceof \Doctrine\ORM\PersistentCollection);
594
        $this->assertEquals(0, count($result[0][0]->articles[1]->comments));
595
        $this->assertTrue($result[1][0]->articles[0]->comments instanceof \Doctrine\ORM\PersistentCollection);
596
        $this->assertEquals(0, count($result[1][0]->articles[0]->comments));
597
        $this->assertTrue($result[1][0]->articles[1]->comments instanceof \Doctrine\ORM\PersistentCollection);
598 599 600 601 602 603 604 605
        $this->assertEquals(0, count($result[1][0]->articles[1]->comments));
    }

    /**
     * Tests that the hydrator does not rely on a particular order of the rows
     * in the result set.
     *
     * DQL:
romanb's avatar
romanb committed
606
     * select c, b from Doctrine\Tests\Models\Forum\ForumCategory c inner join c.boards b
607 608 609 610 611 612 613 614 615 616 617 618
     * order by c.position asc, b.position asc
     *
     * Checks whether the boards are correctly assigned to the categories.
     *
     * The 'evil' result set that confuses the object population is displayed below.
     *
     * c.id  | c.position | c.name   | boardPos | b.id | b.category_id (just for clarity)
     *  1    | 0          | First    | 0        |   1  | 1
     *  2    | 0          | Second   | 0        |   2  | 2   <--
     *  1    | 0          | First    | 1        |   3  | 1
     *  1    | 0          | First    | 2        |   4  | 1
     */
romanb's avatar
romanb committed
619
    public function testEntityQueryCustomResultSetOrder()
620
    {
621
        $rsm = new ResultSetMapping;
622
        $rsm->addEntityResult('Doctrine\Tests\Models\Forum\ForumCategory', 'c');
623
        $rsm->addJoinedEntityResult(
624
                'Doctrine\Tests\Models\Forum\ForumBoard',
625 626
                'b',
                'c',
627
                'boards'
628 629 630 631 632 633
        );
        $rsm->addFieldResult('c', 'c__id', 'id');
        $rsm->addFieldResult('c', 'c__position', 'position');
        $rsm->addFieldResult('c', 'c__name', 'name');
        $rsm->addFieldResult('b', 'b__id', 'id');
        $rsm->addFieldResult('b', 'b__position', 'position');
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670

        // Faked result set
        $resultSet = array(
            array(
                'c__id' => '1',
                'c__position' => '0',
                'c__name' => 'First',
                'b__id' => '1',
                'b__position' => '0',
                //'b__category_id' => '1'
                ),
           array(
                'c__id' => '2',
                'c__position' => '0',
                'c__name' => 'Second',
                'b__id' => '2',
                'b__position' => '0',
                //'b__category_id' => '2'
                ),
            array(
                'c__id' => '1',
                'c__position' => '0',
                'c__name' => 'First',
                'b__id' => '3',
                'b__position' => '1',
                //'b__category_id' => '1'
                ),
           array(
                'c__id' => '1',
                'c__position' => '0',
                'c__name' => 'First',
                'b__id' => '4',
                'b__position' => '2',
                //'b__category_id' => '1'
                )
            );

671 672
        $stmt = new HydratorMockStatement($resultSet);
        $hydrator = new \Doctrine\ORM\Internal\Hydration\ObjectHydrator($this->_em);
673

674
        $result = $hydrator->hydrateAll($stmt, $rsm, array(Query::HINT_FORCE_PARTIAL_LOAD => true));
675 676

        $this->assertEquals(2, count($result));
677 678
        $this->assertTrue($result[0] instanceof \Doctrine\Tests\Models\Forum\ForumCategory);
        $this->assertTrue($result[1] instanceof \Doctrine\Tests\Models\Forum\ForumCategory);
679
        $this->assertTrue($result[0] !== $result[1]);
680 681 682 683 684 685 686 687 688
        $this->assertEquals(1, $result[0]->getId());
        $this->assertEquals(2, $result[1]->getId());
        $this->assertTrue(isset($result[0]->boards));
        $this->assertEquals(3, count($result[0]->boards));
        $this->assertTrue(isset($result[1]->boards));
        $this->assertEquals(1, count($result[1]->boards));

    }

689 690
    public function testResultIteration()
    {
691
        $rsm = new ResultSetMapping;
692
        $rsm->addEntityResult('Doctrine\Tests\Models\CMS\CmsUser', 'u');
693 694
        $rsm->addFieldResult('u', 'u__id', 'id');
        $rsm->addFieldResult('u', 'u__name', 'name');
695 696 697 698 699 700 701 702 703 704 705 706 707 708

        // Faked result set
        $resultSet = array(
            array(
                'u__id' => '1',
                'u__name' => 'romanb'
                ),
            array(
                'u__id' => '2',
                'u__name' => 'jwage'
                )
            );


709 710
        $stmt = new HydratorMockStatement($resultSet);
        $hydrator = new \Doctrine\ORM\Internal\Hydration\ObjectHydrator($this->_em);
711

712
        $iterableResult = $hydrator->iterate($stmt, $rsm, array(Query::HINT_FORCE_PARTIAL_LOAD => true));
713 714 715 716

        $rowNum = 0;
        while (($row = $iterableResult->next()) !== false) {
            $this->assertEquals(1, count($row));
717
            $this->assertTrue($row[0] instanceof \Doctrine\Tests\Models\CMS\CmsUser);
718 719 720 721 722 723 724 725 726 727
            if ($rowNum == 0) {
                $this->assertEquals(1, $row[0]->id);
                $this->assertEquals('romanb', $row[0]->name);
            } else if ($rowNum == 1) {
                $this->assertEquals(2, $row[0]->id);
                $this->assertEquals('jwage', $row[0]->name);
            }
            ++$rowNum;
        }
    }
728
}