-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOutboxMessageDispatcherTest.php
67 lines (55 loc) · 1.94 KB
/
OutboxMessageDispatcherTest.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
<?php
namespace EventSauce\MessageOutbox;
use EventSauce\EventSourcing\DefaultHeadersDecorator;
use EventSauce\EventSourcing\Message;
use EventSauce\EventSourcing\UnableToDispatchMessages;
use PHPUnit\Framework\TestCase;
use RuntimeException;
use function iterator_to_array;
class OutboxMessageDispatcherTest extends TestCase
{
/**
* @test
*/
public function dispatched_messages_end_up_in_the_outbox(): void
{
// Arrange
$repository = new InMemoryOutboxRepository();
$dispatcher = new OutboxMessageDispatcher($repository);
$message1 = $this->createMessage('one');
$message2 = $this->createMessage('two');
// Act
$dispatcher->dispatch($message1, $message2);
// Assert
/** @var list<Message> $messages */
$messages = iterator_to_array($repository->retrieveBatch(10));
$this->assertCount(2, $messages);
$this->assertEquals($message1->event(), $messages[0]->event());
$this->assertEquals($message2->event(), $messages[1]->event());
}
/**
* @test
*/
public function when_storing_fails_we_signal_we_are_unable_to_dispatch_the_messages(): void
{
// Arrange
$repository = new class () extends InMemoryOutboxRepository{
public function persist(Message ...$messages): void
{
throw new RuntimeException('Unable to store the messages');
}
};
$dispatcher = new OutboxMessageDispatcher($repository);
$message1 = $this->createMessage('one');
$message2 = $this->createMessage('two');
// Expect
$this->expectException(UnableToDispatchMessages::class);
// Act
$dispatcher->dispatch($message1, $message2);
}
private function createMessage(string $value): Message
{
$message = new Message(new DummyEvent($value));
return (new DefaultHeadersDecorator())->decorate($message);
}
}