RoleTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 76
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A testSetterGetter() 0 9 1
A setterGetterProvider() 0 6 1
A testHasChildrenReturnsFalseWhenNoChildren() 0 4 1
A testHasChildrenReturnsTrueWhenHasChildren() 0 5 1
A testAddPermissionToRole() 0 13 1
A testHasPermission() 0 6 1
1
<?php
2
3
namespace JhUserTest\Entity;
4
5
use JhUser\Entity\HierarchicalRole;
6
use JhUser\Entity\Permission;
7
8
/**
9
 * Class RoleTest
10
 * @package JhUserTest\Entity
11
 * @author Aydin Hassan <[email protected]>
12
 */
13
class RoleTest extends \PHPUnit_Framework_TestCase
14
{
15
16
    /**
17
     * @var Role
18
     */
19
    protected $role;
20
21
    /**
22
     * SetUp
23
     */
24
    public function setUp()
25
    {
26
        $this->role = new HierarchicalRole;
0 ignored issues
show
Documentation Bug introduced by
It seems like new \JhUser\Entity\HierarchicalRole() of type object<JhUser\Entity\HierarchicalRole> is incompatible with the declared type object<JhUserTest\Entity\Role> of property $role.

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...
27
    }
28
29
    /**
30
     * Test the setter/getters
31
     *
32
     * @param        string $name
33
     * @param        mixed  $value
34
     *
35
     * @dataProvider setterGetterProvider
36
     */
37
    public function testSetterGetter($name, $value)
38
    {
39
        $getMethod = 'get' . ucfirst($name);
40
        $setMethod = 'set' . ucfirst($name);
41
42
        $this->assertNull($this->role->$getMethod());
43
        $this->role->$setMethod($value);
44
        $this->assertSame($value, $this->role->$getMethod());
45
    }
46
47
    /**
48
     * @return array
49
     */
50
    public function setterGetterProvider()
51
    {
52
        return [
53
            ['name', 'admin'],
54
        ];
55
    }
56
57
    public function testHasChildrenReturnsFalseWhenNoChildren()
58
    {
59
        $this->assertFalse($this->role->hasChildren());
60
    }
61
62
    public function testHasChildrenReturnsTrueWhenHasChildren()
63
    {
64
        $this->role->addChild(new HierarchicalRole());
65
        $this->assertTrue($this->role->hasChildren());
66
    }
67
68
    public function testAddPermissionToRole()
69
    {
70
        $permission = new Permission("test");
71
72
        $refObject   = new \ReflectionObject($this->role);
73
        $refProperty = $refObject->getProperty('permissions');
74
        $refProperty->setAccessible(true);
75
76
        $this->assertEmpty($refProperty->getValue($this->role));
77
        $this->role->addPermission($permission);
78
        $this->assertCount(1, $refProperty->getValue($this->role));
79
        $this->assertSame($permission, $refProperty->getValue($this->role)[ (string) $permission]);
80
    }
81
82
    public function testHasPermission()
83
    {
84
        $this->assertFalse($this->role->hasPermission('not-here'));
85
        $this->role->addPermission(new Permission('delete'));
86
        $this->assertTrue($this->role->hasPermission('delete'));
87
    }
88
}
89