-
Notifications
You must be signed in to change notification settings - Fork 440
/
Copy pathAsyncProcessorTest.php
123 lines (100 loc) · 3.88 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\AsyncEventDispatcher\Tests;
use Enqueue\AsyncEventDispatcher\AsyncEventDispatcher;
use Enqueue\AsyncEventDispatcher\AsyncProcessor;
use Enqueue\AsyncEventDispatcher\EventTransformer;
use Enqueue\AsyncEventDispatcher\Registry;
use Enqueue\Consumption\Result;
use Enqueue\NoEffect\NullContext;
use Enqueue\NoEffect\NullMessage;
use Enqueue\Test\ClassExtensionTrait;
use Interop\Queue\Processor;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\EventDispatcher\GenericEvent;
class AsyncProcessorTest extends TestCase
{
use ClassExtensionTrait;
public function testShouldImplementProcessorInterface()
{
$this->assertClassImplements(Processor::class, AsyncProcessor::class);
}
public function testCouldBeConstructedWithRegistryAndProxyEventDispatcher()
{
new AsyncProcessor($this->createRegistryMock(), $this->createProxyEventDispatcherMock());
}
public function testRejectIfMessageMissingEventNameProperty()
{
$processor = new AsyncProcessor($this->createRegistryMock(), $this->createProxyEventDispatcherMock());
$result = $processor->process(new NullMessage(), new NullContext());
$this->assertInstanceOf(Result::class, $result);
$this->assertEquals(Result::REJECT, $result->getStatus());
$this->assertEquals('The message is missing "event_name" property', $result->getReason());
}
public function testRejectIfMessageMissingTransformerNameProperty()
{
$processor = new AsyncProcessor($this->createRegistryMock(), $this->createProxyEventDispatcherMock());
$message = new NullMessage();
$message->setProperty('event_name', 'anEventName');
$result = $processor->process($message, new NullContext());
$this->assertInstanceOf(Result::class, $result);
$this->assertEquals(Result::REJECT, $result->getStatus());
$this->assertEquals('The message is missing "transformer_name" property', $result->getReason());
}
public function testShouldDispatchAsyncListenersOnly()
{
$eventName = 'theEventName';
$transformerName = 'theTransformerName';
$event = new GenericEvent();
$message = new NullMessage();
$message->setProperty('event_name', $eventName);
$message->setProperty('transformer_name', $transformerName);
$transformerMock = $this->createEventTransformerMock();
$transformerMock
->expects($this->once())
->method('toEvent')
->with($eventName, $this->identicalTo($message))
->willReturn($event)
;
$registryMock = $this->createRegistryMock();
$registryMock
->expects($this->once())
->method('getTransformer')
->with($transformerName)
->willReturn($transformerMock)
;
$dispatcherMock = $this->createProxyEventDispatcherMock();
$dispatcherMock
->expects($this->once())
->method('dispatchAsyncListenersOnly')
->with($eventName, $this->identicalTo($event))
;
$dispatcherMock
->expects($this->never())
->method('dispatch')
;
$processor = new AsyncProcessor($registryMock, $dispatcherMock);
$this->assertSame(Result::ACK, $processor->process($message, new NullContext()));
}
/**
* @return MockObject|EventTransformer
*/
private function createEventTransformerMock()
{
return $this->createMock(EventTransformer::class);
}
/**
* @return MockObject|AsyncEventDispatcher
*/
private function createProxyEventDispatcherMock()
{
return $this->createMock(AsyncEventDispatcher::class);
}
/**
* @return MockObject|Registry
*/
private function createRegistryMock()
{
return $this->createMock(Registry::class);
}
}