Skip to content

Commit

Permalink
Update EventProvider and ProvidesEventsForm to call attachDefaultList…
Browse files Browse the repository at this point in the history
…eners if it exists

(Brings both into sync with ZF2's EventManagerAwareTrait)
Closes ZF-Commons#80
  • Loading branch information
adamlundrigan committed Jan 15, 2015
1 parent 2d0e901 commit 84cfe0c
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 19 deletions.
12 changes: 9 additions & 3 deletions src/ZfcBase/EventManager/EventProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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
*
Expand Down
38 changes: 22 additions & 16 deletions src/ZfcBase/Form/ProvidesEventsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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;
}
Expand Down
22 changes: 22 additions & 0 deletions test/ZfcBaseTest/EventManager/EventProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace ZfcBaseTest\EventManager;

use PHPUnit_Framework_TestCase;

class EventProviderTest extends PHPUnit_Framework_TestCase
{
public function testSetEventManagerAttachesDefaultListenersWhenSpecified()
{
$object = new TestAsset\EventProviderWithDefaultListeners();
$this->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());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
namespace ZfcBaseTest\EventManager\TestAsset;

use ZfcBase\EventManager\EventProvider;

class EventProviderWithDefaultListeners extends EventProvider
{
public function attachDefaultListeners()
{
$this->getEventManager()->attach('foo', function() {});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
namespace ZfcBaseTest\EventManager\TestAsset;

use ZfcBase\EventManager\EventProvider;

class EventProviderWithNoDefaultListeners extends EventProvider
{
}
15 changes: 15 additions & 0 deletions test/ZfcBaseTest/Form/ProvidesEventsFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,20 @@ public function testSetEventManagerWorks()
$this->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());
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
namespace ZfcBaseTest\Form\TestAsset;

use ZfcBase\Form\ProvidesEventsForm;

class ProvidesEventsWithDefaultListeners extends ProvidesEventsForm
{
public function attachDefaultListeners()
{
$this->getEventManager()->attach('foo', function() {});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
namespace ZfcBaseTest\Form\TestAsset;

use ZfcBase\Form\ProvidesEventsForm;

class ProvidesEventsWithNoDefaultListeners extends ProvidesEventsForm
{
}

0 comments on commit 84cfe0c

Please sign in to comment.