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\Event\DomainEventSequenceInterface; |
13
|
|
|
use Daikon\Interop\InvalidArgumentException; |
14
|
|
|
use Daikon\Tests\EventSourcing\Aggregate\Mock\BakePizza; |
15
|
|
|
use Daikon\Tests\EventSourcing\Aggregate\Mock\Pizza; |
16
|
|
|
use Daikon\Tests\EventSourcing\Aggregate\Mock\PizzaId; |
17
|
|
|
use Daikon\Tests\EventSourcing\Aggregate\Mock\PizzaWasBaked; |
18
|
|
|
use PHPUnit\Framework\TestCase; |
19
|
|
|
|
20
|
|
|
final class AggregateRootTest extends TestCase |
21
|
|
|
{ |
22
|
1 |
|
public function testStartAggregateRootLifecycle(): void |
23
|
|
|
{ |
24
|
1 |
|
$ingredients = ['mushrooms', 'tomatoes', 'onions']; |
25
|
|
|
/** @var BakePizza $bakePizza */ |
26
|
1 |
|
$bakePizza = BakePizza::fromNative([ |
27
|
1 |
|
'pizzaId' => 'pizza-42-6-23', |
28
|
1 |
|
'ingredients' => $ingredients |
29
|
|
|
]); |
30
|
1 |
|
$pizza = Pizza::bake(PizzaWasBaked::fromCommand($bakePizza)); |
31
|
|
|
|
32
|
1 |
|
$this->assertEquals('pizza-42-6-23', $pizza->getIdentifier()); |
33
|
1 |
|
$this->assertEquals(1, $pizza->getRevision()->toNative()); |
34
|
1 |
|
$this->assertEquals($ingredients, $pizza->getIngredients()->toNative()); |
35
|
1 |
|
$this->assertCount(1, $pizza->getTrackedEvents()); |
36
|
1 |
|
} |
37
|
|
|
|
38
|
1 |
|
public function testReconstituteFromHistory(): void |
39
|
|
|
{ |
40
|
1 |
|
$pizzaId = PizzaId::fromNative('pizza-42-6-23'); |
41
|
1 |
|
$ingredients = ['mushrooms', 'tomatoes', 'onions']; |
42
|
1 |
|
$pizzaWasBaked = PizzaWasBaked::fromNative([ |
43
|
1 |
|
'pizzaId' => (string)$pizzaId, |
44
|
1 |
|
'revision' => 1, |
45
|
1 |
|
'ingredients' => $ingredients |
46
|
|
|
]); |
47
|
|
|
|
48
|
1 |
|
$domainEventSequenceMock = $this->createMock(DomainEventSequenceInterface::class); |
49
|
|
|
$domainEventSequenceMock |
50
|
1 |
|
->expects($this->once()) |
51
|
1 |
|
->method('getIterator') |
52
|
1 |
|
->willReturn(new ArrayIterator([$pizzaWasBaked])); |
53
|
|
|
|
54
|
|
|
/** @var DomainEventSequenceInterface $domainEventSequenceMock */ |
55
|
1 |
|
$pizza = Pizza::reconstituteFromHistory($pizzaId, $domainEventSequenceMock); |
56
|
|
|
|
57
|
1 |
|
$this->assertEquals($pizzaId, $pizza->getIdentifier()); |
58
|
1 |
|
$this->assertEquals(1, $pizza->getRevision()->toNative()); |
59
|
1 |
|
$this->assertEquals($ingredients, $pizza->getIngredients()->toNative()); |
60
|
1 |
|
$this->assertCount(0, $pizza->getTrackedEvents()); |
61
|
1 |
|
} |
62
|
|
|
|
63
|
1 |
|
public function testReconstituteWithUnexpectedRevision(): void |
64
|
|
|
{ |
65
|
1 |
|
$this->expectException(InvalidArgumentException::class); |
66
|
1 |
|
$this->expectExceptionMessage("Given event revision '2' does not match expected AR revision at '1'."); |
67
|
|
|
|
68
|
1 |
|
$pizzaId = PizzaId::fromNative('pizza-42-6-23'); |
69
|
1 |
|
$pizzaWasBaked = PizzaWasBaked::fromNative([ |
70
|
1 |
|
'pizzaId' => (string)$pizzaId, |
71
|
1 |
|
'revision' => 2, // unexpected revision will trigger an error |
72
|
|
|
'ingredients' => [] |
73
|
|
|
]); |
74
|
|
|
|
75
|
1 |
|
$domainEventSequenceMock = $this->createMock(DomainEventSequenceInterface::class); |
76
|
|
|
$domainEventSequenceMock |
77
|
1 |
|
->expects($this->once()) |
78
|
1 |
|
->method('getIterator') |
79
|
1 |
|
->willReturn(new ArrayIterator([$pizzaWasBaked])); |
80
|
|
|
|
81
|
|
|
/** @var DomainEventSequenceInterface $domainEventSequenceMock */ |
82
|
1 |
|
Pizza::reconstituteFromHistory($pizzaId, $domainEventSequenceMock); |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
public function testReconstituteWithUnexpectedId(): void |
86
|
|
|
{ |
87
|
1 |
|
$this->expectException(InvalidArgumentException::class); |
88
|
1 |
|
$this->expectExceptionMessage( |
89
|
1 |
|
"Given event identifier 'pizza-23-22-5' does not match expected AR identifier at 'pizza-42-6-23'." |
90
|
|
|
); |
91
|
|
|
|
92
|
1 |
|
$pizzaId = PizzaId::fromNative('pizza-42-6-23'); |
93
|
1 |
|
$pizzaWasBaked = PizzaWasBaked::fromNative([ |
94
|
1 |
|
'pizzaId' => 'pizza-23-22-5', // unexpected id will trigger an error |
95
|
|
|
'revision' => 1, |
96
|
|
|
'ingredients' => [] |
97
|
|
|
]); |
98
|
|
|
|
99
|
1 |
|
$domainEventSequenceMock = $this->createMock(DomainEventSequenceInterface::class); |
100
|
|
|
$domainEventSequenceMock |
101
|
1 |
|
->expects($this->once()) |
102
|
1 |
|
->method('getIterator') |
103
|
1 |
|
->willReturn(new ArrayIterator([$pizzaWasBaked])); |
104
|
|
|
|
105
|
|
|
/** @var DomainEventSequenceInterface $domainEventSequenceMock */ |
106
|
1 |
|
Pizza::reconstituteFromHistory($pizzaId, $domainEventSequenceMock); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|