Completed
Pull Request — 2.6 (#7882)
by
unknown
06:32
created

GH7877Test::testExtraUpdateWithDifferentEntities()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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