Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update EventProvider and ProvidesEventsForm #81

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/ZfcBase/EventManager/EventProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ 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
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_called_class());
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
{
}