Completed
Push — master ( a18731...e2e070 )
by
unknown
99:54 queued 47:45
created

B2bCustomerTest   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 278
Duplicated Lines 64.39 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 4
dl 179
loc 278
rs 10
c 0
b 0
f 0

19 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A tearDown() 0 4 1
A testGetSet() 7 7 2
B getSetDataProvider() 0 27 1
A testPrePersist() 9 9 1
A testPreUpdate() 9 9 1
A testLeadsInteraction() 20 20 1
A testOpportunitiesInteraction() 20 20 1
A testToSting() 0 5 1
A testAddDuplicatePhone() 17 17 1
A testAddNewPhone() 0 13 1
A testRemoveExistingPhone() 12 12 1
A testRemoveNonExistingPhone() 13 13 1
A testGetPrimaryPhone() 15 15 1
A testAddDuplicateEmail() 17 17 1
A testAddNewEmail() 0 13 1
A testRemoveExistingEmail() 12 12 1
A testRemoveNonExistingEmail() 13 13 1
A testGetPrimaryEmail() 15 15 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 OroCRM\Bundle\SalesBundle\Tests\Unit\Entity;
4
5
use OroCRM\Bundle\SalesBundle\Entity\B2bCustomer;
6
7
use Doctrine\Common\Collections\ArrayCollection;
8
use Doctrine\Common\Util\ClassUtils;
9
use OroCRM\Bundle\SalesBundle\Entity\B2bCustomerEmail;
10
use OroCRM\Bundle\SalesBundle\Entity\B2bCustomerPhone;
11
12
class B2bCustomerTest extends \PHPUnit_Framework_TestCase
13
{
14
    const TEST_ID = 12;
15
    const TEST_NAME = 'test name';
16
17
    /** @var B2bCustomer */
18
    protected $entity;
19
20
    protected function setUp()
21
    {
22
        $this->entity = new B2bCustomer();
23
    }
24
25
    protected function tearDown()
26
    {
27
        unset($this->entity);
28
    }
29
30
    /**
31
     * @dataProvider getSetDataProvider
32
     */
33 View Code Duplication
    public function testGetSet($property, $value, $expected = null)
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...
34
    {
35
        if (null !== $value) {
36
            call_user_func_array([$this->entity, 'set' . ucfirst($property)], [$value]);
37
        }
38
        $this->assertSame($expected, call_user_func([$this->entity, 'get' . ucfirst($property)]));
39
    }
40
41
    /**
42
     * @return array
43
     */
44
    public function getSetDataProvider()
45
    {
46
        $name         = uniqid('name');
47
        $address      = $this->getMock('Oro\Bundle\AddressBundle\Entity\Address');
48
        $account      = $this->getMock('OroCRM\Bundle\AccountBundle\Entity\Account');
49
        $contact      = $this->getMock('OroCRM\Bundle\ContactBundle\Entity\Contact');
50
        $channel      = $this->getMock('OroCRM\Bundle\ChannelBundle\Entity\Channel');
51
        $owner        = $this->getMock('Oro\Bundle\UserBundle\Entity\User');
52
        $organization = $this->getMock('Oro\Bundle\OrganizationBundle\Entity\Organization');
53
        $date         = new \DateTime();
54
        $lifetime     = 12.22;
55
56
        return [
57
            'id'              => ['id', null, null],
58
            'name'            => ['name', $name, $name],
59
            '$lifetime'       => ['lifetime', $lifetime, $lifetime],
60
            'shippingAddress' => ['shippingAddress', $address, $address],
61
            'billingAddress'  => ['billingAddress', $address, $address],
62
            'account'         => ['account', $account, $account],
63
            'contact'         => ['contact', $contact, $contact],
64
            'dataChannel'     => ['dataChannel', $channel, $channel],
65
            'owner'           => ['owner', $owner, $owner],
66
            'organization'    => ['organization', $organization, $organization],
67
            'createdAt'       => ['createdAt', $date, $date],
68
            'updatedAt'       => ['updatedAt', $date, $date],
69
        ];
70
    }
71
    
72 View Code Duplication
    public function testPrePersist()
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...
73
    {
74
        $this->assertNull($this->entity->getCreatedAt());
75
76
        $this->entity->prePersist();
77
78
        $this->assertInstanceOf('DateTime', $this->entity->getCreatedAt());
79
        $this->assertLessThan(3, $this->entity->getCreatedAt()->diff(new \DateTime())->s);
80
    }
81
82 View Code Duplication
    public function testPreUpdate()
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...
83
    {
84
        $this->assertNull($this->entity->getUpdatedAt());
85
86
        $this->entity->preUpdate();
87
88
        $this->assertInstanceOf('DateTime', $this->entity->getUpdatedAt());
89
        $this->assertLessThan(3, $this->entity->getUpdatedAt()->diff(new \DateTime())->s);
90
    }
91
92 View Code Duplication
    public function testLeadsInteraction()
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...
93
    {
94
        $result = $this->entity->getLeads();
95
        $this->assertInstanceOf('Doctrine\Common\Collections\ArrayCollection', $result);
96
        $this->assertCount(0, $result);
97
98
        $lead = $this->getMock('OroCRM\Bundle\SalesBundle\Entity\Lead');
99
        $this->entity->addLead($lead);
100
        $this->assertCount(1, $this->entity->getLeads());
101
        $this->assertTrue($this->entity->getLeads()->contains($lead));
102
103
        $this->entity->removeLead($lead);
104
        $result = $this->entity->getLeads();
105
        $this->assertCount(0, $result);
106
107
        $newCollection = new ArrayCollection();
108
        $this->entity->setLeads($newCollection);
109
        $this->assertNotSame($result, $this->entity->getLeads());
110
        $this->assertSame($newCollection, $this->entity->getLeads());
111
    }
112
113 View Code Duplication
    public function testOpportunitiesInteraction()
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...
114
    {
115
        $result = $this->entity->getOpportunities();
116
        $this->assertInstanceOf('Doctrine\Common\Collections\ArrayCollection', $result);
117
        $this->assertCount(0, $result);
118
119
        $opportunity = $this->getMock('OroCRM\Bundle\SalesBundle\Entity\Opportunity');
120
        $this->entity->addOpportunity($opportunity);
121
        $this->assertCount(1, $this->entity->getOpportunities());
122
        $this->assertTrue($this->entity->getOpportunities()->contains($opportunity));
123
124
        $this->entity->removeOpportunity($opportunity);
125
        $result = $this->entity->getLeads();
126
        $this->assertCount(0, $result);
127
128
        $newCollection = new ArrayCollection();
129
        $this->entity->setOpportunities($newCollection);
130
        $this->assertNotSame($result, $this->entity->getOpportunities());
131
        $this->assertSame($newCollection, $this->entity->getOpportunities());
132
    }
133
134
    public function testToSting()
135
    {
136
        $this->entity->setName(self::TEST_NAME);
137
        $this->assertSame(self::TEST_NAME, (string)$this->entity);
138
    }
139
140 View Code Duplication
    public function testAddDuplicatePhone()
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...
141
    {
142
        $customer = new B2bCustomer();
143
        $firstPhone = new B2bCustomerPhone('06001122334455');
144
        $secondPhone = new B2bCustomerPhone('07001122334455');
145
        $customerPhones = [$firstPhone, $secondPhone];
146
147
        $customer->resetPhones($customerPhones);
148
149
        $actual = $customer->getPhones();
150
        $this->assertInstanceOf('Doctrine\Common\Collections\ArrayCollection', $actual);
151
        $this->assertEquals($customerPhones, $actual->toArray());
152
        $customer->addPhone($secondPhone);
153
154
        $actual = $customer->getPhones();
155
        $this->assertEquals($customerPhones, $actual->toArray());
156
    }
157
158
    public function testAddNewPhone()
159
    {
160
        $customer       = new B2bCustomer();
161
        $firstPhone     = new B2bCustomerPhone('06001122334455');
162
        $secondPhone    = new B2bCustomerPhone('07001122334455');
163
        $thirdPhone     = new B2bCustomerPhone('08001122334455');
164
        $customerPhones = [$firstPhone, $secondPhone];
165
166
        $customer->resetPhones($customerPhones);
167
        $customer->addPhone($thirdPhone);
168
        $actual = $customer->getPhones();
169
        $this->assertEquals([$firstPhone, $secondPhone, $thirdPhone], $actual->toArray());
170
    }
171
172 View Code Duplication
    public function testRemoveExistingPhone()
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...
173
    {
174
        $customer       = new B2bCustomer();
175
        $firstPhone     = new B2bCustomerPhone('06001122334455');
176
        $secondPhone    = new B2bCustomerPhone('07001122334455');
177
        $customerPhones = [$firstPhone, $secondPhone];
178
179
        $customer->resetPhones($customerPhones);
180
        $customer->removePhone($secondPhone);
181
        $actual = $customer->getPhones();
182
        $this->assertEquals([$firstPhone], $actual->toArray());
183
    }
184
185 View Code Duplication
    public function testRemoveNonExistingPhone()
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...
186
    {
187
        $customer       = new B2bCustomer();
188
        $firstPhone     = new B2bCustomerPhone('06001122334455');
189
        $secondPhone    = new B2bCustomerPhone('07001122334455');
190
        $thirdPhone     = new B2bCustomerPhone('08001122334455');
191
        $customerPhones = [$firstPhone, $secondPhone];
192
193
        $customer->resetPhones($customerPhones);
194
        $customer->removePhone($thirdPhone);
195
        $actual = $customer->getPhones();
196
        $this->assertEquals([$firstPhone, $secondPhone], $actual->toArray());
197
    }
198
199 View Code Duplication
    public function testGetPrimaryPhone()
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...
200
    {
201
        $firstPhone = new B2bCustomerPhone('06001122334455');
202
        $secondPhone = new B2bCustomerPhone('07001122334455');
203
        $customer = new B2bCustomer();
204
        $this->assertNull($customer->getPrimaryPhone());
205
        $customer->addPhone($firstPhone);
206
        $this->assertNull($customer->getPrimaryPhone());
207
        $customer->setPrimaryPhone($firstPhone);
208
        $this->assertSame($firstPhone, $customer->getPrimaryPhone());
209
        $customer->addPhone($secondPhone);
210
        $customer->setPrimaryPhone($secondPhone);
211
        $this->assertSame($secondPhone, $customer->getPrimaryPhone());
212
        $this->assertFalse($firstPhone->isPrimary());
213
    }
214
215 View Code Duplication
    public function testAddDuplicateEmail()
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...
216
    {
217
        $customer = new B2bCustomer();
218
        $firstEmail = new B2bCustomerEmail('[email protected]');
219
        $secondEmail = new B2bCustomerEmail('[email protected]');
220
        $customerEmails = [$firstEmail, $secondEmail];
221
222
        $customer->resetEmails($customerEmails);
223
224
        $actual = $customer->getEmails();
225
        $this->assertInstanceOf('Doctrine\Common\Collections\ArrayCollection', $actual);
226
        $this->assertEquals($customerEmails, $actual->toArray());
227
        $customer->addEmail($secondEmail);
228
229
        $actual = $customer->getEmails();
230
        $this->assertEquals($customerEmails, $actual->toArray());
231
    }
232
233
    public function testAddNewEmail()
234
    {
235
        $customer       = new B2bCustomer();
236
        $firstEmail = new B2bCustomerEmail('[email protected]');
237
        $secondEmail = new B2bCustomerEmail('[email protected]');
238
        $thirdEmail     = new B2bCustomerEmail('[email protected]');
239
        $customerEmails = [$firstEmail, $secondEmail];
240
241
        $customer->resetEmails($customerEmails);
242
        $customer->addEmail($thirdEmail);
243
        $actual = $customer->getEmails();
244
        $this->assertEquals([$firstEmail, $secondEmail, $thirdEmail], $actual->toArray());
245
    }
246
247 View Code Duplication
    public function testRemoveExistingEmail()
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...
248
    {
249
        $customer       = new B2bCustomer();
250
        $firstEmail     = new B2bCustomerEmail('[email protected]');
251
        $secondEmail    = new B2bCustomerEmail('[email protected]');
252
        $customerEmails = [$firstEmail, $secondEmail];
253
254
        $customer->resetEmails($customerEmails);
255
        $customer->removeEmail($secondEmail);
256
        $actual = $customer->getEmails();
257
        $this->assertEquals([$firstEmail], $actual->toArray());
258
    }
259
260 View Code Duplication
    public function testRemoveNonExistingEmail()
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...
261
    {
262
        $customer       = new B2bCustomer();
263
        $firstEmail     = new B2bCustomerEmail('[email protected]');
264
        $secondEmail    = new B2bCustomerEmail('[email protected]');
265
        $thirdEmail     = new B2bCustomerEmail('[email protected]');
266
        $customerEmails = [$firstEmail, $secondEmail];
267
268
        $customer->resetEmails($customerEmails);
269
        $customer->removeEmail($thirdEmail);
270
        $actual = $customer->getEmails();
271
        $this->assertEquals([$firstEmail, $secondEmail], $actual->toArray());
272
    }
273
274 View Code Duplication
    public function testGetPrimaryEmail()
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...
275
    {
276
        $customer = new B2bCustomer();
277
        $this->assertNull($customer->getPrimaryEmail());
278
        $email = new B2bCustomerEmail('[email protected]');
279
        $customer->addEmail($email);
280
        $this->assertNull($customer->getPrimaryEmail());
281
        $customer->setPrimaryEmail($email);
282
        $this->assertSame($email, $customer->getPrimaryEmail());
283
        $email2 = new B2bCustomerEmail('[email protected]');
284
        $customer->addEmail($email2);
285
        $customer->setPrimaryEmail($email2);
286
        $this->assertSame($email2, $customer->getPrimaryEmail());
287
        $this->assertFalse($email->isPrimary());
288
    }
289
}
290