Completed
Pull Request — 2.6 (#7882)
by
unknown
08:12
created

testNoExtraUpdateWithApplicationGeneratedId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 15
rs 9.9666
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
        $count = \count($this->_sqlLoggerStack->queries);
35
36
        $parent = new $class($parentId = 1);
37
        $this->_em->persist($parent);
38
39
        $child         = new $class($childId = 2);
40
        $child->parent = $parent;
41
        $this->_em->persist($child);
42
43
        $this->_em->flush();
44
        $this->assertCount($count + 5, $this->_sqlLoggerStack->queries);
45
46
        $this->_em->clear();
47
48
        $child = $this->_em->find($class, $childId);
49
        $this->assertSame($parentId, $child->parent->id);
50
    }
51
52
    public function testNoExtraUpdateWithApplicationGeneratedId()
53
    {
54
        $count = \count($this->_sqlLoggerStack->queries);
55
56
        $entity         = new Issue7877ApplicationGenerated($entityId = 1);
57
        $entity->parent = $entity;
58
        $this->_em->persist($entity);
59
60
        $this->_em->flush();
61
        $this->assertCount($count + 3, $this->_sqlLoggerStack->queries);
62
63
        $this->_em->clear();
64
65
        $child = $this->_em->find(Issue7877ApplicationGenerated::class, $entityId);
66
        $this->assertSame($entityId, $child->parent->id);
67
    }
68
69
    public function textExtraUpdateWithDatabaseGeneratedId()
70
    {
71
        $count = \count($this->_sqlLoggerStack->queries);
72
73
        $entity         = new Issue7877DatabaseGenerated();
74
        $entity->parent = $entity;
75
        $this->_em->persist($entity);
76
77
        $this->_em->flush();
78
        $this->assertCount($count + 4, $this->_sqlLoggerStack->queries);
79
        $entityId = $entity->id;
80
81
        $this->_em->clear();
82
83
        $child = $this->_em->find(Issue7877DatabaseGenerated::class, $entityId);
84
        $this->assertSame($entityId, $child->parent->id);
85
    }
86
}
87
88
/**
89
 * @Entity
90
 */
91
class Issue7877ApplicationGenerated
92
{
93
    public function __construct(int $id)
94
    {
95
        $this->id = $id;
96
    }
97
98
    /**
99
     * @Id
100
     * @Column(type="integer")
101
     * @GeneratedValue(strategy="NONE")
102
     */
103
    public $id;
104
105
    /**
106
     * @ManyToOne(targetEntity="Doctrine\Tests\ORM\Functional\Issue7877ApplicationGenerated")
107
     */
108
    public $parent;
109
}
110
111
/**
112
 * @Entity
113
 */
114
class Issue7877DatabaseGenerated
115
{
116
    /**
117
     * @Id
118
     * @Column(type="integer")
119
     * @GeneratedValue(strategy="AUTO")
120
     */
121
    public $id;
122
123
    /**
124
     * @ManyToOne(targetEntity="Doctrine\Tests\ORM\Functional\Issue7877DatabaseGenerated")
125
     */
126
    public $parent;
127
}
128