Failed Conditions
Pull Request — 2.7 (#7901)
by Luís
06:54
created

MergeSharedEntitiesTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 44
dl 0
loc 96
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testMergeSharedManagedEntities() 0 16 1
A testMergeSharedDetachedSerializedEntities() 0 18 1
A testMergeSharedNewEntities() 0 11 1
A ensureTestGeneratedDeprecationMessages() 0 3 1
A setUp() 0 12 2
A testMergeInheritedTransientPrivateProperties() 0 14 1
1
<?php
2
3
namespace Doctrine\Tests\ORM\Functional;
4
5
use Doctrine\ORM\Tools\ToolsException;
6
use Doctrine\Tests\OrmFunctionalTestCase;
7
use Doctrine\Tests\VerifyDeprecations;
8
9
class MergeSharedEntitiesTest extends OrmFunctionalTestCase
10
{
11
    use VerifyDeprecations;
12
13
    /**
14
     * {@inheritDoc}
15
     */
16
    protected function setUp()
17
    {
18
        parent::setUp();
19
20
        try {
21
            $this->_schemaTool->createSchema(
22
                [
23
                $this->_em->getClassMetadata(MSEFile::class),
24
                $this->_em->getClassMetadata(MSEPicture::class),
25
                ]
26
            );
27
        } catch (ToolsException $ignored) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
28
        }
29
    }
30
31
    /** @after */
32
    public function ensureTestGeneratedDeprecationMessages() : void
33
    {
34
        $this->assertHasDeprecationMessages();
35
    }
36
37
    public function testMergeSharedNewEntities()
38
    {
39
        $file    = new MSEFile;
40
        $picture = new MSEPicture;
41
42
        $picture->file      = $file;
43
        $picture->otherFile = $file;
44
45
        $picture = $this->_em->merge($picture);
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\ORM\EntityManager::merge() has been deprecated: 3.0 This method is being removed from the ORM and won't have any replacement ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

45
        $picture = /** @scrutinizer ignore-deprecated */ $this->_em->merge($picture);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
46
47
        $this->assertEquals($picture->file, $picture->otherFile, 'Identical entities must remain identical');
48
    }
49
50
    public function testMergeSharedManagedEntities()
51
    {
52
        $file    = new MSEFile;
53
        $picture = new MSEPicture;
54
55
        $picture->file      = $file;
56
        $picture->otherFile = $file;
57
58
        $this->_em->persist($file);
59
        $this->_em->persist($picture);
60
        $this->_em->flush();
61
        $this->_em->clear();
62
63
        $picture = $this->_em->merge($picture);
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\ORM\EntityManager::merge() has been deprecated: 3.0 This method is being removed from the ORM and won't have any replacement ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

63
        $picture = /** @scrutinizer ignore-deprecated */ $this->_em->merge($picture);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
64
65
        $this->assertEquals($picture->file, $picture->otherFile, 'Identical entities must remain identical');
66
    }
67
68
    public function testMergeSharedDetachedSerializedEntities()
69
    {
70
        $file    = new MSEFile;
71
        $picture = new MSEPicture;
72
73
        $picture->file      = $file;
74
        $picture->otherFile = $file;
75
76
        $serializedPicture = serialize($picture);
77
78
        $this->_em->persist($file);
79
        $this->_em->persist($picture);
80
        $this->_em->flush();
81
        $this->_em->clear();
82
83
        $picture = $this->_em->merge(unserialize($serializedPicture));
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\ORM\EntityManager::merge() has been deprecated: 3.0 This method is being removed from the ORM and won't have any replacement ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

83
        $picture = /** @scrutinizer ignore-deprecated */ $this->_em->merge(unserialize($serializedPicture));

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
84
85
        $this->assertEquals($picture->file, $picture->otherFile, 'Identical entities must remain identical');
86
    }
87
88
    /**
89
     * @group DDC-2704
90
     */
91
    public function testMergeInheritedTransientPrivateProperties()
92
    {
93
        $admin1 = new MSEAdmin();
94
        $admin2 = new MSEAdmin();
95
96
        $admin1->id = 123;
97
        $admin2->id = 123;
98
99
        $this->_em->persist($admin1);
100
101
        $admin2->setSession('zeh current session data');
102
103
        $this->assertSame($admin1, $this->_em->merge($admin2));
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\ORM\EntityManager::merge() has been deprecated: 3.0 This method is being removed from the ORM and won't have any replacement ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

103
        $this->assertSame($admin1, /** @scrutinizer ignore-deprecated */ $this->_em->merge($admin2));

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
104
        $this->assertSame('zeh current session data', $admin1->getSession());
105
    }
106
}
107
108
/** @Entity */
109
class MSEPicture
110
{
111
    /** @Column(type="integer") @Id @GeneratedValue */
112
    public $id;
113
114
    /** @ManyToOne(targetEntity="MSEFile", cascade={"merge"}) */
115
    public $file;
116
117
    /** @ManyToOne(targetEntity="MSEFile", cascade={"merge"}) */
118
    public $otherFile;
119
}
120
121
/** @Entity */
122
class MSEFile
123
{
124
    /** @Column(type="integer") @Id @GeneratedValue(strategy="AUTO") */
125
    public $id;
126
}
127
128
/** @MappedSuperclass */
129
abstract class MSEUser
130
{
131
    private $session; // intentionally transient property
132
133
    public function getSession()
134
    {
135
        return $this->session;
136
    }
137
138
    public function setSession($session)
139
    {
140
        $this->session = $session;
141
    }
142
}
143
144
/** @Entity */
145
class MSEAdmin extends MSEUser
146
{
147
    /** @Column(type="integer") @Id @GeneratedValue(strategy="NONE") */
148
    public $id;
149
}
150