FileAccess Orion

EventListenerIntrospectionTrait

Trait providing utility methods and assertions for use in PHPUnit test cases.

This trait may be composed into a test case, and provides:

  • methods for introspecting events and listeners
  • methods for asserting listeners are attached at a specific priority

Some functionality in this trait duplicates functionality present in the version 2 EventManagerInterface and/or EventManager implementation, but abstracts that functionality for use in v3. As such, components or code that is testing for listener registration should use the methods in this trait to ensure tests are forwards-compatible between zend-eventmanager versions.

Table of Contents

traverseListeners()  : mixed
Generator for traversing listeners in priority order.
assertListenerAtPriority()  : mixed
Assert that a given listener exists at the specified priority.
getArrayOfListenersForEvent()  : array<string|int, callable>
Returns an indexed array of listeners for an event.
getEventsFromEventManager()  : array<string|int, string>
Retrieve a list of event names from an event manager.
getListenersForEvent()  : Traversable
Retrieve an interable list of listeners for an event.

Methods

traverseListeners()

Generator for traversing listeners in priority order.

public traverseListeners(array<string|int, mixed> $queue[, bool $withPriority = false ]) : mixed
Parameters
$queue : array<string|int, mixed>
$withPriority : bool = false

When true, yields priority as key.

Return values
mixed

assertListenerAtPriority()

Assert that a given listener exists at the specified priority.

private assertListenerAtPriority(callable $expectedListener, int $expectedPriority, string $event, EventManager $events[, string $message = '' ]) : mixed
Parameters
$expectedListener : callable
$expectedPriority : int
$event : string
$events : EventManager
$message : string = ''

Failure message to use, if any.

Return values
mixed

getArrayOfListenersForEvent()

Returns an indexed array of listeners for an event.

private getArrayOfListenersForEvent(string $event, EventManager $events) : array<string|int, callable>

Returns an indexed array of listeners for an event, in priority order. Priority values will not be included; use this only for testing if specific listeners are present, or for a count of listeners.

Parameters
$event : string
$events : EventManager
Return values
array<string|int, callable>

getListenersForEvent()

Retrieve an interable list of listeners for an event.

private getListenersForEvent(string $event, EventManager $events[, bool $withPriority = false ]) : Traversable

Given an event and an event manager, returns an iterator with the listeners for that event, in priority order.

If $withPriority is true, the key values will be the priority at which the given listener is attached.

Do not pass $withPriority if you want to cast the iterator to an array, as many listeners will likely have the same priority, and thus casting will collapse to the last added.

Parameters
$event : string
$events : EventManager
$withPriority : bool = false
Return values
Traversable

Search results