|
13 | 13 | use Zend\Form\Element\Text; |
14 | 14 | use Zend\Stdlib\Hydrator\ObjectProperty; |
15 | 15 | use Zend\Form\FormInterface; |
| 16 | +use Zend\EventManager\EventManager; |
16 | 17 |
|
17 | 18 | class ProfileServiceTest extends \PHPUnit_Framework_TestCase |
18 | 19 | { |
@@ -40,6 +41,19 @@ public function testRegisterExtension() |
40 | 41 | return $ext; |
41 | 42 | } |
42 | 43 |
|
| 44 | + public function testRegisterExtensionFiresEvents() |
| 45 | + { |
| 46 | + $mockEventManager = new TriggerCountingEventManager(); |
| 47 | + $this->service->setEventManager($mockEventManager); |
| 48 | + |
| 49 | + $this->testRegisterExtension(); |
| 50 | + |
| 51 | + $this->assertEquals(array( |
| 52 | + 'LdcUserProfile\Service\ProfileService::registerExtension.pre' => 1, |
| 53 | + 'LdcUserProfile\Service\ProfileService::registerExtension.post' => 1, |
| 54 | + ), $mockEventManager->triggeredEventCount); |
| 55 | + } |
| 56 | + |
43 | 57 | public function testRegisterExtensionRejectsInvalidExtension() |
44 | 58 | { |
45 | 59 | $this->setExpectedException('PHPUnit_Framework_Error'); |
@@ -68,6 +82,21 @@ public function testUnregisterExtensionByName() |
68 | 82 | $this->assertArrayNotHasKey('testext', $this->service->getExtensions()); |
69 | 83 | } |
70 | 84 |
|
| 85 | + public function testUnregisterExtensionFiresEvents() |
| 86 | + { |
| 87 | + $ext = $this->testRegisterExtension(); |
| 88 | + |
| 89 | + $mockEventManager = new TriggerCountingEventManager(); |
| 90 | + $this->service->setEventManager($mockEventManager); |
| 91 | + |
| 92 | + $this->service->unregisterExtension($ext->getName()); |
| 93 | + |
| 94 | + $this->assertEquals(array( |
| 95 | + 'LdcUserProfile\Service\ProfileService::unregisterExtension.pre' => 1, |
| 96 | + 'LdcUserProfile\Service\ProfileService::unregisterExtension.post' => 1, |
| 97 | + ), $mockEventManager->triggeredEventCount); |
| 98 | + } |
| 99 | + |
71 | 100 | public function testHasExtensionByName() |
72 | 101 | { |
73 | 102 | $ext = $this->testRegisterExtension(); |
@@ -102,6 +131,20 @@ public function testSaveCallsSaveOnEachRegsiteredExtensionAndReturnsFalseWhenAnE |
102 | 131 | $this->assertFalse($this->service->save($payload)); |
103 | 132 | } |
104 | 133 |
|
| 134 | + public function testSaveFiresEvents() |
| 135 | + { |
| 136 | + $mockEventManager = new TriggerCountingEventManager(); |
| 137 | + $mockEventManager->matchingRegex = '{^LdcUserProfile\\\\Service\\\\ProfileService::save}is'; |
| 138 | + $this->service->setEventManager($mockEventManager); |
| 139 | + |
| 140 | + $this->testSaveCallsSaveOnEachRegsiteredExtension(); |
| 141 | + |
| 142 | + $this->assertEquals(array( |
| 143 | + 'LdcUserProfile\Service\ProfileService::save.pre' => 1, |
| 144 | + 'LdcUserProfile\Service\ProfileService::save.post' => 1, |
| 145 | + ), $mockEventManager->triggeredEventCount); |
| 146 | + } |
| 147 | + |
105 | 148 | public function testConstructFormForUser() |
106 | 149 | { |
107 | 150 | $mockUserData = new \stdClass(); |
@@ -135,6 +178,21 @@ public function testConstructFormForUser() |
135 | 178 | $this->assertTrue($form->getInputFilter()->get('testext')->has('test')); |
136 | 179 | } |
137 | 180 |
|
| 181 | + public function testConstructFormForUserFiresEvents() |
| 182 | + { |
| 183 | + $mockEventManager = new TriggerCountingEventManager(); |
| 184 | + $mockEventManager->matchingRegex = '{^LdcUserProfile\\\\Service\\\\ProfileService::constructFormForUser}is'; |
| 185 | + $this->service->setEventManager($mockEventManager); |
| 186 | + |
| 187 | + $this->testConstructFormForUser(); |
| 188 | + |
| 189 | + $this->assertEquals(array( |
| 190 | + 'LdcUserProfile\Service\ProfileService::constructFormForUser.pre' => 1, |
| 191 | + 'LdcUserProfile\Service\ProfileService::constructFormForUser.extension' => 1, |
| 192 | + 'LdcUserProfile\Service\ProfileService::constructFormForUser.post' => 1, |
| 193 | + ), $mockEventManager->triggeredEventCount); |
| 194 | + } |
| 195 | + |
138 | 196 | public function testConstructFormForUserObeysValidationGroupOverrides() |
139 | 197 | { |
140 | 198 | $mockUserData = new \stdClass(); |
@@ -257,4 +315,66 @@ public function testConstructFormForUserCanBeUsedMultipleTimesPerRequest() |
257 | 315 | $this->assertTrue($formTwo->getInputFilter()->get('testext')->has('test')); |
258 | 316 | } |
259 | 317 |
|
| 318 | + public function testGetSetEventManager() |
| 319 | + { |
| 320 | + $mock = \Mockery::mock('Zend\EventManager\EventManagerInterface'); |
| 321 | + $mock->shouldReceive('setIdentifiers')->withArgs(array(array( |
| 322 | + 'LdcUserProfile\\Service\\ProfileService', |
| 323 | + 'LdcUserProfile\\Service\\ProfileService', |
| 324 | + )))->andReturnNull(); |
| 325 | + |
| 326 | + $this->service->setEventManager($mock); |
| 327 | + $this->assertSame($mock, $this->service->getEventManager()); |
| 328 | + } |
| 329 | + |
| 330 | + public function testGetSetEventManagerAcceptsIdentifierFromInternalProperty() |
| 331 | + { |
| 332 | + $mock = \Mockery::mock('Zend\EventManager\EventManagerInterface'); |
| 333 | + $mock->shouldReceive('setIdentifiers')->withArgs(array(array( |
| 334 | + 'LdcUserProfile\\Service\\ProfileService', |
| 335 | + 'LdcUserProfileTest\\Service\\ProfileServiceWithExtraEventManagerIdentifier', |
| 336 | + 'someOtherIdentifier' |
| 337 | + )))->andReturnNull(); |
| 338 | + |
| 339 | + $service = new ProfileServiceWithExtraEventManagerIdentifier(); |
| 340 | + $service->eventIdentifier = array('someOtherIdentifier'); |
| 341 | + $service->setEventManager($mock); |
| 342 | + } |
| 343 | + |
| 344 | + public function testGetSetEventManagerAcceptsObjectIdentifierFromInternalProperty() |
| 345 | + { |
| 346 | + $mock = \Mockery::mock('Zend\EventManager\EventManagerInterface'); |
| 347 | + $mock->shouldReceive('setIdentifiers')->withArgs(array(array( |
| 348 | + 'LdcUserProfile\\Service\\ProfileService', |
| 349 | + 'LdcUserProfileTest\\Service\\ProfileServiceWithExtraEventManagerIdentifier', |
| 350 | + new \stdClass(), |
| 351 | + )))->andReturnNull(); |
| 352 | + |
| 353 | + $service = new ProfileServiceWithExtraEventManagerIdentifier(); |
| 354 | + $service->eventIdentifier = new \stdClass(); |
| 355 | + $service->setEventManager($mock); |
| 356 | + } |
| 357 | +} |
| 358 | + |
| 359 | +class ProfileServiceWithExtraEventManagerIdentifier extends ProfileService |
| 360 | +{ |
| 361 | + public $eventIdentifier = null; |
| 362 | +} |
| 363 | + |
| 364 | +class TriggerCountingEventManager extends EventManager |
| 365 | +{ |
| 366 | + public $triggeredEventCount = array(); |
| 367 | + public $matchingRegex = null; |
| 368 | + |
| 369 | + public function trigger($event, $target = null, $argv = array(), $callback = null) |
| 370 | + { |
| 371 | + if ( !empty($this->matchingRegex) && !preg_match($this->matchingRegex, $event) ) { |
| 372 | + return; |
| 373 | + } |
| 374 | + |
| 375 | + if ( ! isset($this->triggeredEventCount[$event]) ) { |
| 376 | + $this->triggeredEventCount[$event] = 0; |
| 377 | + } |
| 378 | + $this->triggeredEventCount[$event]++; |
| 379 | + } |
260 | 380 | } |
0 commit comments