|
11 | 11 |
|
12 | 12 | namespace Symfony\Component\EventDispatcher\Tests; |
13 | 13 |
|
14 | | -use PHPUnit\Framework\MockObject\MockObject; |
15 | 14 | use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Component\EventDispatcher\EventDispatcher; |
16 | 16 | use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
17 | 17 | use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
18 | 18 | use Symfony\Component\EventDispatcher\ImmutableEventDispatcher; |
|
23 | 23 | */ |
24 | 24 | class ImmutableEventDispatcherTest extends TestCase |
25 | 25 | { |
26 | | - private MockObject&EventDispatcherInterface $innerDispatcher; |
27 | | - private ImmutableEventDispatcher $dispatcher; |
28 | | - |
29 | | - protected function setUp(): void |
30 | | - { |
31 | | - $this->innerDispatcher = $this->createMock(EventDispatcherInterface::class); |
32 | | - $this->dispatcher = new ImmutableEventDispatcher($this->innerDispatcher); |
33 | | - } |
34 | | - |
35 | 26 | public function testDispatchDelegates() |
36 | 27 | { |
| 28 | + $innerDispatcher = $this->createMock(EventDispatcherInterface::class); |
| 29 | + $dispatcher = new ImmutableEventDispatcher($innerDispatcher); |
| 30 | + |
37 | 31 | $event = new Event(); |
38 | 32 | $resultEvent = new Event(); |
39 | 33 |
|
40 | | - $this->innerDispatcher->expects($this->once()) |
| 34 | + $innerDispatcher->expects($this->once()) |
41 | 35 | ->method('dispatch') |
42 | 36 | ->with($event, 'event') |
43 | 37 | ->willReturn($resultEvent); |
44 | 38 |
|
45 | | - $this->assertSame($resultEvent, $this->dispatcher->dispatch($event, 'event')); |
| 39 | + $this->assertSame($resultEvent, $dispatcher->dispatch($event, 'event')); |
46 | 40 | } |
47 | 41 |
|
48 | 42 | public function testGetListenersDelegates() |
49 | 43 | { |
50 | | - $this->innerDispatcher->expects($this->once()) |
| 44 | + $innerDispatcher = $this->createMock(EventDispatcherInterface::class); |
| 45 | + $dispatcher = new ImmutableEventDispatcher($innerDispatcher); |
| 46 | + |
| 47 | + $innerDispatcher->expects($this->once()) |
51 | 48 | ->method('getListeners') |
52 | 49 | ->with('event') |
53 | 50 | ->willReturn(['result']); |
54 | 51 |
|
55 | | - $this->assertSame(['result'], $this->dispatcher->getListeners('event')); |
| 52 | + $this->assertSame(['result'], $dispatcher->getListeners('event')); |
56 | 53 | } |
57 | 54 |
|
58 | 55 | public function testHasListenersDelegates() |
59 | 56 | { |
60 | | - $this->innerDispatcher->expects($this->once()) |
| 57 | + $innerDispatcher = $this->createMock(EventDispatcherInterface::class); |
| 58 | + $dispatcher = new ImmutableEventDispatcher($innerDispatcher); |
| 59 | + |
| 60 | + $innerDispatcher->expects($this->once()) |
61 | 61 | ->method('hasListeners') |
62 | 62 | ->with('event') |
63 | 63 | ->willReturn(true); |
64 | 64 |
|
65 | | - $this->assertTrue($this->dispatcher->hasListeners('event')); |
| 65 | + $this->assertTrue($dispatcher->hasListeners('event')); |
66 | 66 | } |
67 | 67 |
|
68 | 68 | public function testAddListenerDisallowed() |
69 | 69 | { |
| 70 | + $dispatcher = new ImmutableEventDispatcher(new EventDispatcher()); |
| 71 | + |
70 | 72 | $this->expectException(\BadMethodCallException::class); |
71 | | - $this->dispatcher->addListener('event', fn () => 'foo'); |
| 73 | + $dispatcher->addListener('event', fn () => 'foo'); |
72 | 74 | } |
73 | 75 |
|
74 | 76 | public function testAddSubscriberDisallowed() |
75 | 77 | { |
| 78 | + $dispatcher = new ImmutableEventDispatcher(new EventDispatcher()); |
| 79 | + |
76 | 80 | $this->expectException(\BadMethodCallException::class); |
77 | | - $subscriber = $this->createMock(EventSubscriberInterface::class); |
| 81 | + $subscriber = $this->createStub(EventSubscriberInterface::class); |
78 | 82 |
|
79 | | - $this->dispatcher->addSubscriber($subscriber); |
| 83 | + $dispatcher->addSubscriber($subscriber); |
80 | 84 | } |
81 | 85 |
|
82 | 86 | public function testRemoveListenerDisallowed() |
83 | 87 | { |
| 88 | + $dispatcher = new ImmutableEventDispatcher(new EventDispatcher()); |
| 89 | + |
84 | 90 | $this->expectException(\BadMethodCallException::class); |
85 | | - $this->dispatcher->removeListener('event', fn () => 'foo'); |
| 91 | + $dispatcher->removeListener('event', fn () => 'foo'); |
86 | 92 | } |
87 | 93 |
|
88 | 94 | public function testRemoveSubscriberDisallowed() |
89 | 95 | { |
| 96 | + $dispatcher = new ImmutableEventDispatcher(new EventDispatcher()); |
| 97 | + |
90 | 98 | $this->expectException(\BadMethodCallException::class); |
91 | | - $subscriber = $this->createMock(EventSubscriberInterface::class); |
| 99 | + $subscriber = $this->createStub(EventSubscriberInterface::class); |
92 | 100 |
|
93 | | - $this->dispatcher->removeSubscriber($subscriber); |
| 101 | + $dispatcher->removeSubscriber($subscriber); |
94 | 102 | } |
95 | 103 | } |
0 commit comments