|
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\AssertionManager; |
|
14
|
|
|
use MyProject\Proxies\__CG__\stdClass; |
|
15
|
|
|
use Zend\EventManager\EventManager; |
|
16
|
|
|
use Zend\EventManager\EventManagerAwareInterface; |
|
17
|
|
|
use Zend\EventManager\SharedEventManager; |
|
18
|
|
|
use Zend\Permissions\Acl\Assertion\AssertionInterface; |
|
19
|
|
|
use Zend\ServiceManager\Exception; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Tests the AssertionManager |
|
23
|
|
|
* |
|
24
|
|
|
* @author Mathias Gelhausen <[email protected]> |
|
25
|
|
|
* @todo write test |
|
26
|
|
|
*/ |
|
27
|
|
|
class AssertionManagerTest extends \PHPUnit_Framework_TestCase |
|
28
|
|
|
{ |
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
public function testExtendsCorrectParent() |
|
32
|
|
|
{ |
|
33
|
|
|
$target = new AssertionManager(); |
|
34
|
|
|
|
|
35
|
|
|
$this->assertInstanceOf('\Zend\ServiceManager\AbstractPluginManager', $target); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function testConstructorAddsInitializer() |
|
39
|
|
|
{ |
|
40
|
|
|
$target = new AssertionManagerMock(); |
|
41
|
|
|
|
|
42
|
|
|
$this->assertTrue($target->wasAddInitializerCalledCorrectly()); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function testInjectEventManagerInitializerCallbackDoesNothingIfAssertionNotEventManagerAware() |
|
46
|
|
|
{ |
|
47
|
|
|
$target = new AssertionManager(); |
|
48
|
|
|
$assertion = $this->getMockForAbstractClass('\Zend\Permissions\Acl\Assertion\AssertionInterface'); |
|
49
|
|
|
$services = $this->getMockBuilder('\Zend\ServiceManager\ServiceManager')->disableOriginalConstructor() |
|
50
|
|
|
->getMock(); |
|
51
|
|
|
$services->expects($this->never())->method('getServiceLocator'); |
|
52
|
|
|
$services->expects($this->never())->method('get'); |
|
53
|
|
|
|
|
54
|
|
|
$this->assertNull($target->injectEventManager($assertion, $services)); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function testInjectEventManagerInitializerCallbackGetsEventManagerFromServicesIfNotSetInAssertion() |
|
58
|
|
|
{ |
|
59
|
|
|
$target = new AssertionManager(); |
|
60
|
|
|
$assertion = $this->getMockForAbstractClass('\AclTest\Assertion\EventManagerAwareAssertionMock'); |
|
61
|
|
|
$services = $this->getMockForAbstractClass('\Zend\ServiceManager\AbstractPluginManager'); |
|
62
|
|
|
$parentServices = $this->getMockBuilder('\Zend\ServiceManager\ServiceManager')->disableOriginalConstructor()->getMock(); |
|
63
|
|
|
$events = new EventManager(); |
|
64
|
|
|
|
|
65
|
|
|
$assertion->expects($this->once())->method('getEventManager')->willReturn(null); |
|
66
|
|
|
$assertion->expects($this->once())->method('setEventManager')->with($events); |
|
67
|
|
|
|
|
68
|
|
|
$parentServices->expects($this->once())->method('get')->with('EventManager')->willReturn($events); |
|
69
|
|
|
/* |
|
70
|
|
|
* Wanted to use: |
|
71
|
|
|
* //$services->expects($this->once())->method('getServiceLocator')->willReturn($parentServices); |
|
72
|
|
|
* but PHPUnit does not allow a concrete method in an abstract class to be mocked. :( |
|
73
|
|
|
* |
|
74
|
|
|
* So I have to do it this way: |
|
75
|
|
|
*/ |
|
76
|
|
|
$services->setServiceLocator($parentServices); |
|
77
|
|
|
|
|
78
|
|
|
$this->assertNull($target->injectEventManager($assertion, $services)); |
|
79
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function testInjectEventManagerInitializerCallbackSetsSharedEventManagerInEventsIfSetInAssertion() |
|
83
|
|
|
{ |
|
84
|
|
|
$target = new AssertionManager(); |
|
85
|
|
|
$assertion = $this->getMockForAbstractClass('\AclTest\Assertion\EventManagerAwareAssertionMock'); |
|
86
|
|
|
$services = $this->getMockForAbstractClass('\Zend\ServiceManager\AbstractPluginManager'); |
|
87
|
|
|
$parentServices = $this->getMockBuilder('\Zend\ServiceManager\ServiceManager')->disableOriginalConstructor()->getMock(); |
|
88
|
|
|
$events = new EventManager(); |
|
89
|
|
|
$sharedEvents = new SharedEventManager(); |
|
90
|
|
|
|
|
91
|
|
|
$services->setServiceLocator($parentServices); |
|
92
|
|
|
|
|
93
|
|
|
$parentServices->expects($this->once())->method('get')->with('SharedEventManager')->willReturn($sharedEvents); |
|
94
|
|
|
$assertion->expects($this->once())->method('getEventManager')->willReturn($events); |
|
95
|
|
|
|
|
96
|
|
|
$this->assertNull($target->injectEventManager($assertion, $services)); |
|
97
|
|
|
$this->assertSame($sharedEvents, $events->getSharedManager()); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @expectedException \RuntimeException |
|
102
|
|
|
* @expectedExceptionMessage Expected plugin to be of type Assertion. |
|
103
|
|
|
*/ |
|
104
|
|
|
public function testValidatePluginThrowsExceptionIfPluginIsInvalid() |
|
105
|
|
|
{ |
|
106
|
|
|
$target = new AssertionManager(); |
|
107
|
|
|
$assertion = $this->getMockForAbstractClass('\Zend\Permissions\Acl\Assertion\AssertionInterface'); |
|
108
|
|
|
|
|
109
|
|
|
$this->assertNull($target->validatePlugin($assertion)); |
|
110
|
|
|
$target->validatePlugin(new \stdClass()); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
class AssertionManagerMock extends AssertionManager |
|
|
|
|
|
|
115
|
|
|
{ |
|
116
|
|
|
private $addInitializerCalledCorrect = false; |
|
117
|
|
|
|
|
118
|
|
|
public function addInitializer($initializer, $topOfStack = true) |
|
119
|
|
|
{ |
|
120
|
|
|
$this->addInitializerCalledCorrect = array($this, 'injectEventManager') === $initializer && false === $topOfStack; |
|
121
|
|
|
return parent::addInitializer($initializer, $topOfStack); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
public function wasAddInitializerCalledCorrectly() |
|
|
|
|
|
|
125
|
|
|
{ |
|
126
|
|
|
return $this->addInitializerCalledCorrect; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
|
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
abstract class EventManagerAwareAssertionMock implements AssertionInterface, EventManagerAwareInterface |
|
|
|
|
|
|
133
|
|
|
{ |
|
134
|
|
|
|
|
135
|
|
|
} |
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.