-
Notifications
You must be signed in to change notification settings - Fork 4
/
AmqpDelayedMessageProducerTest.php
112 lines (97 loc) · 4.09 KB
/
AmqpDelayedMessageProducerTest.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
<?php
/**
* This file is part of the prooph/humus-amqp-producer.
* (c) 2016-2019 Alexander Miertsch <kontakt@codeliner.ws>
* (c) 2016-2019 Sascha-Oliver Prolic <saschaprolic@googlemail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace ProophTest\ServiceBus\Message\HumusAmqp;
use Humus\Amqp\Constants;
use Humus\Amqp\Producer;
use PHPUnit\Framework\TestCase;
use Prooph\Common\Messaging\Message;
use Prooph\Common\Messaging\MessageConverter;
use Prooph\ServiceBus\Exception\RuntimeException;
use Prooph\ServiceBus\Message\HumusAmqp\AmqpDelayedMessageProducer;
use Prooph\ServiceBus\Message\HumusAmqp\DelayedMessage;
use React\Promise\Deferred;
class AmqpDelayedMessageProducerTest extends TestCase
{
/**
* @test
*/
public function it_publishes_messages(): void
{
$now = new \DateTimeImmutable('now', new \DateTimeZone('UTC'));
$producer = $this->prophesize(Producer::class);
$producer->publish(
[
'message_name' => 'test-message',
'uuid' => 'ccefedef-85e1-4fd0-b247-ed13d378b050',
'version' => 1,
'payload' => [],
'metadata' => [
'execute_at' => $now->modify('+5 seconds')->format('Y-m-d\TH:i:s.u'),
],
'created_at' => $now->format('Y-m-d\TH:i:s.u'),
],
'test-message',
Constants::AMQP_NOPARAM,
[
'app_id' => 'test_app',
'timestamp' => $now->getTimestamp(),
'type' => 'test-message',
'headers' => [
'x-delay' => 5000,
],
]
)->shouldBeCalled();
$message = $this->prophesize(DelayedMessage::class);
$message->delay()->willReturn(5000)->shouldBeCalled();
$message->createdAt()->willReturn($now)->shouldBeCalled();
$message->messageName()->willReturn('test-message')->shouldBeCalled();
$messageConverter = $this->prophesize(MessageConverter::class);
$messageConverter->convertToArray($message)->willReturn([
'message_name' => 'test-message',
'uuid' => 'ccefedef-85e1-4fd0-b247-ed13d378b050',
'version' => 1,
'payload' => [],
'metadata' => [
'execute_at' => $now->modify('+5 seconds')->format('Y-m-d\TH:i:s.u'),
],
'created_at' => $now,
])->shouldBeCalled();
$messageProducer = new AmqpDelayedMessageProducer($producer->reveal(), $messageConverter->reveal(), 'test_app');
$messageProducer($message->reveal());
}
/**
* @test
*/
public function it_throws_exception_when_deferred_passed(): void
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Prooph\ServiceBus\Message\HumusAmqp\AmqpDelayedMessageProducer cannot handle query messages which require future responses.');
$producer = $this->prophesize(Producer::class);
$messageConverter = $this->prophesize(MessageConverter::class);
$message = $this->prophesize(DelayedMessage::class);
$deferred = $this->prophesize(Deferred::class);
$messageProducer = new AmqpDelayedMessageProducer($producer->reveal(), $messageConverter->reveal());
$messageProducer($message->reveal(), $deferred->reveal());
}
/**
* @test
*/
public function it_throws_exception_when_no_delayed_message_passed(): void
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Message is not a delayed message (instance of Prooph\ServiceBus\Message\HumusAmqp\DelayedMessage)');
$producer = $this->prophesize(Producer::class);
$messageConverter = $this->prophesize(MessageConverter::class);
$message = $this->prophesize(Message::class);
$messageProducer = new AmqpDelayedMessageProducer($producer->reveal(), $messageConverter->reveal());
$messageProducer($message->reveal());
}
}