-
Notifications
You must be signed in to change notification settings - Fork 440
/
Copy pathAsyncProcessorTest.php
123 lines (96 loc) · 4.42 KB
/
AsyncProcessorTest.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
<?php
namespace Enqueue\Bundle\Tests\Functional\Events;
use Enqueue\AsyncEventDispatcher\AsyncListener;
use Enqueue\AsyncEventDispatcher\AsyncProcessor;
use Enqueue\Bundle\Tests\Functional\App\TestAsyncListener;
use Enqueue\Bundle\Tests\Functional\App\TestAsyncSubscriber;
use Enqueue\Bundle\Tests\Functional\WebTestCase;
use Enqueue\NoEffect\NullContext;
use Enqueue\NoEffect\NullMessage;
use Enqueue\Util\JSON;
use Interop\Queue\Processor;
use Symfony\Component\EventDispatcher\GenericEvent;
/**
* @group functional
*/
class AsyncProcessorTest extends WebTestCase
{
protected function setUp(): void
{
parent::setUp();
/** @var AsyncListener $asyncListener */
$asyncListener = static::$container->get('enqueue.events.async_listener');
$asyncListener->resetSyncMode();
static::$container->get('test_async_subscriber')->calls = [];
static::$container->get('test_async_listener')->calls = [];
}
public function testCouldBeGetFromContainerAsService()
{
/** @var AsyncProcessor $processor */
$processor = static::$container->get('test.enqueue.events.async_processor');
$this->assertInstanceOf(AsyncProcessor::class, $processor);
}
public function testShouldRejectIfMessageDoesNotContainEventNameProperty()
{
/** @var AsyncProcessor $processor */
$processor = static::$container->get('test.enqueue.events.async_processor');
$message = new NullMessage();
$this->assertEquals(Processor::REJECT, $processor->process($message, new NullContext()));
}
public function testShouldRejectIfMessageDoesNotContainTransformerNameProperty()
{
/** @var AsyncProcessor $processor */
$processor = static::$container->get('test.enqueue.events.async_processor');
$message = new NullMessage();
$message->setProperty('event_name', 'anEventName');
$this->assertEquals(Processor::REJECT, $processor->process($message, new NullContext()));
}
public function testShouldCallRealListener()
{
/** @var AsyncProcessor $processor */
$processor = static::$container->get('test.enqueue.events.async_processor');
$message = new NullMessage();
$message->setProperty('event_name', 'test_async');
$message->setProperty('transformer_name', 'test_async');
$message->setBody(JSON::encode([
'subject' => 'theSubject',
'arguments' => ['fooArg' => 'fooVal'],
]));
$this->assertEquals(Processor::ACK, $processor->process($message, new NullContext()));
/** @var TestAsyncListener $listener */
$listener = static::$container->get('test_async_listener');
$this->assertNotEmpty($listener->calls);
$this->assertInstanceOf(GenericEvent::class, $listener->calls[0][0]);
$this->assertEquals('theSubject', $listener->calls[0][0]->getSubject());
$this->assertEquals(['fooArg' => 'fooVal'], $listener->calls[0][0]->getArguments());
$this->assertEquals('test_async', $listener->calls[0][1]);
$this->assertSame(
static::$container->get('enqueue.events.event_dispatcher'),
$listener->calls[0][2]
);
}
public function testShouldCallRealSubscriber()
{
/** @var AsyncProcessor $processor */
$processor = static::$container->get('test.enqueue.events.async_processor');
$message = new NullMessage();
$message->setProperty('event_name', 'test_async_subscriber');
$message->setProperty('transformer_name', 'test_async');
$message->setBody(JSON::encode([
'subject' => 'theSubject',
'arguments' => ['fooArg' => 'fooVal'],
]));
$this->assertEquals(Processor::ACK, $processor->process($message, new NullContext()));
/** @var TestAsyncSubscriber $subscriber */
$subscriber = static::$container->get('test_async_subscriber');
$this->assertNotEmpty($subscriber->calls);
$this->assertInstanceOf(GenericEvent::class, $subscriber->calls[0][0]);
$this->assertEquals('theSubject', $subscriber->calls[0][0]->getSubject());
$this->assertEquals(['fooArg' => 'fooVal'], $subscriber->calls[0][0]->getArguments());
$this->assertEquals('test_async_subscriber', $subscriber->calls[0][1]);
$this->assertSame(
static::$container->get('enqueue.events.event_dispatcher'),
$subscriber->calls[0][2]
);
}
}