Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Created EventListenerIntrospectionTrait #19

Conversation

weierophinney
Copy link
Member

This trait is intended to provide a forwards-compatibility shim for use when testing event listener registration. Methods mimic some of the functionality already in v2 (via getEvents() and getListeners()), but in such a way as to allow common behavior when testing the two versions.

In particular, it provides the assertion method assertListenerAtPriority(), allowing developers to test that a listener was registered for the specified event at the specified priority; this is useful when testing attachment by aggregates or of default listeners.

A related pull request will also be submitted against the develop branch.

This trait is intended to provide a forwards-compatibility shim for use when
testing event listener registration. Methods mimic some of the functionality
already in v2 (via `getEvents()` and `getListeners()`), but in such a way as
to allow common behavior when testing the two versions.

In particular, it provides the assertion method `assertListenerAtPriority()`,
allowing developers to test that a listener was registered for the specified
event at the specified priority; this is useful when testing attachment by
aggregates or of default listeners.

namespace Zend\EventManager\Test;

use PHPUnit_Framework_TestCase as TestCase;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be PHPUnit_Framework_Assert

weierophinney added a commit to weierophinney/zend-eventmanager that referenced this pull request Jan 12, 2016
This patch ports the changes from zendframework#19 to the develop branch, and adapts them for
the v3 API.
EventManager $events,
$message = ''
) {
$message = $message ?: sprintf(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too much logic in this method.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How so? Do you have any suggestions to improve it?

Currently, it does exactly the following:

  • Sets a default message if none was provided
  • Retrieves the listeners, segregated by priority
  • Loops through the listeners to test if any match the specified conditions
  • Asserts that it found a match

The total number of lines is 15. I'm not seeing how this is "too much logic", personally.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally think this is fine, especially considering it is a private method it would easily allow things to be separated out if it gets more complex in the future. Right now this method has low cyclomatic complexity, I think the larger issue is that there is a ton of parameters but again, it's a private method so I feel there is no issue to really raise here.

@weierophinney
Copy link
Member Author

I should point out that the point of this is to simplify testing in components that are registering listeners, such as zend-modulemanager. In many of these, when refactoring for version 3, we introduced similar traits so that we could introspect the state of the event manager after attaching listeners, as the methods for doing so (getEvents() and getListeners(), in particular) do not exist in version 3; on top of that, the fact that listeners are not returned as CallbackHandlers in version 3 means tests written for version 2 could not be used in version 3 for asserting the listeners were registered at the correct priority.

In many cases, such tests did not exist prior to the refactors, which means that now, when we can attempt to see if the code works against both v2 and v3, we need traits such as this that can be re-used based on the event manager version in use. I'll be using this code going forward to refactor tests in the few components that use the event manager so that we can do v2 releases that will be forwards-compatible with zend-eventmanager v3.

@weierophinney
Copy link
Member Author

Also, this will address the lingering issue in #17 that currently prevents me tagging 3.0; once this is in place, we can tag 3.0, and merge #17 later as an internal implementation detail.

* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zend-eventmanager for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's 2016 :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can update that on merge; thanks for the heads-up! (copy-paste fail!)

@weierophinney weierophinney merged commit be75ec1 into zendframework:master Jan 12, 2016
weierophinney added a commit that referenced this pull request Jan 12, 2016
weierophinney added a commit that referenced this pull request Jan 12, 2016
@weierophinney weierophinney deleted the feature/listener-introspection branch January 12, 2016 23:10
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants