Failed Conditions
Pull Request — master (#7688)
by Gabriel
09:49
created

GH6217FetchedEntity::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\ORM\Functional\Ticket;
6
7
use Doctrine\ORM\Annotation as ORM;
8
use Doctrine\Tests\OrmFunctionalTestCase;
9
use function uniqid;
10
11
/**
12
 * @group #6217
13
 */
14
final class GH6217Test extends OrmFunctionalTestCase
15
{
16
    public function setUp() : void
17
    {
18
        $this->enableSecondLevelCache();
19
20
        parent::setUp();
21
22
        $this->schemaTool->createSchema([
23
            $this->em->getClassMetadata(GH6217AssociatedEntity::class),
24
            $this->em->getClassMetadata(GH6217FetchedEntity::class),
25
        ]);
26
    }
27
28
    public function testLoadingOfSecondLevelCacheOnEagerAssociations() : void
29
    {
30
        $lazy    = new GH6217AssociatedEntity();
31
        $eager   = new GH6217AssociatedEntity();
32
        $fetched = new GH6217FetchedEntity($lazy, $eager);
33
34
        $this->em->persist($eager);
35
        $this->em->persist($lazy);
36
        $this->em->persist($fetched);
37
        $this->em->flush();
38
        $this->em->clear();
39
40
        $repository = $this->em->getRepository(GH6217FetchedEntity::class);
41
        $filters    = ['eager' => $eager->id];
42
43
        self::assertCount(1, $repository->findBy($filters));
44
        $queryCount = $this->getCurrentQueryCount();
45
46
        /** @var GH6217FetchedEntity[] $found */
47
        $found = $repository->findBy($filters);
48
49
        self::assertCount(1, $found);
50
        self::assertInstanceOf(GH6217FetchedEntity::class, $found[0]);
51
        self::assertSame($lazy->id, $found[0]->lazy->id);
52
        self::assertSame($eager->id, $found[0]->eager->id);
53
        self::assertEquals($queryCount, $this->getCurrentQueryCount(), 'No queries were executed in `findBy`');
54
    }
55
}
56
57
/** @ORM\Entity @ORM\Cache(usage="NONSTRICT_READ_WRITE") */
58
class GH6217AssociatedEntity
59
{
60
    /** @ORM\Id @ORM\Column(type="string") @ORM\GeneratedValue(strategy="NONE") */
61
    public $id;
62
63
    public function __construct()
64
    {
65
        $this->id = uniqid(self::class, true);
66
    }
67
}
68
69
/** @ORM\Entity @ORM\Cache(usage="NONSTRICT_READ_WRITE") */
70
class GH6217FetchedEntity
71
{
72
    /** @ORM\Id @ORM\Cache("NONSTRICT_READ_WRITE") @ORM\ManyToOne(targetEntity=GH6217AssociatedEntity::class) */
73
    public $lazy;
74
75
    /** @ORM\Id @ORM\Cache("NONSTRICT_READ_WRITE") @ORM\ManyToOne(targetEntity=GH6217AssociatedEntity::class, fetch="EAGER") */
76
    public $eager;
77
78
    public function __construct(GH6217AssociatedEntity $lazy, GH6217AssociatedEntity $eager)
79
    {
80
        $this->lazy  = $lazy;
81
        $this->eager = $eager;
82
    }
83
}
84