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; |
|
|
|
|
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
|
|
|
|
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..