|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* YAWIK |
|
4
|
|
|
* |
|
5
|
|
|
* @filesource |
|
6
|
|
|
* @license MIT |
|
7
|
|
|
* @copyright 2013 - 2015 Cross Solution <http://cross-solution.de> |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
/** */ |
|
11
|
|
|
namespace AclTest\Assertion; |
|
12
|
|
|
|
|
13
|
|
|
use Acl\Assertion\AbstractEventManagerAwareAssertion; |
|
14
|
|
|
use Zend\EventManager\EventManager; |
|
15
|
|
|
use Zend\EventManager\ResponseCollection; |
|
16
|
|
|
use Zend\Permissions\Acl\Acl; |
|
17
|
|
|
use Zend\Permissions\Acl\Resource\GenericResource; |
|
18
|
|
|
use Zend\Permissions\Acl\Resource\ResourceInterface; |
|
19
|
|
|
use Zend\Permissions\Acl\Role\GenericRole; |
|
20
|
|
|
use Zend\Permissions\Acl\Role\RoleInterface; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Tests the AbstractEventManagerAwareAssertion |
|
24
|
|
|
* |
|
25
|
|
|
* @covers \Acl\Assertion\AbstractEventManagerAwareAssertion |
|
26
|
|
|
* @author Mathias Gelhausen <[email protected]> |
|
27
|
|
|
* @group Acl |
|
28
|
|
|
* @group Acl.Assertion |
|
29
|
|
|
*/ |
|
30
|
|
|
class AbstractEventManagerAwareAssertionTest extends \PHPUnit_Framework_TestCase |
|
31
|
|
|
{ |
|
32
|
|
|
|
|
33
|
|
|
public function testImplementsInterfaces() |
|
34
|
|
|
{ |
|
35
|
|
|
$target = new TargetMock(); |
|
36
|
|
|
|
|
37
|
|
|
$this->assertInstanceOf('\Zend\Permissions\Acl\Assertion\AssertionInterface', $target); |
|
38
|
|
|
$this->assertInstanceOf('\Zend\EventManager\EventManagerAwareInterface', $target); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function testGetEventManagerReturnsNewOrInjectedInstance() |
|
42
|
|
|
{ |
|
43
|
|
|
$target = new TargetMock(); |
|
44
|
|
|
$events = new EventManager(); |
|
45
|
|
|
|
|
46
|
|
|
$this->assertInstanceOf('\Zend\EventManager\EventManager', $target->getEventManager()); |
|
47
|
|
|
|
|
48
|
|
|
$target->setEventManager($events); |
|
49
|
|
|
|
|
50
|
|
|
$this->assertSame($events, $target->getEventManager()); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @dataProvider provideEventManagerIdentifiers |
|
55
|
|
|
* |
|
56
|
|
|
* @param $ids |
|
57
|
|
|
*/ |
|
58
|
|
|
public function testIdentifiersAreSetOnInjectedEventManager($ids) |
|
59
|
|
|
{ |
|
60
|
|
|
$target = new TargetMock(); |
|
61
|
|
|
$events = new EventManager(); |
|
62
|
|
|
$expected = array('Acl\Assertion', 'Acl\Assertion\AbstractEventManagerAwareAssertion', get_class($target), 'Acl/Assertion'); |
|
63
|
|
|
|
|
64
|
|
|
if (null !== $ids) { |
|
65
|
|
|
$target->setEventManagerIdentifiers($ids); |
|
66
|
|
|
$expected = $ids + $expected; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$target->setEventManager($events); |
|
70
|
|
|
|
|
71
|
|
|
$this->assertEquals($expected, $events->getIdentifiers()); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function provideEventManagerIdentifiers() |
|
75
|
|
|
{ |
|
76
|
|
|
return array( |
|
77
|
|
|
array(null), |
|
78
|
|
|
array(array('test')) |
|
79
|
|
|
); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function testAssertCallsPreAssertAndTriggersEvent() |
|
83
|
|
|
{ |
|
84
|
|
|
$target = new TargetMock(); |
|
85
|
|
|
$acl = new Acl(); |
|
86
|
|
|
|
|
87
|
|
|
$events = $this->getMockBuilder('\Zend\EventManager\EventManager') |
|
88
|
|
|
->disableOriginalConstructor() |
|
89
|
|
|
->getMock(); |
|
90
|
|
|
|
|
91
|
|
|
$events->expects($this->once()) |
|
92
|
|
|
->method('trigger') |
|
93
|
|
|
->willReturn(new ResponseCollection()); |
|
94
|
|
|
|
|
95
|
|
|
$target->setEventManager($events); |
|
96
|
|
|
|
|
97
|
|
|
$this->assertTrue($target->assert($acl)); |
|
98
|
|
|
$target->setPreAssertWillPass(); |
|
99
|
|
|
$this->assertTrue($target->assert($acl)); |
|
100
|
|
|
$target->setPreAssertWillFail(); |
|
101
|
|
|
$this->assertFalse($target->assert($acl)); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function testAssertReturnsExpectedResult() |
|
105
|
|
|
{ |
|
106
|
|
|
$target = new TargetMock(); |
|
107
|
|
|
|
|
108
|
|
|
$responseNull = $this->createResponseMock(null); |
|
109
|
|
|
$responseEmpty = $this->createResponseMock(''); |
|
110
|
|
|
$responseZero = $this->createResponseMock(0); |
|
111
|
|
|
$responseTrue = $this->createResponseMock(true); |
|
112
|
|
|
$responseFalse = $this->createResponseMock(false); |
|
113
|
|
|
|
|
114
|
|
|
$events = $this->getMockBuilder('\Zend\EventManager\EventManager') |
|
115
|
|
|
->disableOriginalConstructor() |
|
116
|
|
|
->getMock(); |
|
117
|
|
|
|
|
118
|
|
|
$events->expects($this->exactly(5)) |
|
119
|
|
|
->method('trigger') |
|
120
|
|
|
->will($this->onConsecutiveCalls($responseNull, $responseEmpty, $responseZero, $responseTrue, $responseFalse)); |
|
121
|
|
|
|
|
122
|
|
|
$target->setEventManager($events); |
|
123
|
|
|
|
|
124
|
|
|
$acl = new Acl(); |
|
125
|
|
|
|
|
126
|
|
|
$this->assertTrue($target->assert($acl), 'responseNull must return TRUE'); // responseNull |
|
127
|
|
|
$this->assertTrue($target->assert($acl), 'responseEmpty must return TRUE'); // responseEmpty |
|
128
|
|
|
$this->assertTrue($target->assert($acl), 'responseZero must return TRUE'); // responseZero |
|
129
|
|
|
$this->assertTrue($target->assert($acl), 'responseTrue must return TRUE'); // responseTrue |
|
130
|
|
|
$this->assertFalse($target->assert($acl), 'responseFalse must return FALSE'); // responseFalse |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
protected function createResponseMock($returnValue) |
|
134
|
|
|
{ |
|
135
|
|
|
$response = $this->getMockBuilder('\Zend\EventManager\ResponseCollection') |
|
136
|
|
|
->disableOriginalConstructor() |
|
137
|
|
|
->getMock(); |
|
138
|
|
|
$response->method('last')->willReturn($returnValue); |
|
139
|
|
|
|
|
140
|
|
|
return $response; |
|
141
|
|
|
|
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
public function testPassingArgumentsToEvent() |
|
145
|
|
|
{ |
|
146
|
|
|
$target = new TargetMock(); |
|
147
|
|
|
|
|
148
|
|
|
$events = $this->getMockBuilder('\Zend\EventManager\EventManager') |
|
149
|
|
|
->disableOriginalConstructor() |
|
150
|
|
|
->getMock(); |
|
151
|
|
|
|
|
152
|
|
|
$acl = new Acl(); |
|
153
|
|
|
$role = new GenericRole('testRole'); |
|
154
|
|
|
$resource = new GenericResource('testResource'); |
|
155
|
|
|
$privilege = "doTest"; |
|
156
|
|
|
$self = $this; |
|
157
|
|
|
|
|
158
|
|
|
$events->expects($this->once())->method('trigger') |
|
159
|
|
|
->will($this->returnCallback(function($event) use ($acl, $role, $resource, $privilege, $self) { |
|
160
|
|
|
$self->assertSame($acl, $event->getAcl()); |
|
161
|
|
|
$self->assertSame($role, $event->getRole()); |
|
162
|
|
|
$self->assertSame($resource, $event->getResource()); |
|
163
|
|
|
$self->assertSame($privilege, $event->getPrivilege()); |
|
164
|
|
|
|
|
165
|
|
|
return new ResponseCollection(); |
|
166
|
|
|
})); |
|
167
|
|
|
|
|
168
|
|
|
$target->setEventManager($events); |
|
169
|
|
|
$target->assert($acl, $role, $resource, $privilege); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
class TargetMock extends AbstractEventManagerAwareAssertion |
|
|
|
|
|
|
175
|
|
|
{ |
|
176
|
|
|
private $preAssertCalled = false; |
|
177
|
|
|
private $preAssertReturns = null; |
|
178
|
|
|
|
|
179
|
|
|
public function setEventManagerIdentifiers(array $identifiers) |
|
180
|
|
|
{ |
|
181
|
|
|
$this->identifiers = $identifiers; |
|
182
|
|
|
|
|
183
|
|
|
return $this; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
protected function preAssert(Acl $acl, RoleInterface $role = null, ResourceInterface $resource = null, $privilege = null) |
|
|
|
|
|
|
187
|
|
|
{ |
|
188
|
|
|
$this->preAssertCalled = true; |
|
189
|
|
|
parent::preAssert($acl, $role, $resource, $privilege); // for code coverage report. |
|
190
|
|
|
|
|
191
|
|
|
return $this->preAssertReturns; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
public function wasPreAssertCalled() |
|
|
|
|
|
|
195
|
|
|
{ |
|
196
|
|
|
return $this->preAssertCalled; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
public function setPreAssertWillPass() |
|
200
|
|
|
{ |
|
201
|
|
|
$this->preAssertReturns = true; |
|
202
|
|
|
|
|
203
|
|
|
return $this; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
public function setPreAssertWillFail() |
|
207
|
|
|
{ |
|
208
|
|
|
$this->preAssertReturns = false; |
|
209
|
|
|
|
|
210
|
|
|
return $this; |
|
211
|
|
|
} |
|
212
|
|
|
} |
Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.