|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Enqueue\Bundle\Tests\Functional\Events; |
| 4 | + |
| 5 | +use Enqueue\Bundle\Events\AsyncListener; |
| 6 | +use Enqueue\Bundle\Tests\Functional\App\TestAsyncListener; |
| 7 | +use Enqueue\Bundle\Tests\Functional\WebTestCase; |
| 8 | +use Enqueue\Client\TraceableProducer; |
| 9 | +use Symfony\Component\EventDispatcher\Event; |
| 10 | +use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
| 11 | +use Symfony\Component\EventDispatcher\GenericEvent; |
| 12 | + |
| 13 | +class AsyncListenerTest extends WebTestCase |
| 14 | +{ |
| 15 | + public function setUp() |
| 16 | + { |
| 17 | + parent::setUp(); |
| 18 | + |
| 19 | + /** @var AsyncListener $asyncListener */ |
| 20 | + $asyncListener = $this->container->get('enqueue.events.async_listener'); |
| 21 | + |
| 22 | + $asyncListener->resetSyncMode(); |
| 23 | + } |
| 24 | + |
| 25 | + public function testShouldNotCallRealListenerIfMarkedAsAsync() |
| 26 | + { |
| 27 | + /** @var EventDispatcherInterface $dispatcher */ |
| 28 | + $dispatcher = $this->container->get('event_dispatcher'); |
| 29 | + |
| 30 | + $dispatcher->dispatch('test_async', new GenericEvent('aSubject')); |
| 31 | + |
| 32 | + /** @var TestAsyncListener $listner */ |
| 33 | + $listner = $this->container->get('test_async_listener'); |
| 34 | + |
| 35 | + $this->assertEmpty($listner->calls); |
| 36 | + } |
| 37 | + |
| 38 | + public function testShouldSendMessageToExpectedTopicInsteadOfCallingRealListener() |
| 39 | + { |
| 40 | + /** @var EventDispatcherInterface $dispatcher */ |
| 41 | + $dispatcher = $this->container->get('event_dispatcher'); |
| 42 | + |
| 43 | + $event = new GenericEvent('theSubject', ['fooArg' => 'fooVal']); |
| 44 | + |
| 45 | + $dispatcher->dispatch('test_async', $event); |
| 46 | + |
| 47 | + /** @var TraceableProducer $producer */ |
| 48 | + $producer = $this->container->get('enqueue.producer'); |
| 49 | + |
| 50 | + $traces = $producer->getTopicTraces('event.test_async'); |
| 51 | + |
| 52 | + $this->assertCount(1, $traces); |
| 53 | + |
| 54 | + $this->assertEquals('event.test_async', $traces[0]['topic']); |
| 55 | + $this->assertEquals(serialize($event), $traces[0]['body']); |
| 56 | + } |
| 57 | + |
| 58 | + public function testShouldSendMessageForEveryDispatchCall() |
| 59 | + { |
| 60 | + /** @var EventDispatcherInterface $dispatcher */ |
| 61 | + $dispatcher = $this->container->get('event_dispatcher'); |
| 62 | + |
| 63 | + $dispatcher->dispatch('test_async', new GenericEvent('theSubject', ['fooArg' => 'fooVal'])); |
| 64 | + $dispatcher->dispatch('test_async', new GenericEvent('theSubject', ['fooArg' => 'fooVal'])); |
| 65 | + $dispatcher->dispatch('test_async', new GenericEvent('theSubject', ['fooArg' => 'fooVal'])); |
| 66 | + |
| 67 | + /** @var TraceableProducer $producer */ |
| 68 | + $producer = $this->container->get('enqueue.producer'); |
| 69 | + |
| 70 | + $traces = $producer->getTopicTraces('event.test_async'); |
| 71 | + |
| 72 | + $this->assertCount(3, $traces); |
| 73 | + } |
| 74 | + |
| 75 | + public function testShouldSendMessageIfDispatchedFromInsideListener() |
| 76 | + { |
| 77 | + /** @var EventDispatcherInterface $dispatcher */ |
| 78 | + $dispatcher = $this->container->get('event_dispatcher'); |
| 79 | + |
| 80 | + $dispatcher->addListener('foo', function (Event $event, $eventName, EventDispatcherInterface $dispatcher) { |
| 81 | + $dispatcher->dispatch('test_async', new GenericEvent('theSubject', ['fooArg' => 'fooVal'])); |
| 82 | + }); |
| 83 | + |
| 84 | + $dispatcher->dispatch('foo'); |
| 85 | + |
| 86 | + /** @var TraceableProducer $producer */ |
| 87 | + $producer = $this->container->get('enqueue.producer'); |
| 88 | + |
| 89 | + $traces = $producer->getTopicTraces('event.test_async'); |
| 90 | + |
| 91 | + $this->assertCount(1, $traces); |
| 92 | + } |
| 93 | +} |
0 commit comments