1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace KochTest\Permissions; |
4
|
|
|
|
5
|
|
|
use Koch\Permissions\Acl; |
|
|
|
|
6
|
|
|
|
7
|
|
|
class ACL extends \PHPUnit_Framework_TestCase |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var Acl |
11
|
|
|
*/ |
12
|
|
|
protected $acl; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Sets up the fixture, for example, opens a network connection. |
16
|
|
|
* This method is called before a test is executed. |
17
|
|
|
*/ |
18
|
|
|
protected function setUp() |
19
|
|
|
{ |
20
|
|
|
$this->acl = new self(); |
|
|
|
|
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Tears down the fixture, for example, closes a network connection. |
25
|
|
|
* This method is called after a test is executed. |
26
|
|
|
*/ |
27
|
|
|
protected function tearDown() |
28
|
|
|
{ |
29
|
|
|
unset($this->acl); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @covers Koch\Permissions\Acl::addRole |
34
|
|
|
*/ |
35
|
|
|
public function testAddRole() |
36
|
|
|
{ |
37
|
|
|
$this->acl->addRole('Hausbewohner'); |
|
|
|
|
38
|
|
|
$this->acl->addRole('Hausbewohner', 'Mieter1'); |
39
|
|
|
$this->acl->addRole('Hausbewohner', 'Mieter2'); |
40
|
|
|
$this->acl->addRole('Hausverwalter'); |
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @covers Koch\Permissions\Acl::addResource |
45
|
|
|
*/ |
46
|
|
|
public function testAddResource() |
47
|
|
|
{ |
48
|
|
|
$this->acl->addResource('Haus'); |
|
|
|
|
49
|
|
|
$this->acl->addResource('Haus', 'Wohnung1'); |
50
|
|
|
$this->acl->addResource('Haus', 'Wohnung2'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @covers Koch\Permissions\Acl::addRuleAllow |
55
|
|
|
*/ |
56
|
|
|
public function testRuleAllow() |
57
|
|
|
{ |
58
|
|
|
$this->acl->ruleAllow('Hausverwalter', 'view', 'Haus'); |
59
|
|
|
$this->acl->ruleAllow('Mieter1', 'view', 'Wohnung1'); |
60
|
|
|
$this->acl->ruleAllow('Mieter2', 'view', 'Wohnung2'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @covers Koch\Permissions\Acl::addRuleDeny |
65
|
|
|
*/ |
66
|
|
|
public function testRuleDeny() |
67
|
|
|
{ |
68
|
|
|
$this->acl->ruleDeny('Mieter1', 'view', 'Wohnung2'); |
69
|
|
|
$this->acl->ruleDeny('Mieter2', 'view', 'Wohnung1'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @covers Koch\Permissions\Acl::isAllowed |
74
|
|
|
*/ |
75
|
|
|
public function testIsAllowed() |
76
|
|
|
{ |
77
|
|
|
// the shorthand in the user object is $user->isAllowed($action, $resource); |
78
|
|
|
// the Role is incomming via the user object (user_id -> roles table) |
79
|
|
|
// action and resource are identified by the router and exist in the TargetRoute object |
80
|
|
|
$this->acl->isAllowed($role, $action, $resource); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: