Passed
Pull Request — 2.6 (#7882)
by
unknown
07:46
created

SelfReferencingTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 10
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\ORM\Functional;
6
7
use Doctrine\Tests\OrmFunctionalTestCase;
8
9
class SelfReferencingTest extends OrmFunctionalTestCase
10
{
11
    protected function setUp() : void
12
    {
13
        parent::setUp();
14
        $classMetadatas = [
15
            $this->_em->getClassMetadata(Issue7877ApplicationGenerated::class),
16
            $this->_em->getClassMetadata(Issue7877DatabaseGenerated::class),
17
        ];
18
        // We first drop the schema to avoid collision between tests
19
        $this->_schemaTool->dropSchema($classMetadatas);
20
        $this->_schemaTool->createSchema($classMetadatas);
21
    }
22
23
    public function providerDifferentEntity()
24
    {
25
        yield [Issue7877ApplicationGenerated::class];
26
        yield [Issue7877DatabaseGenerated::class];
27
    }
28
29
    /**
30
     * @dataProvider providerDifferentEntity
31
     */
32
    public function testExtraUpdateWithDifferentEntities(string $class)
33
    {
34
        $parent = new $class($parentId = 1);
35
        $this->_em->persist($parent);
36
37
        $child         = new $class($childId = 2);
38
        $child->parent = $parent;
39
        $this->_em->persist($child);
40
41
        $count = \count($this->_sqlLoggerStack->queries);
42
        $this->_em->flush();
43
        $this->assertCount($count + 5, $this->_sqlLoggerStack->queries);
44
45
        $this->_em->clear();
46
47
        $child = $this->_em->find($class, $childId);
48
        $this->assertSame($parentId, $child->parent->id);
49
    }
50
51
    public function testNoExtraUpdateWithApplicationGeneratedId()
52
    {
53
        $entity         = new Issue7877ApplicationGenerated($entityId = 1);
54
        $entity->parent = $entity;
55
        $this->_em->persist($entity);
56
57
        $count = \count($this->_sqlLoggerStack->queries);
58
        $this->_em->flush();
59
        $this->assertCount($count + 3, $this->_sqlLoggerStack->queries);
60
61
        $this->_em->clear();
62
63
        $child = $this->_em->find(Issue7877ApplicationGenerated::class, $entityId);
64
        $this->assertSame($entityId, $child->parent->id);
65
    }
66
67
    public function textExtraUpdateWithDatabaseGeneratedId()
68
    {
69
        $entity         = new Issue7877DatabaseGenerated();
70
        $entity->parent = $entity;
71
        $this->_em->persist($entity);
72
73
        $count = \count($this->_sqlLoggerStack->queries);
74
        $this->_em->flush();
75
        $this->assertCount($count + 4, $this->_sqlLoggerStack->queries);
76
        $entityId = $entity->id;
77
78
        $this->_em->clear();
79
80
        $child = $this->_em->find(Issue7877DatabaseGenerated::class, $entityId);
81
        $this->assertSame($entityId, $child->parent->id);
82
    }
83
}
84
85
/**
86
 * @Entity
87
 */
88
class Issue7877ApplicationGenerated
89
{
90
    public function __construct(int $id)
91
    {
92
        $this->id = $id;
93
    }
94
95
    /**
96
     * @Id
97
     * @Column(type="integer")
98
     * @GeneratedValue(strategy="NONE")
99
     */
100
    public $id;
101
102
    /**
103
     * @ManyToOne(targetEntity="Doctrine\Tests\ORM\Functional\Issue7877ApplicationGenerated")
104
     */
105
    public $parent;
106
}
107
108
/**
109
 * @Entity
110
 */
111
class Issue7877DatabaseGenerated
112
{
113
    /**
114
     * @Id
115
     * @Column(type="integer")
116
     * @GeneratedValue(strategy="AUTO")
117
     */
118
    public $id;
119
120
    /**
121
     * @ManyToOne(targetEntity="Doctrine\Tests\ORM\Functional\Issue7877DatabaseGenerated")
122
     */
123
    public $parent;
124
}
125