Skip to content

Commit 1cc671c

Browse files
committed
Merge branch 'feature/eventmanager'
Closes #10
2 parents b1ee589 + dc69c77 commit 1cc671c

File tree

2 files changed

+203
-0
lines changed

2 files changed

+203
-0
lines changed

src/LdcUserProfile/Service/ProfileService.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace LdcUserProfile\Service;
1111

1212
use Zend\Form\FormInterface;
13+
use Zend\EventManager\EventManagerInterface;
1314
use LdcUserProfile\Extensions\AbstractExtension;
1415
use ZfcUser\Entity\UserInterface;
1516
use LdcUserProfile\Form\PrototypeForm;
@@ -32,20 +33,38 @@ class ProfileService
3233
*/
3334
protected $moduleOptions;
3435

36+
/**
37+
* @var EventManagerInterface
38+
*/
39+
protected $events;
40+
41+
/**
42+
* @var mixed
43+
*/
44+
protected $eventIdentifier;
45+
3546
public function registerExtension(AbstractExtension $e)
3647
{
48+
$argv = array('extension' => $e);
49+
50+
$this->getEventManager()->trigger(__METHOD__ . '.pre', $this, $argv);
3751
$this->extensions[$e->getName()] = $e;
52+
$this->getEventManager()->trigger(__METHOD__ . '.post', $this, $argv);
3853

3954
return $this;
4055
}
4156

4257
public function unregisterExtension($nameOrInstance)
4358
{
59+
$argv = array('extension' => $nameOrInstance);
60+
61+
$this->getEventManager()->trigger(__METHOD__ . '.pre', $this, $argv);
4462
unset($this->extensions[
4563
$nameOrInstance instanceof AbstractExtension
4664
? $nameOrInstance->getName()
4765
: (string) $nameOrInstance]
4866
);
67+
$this->getEventManager()->trigger(__METHOD__ . '.post', $this, $argv);
4968

5069
return $this;
5170
}
@@ -69,15 +88,22 @@ public function constructFormForUser(UserInterface $user)
6988
{
7089
$form = clone $this->getFormPrototype();
7190
$entity = clone $form->getObject();
91+
$argv = compact('form', 'entity', 'user');
7292

7393
$vgOverrides = $this->getModuleOptions()->getValidationGroupOverrides();
7494

95+
$this->getEventManager()->trigger(__METHOD__ . '.pre', $this, $argv);
96+
7597
$validationGroup = array();
7698
foreach ( $this->getExtensions() as $name => $ext ) {
7799
$form->add(clone $ext->getFieldset(), array('name' => $name));
78100
$form->getInputFilter()->add(clone $ext->getInputFilter(), $name);
79101
$entity->{$name} = $ext->getObjectForUser($user);
80102

103+
$this->getEventManager()->trigger(__METHOD__ . '.extension', $this, $argv + array(
104+
'extension' => $ext,
105+
));
106+
81107
// Process validation group + overrides
82108
if ( isset($vgOverrides[$name]) ) {
83109
$ext->setFieldsetValidationGroup($vgOverrides[$name]);
@@ -91,16 +117,28 @@ public function constructFormForUser(UserInterface $user)
91117

92118
$form->bind($entity);
93119

120+
unset($argv['extension']);
121+
$this->getEventManager()->trigger(__METHOD__ . '.post', $this, $argv);
122+
94123
return $form;
95124
}
96125

97126
public function save($entity)
98127
{
128+
$argv = compact('entity');
129+
130+
$this->getEventManager()->trigger(__METHOD__ . '.pre', $this, $argv);
131+
99132
$result = true;
100133
foreach ( $this->getExtensions() as $name => $ext ) {
101134
$result = $result && $ext->save($entity);
102135
}
103136

137+
unset($argv['extension']);
138+
$this->getEventManager()->trigger(__METHOD__ . '.post', $this, $argv + array(
139+
'result' => $result
140+
));
141+
104142
return $result;
105143
}
106144

@@ -136,4 +174,49 @@ public function setModuleOptions(ModuleOptions $moduleOptions)
136174
return $this;
137175
}
138176

177+
/**
178+
* Set the event manager instance used by this context.
179+
*
180+
* For convenience, this method will also set the class name / LSB name as
181+
* identifiers, in addition to any string or array of strings set to the
182+
* $this->eventIdentifier property.
183+
*
184+
* @param EventManagerInterface $events
185+
* @return mixed
186+
*/
187+
public function setEventManager(EventManagerInterface $events)
188+
{
189+
$identifiers = array(__CLASS__, get_class($this));
190+
if (isset($this->eventIdentifier)) {
191+
if ((is_string($this->eventIdentifier))
192+
|| (is_array($this->eventIdentifier))
193+
|| ($this->eventIdentifier instanceof \Traversable)
194+
) {
195+
$identifiers = array_unique(array_merge($identifiers, (array) $this->eventIdentifier));
196+
} elseif (is_object($this->eventIdentifier)) {
197+
$identifiers[] = $this->eventIdentifier;
198+
}
199+
// silently ignore invalid eventIdentifier types
200+
}
201+
$events->setIdentifiers($identifiers);
202+
$this->events = $events;
203+
204+
return $this;
205+
}
206+
207+
/**
208+
* Retrieve the event manager
209+
*
210+
* Lazy-loads an EventManager instance if none registered.
211+
*
212+
* @return EventManagerInterface
213+
*/
214+
public function getEventManager()
215+
{
216+
if (!$this->events instanceof EventManagerInterface) {
217+
$this->setEventManager(new \Zend\EventManager\EventManager());
218+
}
219+
220+
return $this->events;
221+
}
139222
}

tests/LdcUserProfileTest/Service/ProfileServiceTest.php

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Zend\Form\Element\Text;
1414
use Zend\Stdlib\Hydrator\ObjectProperty;
1515
use Zend\Form\FormInterface;
16+
use Zend\EventManager\EventManager;
1617

1718
class ProfileServiceTest extends \PHPUnit_Framework_TestCase
1819
{
@@ -40,6 +41,19 @@ public function testRegisterExtension()
4041
return $ext;
4142
}
4243

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+
4357
public function testRegisterExtensionRejectsInvalidExtension()
4458
{
4559
$this->setExpectedException('PHPUnit_Framework_Error');
@@ -68,6 +82,21 @@ public function testUnregisterExtensionByName()
6882
$this->assertArrayNotHasKey('testext', $this->service->getExtensions());
6983
}
7084

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+
71100
public function testHasExtensionByName()
72101
{
73102
$ext = $this->testRegisterExtension();
@@ -102,6 +131,20 @@ public function testSaveCallsSaveOnEachRegsiteredExtensionAndReturnsFalseWhenAnE
102131
$this->assertFalse($this->service->save($payload));
103132
}
104133

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+
105148
public function testConstructFormForUser()
106149
{
107150
$mockUserData = new \stdClass();
@@ -135,6 +178,21 @@ public function testConstructFormForUser()
135178
$this->assertTrue($form->getInputFilter()->get('testext')->has('test'));
136179
}
137180

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+
138196
public function testConstructFormForUserObeysValidationGroupOverrides()
139197
{
140198
$mockUserData = new \stdClass();
@@ -257,4 +315,66 @@ public function testConstructFormForUserCanBeUsedMultipleTimesPerRequest()
257315
$this->assertTrue($formTwo->getInputFilter()->get('testext')->has('test'));
258316
}
259317

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+
}
260380
}

0 commit comments

Comments
 (0)