Completed
Pull Request — master (#5823)
by Mikhail
13:41
created

DDC2346Test::testIssue()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 29
rs 8.8571
cc 1
eloc 19
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Tests\ORM\Functional\Ticket;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\DBAL\Logging\DebugStack;
7
8
/**
9
 * @group DDC-2346
10
 */
11
class DDC2346Test extends \Doctrine\Tests\OrmFunctionalTestCase
12
{
13
    /**
14
     * @var \Doctrine\DBAL\Logging\DebugStack
15
     */
16
    protected $logger;
17
18
    /**
19
     * {@inheritDoc}
20
     */
21
    protected function setUp()
22
    {
23
        parent::setUp();
24
25
        $this->_schemaTool->createSchema(array(
26
            $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC2346Foo'),
27
            $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC2346Bar'),
28
            $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC2346Baz'),
29
        ));
30
31
        $this->logger = new DebugStack();
32
    }
33
34
    /**
35
     * Verifies that fetching a OneToMany association with fetch="EAGER" does not cause N+1 queries
36
     */
37
    public function testIssue()
38
    {
39
        $foo1        = new DDC2346Foo();
40
        $foo2        = new DDC2346Foo();
41
42
        $baz1        = new DDC2346Baz();
43
        $baz2        = new DDC2346Baz();
44
45
        $baz1->foo   = $foo1;
46
        $baz2->foo   = $foo2;
47
48
        $foo1->bars[] = $baz1;
49
        $foo1->bars[] = $baz2;
50
51
        $this->_em->persist($foo1);
52
        $this->_em->persist($foo2);
53
        $this->_em->persist($baz1);
54
        $this->_em->persist($baz2);
55
56
        $this->_em->flush();
57
        $this->_em->clear();
58
59
        $this->_em->getConnection()->getConfiguration()->setSQLLogger($this->logger);
60
61
        $fetchedBazs = $this->_em->getRepository(__NAMESPACE__ . '\\DDC2346Baz')->findAll();
62
63
        $this->assertCount(2, $fetchedBazs);
64
        $this->assertCount(2, $this->logger->queries, 'The total number of executed queries is 2, and not n+1');
65
    }
66
}
67
68
/** @Entity */
69
class DDC2346Foo
70
{
71
    /** @Id @Column(type="integer") @GeneratedValue */
72
    public $id;
73
74
    /**
75
     * @var DDC2346Bar[]|\Doctrine\Common\Collections\Collection
76
     *
77
     * @OneToMany(targetEntity="DDC2346Bar", mappedBy="foo")
78
     */
79
    public $bars;
80
81
    /** Constructor */
82
    public function __construct() {
83
        $this->bars = new ArrayCollection();
84
    }
85
}
86
87
/**
88
 * @Entity
89
 * @InheritanceType("JOINED")
90
 * @DiscriminatorColumn(name="discr", type="string")
91
 * @DiscriminatorMap({"bar" = "DDC2346Bar", "baz" = "DDC2346Baz"})
92
 */
93
class DDC2346Bar
94
{
95
    /** @Id @Column(type="integer") @GeneratedValue */
96
    public $id;
97
98
    /** @ManyToOne(targetEntity="DDC2346Foo", inversedBy="bars", fetch="EAGER") */
99
    public $foo;
100
}
101
102
103
/**
104
 * @Entity
105
 */
106
class DDC2346Baz extends DDC2346Bar
107
{
108
109
}