1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* This file is part of the daikon-cqrs/event-sourcing project. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Daikon\Tests\EventSourcing; |
10
|
|
|
|
11
|
|
|
use ArrayIterator; |
12
|
|
|
use Daikon\EventSourcing\Aggregate\AggregateRootInterface; |
13
|
|
|
use Daikon\EventSourcing\Aggregate\Command\CommandHandler; |
14
|
|
|
use Daikon\EventSourcing\Aggregate\Command\CommandInterface; |
15
|
|
|
use Daikon\EventSourcing\EventStore\Commit\CommitInterface; |
16
|
|
|
use Daikon\EventSourcing\EventStore\Commit\CommitSequenceInterface; |
17
|
|
|
use Daikon\EventSourcing\EventStore\UnitOfWorkInterface; |
18
|
|
|
use Daikon\MessageBus\EnvelopeInterface; |
19
|
|
|
use Daikon\MessageBus\MessageBusInterface; |
20
|
|
|
use Daikon\Metadata\Metadata; |
21
|
|
|
use PHPUnit\Framework\TestCase; |
22
|
|
|
|
23
|
|
|
final class CommandHandlerTest extends TestCase |
24
|
|
|
{ |
25
|
1 |
|
public function testHandleNewAggregate(): void |
26
|
|
|
{ |
27
|
1 |
|
$commitStub = $this->createMock(CommitInterface::class); |
28
|
1 |
|
$commitSequenceStub = $this->createMock(CommitSequenceInterface::class); |
29
|
|
|
$commitSequenceStub |
30
|
1 |
|
->expects($this->once()) |
31
|
1 |
|
->method('getIterator') |
32
|
1 |
|
->willReturn(new ArrayIterator([$commitStub])); |
33
|
|
|
|
34
|
1 |
|
$unitOfWorkStub = $this->createMock(UnitOfWorkInterface::class); |
35
|
|
|
$unitOfWorkStub |
36
|
1 |
|
->expects($this->once()) |
37
|
1 |
|
->method('commit') |
38
|
1 |
|
->with($this->isInstanceOf(AggregateRootInterface::class)) |
39
|
1 |
|
->willReturn($commitSequenceStub); |
40
|
|
|
|
41
|
1 |
|
$messageBusStub = $this->createMock(MessageBusInterface::class); |
42
|
|
|
$messageBusStub |
43
|
1 |
|
->expects($this->once()) |
44
|
1 |
|
->method('publish') |
45
|
1 |
|
->with($commitStub, 'commits'); |
46
|
|
|
|
47
|
1 |
|
$envelopeStub = $this->createMock(EnvelopeInterface::class); |
48
|
|
|
$envelopeStub |
49
|
1 |
|
->expects($this->once()) |
50
|
1 |
|
->method('getMetadata') |
51
|
1 |
|
->willReturn(Metadata::makeEmpty()); |
52
|
|
|
$envelopeStub |
53
|
1 |
|
->expects($this->once()) |
54
|
1 |
|
->method('getMessage') |
55
|
1 |
|
->willReturn( |
56
|
1 |
|
$this->getMockBuilder(CommandInterface::class)->setMockClassName('FooBar')->getMock() |
57
|
|
|
); |
58
|
|
|
|
59
|
1 |
|
$commandHandler = $this->getMockBuilder(CommandHandler::class) |
60
|
1 |
|
->setConstructorArgs([$unitOfWorkStub, $messageBusStub]) |
61
|
1 |
|
->addMethods(['handleFooBar']) |
62
|
1 |
|
->getMock(); |
63
|
|
|
$commandHandler |
64
|
1 |
|
->expects($this->once()) |
65
|
1 |
|
->method('handleFooBar') |
66
|
1 |
|
->willReturn([$this->createMock(AggregateRootInterface::class), Metadata::makeEmpty()]); |
67
|
|
|
|
68
|
|
|
/** @psalm-suppress UndefinedInterfaceMethod */ |
69
|
1 |
|
$commandHandler->handle($envelopeStub); |
70
|
1 |
|
} |
71
|
|
|
} |
72
|
|
|
|