Completed
Push — master ( 935ca4...f651ab )
by
unknown
08:26
created

AccountTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 109
Duplicated Lines 36.7 %

Coupling/Cohesion

Components 0
Dependencies 5
Metric Value
wmc 8
lcom 0
cbo 5
dl 40
loc 109
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A testGettersSetters() 0 14 1
A testBeforeSave() 0 6 1
A testDoPreUpdate() 0 6 1
A testAddContact() 15 15 1
A testRemoveContact() 14 14 1
A testOwners() 11 11 1
A testGetEmail() 0 15 1
A testSetDefaultContact() 0 18 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\AccountBundle\Tests\Unit\Entity;
4
5
use OroCRM\Bundle\AccountBundle\Entity\Account;
6
use OroCRM\Bundle\ContactBundle\Entity\Contact;
7
8
use Oro\Bundle\UserBundle\Entity\User;
9
use Oro\Bundle\OrganizationBundle\Entity\Organization;
10
11
class AccountTest extends \PHPUnit_Framework_TestCase
12
{
13
    public function testGettersSetters()
14
    {
15
        $entity = new Account();
16
        $entity->setName('Test');
17
18
        $this->assertEquals('Test', $entity->getName());
19
        $this->assertEquals('Test', (string)$entity);
20
21
        $organization = new Organization();
22
        $this->assertNull($entity->getOrganization());
23
        $entity->setOrganization($organization);
24
        $this->assertSame($organization, $entity->getOrganization());
25
26
    }
27
28
    public function testBeforeSave()
29
    {
30
        $entity = new Account();
31
        $entity->beforeSave();
32
        $this->assertInstanceOf('\DateTime', $entity->getCreatedAt());
33
    }
34
35
    public function testDoPreUpdate()
36
    {
37
        $entity = new Account();
38
        $entity->doPreUpdate();
39
        $this->assertInstanceOf('\DateTime', $entity->getUpdatedAt());
40
    }
41
42 View Code Duplication
    public function testAddContact()
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...
43
    {
44
        $account = new Account();
45
        $account->setId(1);
46
47
        $contact = new Contact();
48
        $contact->setId(2);
49
50
        $this->assertEmpty($account->getContacts()->toArray());
51
52
        $account->addContact($contact);
53
        $actualContacts = $account->getContacts()->toArray();
54
        $this->assertCount(1, $actualContacts);
55
        $this->assertEquals($contact, current($actualContacts));
56
    }
57
58 View Code Duplication
    public function testRemoveContact()
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...
59
    {
60
        $account = new Account();
61
        $account->setId(1);
62
63
        $contact = new Contact();
64
        $contact->setId(2);
65
66
        $account->addContact($contact);
67
        $this->assertCount(1, $account->getContacts()->toArray());
68
69
        $account->removeContact($contact);
70
        $this->assertEmpty($account->getContacts()->toArray());
71
    }
72
73 View Code Duplication
    public function testOwners()
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...
74
    {
75
        $entity = new Account();
76
        $user = new User();
77
78
        $this->assertEmpty($entity->getOwner());
79
80
        $entity->setOwner($user);
81
82
        $this->assertEquals($user, $entity->getOwner());
83
    }
84
85
    public function testGetEmail()
86
    {
87
        $account = new Account();
88
        $contact = $this->getMockBuilder('OroCRM\Bundle\ContactBundle\Entity\Contact')
89
            ->disableOriginalConstructor()
90
            ->getMock();
91
92
        $this->assertNull($account->getEmail());
93
94
        $account->setDefaultContact($contact);
95
        $contact->expects($this->once())
96
            ->method('getEmail')
97
            ->will($this->returnValue('[email protected]'));
98
        $this->assertEquals('[email protected]', $account->getEmail());
99
    }
100
101
    public function testSetDefaultContact()
102
    {
103
        $account = new Account();
104
        $this->assertNull($account->getDefaultContact());
105
106
        $contact = new Contact();
107
        $account->setDefaultContact($contact);
108
        $this->assertSame($contact, $account->getDefaultContact());
109
        $this->assertCount(1, $contact->getDefaultInAccounts());
110
        $this->assertSame($account, $contact->getDefaultInAccounts()->first());
111
112
        $contact2 = new Contact();
113
        $account->setDefaultContact($contact2);
114
        $this->assertCount(0, $contact->getDefaultInAccounts());
115
        $this->assertCount(1, $contact2->getDefaultInAccounts());
116
        $this->assertSame($contact2, $account->getDefaultContact());
117
        $this->assertSame($account, $contact2->getDefaultInAccounts()->first());
118
    }
119
}
120