Failed Conditions
Pull Request — 2.6 (#7882)
by
unknown
06:45
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\ORM\Mapping as ORM;
8
use Doctrine\Tests\OrmFunctionalTestCase;
9
10
class SelfReferencingTest extends OrmFunctionalTestCase
11
{
12
    protected function setUp() : void
13
    {
14
        parent::setUp();
15
        $classMetadatas = [
16
            $this->_em->getClassMetadata(Issue7877ApplicationGenerated::class),
17
            $this->_em->getClassMetadata(Issue7877DatabaseGenerated::class),
18
        ];
19
        // We first drop the schema to avoid collision between tests
20
        $this->_schemaTool->dropSchema($classMetadatas);
21
        $this->_schemaTool->createSchema($classMetadatas);
22
    }
23
24
    public function providerDifferentEntity()
25
    {
26
        yield [Issue7877ApplicationGenerated::class];
27
        yield [Issue7877DatabaseGenerated::class];
28
    }
29
30
    /**
31
     * @dataProvider providerDifferentEntity
32
     */
33
    public function testExtraUpdateWithDifferentEntities(string $class)
34
    {
35
        $count = \count($this->_sqlLoggerStack->queries);
36
37
        $parent = new $class($parentId = 1);
38
        $this->_em->persist($parent);
39
40
        $child         = new $class($childId = 2);
41
        $child->parent = $parent;
42
        $this->_em->persist($child);
43
44
        $this->_em->flush();
45
        $this->assertCount($count + 4, $this->_sqlLoggerStack->queries);
46
47
        $this->_em->clear();
48
49
        $child = $this->_em->find($class, $childId);
50
        $this->assertSame($parentId, $child->parent->id);
51
    }
52
53
    public function testNoExtraUpdateWithApplicationGeneratedId()
54
    {
55
        $count = \count($this->_sqlLoggerStack->queries);
56
57
        $entity         = new Issue7877ApplicationGenerated($entityId = 1);
58
        $entity->parent = $entity;
59
        $this->_em->persist($entity);
60
61
        $this->_em->flush();
62
        $this->assertCount($count + 3, $this->_sqlLoggerStack->queries);
63
64
        $this->_em->clear();
65
66
        $child = $this->_em->find(Issue7877ApplicationGenerated::class, $entityId);
67
        $this->assertSame($entityId, $child->parent->id);
68
    }
69
70
    public function textExtraUpdateWithDatabaseGeneratedId()
71
    {
72
        $count = \count($this->_sqlLoggerStack->queries);
73
74
        $entity         = new Issue7877DatabaseGenerated();
75
        $entity->parent = $entity;
76
        $this->_em->persist($entity);
77
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(Issue7877DatabaseGenerated::class, $entityId);
85
        $this->assertSame($entityId, $child->paren->id);
0 ignored issues
show
Bug introduced by
The property paren does not exist on Doctrine\Tests\ORM\Funct...ue7877DatabaseGenerated. Did you mean parent?
Loading history...
86
    }
87
}
88
89
/**
90
 * @ORM\Entity
91
 */
92
class Issue7877ApplicationGenerated
93
{
94
    public function __construct(int $id)
95
    {
96
        $this->id = $id;
97
    }
98
99
    /**
100
     * @ORM\Id
101
     * @ORM\Column(type="integer")
102
     * @ORM\GeneratedValue(strategy="NONE")
103
     */
104
    public $id;
105
106
    /**
107
     * @ORM\ManyToOne(targetEntity="Doctrine\Tests\ORM\Functional\Issue7877ApplicationGenerated")
108
     */
109
    public $parent;
110
}
111
112
/**
113
 * @ORM\Entity
114
 */
115
class Issue7877DatabaseGenerated
116
{
117
    /**
118
     * @ORM\Id
119
     * @ORM\Column(type="integer")
120
     * @ORM\GeneratedValue(strategy="AUTO")
121
     */
122
    public $id;
123
124
    /**
125
     * @ORM\ManyToOne(targetEntity="Doctrine\Tests\ORM\Functional\Issue7877DatabaseGenerated")
126
     */
127
    public $parent;
128
}
129