Skip to content

Commit 0d7b60f

Browse files
committed
Symfony async events. Add tests for async processor.
1 parent 4d28cf9 commit 0d7b60f

File tree

4 files changed

+99
-6
lines changed

4 files changed

+99
-6
lines changed

pkg/enqueue-bundle/Events/ProxyEventDispatcher.php

+7-3
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
namespace Enqueue\Bundle\Events;
44

5+
use Symfony\Component\DependencyInjection\ContainerInterface;
6+
use Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher;
57
use Symfony\Component\EventDispatcher\Event;
6-
use Symfony\Component\EventDispatcher\EventDispatcher;
78
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
89

9-
class ProxyEventDispatcher extends EventDispatcher
10+
class ProxyEventDispatcher extends ContainerAwareEventDispatcher
1011
{
1112
/**
1213
* @var EventDispatcherInterface
@@ -19,11 +20,14 @@ class ProxyEventDispatcher extends EventDispatcher
1920
private $asyncListener;
2021

2122
/**
23+
* @param ContainerInterface $container
2224
* @param EventDispatcherInterface $trueEventDispatcher
2325
* @param AsyncListener $asyncListener
2426
*/
25-
public function __construct(EventDispatcherInterface $trueEventDispatcher, AsyncListener $asyncListener)
27+
public function __construct(ContainerInterface $container, EventDispatcherInterface $trueEventDispatcher, AsyncListener $asyncListener)
2628
{
29+
parent::__construct($container);
30+
2731
$this->trueEventDispatcher = $trueEventDispatcher;
2832
$this->asyncListener = $asyncListener;
2933
}

pkg/enqueue-bundle/Resources/config/events.yml

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ services:
1414
enqueue.events.event_dispatcher:
1515
class: 'Enqueue\Bundle\Events\ProxyEventDispatcher'
1616
arguments:
17+
- '@service_container'
1718
- '@event_dispatcher'
1819
- '@enqueue.events.async_listener'
1920

pkg/enqueue-bundle/Tests/Functional/Events/AsyncListenerTest.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
1111
use Symfony\Component\EventDispatcher\GenericEvent;
1212

13+
/**
14+
* @group functional
15+
*/
1316
class AsyncListenerTest extends WebTestCase
1417
{
1518
public function setUp()
@@ -29,10 +32,10 @@ public function testShouldNotCallRealListenerIfMarkedAsAsync()
2932

3033
$dispatcher->dispatch('test_async', new GenericEvent('aSubject'));
3134

32-
/** @var TestAsyncListener $listner */
33-
$listner = $this->container->get('test_async_listener');
35+
/** @var TestAsyncListener $listener */
36+
$listener = $this->container->get('test_async_listener');
3437

35-
$this->assertEmpty($listner->calls);
38+
$this->assertEmpty($listener->calls);
3639
}
3740

3841
public function testShouldSendMessageToExpectedTopicInsteadOfCallingRealListener()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
namespace Enqueue\Bundle\Tests\Functional\Events;
4+
5+
use Enqueue\Bundle\Events\AsyncListener;
6+
use Enqueue\Bundle\Events\AsyncProcessor;
7+
use Enqueue\Bundle\Tests\Functional\App\TestAsyncListener;
8+
use Enqueue\Bundle\Tests\Functional\WebTestCase;
9+
use Enqueue\Null\NullContext;
10+
use Enqueue\Null\NullMessage;
11+
use Enqueue\Psr\PsrProcessor;
12+
use Symfony\Component\EventDispatcher\GenericEvent;
13+
14+
/**
15+
* @group functional
16+
*/
17+
class AsyncProcessorTest extends WebTestCase
18+
{
19+
public function setUp()
20+
{
21+
parent::setUp();
22+
23+
/** @var AsyncListener $asyncListener */
24+
$asyncListener = $this->container->get('enqueue.events.async_listener');
25+
26+
$asyncListener->resetSyncMode();
27+
}
28+
29+
public function testCouldBeGetFromContainerAsService()
30+
{
31+
/** @var AsyncProcessor $processor */
32+
$processor = $this->container->get('enqueue.events.async_processor');
33+
34+
$this->assertInstanceOf(AsyncProcessor::class, $processor);
35+
}
36+
37+
public function testShouldRejectIfMessageDoesNotContainEventNameProperty()
38+
{
39+
/** @var AsyncProcessor $processor */
40+
$processor = $this->container->get('enqueue.events.async_processor');
41+
42+
$message = new NullMessage();
43+
44+
$this->assertEquals(PsrProcessor::REJECT, $processor->process($message, new NullContext()));
45+
}
46+
47+
public function testShouldRejectIfMessageDoesNotContainTransformerNameProperty()
48+
{
49+
/** @var AsyncProcessor $processor */
50+
$processor = $this->container->get('enqueue.events.async_processor');
51+
52+
$message = new NullMessage();
53+
$message->setProperty('event_name', 'anEventName');
54+
55+
$this->assertEquals(PsrProcessor::REJECT, $processor->process($message, new NullContext()));
56+
}
57+
58+
public function testShouldCallRealListener()
59+
{
60+
/** @var AsyncProcessor $processor */
61+
$processor = $this->container->get('enqueue.events.async_processor');
62+
63+
$event = new GenericEvent('theSubject', ['fooArg' => 'fooVal']);
64+
65+
$message = new NullMessage();
66+
$message->setProperty('event_name', 'test_async');
67+
$message->setProperty('transformer_name', 'php_serializer');
68+
$message->setBody(serialize($event));
69+
70+
$this->assertEquals(PsrProcessor::ACK, $processor->process($message, new NullContext()));
71+
72+
/** @var TestAsyncListener $listener */
73+
$listener = $this->container->get('test_async_listener');
74+
75+
$this->assertNotEmpty($listener->calls);
76+
77+
$this->assertEquals($event, $listener->calls[0][0]);
78+
$this->assertEquals('test_async', $listener->calls[0][1]);
79+
80+
$this->assertSame(
81+
$this->container->get('enqueue.events.event_dispatcher'),
82+
$listener->calls[0][2]
83+
);
84+
}
85+
}

0 commit comments

Comments
 (0)