Completed
Push — master ( efde7d...b6dd08 )
by Alexis
07:32
created

UserTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 98
Duplicated Lines 18.37 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 18
loc 98
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A testUsername() 0 7 1
A testEmail() 0 7 1
A testPassword() 0 7 1
A testPlainPassword() 0 7 1
A testSalt() 9 9 1
A testEnabled() 0 7 1
A testConfirmationToken() 9 9 1
A testRoles() 0 7 1
A testHasRole() 0 17 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 AWurth\SilexUser\Tests\Entity;
4
5
use AWurth\SilexUser\Entity\User;
6
use PHPUnit\Framework\TestCase;
7
8
class UserTest extends TestCase
9
{
10
    /**
11
     * @var User
12
     */
13
    protected $user;
14
15
    public function setUp()
16
    {
17
        $this->user = $this->getMockForAbstractClass(User::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMockForAbstrac...ser\Entity\User::class) of type object<PHPUnit_Framework_MockObject_MockObject> is incompatible with the declared type object<AWurth\SilexUser\Entity\User> of property $user.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
18
    }
19
    
20
    public function testUsername()
21
    {
22
        $this->assertNull($this->user->getUsername());
23
24
        $this->user->setUsername('awurth');
25
        $this->assertSame('awurth', $this->user->getUsername());
26
    }
27
28
    public function testEmail()
29
    {
30
        $this->assertNull($this->user->getEmail());
31
32
        $this->user->setEmail('[email protected]');
33
        $this->assertSame('[email protected]', $this->user->getEmail());
34
    }
35
36
    public function testPassword()
37
    {
38
        $this->assertNull($this->user->getPassword());
39
40
        $this->user->setPassword('my_password');
41
        $this->assertSame('my_password', $this->user->getPassword());
42
    }
43
44
    public function testPlainPassword()
45
    {
46
        $this->assertNull($this->user->getPlainPassword());
47
48
        $this->user->setPlainPassword('my_plain_password');
49
        $this->assertSame('my_plain_password', $this->user->getPlainPassword());
50
    }
51
52 View Code Duplication
    public function testSalt()
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...
53
    {
54
        $this->assertNull($this->user->getSalt());
55
56
        $salt = rtrim(str_replace('+', '.', base64_encode(random_bytes(32))), '=');
57
58
        $this->user->setSalt($salt);
59
        $this->assertSame($salt, $this->user->getSalt());
60
    }
61
62
    public function testEnabled()
63
    {
64
        $this->assertFalse($this->user->isEnabled());
65
66
        $this->user->setEnabled(true);
67
        $this->assertTrue($this->user->isEnabled());
68
    }
69
70 View Code Duplication
    public function testConfirmationToken()
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...
71
    {
72
        $this->assertNull($this->user->getConfirmationToken());
73
74
        $token = rtrim(strtr(base64_encode(random_bytes(32)), '+/', '-_'), '=');
75
76
        $this->user->setConfirmationToken($token);
77
        $this->assertSame($token, $this->user->getConfirmationToken());
78
    }
79
80
    public function testRoles()
81
    {
82
        $this->assertSame([User::ROLE_DEFAULT], $this->user->getRoles());
83
84
        $this->user->setRoles(['ROLE_ADMIN', 'ROLE_SUPER_ADMIN']);
85
        $this->assertSame(['ROLE_ADMIN', 'ROLE_SUPER_ADMIN', User::ROLE_DEFAULT], $this->user->getRoles());
86
    }
87
    
88
    public function testHasRole()
89
    {
90
        $defaultRole = User::ROLE_DEFAULT;
91
        $newRole = 'ROLE_ADMIN';
92
        $this->assertTrue($this->user->hasRole($defaultRole));
93
        $this->assertFalse($this->user->hasRole($newRole));
94
95
        $this->user->addRole($defaultRole);
96
        $this->assertTrue($this->user->hasRole($defaultRole));
97
        $this->user->addRole($newRole);
98
        $this->assertTrue($this->user->hasRole($newRole));
99
100
        $this->user->removeRole($defaultRole);
101
        $this->assertTrue($this->user->hasRole($defaultRole));
102
        $this->user->removeRole($newRole);
103
        $this->assertFalse($this->user->hasRole($newRole));
104
    }
105
}
106