-
Notifications
You must be signed in to change notification settings - Fork 439
/
Copy pathProxyEventDispatcherTest.php
130 lines (105 loc) · 4.59 KB
/
ProxyEventDispatcherTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
namespace Enqueue\AsyncEventDispatcher\Tests;
use Enqueue\AsyncEventDispatcher\AsyncEventDispatcher;
use Enqueue\AsyncEventDispatcher\AsyncListener;
use Enqueue\Test\ClassExtensionTrait;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\GenericEvent;
class ProxyEventDispatcherTest extends TestCase
{
use ClassExtensionTrait;
public function testShouldBeSubClassOfEventDispatcher()
{
$this->assertClassExtends(EventDispatcher::class, AsyncEventDispatcher::class);
}
public function testShouldSetSyncModeForGivenEventNameOnDispatchAsyncListenersOnly()
{
$asyncListenerMock = $this->createAsyncListenerMock();
$asyncListenerMock
->expects($this->once())
->method('resetSyncMode')
;
$asyncListenerMock
->expects($this->once())
->method('syncMode')
->with('theEvent')
;
$trueEventDispatcher = new EventDispatcher();
$dispatcher = new AsyncEventDispatcher($trueEventDispatcher, $asyncListenerMock);
$event = new GenericEvent();
$dispatcher->dispatchAsyncListenersOnly('theEvent', $event);
}
public function testShouldCallAsyncEventButNotOtherOnDispatchAsyncListenersOnly()
{
$otherEventWasCalled = false;
$trueEventDispatcher = new EventDispatcher();
$trueEventDispatcher->addListener('theEvent', function () use (&$otherEventWasCalled) {
$this->assertInstanceOf(AsyncEventDispatcher::class, func_get_arg(2));
$otherEventWasCalled = true;
});
$asyncEventWasCalled = false;
$dispatcher = new AsyncEventDispatcher($trueEventDispatcher, $this->createAsyncListenerMock());
$dispatcher->addListener('theEvent', function () use (&$asyncEventWasCalled) {
$this->assertInstanceOf(AsyncEventDispatcher::class, func_get_arg(2));
$asyncEventWasCalled = true;
});
$event = new GenericEvent();
$dispatcher->dispatchAsyncListenersOnly('theEvent', $event);
$this->assertFalse($otherEventWasCalled);
$this->assertTrue($asyncEventWasCalled);
}
public function testShouldCallOtherEventIfDispatchedFromAsyncEventOnDispatchAsyncListenersOnly()
{
$otherEventWasCalled = false;
$trueEventDispatcher = new EventDispatcher();
$trueEventDispatcher->addListener('theOtherEvent', function () use (&$otherEventWasCalled) {
$this->assertNotInstanceOf(AsyncEventDispatcher::class, func_get_arg(2));
$otherEventWasCalled = true;
});
$asyncEventWasCalled = false;
$dispatcher = new AsyncEventDispatcher($trueEventDispatcher, $this->createAsyncListenerMock());
$dispatcher->addListener('theEvent', function () use (&$asyncEventWasCalled) {
$this->assertInstanceOf(AsyncEventDispatcher::class, func_get_arg(2));
$asyncEventWasCalled = true;
if (!class_exists(Event::class)) {
// Symfony 5
func_get_arg(2)->dispatch(func_get_arg(0), 'theOtherEvent');
} else {
// Symfony < 5
func_get_arg(2)->dispatch('theOtherEvent');
}
});
$event = new GenericEvent();
$dispatcher->dispatchAsyncListenersOnly('theEvent', $event);
$this->assertTrue($otherEventWasCalled);
$this->assertTrue($asyncEventWasCalled);
}
public function testShouldNotCallAsyncEventIfDispatchedFromOtherEventOnDispatchAsyncListenersOnly()
{
$trueEventDispatcher = new EventDispatcher();
$trueEventDispatcher->addListener('theOtherEvent', function () {
func_get_arg(2)->dispatch('theOtherAsyncEvent');
});
$dispatcher = new AsyncEventDispatcher($trueEventDispatcher, $this->createAsyncListenerMock());
$dispatcher->addListener('theAsyncEvent', function () {
func_get_arg(2)->dispatch('theOtherEvent');
});
$asyncEventWasCalled = false;
$dispatcher->addListener('theOtherAsyncEvent', function () use (&$asyncEventWasCalled) {
$asyncEventWasCalled = true;
});
$event = new GenericEvent();
$dispatcher->dispatchAsyncListenersOnly('theEvent', $event);
$this->assertFalse($asyncEventWasCalled);
}
/**
* @return MockObject|AsyncListener
*/
private function createAsyncListenerMock()
{
return $this->createMock(AsyncListener::class);
}
}