From 84cfe0ca197154fbe73d1e85d2aa619d2a461f9e Mon Sep 17 00:00:00 2001 From: Adam Lundrigan Date: Wed, 14 Jan 2015 22:26:13 -0330 Subject: [PATCH] Update EventProvider and ProvidesEventsForm to call attachDefaultListeners if it exists (Brings both into sync with ZF2's EventManagerAwareTrait) Closes #80 --- src/ZfcBase/EventManager/EventProvider.php | 12 ++++-- src/ZfcBase/Form/ProvidesEventsForm.php | 38 +++++++++++-------- .../EventManager/EventProviderTest.php | 22 +++++++++++ .../EventProviderWithDefaultListeners.php | 12 ++++++ .../EventProviderWithNoDefaultListeners.php | 8 ++++ .../Form/ProvidesEventsFormTest.php | 15 ++++++++ .../ProvidesEventsWithDefaultListeners.php | 12 ++++++ .../ProvidesEventsWithNoDefaultListeners.php | 8 ++++ 8 files changed, 108 insertions(+), 19 deletions(-) create mode 100644 test/ZfcBaseTest/EventManager/EventProviderTest.php create mode 100644 test/ZfcBaseTest/EventManager/TestAsset/EventProviderWithDefaultListeners.php create mode 100644 test/ZfcBaseTest/EventManager/TestAsset/EventProviderWithNoDefaultListeners.php create mode 100644 test/ZfcBaseTest/Form/TestAsset/ProvidesEventsWithDefaultListeners.php create mode 100644 test/ZfcBaseTest/Form/TestAsset/ProvidesEventsWithNoDefaultListeners.php diff --git a/src/ZfcBase/EventManager/EventProvider.php b/src/ZfcBase/EventManager/EventProvider.php index d4a104a..58bda3f 100644 --- a/src/ZfcBase/EventManager/EventProvider.php +++ b/src/ZfcBase/EventManager/EventProvider.php @@ -15,14 +15,18 @@ abstract class EventProvider implements EventManagerAwareInterface protected $events; /** - * Set the event manager instance used by this context + * Set the event manager instance used by this context. + * + * For convenience, this method will also set the class name / LSB name as + * identifiers, in addition to any string or array of strings set to the + * $this->eventIdentifier property. * * @param EventManagerInterface $events * @return mixed */ public function setEventManager(EventManagerInterface $events) { - $identifiers = array(__CLASS__, get_called_class()); + $identifiers = array(__CLASS__, get_class($this)); if (isset($this->eventIdentifier)) { if ((is_string($this->eventIdentifier)) || (is_array($this->eventIdentifier)) @@ -36,9 +40,11 @@ public function setEventManager(EventManagerInterface $events) } $events->setIdentifiers($identifiers); $this->events = $events; + if (method_exists($this, 'attachDefaultListeners')) { + $this->attachDefaultListeners(); + } return $this; } - /** * Retrieve the event manager * diff --git a/src/ZfcBase/Form/ProvidesEventsForm.php b/src/ZfcBase/Form/ProvidesEventsForm.php index 17514ca..048dd68 100644 --- a/src/ZfcBase/Form/ProvidesEventsForm.php +++ b/src/ZfcBase/Form/ProvidesEventsForm.php @@ -13,19 +13,37 @@ class ProvidesEventsForm extends Form * @var EventManagerInterface */ protected $events; - /** - * Set the event manager instance used by this context + * Set the event manager instance used by this context. + * + * For convenience, this method will also set the class name / LSB name as + * identifiers, in addition to any string or array of strings set to the + * $this->eventIdentifier property. * * @param EventManagerInterface $events * @return mixed */ public function setEventManager(EventManagerInterface $events) { + $identifiers = array(__CLASS__, get_class($this)); + if (isset($this->eventIdentifier)) { + if ((is_string($this->eventIdentifier)) + || (is_array($this->eventIdentifier)) + || ($this->eventIdentifier instanceof Traversable) + ) { + $identifiers = array_unique(array_merge($identifiers, (array) $this->eventIdentifier)); + } elseif (is_object($this->eventIdentifier)) { + $identifiers[] = $this->eventIdentifier; + } + // silently ignore invalid eventIdentifier types + } + $events->setIdentifiers($identifiers); $this->events = $events; + if (method_exists($this, 'attachDefaultListeners')) { + $this->attachDefaultListeners(); + } return $this; } - /** * Retrieve the event manager * @@ -36,19 +54,7 @@ public function setEventManager(EventManagerInterface $events) public function getEventManager() { if (!$this->events instanceof EventManagerInterface) { - $identifiers = array(__CLASS__, get_called_class()); - if (isset($this->eventIdentifier)) { - if ((is_string($this->eventIdentifier)) - || (is_array($this->eventIdentifier)) - || ($this->eventIdentifier instanceof Traversable) - ) { - $identifiers = array_unique($identifiers + (array) $this->eventIdentifier); - } elseif (is_object($this->eventIdentifier)) { - $identifiers[] = $this->eventIdentifier; - } - // silently ignore invalid eventIdentifier types - } - $this->setEventManager(new EventManager($identifiers)); + $this->setEventManager(new EventManager()); } return $this->events; } diff --git a/test/ZfcBaseTest/EventManager/EventProviderTest.php b/test/ZfcBaseTest/EventManager/EventProviderTest.php new file mode 100644 index 0000000..a3230aa --- /dev/null +++ b/test/ZfcBaseTest/EventManager/EventProviderTest.php @@ -0,0 +1,22 @@ +assertInstanceOf('Zend\EventManager\EventManagerInterface', $object->getEventManager()); + $this->assertCount(1, $object->getEventManager()->getListeners('foo')); + } + + public function testSetEventManagerBehavesAsNormalWhenNoDefaultListenersSupplied() + { + $object = new TestAsset\EventProviderWithNoDefaultListeners(); + $this->assertInstanceOf('Zend\EventManager\EventManagerInterface', $object->getEventManager()); + $this->assertCount(0, $object->getEventManager()->getEvents()); + } +} diff --git a/test/ZfcBaseTest/EventManager/TestAsset/EventProviderWithDefaultListeners.php b/test/ZfcBaseTest/EventManager/TestAsset/EventProviderWithDefaultListeners.php new file mode 100644 index 0000000..9c10347 --- /dev/null +++ b/test/ZfcBaseTest/EventManager/TestAsset/EventProviderWithDefaultListeners.php @@ -0,0 +1,12 @@ +getEventManager()->attach('foo', function() {}); + } +} diff --git a/test/ZfcBaseTest/EventManager/TestAsset/EventProviderWithNoDefaultListeners.php b/test/ZfcBaseTest/EventManager/TestAsset/EventProviderWithNoDefaultListeners.php new file mode 100644 index 0000000..6db882e --- /dev/null +++ b/test/ZfcBaseTest/EventManager/TestAsset/EventProviderWithNoDefaultListeners.php @@ -0,0 +1,8 @@ +form->setEventManager($em); $this->assertSame($this->form->getEventManager(), $em); } + + public function testSetEventManagerAttachesDefaultListenersWhenSpecified() + { + $object = new TestAsset\ProvidesEventsWithDefaultListeners(); + $this->assertInstanceOf('Zend\EventManager\EventManagerInterface', $object->getEventManager()); + $this->assertCount(1, $object->getEventManager()->getListeners('foo')); + } + + public function testSetEventManagerBehavesAsNormalWhenNoDefaultListenersSupplied() + { + $object = new TestAsset\ProvidesEventsWithNoDefaultListeners(); + $this->assertInstanceOf('Zend\EventManager\EventManagerInterface', $object->getEventManager()); + $this->assertCount(0, $object->getEventManager()->getEvents()); + } + } diff --git a/test/ZfcBaseTest/Form/TestAsset/ProvidesEventsWithDefaultListeners.php b/test/ZfcBaseTest/Form/TestAsset/ProvidesEventsWithDefaultListeners.php new file mode 100644 index 0000000..5cb9cb8 --- /dev/null +++ b/test/ZfcBaseTest/Form/TestAsset/ProvidesEventsWithDefaultListeners.php @@ -0,0 +1,12 @@ +getEventManager()->attach('foo', function() {}); + } +} diff --git a/test/ZfcBaseTest/Form/TestAsset/ProvidesEventsWithNoDefaultListeners.php b/test/ZfcBaseTest/Form/TestAsset/ProvidesEventsWithNoDefaultListeners.php new file mode 100644 index 0000000..11af9a4 --- /dev/null +++ b/test/ZfcBaseTest/Form/TestAsset/ProvidesEventsWithNoDefaultListeners.php @@ -0,0 +1,8 @@ +