Completed
Push — master ( 707688...6da863 )
by Mike
04:47 queued 02:08
created

EmbeddedAssociationTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 133
Duplicated Lines 9.02 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 6
dl 12
loc 133
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testShouldNotSaveUnchanged() 0 15 1
A testSaveModifiedEmbedded() 0 21 1
A testSaveEmbedded() 0 22 1
B setUp() 0 30 1
A testUpdateBooleanToFalseInEmbeddedDocument() 0 15 1
A testEmptyStringInEmbeddedDocument() 12 12 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Doctrine\Tests\ODM\CouchDB\Functional;
4
5
use Doctrine\Common\EventSubscriber;
6
7
class EmbeddedAssociationTest extends \Doctrine\Tests\ODM\CouchDB\CouchDBFunctionalTestCase
8
{
9
    private $userId;
10
    private $groupIds = array();
0 ignored issues
show
Unused Code introduced by
The property $groupIds is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
11
    private $dm;
12
13
    public function setUp()
14
    {
15
        $this->dm = $this->createDocumentManager();
16
17
        $user1 = new \Doctrine\Tests\Models\CMS\CmsUser();
18
        $user1->username = "beberlei";
19
        $user1->status = "active";
20
        $user1->name = "Benjamin";
21
22
        $user2 = new \Doctrine\Tests\Models\CMS\CmsUser();
23
        $user2->username = "lsmith";
24
        $user2->status = "active";
25
        $user2->name = "Lukas";
26
27
        $address1 = new \Doctrine\Tests\Models\CMS\CmsAddress();
28
        $address1->country = "Hungary";
29
        $address1->zip = "1122";
30
        $address1->city = "Budapest";
31
        $address1->mainAddress = true;
32
33
        $user1->setAddress($address1);
34
35
        $this->dm->persist($user1);
36
        $this->dm->persist($user2);
37
38
        $this->dm->flush();
39
        $this->dm->clear();
40
41
        $this->userId = $user1->id;
42
    }
43
44
    public function testShouldNotSaveUnchanged()
45
    {
46
        $listener = new PreUpdateSubscriber;
47
        $this->dm->getEventManager()->addEventListener('preUpdate', $listener);
48
49
        $user = $this->dm->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
50
        $this->assertInstanceOf('\Doctrine\Tests\Models\CMS\CmsAddress', $user->address);
51
        $this->assertEquals('Hungary', $user->address->country);
52
        $this->assertEquals('1122', $user->address->zip);
53
        $this->assertEquals('Budapest', $user->address->city);
54
55
        $this->dm->flush();
56
57
        $this->assertEquals(0, count($listener->eventArgs));
58
    }
59
60
    public function testSaveModifiedEmbedded()
61
    {
62
        $user = $this->dm->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
63
        $this->assertInstanceOf('\Doctrine\Tests\Models\CMS\CmsAddress', $user->address);
64
        $this->assertEquals('Hungary', $user->address->country);
65
        $this->assertEquals('1122', $user->address->zip);
66
        $this->assertEquals('Budapest', $user->address->city);
67
68
        $user->address->country = "Spain";
69
        $user->address->zip = "1234";
70
        $user->address->city = "Cartagena";
71
72
        $this->dm->flush();
73
        $this->dm->clear();
74
75
        $user = $this->dm->find('Doctrine\Tests\Models\CMS\CmsUser', $user->id);
76
        $this->assertEquals('Spain', $user->address->country);
77
        $this->assertEquals('1234', $user->address->zip);
78
        $this->assertEquals('Cartagena', $user->address->city);
79
80
    }
81
82
    public function testSaveEmbedded()
83
    {
84
        $user = $this->dm->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
85
        $this->assertInstanceOf('\Doctrine\Tests\Models\CMS\CmsAddress', $user->address);
86
        $this->assertEquals('Hungary', $user->address->country);
87
        $this->assertEquals('1122', $user->address->zip);
88
        $this->assertEquals('Budapest', $user->address->city);
89
90
        $address3 = new \Doctrine\Tests\Models\CMS\CmsAddress();
91
        $address3->country = "Spain";
92
        $address3->zip = "1234";
93
        $address3->city = "Cartagena";
94
        $user->setAddress($address3);
95
96
        $this->dm->flush();
97
        $this->dm->clear();
98
99
        $user = $this->dm->find('Doctrine\Tests\Models\CMS\CmsUser', $user->id);
100
        $this->assertEquals('Spain', $user->address->country);
101
        $this->assertEquals('1234', $user->address->zip);
102
        $this->assertEquals('Cartagena', $user->address->city);
103
    }
104
105
    /**
106
     * @group GH-80
107
     */
108
    public function testUpdateBooleanToFalseInEmbeddedDocument()
109
    {
110
        $user = $this->dm->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
111
112
        $this->assertTrue($user->address->mainAddress);
113
114
        $user->address->mainAddress = false;
115
116
        $this->dm->flush();
117
        $this->dm->clear();
118
119
        $user = $this->dm->find('Doctrine\Tests\Models\CMS\CmsUser', $user->id);
120
121
        $this->assertFalse($user->address->mainAddress);
122
    }
123
124
    /**
125
     * @group GH-80
126
     */
127 View Code Duplication
    public function testEmptyStringInEmbeddedDocument()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
128
    {
129
        $user = $this->dm->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
130
        $user->address->street = "";
131
132
        $this->dm->flush();
133
        $this->dm->clear();
134
135
        $user = $this->dm->find('Doctrine\Tests\Models\CMS\CmsUser', $user->id);
136
137
        $this->assertEquals("", $user->address->street);
138
    }
139
}
140
141 View Code Duplication
class PreUpdateSubscriber2 implements EventSubscriber
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
142
{
143
    public $eventArgs = array();
144
    public function getSubscribedEvents()
145
    {
146
        return array(\Doctrine\ODM\CouchDB\Event::preUpdate);
147
    }
148
149
    public function preUpdate(\Doctrine\ODM\CouchDB\Event\LifecycleEventArgs $args)
150
    {
151
        $this->eventArgs[] = $args;
152
    }
153
154
}
155