Failed Conditions
Pull Request — 2.6 (#7506)
by
unknown
09:52
created

ensureTestGeneratedDeprecationMessages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Tests\ORM\Functional\Ticket;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\Tests\Models\CMS\CmsUser;
7
use Doctrine\Tests\Models\CMS\CmsGroup;
8
use Doctrine\Tests\VerifyDeprecations;
9
10
class DDC758Test extends \Doctrine\Tests\OrmFunctionalTestCase
11
{
12
    use VerifyDeprecations;
13
14
    public function setUp()
15
    {
16
        $this->markTestSkipped('Destroys testsuite');
17
        $this->useModelSet("cms");
18
19
        parent::setUp();
20
    }
21
22
    /** @after */
23
    public function ensureTestGeneratedDeprecationMessages() : void
24
    {
25
        $this->assertHasDeprecationMessages();
26
    }
27
28
    /**
29
     * Helper method to set cascade to merge only
30
     */
31
    private function setCascadeMergeFor($class)
32
    {
33
        $metadata = $this->_em->getMetadataFactory()->getMetadataFor($class);
34
        foreach ($metadata->associationMappings as $key => $associationMapping) {
35
            $metadata->associationMappings[$key]["isCascadePersist"] = false;
0 ignored issues
show
Bug introduced by
Accessing associationMappings on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
36
            $metadata->associationMappings[$key]["isCascadeMerge"] = true;
37
            $metadata->associationMappings[$key]["isCascadeRemove"] = false;
38
            $metadata->associationMappings[$key]["isCascadeDetach"] = false;
39
        }
40
    }
41
42
    /**
43
     * Test that changing associations on detached entities and then cascade merging them
44
     * causes the database to be updated with the new associations.
45
     * This specifically tests adding new associations.
46
     */
47
    public function testManyToManyMergeAssociationAdds()
48
    {
49
        $this->setCascadeMergeFor(CmsUser::class);
50
        $this->setCascadeMergeFor(CmsGroup::class);
51
52
        // Put entities in the database
53
        $cmsUser = new CmsUser();
54
        $cmsUser->username = "dave";
55
        $cmsUser->name = "Dave Keen";
56
        $cmsUser->status = "testing";
57
58
        $group1 = new CmsGroup();
59
        $group1->name = "Group 1";
60
61
        $group2 = new CmsGroup();
62
        $group2->name = "Group 2";
63
64
        $this->_em->persist($cmsUser);
65
        $this->_em->persist($group1);
66
        $this->_em->persist($group2);
67
        $this->_em->flush();
68
69
        $cmsUserId = $cmsUser->id;
70
        $group1Id = $group1->id;
71
        $group2Id = $group2->id;
72
73
        $this->_em->clear();
74
75
        // Now create detached versions of the entities with some new associations.
76
        $cmsUser = new CmsUser();
77
        $cmsUser->id = $cmsUserId;
78
        $cmsUser->username = "dave";
79
        $cmsUser->name = "Dave Keen";
80
        $cmsUser->status = "testing";
81
        $cmsUser->groups = new ArrayCollection();
82
83
        $group1 = new CmsGroup();
84
        $group1->id = $group1Id;
85
        $group1->name = "Group 1";
86
        $group1->users = new ArrayCollection();
87
88
        $group2 = new CmsGroup();
89
        $group2->id = $group2Id;
90
        $group2->name = "Group 2";
91
        $group2->users = new ArrayCollection();
92
93
        $cmsUser->addGroup($group1);
94
        $cmsUser->addGroup($group2);
95
96
        // Cascade merge of cmsUser followed by a flush should add in the bidirectional new many-to-many associations between the user and the groups
97
        $this->_em->merge($cmsUser);
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\ORM\EntityManager::merge() has been deprecated: 2.7 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

97
        /** @scrutinizer ignore-deprecated */ $this->_em->merge($cmsUser);

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...
98
        $this->_em->flush();
99
100
        $this->_em->clear();
101
102
        $cmsUsers = $this->_em->getRepository(CmsUser::class)->findAll();
103
        $cmsGroups = $this->_em->getRepository(CmsGroup::class)->findAll();
104
105
        // Check the entities are in the database
106
        $this->assertEquals(1, sizeof($cmsUsers));
107
        $this->assertEquals(2, sizeof($cmsGroups));
108
109
        // Check the associations between the entities are now in the database
110
        $this->assertEquals(2, sizeof($cmsUsers[0]->groups));
111
        $this->assertEquals(1, sizeof($cmsGroups[0]->users));
112
        $this->assertEquals(1, sizeof($cmsGroups[1]->users));
113
114
        $this->assertSame($cmsUsers[0]->groups[0], $cmsGroups[0]);
115
        $this->assertSame($cmsUsers[0]->groups[1], $cmsGroups[1]);
116
        $this->assertSame($cmsGroups[0]->users[0], $cmsUsers[0]);
117
        $this->assertSame($cmsGroups[1]->users[0], $cmsUsers[0]);
118
    }
119
120
    /**
121
     * Test that changing associations on detached entities and then cascade merging them causes the
122
     * database to be updated with the new associations.
123
     */
124
    public function testManyToManyMergeAssociationRemoves()
125
    {
126
        $this->setCascadeMergeFor(CmsUser::class);
127
        $this->setCascadeMergeFor(CmsGroup::class);
128
129
        $cmsUser = new CmsUser();
130
        $cmsUser->username = "dave";
131
        $cmsUser->name = "Dave Keen";
132
        $cmsUser->status = "testing";
133
134
        $group1 = new CmsGroup();
135
        $group1->name = "Group 1";
136
137
        $group2 = new CmsGroup();
138
        $group2->name = "Group 2";
139
140
        $cmsUser->addGroup($group1);
141
        $cmsUser->addGroup($group2);
142
143
        $this->_em->persist($cmsUser);
144
        $this->_em->persist($group1);
145
        $this->_em->persist($group2);
146
        $this->_em->flush();
147
148
        $cmsUserId = $cmsUser->id;
149
        $group1Id = $group1->id;
150
        $group2Id = $group2->id;
151
152
        $this->_em->clear();
153
154
        // Now create detached versions of the entities with NO associations.
155
        $cmsUser = new CmsUser();
156
        $cmsUser->id = $cmsUserId;
157
        $cmsUser->username = "dave";
158
        $cmsUser->name = "Dave Keen";
159
        $cmsUser->status = "testing";
160
        $cmsUser->groups = new ArrayCollection();
161
162
        $group1 = new CmsGroup();
163
        $group1->id = $group1Id;
164
        $group1->name = "Group 1";
165
        $group1->users = new ArrayCollection();
166
167
        $group2 = new CmsGroup();
168
        $group2->id = $group2Id;
169
        $group2->name = "Group 2";
170
        $group2->users = new ArrayCollection();
171
172
        // Cascade merge of cmsUser followed by a flush should result in the association array collection being empty
173
        $this->_em->merge($cmsUser);
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\ORM\EntityManager::merge() has been deprecated: 2.7 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

173
        /** @scrutinizer ignore-deprecated */ $this->_em->merge($cmsUser);

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...
174
        $this->_em->flush();
175
176
        $this->_em->clear();
177
178
        $cmsUsers = $this->_em->getRepository(CmsUser::class)->findAll();
179
        $cmsGroups = $this->_em->getRepository(CmsGroup::class)->findAll();
180
181
        // Check the entities are in the database
182
        $this->assertEquals(1, sizeof($cmsUsers));
183
        $this->assertEquals(2, sizeof($cmsGroups));
184
185
        // Check the associations between the entities are now in the database
186
        $this->assertEquals(0, sizeof($cmsUsers[0]->groups));
187
        $this->assertEquals(0, sizeof($cmsGroups[0]->users));
188
        $this->assertEquals(0, sizeof($cmsGroups[1]->users));
189
    }
190
}
191