|
| 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 ZendTest\EventManager\Test; |
| 11 | + |
| 12 | +use PHPUnit_Framework_ExpectationFailedException as ExpectationFailedException; |
| 13 | +use PHPUnit_Framework_TestCase as TestCase; |
| 14 | +use Traversable; |
| 15 | +use Zend\EventManager\EventManager; |
| 16 | +use Zend\EventManager\Test\EventListenerIntrospectionTrait; |
| 17 | + |
| 18 | +class EventListenerIntrospectionTraitTest extends TestCase |
| 19 | +{ |
| 20 | + use EventListenerIntrospectionTrait; |
| 21 | + |
| 22 | + public function setUp() |
| 23 | + { |
| 24 | + $this->events = new EventManager(); |
| 25 | + } |
| 26 | + |
| 27 | + public function testGetEventsFromEventManagerReturnsEventList() |
| 28 | + { |
| 29 | + // @codingStandardsIgnoreStart |
| 30 | + $this->events->attach('foo', function ($e) {}); |
| 31 | + $this->events->attach('bar', function ($e) {}); |
| 32 | + $this->events->attach('baz', function ($e) {}); |
| 33 | + // @codingStandardsIgnoreEnd |
| 34 | + |
| 35 | + $this->assertEquals(['foo', 'bar', 'baz'], $this->getEventsFromEventManager($this->events)); |
| 36 | + } |
| 37 | + |
| 38 | + public function testGetListenersForEventReturnsIteratorOfListenersForEventInPriorityOrder() |
| 39 | + { |
| 40 | + // @codingStandardsIgnoreStart |
| 41 | + $callback1 = function ($e) {}; |
| 42 | + $callback2 = function ($e) {}; |
| 43 | + $callback3 = function ($e) {}; |
| 44 | + $callback4 = function ($e) {}; |
| 45 | + $callback5 = function ($e) {}; |
| 46 | + // @codingStandardsIgnoreEnd |
| 47 | + |
| 48 | + $this->events->attach('foo', $callback5, 1); |
| 49 | + $this->events->attach('foo', $callback1, 2); |
| 50 | + $this->events->attach('foo', $callback4, 3); |
| 51 | + $this->events->attach('foo', $callback3, 4); |
| 52 | + $this->events->attach('foo', $callback2, 5); |
| 53 | + |
| 54 | + $listeners = $this->getListenersForEvent('foo', $this->events); |
| 55 | + $this->assertInstanceOf(Traversable::class, $listeners); |
| 56 | + $listeners = iterator_to_array($listeners); |
| 57 | + |
| 58 | + $this->assertEquals([ |
| 59 | + $callback5, |
| 60 | + $callback1, |
| 61 | + $callback4, |
| 62 | + $callback3, |
| 63 | + $callback2, |
| 64 | + ], $listeners); |
| 65 | + } |
| 66 | + |
| 67 | + public function testGetListenersForEventReturnsIteratorOfListenersInAttachmentOrderWhenSamePriority() |
| 68 | + { |
| 69 | + // @codingStandardsIgnoreStart |
| 70 | + $callback1 = function ($e) {}; |
| 71 | + $callback2 = function ($e) {}; |
| 72 | + $callback3 = function ($e) {}; |
| 73 | + $callback4 = function ($e) {}; |
| 74 | + $callback5 = function ($e) {}; |
| 75 | + // @codingStandardsIgnoreEnd |
| 76 | + |
| 77 | + $this->events->attach('foo', $callback5); |
| 78 | + $this->events->attach('foo', $callback1); |
| 79 | + $this->events->attach('foo', $callback4); |
| 80 | + $this->events->attach('foo', $callback3); |
| 81 | + $this->events->attach('foo', $callback2); |
| 82 | + |
| 83 | + $listeners = $this->getListenersForEvent('foo', $this->events); |
| 84 | + $this->assertInstanceOf(Traversable::class, $listeners); |
| 85 | + $listeners = iterator_to_array($listeners); |
| 86 | + |
| 87 | + $this->assertEquals([ |
| 88 | + $callback5, |
| 89 | + $callback1, |
| 90 | + $callback4, |
| 91 | + $callback3, |
| 92 | + $callback2, |
| 93 | + ], $listeners); |
| 94 | + } |
| 95 | + |
| 96 | + public function testGetListenersForEventCanReturnPriorityKeysWhenRequested() |
| 97 | + { |
| 98 | + // @codingStandardsIgnoreStart |
| 99 | + $callback1 = function ($e) {}; |
| 100 | + $callback2 = function ($e) {}; |
| 101 | + $callback3 = function ($e) {}; |
| 102 | + $callback4 = function ($e) {}; |
| 103 | + $callback5 = function ($e) {}; |
| 104 | + // @codingStandardsIgnoreEnd |
| 105 | + |
| 106 | + $this->events->attach('foo', $callback5, 1); |
| 107 | + $this->events->attach('foo', $callback1, 2); |
| 108 | + $this->events->attach('foo', $callback4, 3); |
| 109 | + $this->events->attach('foo', $callback3, 4); |
| 110 | + $this->events->attach('foo', $callback2, 5); |
| 111 | + |
| 112 | + $listeners = $this->getListenersForEvent('foo', $this->events, true); |
| 113 | + $this->assertInstanceOf(Traversable::class, $listeners); |
| 114 | + $listeners = iterator_to_array($listeners); |
| 115 | + |
| 116 | + $this->assertEquals([ |
| 117 | + 1 => $callback5, |
| 118 | + 2 => $callback1, |
| 119 | + 3 => $callback4, |
| 120 | + 4 => $callback3, |
| 121 | + 5 => $callback2, |
| 122 | + ], $listeners); |
| 123 | + } |
| 124 | + |
| 125 | + public function testGetArrayOfListenersForEventReturnsArrayOfListenersInPriorityOrder() |
| 126 | + { |
| 127 | + // @codingStandardsIgnoreStart |
| 128 | + $callback1 = function ($e) {}; |
| 129 | + $callback2 = function ($e) {}; |
| 130 | + $callback3 = function ($e) {}; |
| 131 | + $callback4 = function ($e) {}; |
| 132 | + $callback5 = function ($e) {}; |
| 133 | + // @codingStandardsIgnoreEnd |
| 134 | + |
| 135 | + $this->events->attach('foo', $callback5, 1); |
| 136 | + $this->events->attach('foo', $callback1, 1); |
| 137 | + $this->events->attach('foo', $callback4, 3); |
| 138 | + $this->events->attach('foo', $callback3, 2); |
| 139 | + $this->events->attach('foo', $callback2, 2); |
| 140 | + |
| 141 | + $listeners = $this->getArrayOfListenersForEvent('foo', $this->events); |
| 142 | + $this->assertInternalType('array', $listeners); |
| 143 | + |
| 144 | + $this->assertEquals([ |
| 145 | + $callback5, |
| 146 | + $callback1, |
| 147 | + $callback3, |
| 148 | + $callback2, |
| 149 | + $callback4, |
| 150 | + ], $listeners); |
| 151 | + } |
| 152 | + |
| 153 | + public function testAssertListenerAtPriorityPassesWhenListenerIsFound() |
| 154 | + { |
| 155 | + // @codingStandardsIgnoreStart |
| 156 | + $callback = function ($e) {}; |
| 157 | + // @codingStandardsIgnoreEnd |
| 158 | + |
| 159 | + $this->events->attach('foo', $callback, 7); |
| 160 | + |
| 161 | + $this->assertListenerAtPriority($callback, 7, 'foo', $this->events); |
| 162 | + } |
| 163 | + |
| 164 | + public function testAssertListenerAtPriorityFailsWhenListenerIsNotFound() |
| 165 | + { |
| 166 | + // @codingStandardsIgnoreStart |
| 167 | + $event = 'foo'; |
| 168 | + $listener = function ($e) {}; |
| 169 | + $priority = 7; |
| 170 | + $this->events->attach($event, $listener, $priority); |
| 171 | + |
| 172 | + $alternate = function ($e) {}; |
| 173 | + |
| 174 | + $permutations = [ |
| 175 | + 'different-listener' => ['listener' => $alternate, 'priority' => $priority, 'event' => $event], |
| 176 | + 'different-priority' => ['listener' => $listener, 'priority' => $priority + 1, 'event' => $event], |
| 177 | + 'different-event' => ['listener' => $listener, 'priority' => $priority, 'event' => $event . '-FOO'], |
| 178 | + ]; |
| 179 | + // @codingStandardsIgnoreEnd |
| 180 | + |
| 181 | + foreach ($permutations as $case => $arguments) { |
| 182 | + try { |
| 183 | + $this->assertListenerAtPriority( |
| 184 | + $arguments['listener'], |
| 185 | + $arguments['priority'], |
| 186 | + $arguments['event'], |
| 187 | + $this->events |
| 188 | + ); |
| 189 | + $this->fail('assertListenerAtPriority assertion had a false positive for case ' . $case); |
| 190 | + } catch (ExpectationFailedException $e) { |
| 191 | + $this->assertContains(sprintf( |
| 192 | + 'Listener not found for event "%s" and priority %d', |
| 193 | + $arguments['event'], |
| 194 | + $arguments['priority'] |
| 195 | + ), $e->getMessage(), sprintf('Assertion failure message was unexpected: %s', $e->getMessage())); |
| 196 | + } |
| 197 | + } |
| 198 | + } |
| 199 | +} |
0 commit comments