File: DefaultCollectionHydratorTest.php

package info (click to toggle)
doctrine 3.5.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 11,552 kB
  • sloc: php: 108,302; xml: 1,340; makefile: 35; sh: 14
file content (77 lines) | stat: -rw-r--r-- 3,088 bytes parent folder | download | duplicates (3)
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
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Cache;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Cache\CollectionCacheEntry;
use Doctrine\ORM\Cache\CollectionCacheKey;
use Doctrine\ORM\Cache\DefaultCollectionHydrator;
use Doctrine\ORM\Cache\EntityCacheEntry;
use Doctrine\ORM\Cache\EntityCacheKey;
use Doctrine\ORM\PersistentCollection;
use Doctrine\ORM\UnitOfWork;
use Doctrine\Tests\Models\Cache\City;
use Doctrine\Tests\Models\Cache\State;
use Doctrine\Tests\OrmFunctionalTestCase;
use PHPUnit\Framework\Attributes\Group;

#[Group('DDC-2183')]
class DefaultCollectionHydratorTest extends OrmFunctionalTestCase
{
    private DefaultCollectionHydrator $structure;

    protected function setUp(): void
    {
        $this->enableSecondLevelCache();

        parent::setUp();

        $this->structure = new DefaultCollectionHydrator($this->_em);
    }

    public function testImplementsCollectionEntryStructure(): void
    {
        self::assertInstanceOf(DefaultCollectionHydrator::class, $this->structure);
    }

    public function testLoadCacheCollection(): void
    {
        $targetRegion = $this->_em->getCache()->getEntityCacheRegion(City::class);
        $entry        = new CollectionCacheEntry(
            [
                new EntityCacheKey(City::class, ['id' => 31]),
                new EntityCacheKey(City::class, ['id' => 32]),
            ],
        );

        $targetRegion->put(new EntityCacheKey(City::class, ['id' => 31]), new EntityCacheEntry(City::class, ['id' => 31, 'name' => 'Foo']));
        $targetRegion->put(new EntityCacheKey(City::class, ['id' => 32]), new EntityCacheEntry(City::class, ['id' => 32, 'name' => 'Bar']));

        $sourceClass = $this->_em->getClassMetadata(State::class);
        $targetClass = $this->_em->getClassMetadata(City::class);
        $key         = new CollectionCacheKey($sourceClass->name, 'cities', ['id' => 21]);
        $collection  = new PersistentCollection($this->_em, $targetClass, new ArrayCollection());
        $list        = $this->structure->loadCacheEntry($sourceClass, $key, $entry, $collection);

        self::assertNotNull($list);
        self::assertCount(2, $list);
        self::assertCount(2, $collection);

        self::assertInstanceOf($targetClass->name, $list[0]);
        self::assertInstanceOf($targetClass->name, $list[1]);
        self::assertInstanceOf($targetClass->name, $collection[0]);
        self::assertInstanceOf($targetClass->name, $collection[1]);

        self::assertSame($list[0], $collection[0]);
        self::assertSame($list[1], $collection[1]);

        self::assertEquals(31, $list[0]->getId());
        self::assertEquals(32, $list[1]->getId());
        self::assertEquals($list[0]->getId(), $collection[0]->getId());
        self::assertEquals($list[1]->getId(), $collection[1]->getId());
        self::assertEquals(UnitOfWork::STATE_MANAGED, $this->_em->getUnitOfWork()->getEntityState($collection[0]));
        self::assertEquals(UnitOfWork::STATE_MANAGED, $this->_em->getUnitOfWork()->getEntityState($collection[1]));
    }
}