‪TYPO3CMS  10.4
ListenerProviderTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Prophecy\Prophecy\ObjectProphecy;
21 use Psr\Container\ContainerInterface;
22 use Psr\EventDispatcher\ListenerProviderInterface;
24 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
25 
29 class ‪ListenerProviderTest extends UnitTestCase
30 {
34  protected ‪$containerProphecy;
35 
40 
41  protected function ‪setUp(): void
42  {
43  parent::setUp();
44 
45  $this->containerProphecy = $this->prophesize();
46  $this->containerProphecy->willImplement(ContainerInterface::class);
47 
48  $this->listenerProvider = new ‪ListenerProvider(
49  $this->containerProphecy->reveal()
50  );
51  }
52 
56  public function ‪implementsPsrInterface()
57  {
58  self::assertInstanceOf(ListenerProviderInterface::class, $this->listenerProvider);
59  }
60 
65  {
66  $this->listenerProvider->addListener('Event\\Name', 'listener1');
67  $this->listenerProvider->addListener('Event\\Name', 'listener2', 'methodName');
68 
69  self::assertEquals($this->listenerProvider->getAllListenerDefinitions(), [
70  'Event\\Name' => [
71  [ 'service' => 'listener1', 'method' => null ],
72  [ 'service' => 'listener2', 'method' => 'methodName' ],
73  ]
74  ]);
75  }
76 
81  public function ‪dispatchesEvent($listener, string $method = null)
82  {
83  $event = new \stdClass();
84  $event->invoked = 0;
85 
86  $this->containerProphecy->get('listener')->willReturn($listener);
87  $this->listenerProvider->addListener(\stdClass::class, 'listener', $method);
88 
89  foreach ($this->listenerProvider->getListenersForEvent($event) as $listener) {
90  $listener($event);
91  }
92 
93  self::assertEquals(1, $event->invoked);
94  }
95 
100  public function ‪associatesToEventParentClass($listener, string $method = null)
101  {
102  $extendedEvent = new class() extends \stdClass {
103  public $invoked = 0;
104  };
105 
106  $this->containerProphecy->get('listener')->willReturn($listener);
107  $this->listenerProvider->addListener(\stdClass::class, 'listener', $method);
108  foreach ($this->listenerProvider->getListenersForEvent($extendedEvent) as $listener) {
109  $listener($extendedEvent);
110  }
111 
112  self::assertEquals(1, $extendedEvent->invoked);
113  }
114 
119  public function ‪associatesToImplementedInterfaces($listener, string $method = null)
120  {
121  $eventImplementation = new class() implements \IteratorAggregate {
122  public $invoked = 0;
123 
124  public function getIterator(): \Traversable
125  {
126  throw new \BadMethodCallException('Test', 1586942436);
127  }
128  };
129 
130  $this->containerProphecy->get('listener')->willReturn($listener);
131  $this->listenerProvider->addListener(\IteratorAggregate::class, 'listener', $method);
132  foreach ($this->listenerProvider->getListenersForEvent($eventImplementation) as $listener) {
133  $listener($eventImplementation);
134  }
135 
136  self::assertEquals(1, $eventImplementation->invoked);
137  }
138 
142  public function ‪addListenerPreservesOrder()
143  {
144  $this->listenerProvider->addListener(\stdClass::class, 'listener1');
145  $this->listenerProvider->addListener(\stdClass::class, 'listener2');
146 
147  $event = new \stdClass();
148  $event->sequence = '';
149  $this->containerProphecy->get('listener1')->willReturn(function (object $event): void {
150  $event->sequence .= 'a';
151  });
152  $this->containerProphecy->get('listener2')->willReturn(function (object $event): void {
153  $event->sequence .= 'b';
154  });
155  foreach ($this->listenerProvider->getListenersForEvent($event) as $listener) {
156  $listener($event);
157  }
158 
159  self::assertEquals('ab', $event->sequence);
160  }
161 
165  public function ‪throwsExceptionForInvalidCallable()
166  {
167  $this->expectException(\InvalidArgumentException::class);
168  $this->expectExceptionCode(1549988537);
169 
170  $event = new \stdClass();
171  $this->containerProphecy->get('listener')->willReturn(new \stdClass());
172  $this->listenerProvider->addListener(\stdClass::class, 'listener');
173  foreach ($this->listenerProvider->getListenersForEvent($event) as $listener) {
174  $listener($event);
175  }
176  }
177 
182  public function ‪listeners(): array
183  {
184  return [
185  [
186  // Invokable
187  'listener' => new class() {
188  public function __invoke(object $event): void
189  {
190  $event->invoked = 1;
191  }
192  },
193  'method' => null,
194  ],
195  [
196  // Class + method
197  'listener' => new class() {
198  public function onEvent(object $event): void
199  {
200  $event->invoked = 1;
201  }
202  },
203  'method' => 'onEvent',
204  ],
205  [
206  // Closure
207  'listener' => function (object $event): void {
208  $event->invoked = 1;
209  },
210  'method' => null,
211  ]
212  ];
213  }
214 }
‪TYPO3\CMS\Core\Tests\Unit\EventDispatcher\ListenerProviderTest\implementsPsrInterface
‪implementsPsrInterface()
Definition: ListenerProviderTest.php:54
‪TYPO3\CMS\Core\Tests\Unit\EventDispatcher\ListenerProviderTest\addListenerPreservesOrder
‪addListenerPreservesOrder()
Definition: ListenerProviderTest.php:140
‪TYPO3\CMS\Core\Tests\Unit\EventDispatcher\ListenerProviderTest
Definition: ListenerProviderTest.php:30
‪TYPO3\CMS\Core\Tests\Unit\EventDispatcher\ListenerProviderTest\$listenerProvider
‪ListenerProvider $listenerProvider
Definition: ListenerProviderTest.php:37
‪TYPO3\CMS\Core\Tests\Unit\EventDispatcher\ListenerProviderTest\$containerProphecy
‪ContainerInterface ObjectProphecy $containerProphecy
Definition: ListenerProviderTest.php:33
‪TYPO3\CMS\Core\Tests\Unit\EventDispatcher\ListenerProviderTest\setUp
‪setUp()
Definition: ListenerProviderTest.php:39
‪TYPO3\CMS\Core\Tests\Unit\EventDispatcher\ListenerProviderTest\associatesToEventParentClass
‪associatesToEventParentClass($listener, string $method=null)
Definition: ListenerProviderTest.php:98
‪TYPO3\CMS\Core\Tests\Unit\EventDispatcher\ListenerProviderTest\addedListenersAreReturnedByGetAllListenerDefinitions
‪addedListenersAreReturnedByGetAllListenerDefinitions()
Definition: ListenerProviderTest.php:62
‪TYPO3\CMS\Core\Tests\Unit\EventDispatcher\ListenerProviderTest\throwsExceptionForInvalidCallable
‪throwsExceptionForInvalidCallable()
Definition: ListenerProviderTest.php:163
‪TYPO3\CMS\Core\Tests\Unit\EventDispatcher\ListenerProviderTest\dispatchesEvent
‪dispatchesEvent($listener, string $method=null)
Definition: ListenerProviderTest.php:79
‪TYPO3\CMS\Core\Tests\Unit\EventDispatcher
Definition: EventDispatcherTest.php:18
‪TYPO3\CMS\Core\Tests\Unit\EventDispatcher\ListenerProviderTest\listeners
‪listeners()
Definition: ListenerProviderTest.php:180
‪TYPO3\CMS\Core\EventDispatcher\ListenerProvider
Definition: ListenerProvider.php:30
‪TYPO3\CMS\Core\Tests\Unit\EventDispatcher\ListenerProviderTest\associatesToImplementedInterfaces
‪associatesToImplementedInterfaces($listener, string $method=null)
Definition: ListenerProviderTest.php:117