‪TYPO3CMS  11.5
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\PhpUnit\ProphecyTrait;
21 use Prophecy\Prophecy\ObjectProphecy;
22 use Psr\EventDispatcher\EventDispatcherInterface;
23 use Psr\EventDispatcher\ListenerProviderInterface;
24 use Psr\EventDispatcher\StoppableEventInterface;
26 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
27 
31 class ‪EventDispatcherTest extends UnitTestCase
32 {
33  use ProphecyTrait;
34 
36  protected ObjectProphecy ‪$listenerProviderProphecy;
37 
39 
40  protected function ‪setUp(): void
41  {
42  parent::setUp();
43  $this->listenerProviderProphecy = $this->prophesize(ListenerProviderInterface::class);
44  $this->eventDispatcher = new ‪EventDispatcher(
45  $this->listenerProviderProphecy->reveal()
46  );
47  }
48 
52  public function ‪implementsPsrInterface(): void
53  {
54  self::assertInstanceOf(EventDispatcherInterface::class, $this->eventDispatcher);
55  }
56 
61  public function ‪dispatchesEvent(callable $callable): void
62  {
63  $event = new \stdClass();
64  $event->invoked = 0;
65 
66  $this->listenerProviderProphecy->getListenersForEvent($event)->will(static function () use ($callable): iterable {
67  yield $callable;
68  });
69 
70  $ret = $this->eventDispatcher->dispatch($event);
71  self::assertSame($event, $ret);
72  self::assertEquals(1, $event->invoked);
73  }
74 
79  public function ‪doesNotDispatchStoppedEvent(callable $callable): void
80  {
81  $event = new class () implements StoppableEventInterface {
82  public $invoked = 0;
83 
84  public function isPropagationStopped(): bool
85  {
86  return true;
87  }
88  };
89 
90  $this->listenerProviderProphecy->getListenersForEvent($event)->will(static function () use ($callable): iterable {
91  yield $callable;
92  });
93 
94  $ret = $this->eventDispatcher->dispatch($event);
95  self::assertSame($event, $ret);
96  self::assertEquals(0, $event->invoked);
97  }
98 
103  public function ‪dispatchesMultipleListeners(callable $callable): void
104  {
105  $event = new \stdClass();
106  $event->invoked = 0;
107 
108  $this->listenerProviderProphecy->getListenersForEvent($event)->will(static function () use ($callable): iterable {
109  yield $callable;
110  yield $callable;
111  });
112 
113  $ret = $this->eventDispatcher->dispatch($event);
114  self::assertSame($event, $ret);
115  self::assertEquals(2, $event->invoked);
116  }
117 
122  public function ‪stopsOnStoppedEvent(callable $callable): void
123  {
124  $event = new class () implements StoppableEventInterface {
125  public $invoked = 0;
126  public $stopped = false;
127 
128  public function isPropagationStopped(): bool
129  {
130  return $this->stopped;
131  }
132  };
133 
134  $this->listenerProviderProphecy->getListenersForEvent($event)->will(static function () use ($callable): iterable {
135  yield $callable;
136  yield static function (object $event): void {
137  $event->invoked += 1;
138  $event->stopped = true;
139  };
140  yield $callable;
141  });
142 
143  $ret = $this->eventDispatcher->dispatch($event);
144  self::assertSame($event, $ret);
145  self::assertEquals(2, $event->invoked);
146  }
147 
151  public function ‪listenerExceptionIsPropagated(): void
152  {
153  $this->expectException(\BadMethodCallException::class);
154  $this->expectExceptionCode(1563270337);
155 
156  $event = new \stdClass();
157 
158  $this->listenerProviderProphecy->getListenersForEvent($event)->will(static function (): iterable {
159  yield static function (object $event): void {
160  throw new \BadMethodCallException('some invalid state', 1563270337);
161  };
162  });
163 
164  $this->eventDispatcher->dispatch($event);
165  }
166 
171  public function ‪callables(): array
172  {
173  return [
174  [
175  // Invokable
176  new class () {
177  public function __invoke(object $event): void
178  {
179  $event->invoked += 1;
180  }
181  },
182  ],
183  [
184  // Class + method
185  [
186  new class () {
187  public function onEvent(object $event): void
188  {
189  $event->invoked += 1;
190  }
191  },
192  'onEvent',
193  ],
194  ],
195  [
196  // Closure
197  static function (object $event): void {
198  $event->invoked += 1;
199  },
200  ],
201  ];
202  }
203 }
‪TYPO3\CMS\Core\Tests\Unit\EventDispatcher\EventDispatcherTest\dispatchesMultipleListeners
‪dispatchesMultipleListeners(callable $callable)
Definition: EventDispatcherTest.php:102
‪TYPO3\CMS\Core\Tests\Unit\EventDispatcher\EventDispatcherTest\$eventDispatcher
‪EventDispatcher $eventDispatcher
Definition: EventDispatcherTest.php:37
‪TYPO3\CMS\Core\Tests\Unit\EventDispatcher\EventDispatcherTest\doesNotDispatchStoppedEvent
‪doesNotDispatchStoppedEvent(callable $callable)
Definition: EventDispatcherTest.php:78
‪TYPO3\CMS\Core\EventDispatcher\EventDispatcher
Definition: EventDispatcher.php:30
‪TYPO3\CMS\Core\Tests\Unit\EventDispatcher\EventDispatcherTest\implementsPsrInterface
‪implementsPsrInterface()
Definition: EventDispatcherTest.php:51
‪TYPO3\CMS\Core\Tests\Unit\EventDispatcher\EventDispatcherTest\dispatchesEvent
‪dispatchesEvent(callable $callable)
Definition: EventDispatcherTest.php:60
‪TYPO3\CMS\Core\Tests\Unit\EventDispatcher\EventDispatcherTest\listenerExceptionIsPropagated
‪listenerExceptionIsPropagated()
Definition: EventDispatcherTest.php:150
‪TYPO3\CMS\Core\Tests\Unit\EventDispatcher\EventDispatcherTest\callables
‪callables()
Definition: EventDispatcherTest.php:170
‪TYPO3\CMS\Core\Tests\Unit\EventDispatcher\EventDispatcherTest\setUp
‪setUp()
Definition: EventDispatcherTest.php:39
‪TYPO3\CMS\Core\Tests\Unit\EventDispatcher
Definition: EventDispatcherTest.php:18
‪TYPO3\CMS\Core\Tests\Unit\EventDispatcher\EventDispatcherTest\$listenerProviderProphecy
‪ObjectProphecy $listenerProviderProphecy
Definition: EventDispatcherTest.php:35
‪TYPO3\CMS\Core\Tests\Unit\EventDispatcher\EventDispatcherTest\stopsOnStoppedEvent
‪stopsOnStoppedEvent(callable $callable)
Definition: EventDispatcherTest.php:121
‪TYPO3\CMS\Core\Tests\Unit\EventDispatcher\EventDispatcherTest
Definition: EventDispatcherTest.php:32