‪TYPO3CMS  10.4
EventDispatcherTest.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\EventDispatcher\EventDispatcherInterface;
22 use Psr\EventDispatcher\ListenerProviderInterface;
23 use Psr\EventDispatcher\StoppableEventInterface;
25 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
26 
30 class ‪EventDispatcherTest extends UnitTestCase
31 {
35  protected ‪$containerProphecy;
36 
41 
42  protected function ‪setUp(): void
43  {
44  parent::setUp();
45 
46  $this->listenerProviderProphecy = $this->prophesize();
47  $this->listenerProviderProphecy->willImplement(ListenerProviderInterface::class);
48 
49  $this->eventDispatcher = new ‪EventDispatcher(
50  $this->listenerProviderProphecy->reveal()
51  );
52  }
53 
57  public function ‪implementsPsrInterface()
58  {
59  self::assertInstanceOf(EventDispatcherInterface::class, $this->eventDispatcher);
60  }
61 
66  public function ‪dispatchesEvent(callable $callable)
67  {
68  $event = new \stdClass();
69  $event->invoked = 0;
70 
71  $this->listenerProviderProphecy->getListenersForEvent($event)->will(function () use ($callable): iterable {
72  yield $callable;
73  });
74 
75  $ret = $this->eventDispatcher->dispatch($event);
76  self::assertSame($event, $ret);
77  self::assertEquals(1, $event->invoked);
78  }
79 
84  public function ‪doesNotDispatchStoppedEvent(callable $callable)
85  {
86  $event = new class() implements StoppableEventInterface {
87  public $invoked = 0;
88 
89  public function isPropagationStopped(): bool
90  {
91  return true;
92  }
93  };
94 
95  $this->listenerProviderProphecy->getListenersForEvent($event)->will(function () use ($callable): iterable {
96  yield $callable;
97  });
98 
99  $ret = $this->eventDispatcher->dispatch($event);
100  self::assertSame($event, $ret);
101  self::assertEquals(0, $event->invoked);
102  }
103 
108  public function ‪dispatchesMultipleListeners(callable $callable)
109  {
110  $event = new \stdClass();
111  $event->invoked = 0;
112 
113  $this->listenerProviderProphecy->getListenersForEvent($event)->will(function () use ($callable): iterable {
114  yield $callable;
115  yield $callable;
116  });
117 
118  $ret = $this->eventDispatcher->dispatch($event);
119  self::assertSame($event, $ret);
120  self::assertEquals(2, $event->invoked);
121  }
122 
127  public function ‪stopsOnStoppedEvent(callable $callable)
128  {
129  $event = new class() implements StoppableEventInterface {
130  public $invoked = 0;
131  public $stopped = false;
132 
133  public function isPropagationStopped(): bool
134  {
135  return $this->stopped;
136  }
137  };
138 
139  $this->listenerProviderProphecy->getListenersForEvent($event)->will(function () use ($callable): iterable {
140  yield $callable;
141  yield function (object $event): void {
142  $event->invoked += 1;
143  $event->stopped = true;
144  };
145  yield $callable;
146  });
147 
148  $ret = $this->eventDispatcher->dispatch($event);
149  self::assertSame($event, $ret);
150  self::assertEquals(2, $event->invoked);
151  }
152 
156  public function ‪listenerExceptionIsPropagated()
157  {
158  $this->expectException(\BadMethodCallException::class);
159  $this->expectExceptionCode(1563270337);
160 
161  $event = new \stdClass();
162 
163  $this->listenerProviderProphecy->getListenersForEvent($event)->will(function (): iterable {
164  yield function (object $event): void {
165  throw new \BadMethodCallException('some invalid state', 1563270337);
166  };
167  });
168 
169  $this->eventDispatcher->dispatch($event);
170  }
171 
176  public function ‪callables(): array
177  {
178  return [
179  [
180  // Invokable
181  new class() {
182  public function __invoke(object $event): void
183  {
184  $event->invoked += 1;
185  }
186  },
187  ],
188  [
189  // Class + method
190  [
191  new class() {
192  public function onEvent(object $event): void
193  {
194  $event->invoked += 1;
195  }
196  },
197  'onEvent',
198  ]
199  ],
200  [
201  // Closure
202  function (object $event): void {
203  $event->invoked += 1;
204  },
205  ]
206  ];
207  }
208 }
‪TYPO3\CMS\Core\Tests\Unit\EventDispatcher\EventDispatcherTest\dispatchesMultipleListeners
‪dispatchesMultipleListeners(callable $callable)
Definition: EventDispatcherTest.php:106
‪TYPO3\CMS\Core\Tests\Unit\EventDispatcher\EventDispatcherTest\$eventDispatcher
‪EventDispatcher $eventDispatcher
Definition: EventDispatcherTest.php:38
‪TYPO3\CMS\Core\Tests\Unit\EventDispatcher\EventDispatcherTest\$containerProphecy
‪ListenerProviderInterface ObjectProphecy $containerProphecy
Definition: EventDispatcherTest.php:34
‪TYPO3\CMS\Core\Tests\Unit\EventDispatcher\EventDispatcherTest\doesNotDispatchStoppedEvent
‪doesNotDispatchStoppedEvent(callable $callable)
Definition: EventDispatcherTest.php:82
‪TYPO3\CMS\Core\EventDispatcher\EventDispatcher
Definition: EventDispatcher.php:30
‪TYPO3\CMS\Core\Tests\Unit\EventDispatcher\EventDispatcherTest\implementsPsrInterface
‪implementsPsrInterface()
Definition: EventDispatcherTest.php:55
‪TYPO3\CMS\Core\Tests\Unit\EventDispatcher\EventDispatcherTest\dispatchesEvent
‪dispatchesEvent(callable $callable)
Definition: EventDispatcherTest.php:64
‪TYPO3\CMS\Core\Tests\Unit\EventDispatcher\EventDispatcherTest\listenerExceptionIsPropagated
‪listenerExceptionIsPropagated()
Definition: EventDispatcherTest.php:154
‪TYPO3\CMS\Core\Tests\Unit\EventDispatcher\EventDispatcherTest\callables
‪callables()
Definition: EventDispatcherTest.php:174
‪TYPO3\CMS\Core\Tests\Unit\EventDispatcher\EventDispatcherTest\setUp
‪setUp()
Definition: EventDispatcherTest.php:40
‪TYPO3\CMS\Core\Tests\Unit\EventDispatcher
Definition: EventDispatcherTest.php:18
‪TYPO3\CMS\Core\Tests\Unit\EventDispatcher\EventDispatcherTest\stopsOnStoppedEvent
‪stopsOnStoppedEvent(callable $callable)
Definition: EventDispatcherTest.php:125
‪TYPO3\CMS\Core\Tests\Unit\EventDispatcher\EventDispatcherTest
Definition: EventDispatcherTest.php:31