-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathBernardMessageProducerTest.php
211 lines (167 loc) · 7.15 KB
/
BernardMessageProducerTest.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
/*
* This file is part of the prooph/psb-bernard-producer.
* (c) 2014 - 2015 prooph software GmbH <contact@prooph.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Date: 10/31/14 - 03:08 PM
*/
namespace ProophTest\ServiceBus;
use Bernard\Consumer;
use Bernard\Doctrine\MessagesSchema;
use Bernard\Driver\DoctrineDriver;
use Bernard\Producer;
use Bernard\QueueFactory\PersistentFactory;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Schema\Schema;
use Prooph\Common\Messaging\FQCNMessageFactory;
use Prooph\Common\Messaging\Message;
use Prooph\Common\Messaging\NoOpMessageConverter;
use Prooph\ServiceBus\CommandBus;
use Prooph\ServiceBus\EventBus;
use Prooph\ServiceBus\Message\Bernard\BernardMessage;
use Prooph\ServiceBus\Message\Bernard\BernardRouter;
use Prooph\ServiceBus\Message\Bernard\BernardSerializer;
use Prooph\ServiceBus\Message\Bernard\BernardMessageProducer;
use Prooph\ServiceBus\Plugin\Router\CommandRouter;
use Prooph\ServiceBus\Plugin\Router\EventRouter;
use ProophTest\ServiceBus\Mock\DoSomething;
use ProophTest\ServiceBus\Mock\MessageHandler;
use ProophTest\ServiceBus\Mock\SomethingDone;
use Prophecy\Argument;
use React\Promise\Deferred;
use Symfony\Component\EventDispatcher\EventDispatcher;
use PHPUnit\Framework\TestCase;
/**
* Class BernardMessageProducerTest
*
* @package Prooph\ServiceBusTest
* @author Alexander Miertsch <kontakt@codeliner.ws>
*/
class BernardMessageProducerTest extends TestCase
{
/**
* @var Producer
*/
private $bernardProducer;
/**
* @var PersistentFactory
*/
private $persistentFactory;
protected function setUp()
{
$connection = DriverManager::getConnection([
'driver' => 'pdo_sqlite',
'dbname' => ':memory:'
]);
$schema = new Schema();
MessagesSchema::create($schema);
array_map([$connection, "executeQuery"], $schema->toSql($connection->getDatabasePlatform()));
$doctrineDriver = new DoctrineDriver($connection);
//Use the serializer provided by psb-bernard-dispatcher
$this->persistentFactory = new PersistentFactory(
$doctrineDriver,
new BernardSerializer(
new FQCNMessageFactory(),
new NoOpMessageConverter()
)
);
$this->bernardProducer = new Producer($this->persistentFactory, new EventDispatcher());
}
/**
* @test
* @expectedException \Prooph\ServiceBus\Exception\InvalidArgumentException
*/
public function it_does_not_allow_empty_queue_name()
{
new BernardMessageProducer($this->bernardProducer, '');
}
/**
* @test
* @expectedException \Prooph\ServiceBus\Exception\InvalidArgumentException
*/
public function it_does_not_allow_non_string_queue_name()
{
new BernardMessageProducer($this->bernardProducer, 123);
}
/**
* @test
*/
public function it_allows_null_as_queue()
{
$bernardProducer = $this->prophesize(Producer::class);
$bernardProducer->produce(Argument::type(BernardMessage::class), null)->shouldBeCalled();
$proophBernardProducer = new BernardMessageProducer($bernardProducer->reveal());
$message = $this->prophesize(Message::class);
$proophBernardProducer($message->reveal());
}
/**
* @test
*/
public function it_sends_a_command_to_queue_pulls_it_with_consumer_and_forwards_it_to_command_bus()
{
$command = new DoSomething(['data' => 'test command']);
//The message dispatcher works with a ready-to-use bernard producer and one queue
$messageProducer = new BernardMessageProducer($this->bernardProducer, 'test-queue');
//Normally you would send the command on a command bus. We skip this step here cause we are only
//interested in the function of the message dispatcher
$messageProducer($command);
//Set up command bus which will receive the command message from the bernard consumer
$consumerCommandBus = new CommandBus();
$doSomethingHandler = new MessageHandler();
$router = new CommandRouter([
$command->messageName() => $doSomethingHandler
]);
$router->attachToMessageBus($consumerCommandBus);
$consumerCommandBus->dispatch($command);
//We use a special bernard router which forwards all messages to a command bus or event bus depending on the
//Prooph\ServiceBus\Message\MessageHeader::TYPE
$bernardRouter = new BernardRouter($consumerCommandBus, new EventBus());
$bernardConsumer = new Consumer($bernardRouter, new EventDispatcher());
//We use the same queue name here as we've defined for the message dispatcher above
$bernardConsumer->tick($this->persistentFactory->create('test-queue'));
$this->assertNotNull($doSomethingHandler->getLastMessage());
$this->assertEquals($command->payload(), $doSomethingHandler->getLastMessage()->payload());
}
/**
* @test
*/
public function it_sends_an_event_to_queue_pulls_it_with_consumer_and_forwards_it_to_event_bus()
{
$event = new SomethingDone(['data' => 'test event']);
//The message dispatcher works with a ready-to-use bernard producer and one queue
$messageProducer = new BernardMessageProducer($this->bernardProducer, 'test-queue');
//Normally you would send the event on a event bus. We skip this step here cause we are only
//interested in the function of the message dispatcher
$messageProducer($event);
//Set up event bus which will receive the event message from the bernard consumer
$consumerEventBus = new EventBus();
$somethingDoneListener = new MessageHandler();
$router = new EventRouter([
$event->messageName() => [$somethingDoneListener]
]);
$router->attachToMessageBus($consumerEventBus);
//We use a special bernard router which forwards all messages to a command bus or event bus depending on the
//Prooph\ServiceBus\Message\MessageHeader::TYPE
$bernardRouter = new BernardRouter(new CommandBus(), $consumerEventBus);
$bernardConsumer = new Consumer($bernardRouter, new EventDispatcher());
//We use the same queue name here as we've defined for the message dispatcher above
$bernardConsumer->tick($this->persistentFactory->create('test-queue'));
$this->assertNotNull($somethingDoneListener->getLastMessage());
$this->assertEquals($event->payload(), $somethingDoneListener->getLastMessage()->payload());
}
/**
* @test
* @expectedException \Prooph\ServiceBus\Exception\RuntimeException
*/
public function it_throws_runtime_exception_if_a_deferred_is_passed_to_invoke()
{
$event = new SomethingDone(['data' => 'test event']);
//The message dispatcher works with a ready-to-use bernard producer and one queue
$messageProducer = new BernardMessageProducer($this->bernardProducer, 'test-queue');
$deferred = $this->prophesize(Deferred::class);
$messageProducer($event, $deferred->reveal());
}
}