Conditions | 1 |
Paths | 1 |
Total Lines | 71 |
Code Lines | 48 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
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); |
||
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 | } |
||
191 |