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