Failed Conditions
Pull Request — 2.6 (#7882)
by
unknown
07:43
created

Issue7877ApplicationGenerated   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 18
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 1
eloc 4
c 1
b 0
f 0
dl 0
loc 18
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
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
        var_dump(array_slice($this->_sqlLoggerStack->queries, -10));
0 ignored issues
show
Security Debugging Code introduced by
var_dump(array_slice($th...erStack->queries, -10)) looks like debug code. Are you sure you do not want to remove it?
Loading history...
45
        $this->assertCount($count + 5, $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->parent->id);
86
    }
87
}
88
89
/**
90
 * @Entity
91
 */
92
class Issue7877ApplicationGenerated
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
    /**
107
     * @ManyToOne(targetEntity="Doctrine\Tests\ORM\Functional\Issue7877ApplicationGenerated")
108
     */
109
    public $parent;
110
}
111
112
/**
113
 * @Entity
114
 */
115
class Issue7877DatabaseGenerated
116
{
117
    /**
118
     * @Id
119
     * @Column(type="integer")
120
     * @GeneratedValue(strategy="AUTO")
121
     */
122
    public $id;
123
124
    /**
125
     * @ManyToOne(targetEntity="Doctrine\Tests\ORM\Functional\Issue7877DatabaseGenerated")
126
     */
127
    public $parent;
128
}
129