[ Index ]

PHP Cross Reference of phpBB-3.3.5-deutsch

title

Body

[close]

/vendor/zendframework/zend-eventmanager/src/Test/ -> EventListenerIntrospectionTrait.php (source)

   1  <?php
   2  /**
   3   * Zend Framework (http://framework.zend.com/)
   4   *
   5   * @link      http://github.com/zendframework/zend-eventmanager for the canonical source repository
   6   * @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
   7   * @license   https://github.com/zendframework/zend-eventmanager/blob/master/LICENSE.md
   8   */
   9  
  10  namespace Zend\EventManager\Test;
  11  
  12  use PHPUnit\Framework\Assert;
  13  use ReflectionProperty;
  14  use Zend\EventManager\EventManager;
  15  
  16  /**
  17   * Trait providing utility methods and assertions for use in PHPUnit test cases.
  18   *
  19   * This trait may be composed into a test case, and provides:
  20   *
  21   * - methods for introspecting events and listeners
  22   * - methods for asserting listeners are attached at a specific priority
  23   *
  24   * Some functionality in this trait duplicates functionality present in the
  25   * version 2 EventManagerInterface and/or EventManager implementation, but
  26   * abstracts that functionality for use in v3. As such, components or code
  27   * that is testing for listener registration should use the methods in this
  28   * trait to ensure tests are forwards-compatible between zend-eventmanager
  29   * versions.
  30   */
  31  trait EventListenerIntrospectionTrait
  32  {
  33      /**
  34       * Retrieve a list of event names from an event manager.
  35       *
  36       * @param EventManager $events
  37       * @return string[]
  38       */
  39      private function getEventsFromEventManager(EventManager $events)
  40      {
  41          $r = new ReflectionProperty($events, 'events');
  42          $r->setAccessible(true);
  43          $listeners = $r->getValue($events);
  44          return array_keys($listeners);
  45      }
  46  
  47      /**
  48       * Retrieve an interable list of listeners for an event.
  49       *
  50       * Given an event and an event manager, returns an iterator with the
  51       * listeners for that event, in priority order.
  52       *
  53       * If $withPriority is true, the key values will be the priority at which
  54       * the given listener is attached.
  55       *
  56       * Do not pass $withPriority if you want to cast the iterator to an array,
  57       * as many listeners will likely have the same priority, and thus casting
  58       * will collapse to the last added.
  59       *
  60       * @param string $event
  61       * @param EventManager $events
  62       * @param bool $withPriority
  63       * @return \Traversable
  64       */
  65      private function getListenersForEvent($event, EventManager $events, $withPriority = false)
  66      {
  67          $r = new ReflectionProperty($events, 'events');
  68          $r->setAccessible(true);
  69          $internal = $r->getValue($events);
  70  
  71          $listeners = [];
  72          foreach (isset($internal[$event]) ? $internal[$event] : [] as $p => $listOfListeners) {
  73              foreach ($listOfListeners as $l) {
  74                  $listeners[$p] = isset($listeners[$p]) ? array_merge($listeners[$p], $l) : $l;
  75              }
  76          }
  77  
  78          return $this->traverseListeners($listeners, $withPriority);
  79      }
  80  
  81      /**
  82       * Assert that a given listener exists at the specified priority.
  83       *
  84       * @param callable $expectedListener
  85       * @param int $expectedPriority
  86       * @param string $event
  87       * @param EventManager $events
  88       * @param string $message Failure message to use, if any.
  89       */
  90      private function assertListenerAtPriority(
  91          callable $expectedListener,
  92          $expectedPriority,
  93          $event,
  94          EventManager $events,
  95          $message = ''
  96      ) {
  97          $message = $message ?: sprintf(
  98              'Listener not found for event "%s" and priority %d',
  99              $event,
 100              $expectedPriority
 101          );
 102          $listeners = $this->getListenersForEvent($event, $events, true);
 103          $found     = false;
 104          foreach ($listeners as $priority => $listener) {
 105              if ($listener === $expectedListener
 106                  && $priority === $expectedPriority
 107              ) {
 108                  $found = true;
 109                  break;
 110              }
 111          }
 112          Assert::assertTrue($found, $message);
 113      }
 114  
 115      /**
 116       * Returns an indexed array of listeners for an event.
 117       *
 118       * Returns an indexed array of listeners for an event, in priority order.
 119       * Priority values will not be included; use this only for testing if
 120       * specific listeners are present, or for a count of listeners.
 121       *
 122       * @param string $event
 123       * @param EventManager $events
 124       * @return callable[]
 125       */
 126      private function getArrayOfListenersForEvent($event, EventManager $events)
 127      {
 128          return iterator_to_array($this->getListenersForEvent($event, $events));
 129      }
 130  
 131      /**
 132       * Generator for traversing listeners in priority order.
 133       *
 134       * @param array $listeners
 135       * @param bool $withPriority When true, yields priority as key.
 136       */
 137      public function traverseListeners(array $queue, $withPriority = false)
 138      {
 139          krsort($queue, SORT_NUMERIC);
 140  
 141          foreach ($queue as $priority => $listeners) {
 142              $priority = (int) $priority;
 143              foreach ($listeners as $listener) {
 144                  if ($withPriority) {
 145                      yield $priority => $listener;
 146                  } else {
 147                      yield $listener;
 148                  }
 149              }
 150          }
 151      }
 152  }


Generated: Mon Oct 4 17:42:11 2021 Cross-referenced by PHPXref 0.7.1