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

Issue7877ApplicationGenerated::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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