-
-
Notifications
You must be signed in to change notification settings - Fork 224
/
Copy pathCatchUpHookErrorTest.php
201 lines (162 loc) · 9.12 KB
/
CatchUpHookErrorTest.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
<?php
declare(strict_types=1);
namespace Neos\ContentRepository\BehavioralTests\Tests\Functional\Subscription;
use Neos\ContentRepository\Core\Feature\ContentStreamCreation\Event\ContentStreamWasCreated;
use Neos\ContentRepository\Core\Projection\ProjectionStatus;
use Neos\ContentRepository\Core\Subscription\Exception\CatchUpFailed;
use Neos\ContentRepository\Core\Subscription\ProjectionSubscriptionStatus;
use Neos\ContentRepository\Core\Subscription\SubscriptionError;
use Neos\ContentRepository\Core\Subscription\SubscriptionId;
use Neos\ContentRepository\Core\Subscription\SubscriptionStatus;
use Neos\EventStore\Model\Event\SequenceNumber;
final class CatchUpHookErrorTest extends AbstractSubscriptionEngineTestCase
{
/** @test */
public function error_onBeforeEvent_projectionIsNotRun()
{
$this->eventStore->setup();
$this->fakeProjection->expects(self::once())->method('setUp');
$this->fakeProjection->expects(self::once())->method('apply');
$this->subscriptionEngine->setup();
$this->subscriptionEngine->boot();
// commit an event
$this->commitExampleContentStreamEvent();
$this->catchupHookForFakeProjection->expects(self::once())->method('onBeforeCatchUp')->with(SubscriptionStatus::ACTIVE);
// Todo test that onBeforeEvent|onAfterEvent are in the same transaction and that a rollback will also revert their state
$this->catchupHookForFakeProjection->expects(self::once())->method('onBeforeEvent')->with(self::isInstanceOf(ContentStreamWasCreated::class))->willThrowException(
$exception = new \RuntimeException('This catchup hook is kaputt.')
);
$this->catchupHookForFakeProjection->expects(self::never())->method('onAfterEvent');
$this->catchupHookForFakeProjection->expects(self::once())->method('onAfterCatchUp');
$this->secondFakeProjection->injectSaboteur(fn () => self::fail('Projection apply is not expected to be called!'));
$expectedFailure = ProjectionSubscriptionStatus::create(
subscriptionId: SubscriptionId::fromString('Vendor.Package:SecondFakeProjection'),
subscriptionStatus: SubscriptionStatus::ERROR,
subscriptionPosition: SequenceNumber::none(),
subscriptionError: SubscriptionError::fromPreviousStatusAndException(SubscriptionStatus::ACTIVE, $exception),
setupStatus: ProjectionStatus::ok(),
);
self::assertEmpty(
$this->secondFakeProjection->getState()->findAppliedSequenceNumbers()
);
$result = $this->subscriptionEngine->catchUpActive();
self::assertSame($result->errors?->first()->message, 'This catchup hook is kaputt.');
self::assertEquals(
$expectedFailure,
$this->subscriptionStatus('Vendor.Package:SecondFakeProjection')
);
// must be still empty because apply was never called
self::assertEmpty(
$this->secondFakeProjection->getState()->findAppliedSequenceNumbers()
);
}
/** @test */
public function error_onAfterEvent_projectionIsRolledBack()
{
$this->eventStore->setup();
$this->fakeProjection->expects(self::once())->method('setUp');
$this->fakeProjection->expects(self::once())->method('apply');
$this->subscriptionEngine->setup();
$this->subscriptionEngine->boot();
// commit an event
$this->commitExampleContentStreamEvent();
$this->catchupHookForFakeProjection->expects(self::once())->method('onBeforeCatchUp')->with(SubscriptionStatus::ACTIVE);
$this->catchupHookForFakeProjection->expects(self::once())->method('onBeforeEvent')->with(self::isInstanceOf(ContentStreamWasCreated::class));
$this->catchupHookForFakeProjection->expects(self::once())->method('onAfterEvent')->with(self::isInstanceOf(ContentStreamWasCreated::class))->willThrowException(
$exception = new \RuntimeException('This catchup hook is kaputt.')
);
// TODO pass the error subscription status to onAfterCatchUp, so that in case of an error it can be prevented that mails f.x. will be sent?
$this->catchupHookForFakeProjection->expects(self::once())->method('onAfterCatchUp');
$expectedFailure = ProjectionSubscriptionStatus::create(
subscriptionId: SubscriptionId::fromString('Vendor.Package:SecondFakeProjection'),
subscriptionStatus: SubscriptionStatus::ERROR,
subscriptionPosition: SequenceNumber::none(),
subscriptionError: SubscriptionError::fromPreviousStatusAndException(SubscriptionStatus::ACTIVE, $exception),
setupStatus: ProjectionStatus::ok(),
);
self::assertEmpty(
$this->secondFakeProjection->getState()->findAppliedSequenceNumbers()
);
$result = $this->subscriptionEngine->catchUpActive();
self::assertSame($result->errors?->first()->message, 'This catchup hook is kaputt.');
self::assertEquals(
$expectedFailure,
$this->subscriptionStatus('Vendor.Package:SecondFakeProjection')
);
// should be empty as we need an exact once delivery
self::assertEmpty(
$this->secondFakeProjection->getState()->findAppliedSequenceNumbers()
);
}
/** @test */
public function error_onBeforeCatchUp_abortsCatchup()
{
$this->eventStore->setup();
$this->fakeProjection->expects(self::once())->method('setUp');
$this->fakeProjection->expects(self::never())->method('apply');
$this->subscriptionEngine->setup();
$this->subscriptionEngine->boot();
$this->expectOkayStatus('Vendor.Package:SecondFakeProjection', SubscriptionStatus::ACTIVE, SequenceNumber::none());
// commit an event
$this->commitExampleContentStreamEvent();
$this->catchupHookForFakeProjection->expects(self::once())->method('onBeforeCatchUp')->with(SubscriptionStatus::ACTIVE)->willThrowException(
new \RuntimeException('This catchup hook is kaputt.')
);
$this->catchupHookForFakeProjection->expects(self::never())->method('onBeforeEvent');
$this->catchupHookForFakeProjection->expects(self::never())->method('onAfterEvent');
$this->catchupHookForFakeProjection->expects(self::never())->method('onAfterCatchUp');
$this->secondFakeProjection->injectSaboteur(fn () => self::fail('Projection apply is not expected to be called!'));
self::assertEmpty(
$this->secondFakeProjection->getState()->findAppliedSequenceNumbers()
);
$expectedFailure = null;
try {
$this->subscriptionEngine->catchUpActive();
} catch (\Throwable $e) {
$expectedFailure = $e;
}
self::assertInstanceOf(CatchUpFailed::class, $expectedFailure);
self::assertSame($expectedFailure->getMessage(), 'Subscriber "Vendor.Package:SecondFakeProjection" failed onBeforeCatchUp: This catchup hook is kaputt.');
// still the initial status
$this->expectOkayStatus('Vendor.Package:SecondFakeProjection', SubscriptionStatus::ACTIVE, SequenceNumber::none());
// must be still empty because apply was never called
self::assertEmpty(
$this->secondFakeProjection->getState()->findAppliedSequenceNumbers()
);
}
/** @test */
public function error_onAfterCatchUp_abortsCatchupAndRollBack()
{
$this->eventStore->setup();
$this->fakeProjection->expects(self::once())->method('setUp');
$this->fakeProjection->expects(self::once())->method('apply');
$this->subscriptionEngine->setup();
$this->subscriptionEngine->boot();
$this->expectOkayStatus('Vendor.Package:SecondFakeProjection', SubscriptionStatus::ACTIVE, SequenceNumber::none());
// commit an event
$this->commitExampleContentStreamEvent();
$this->catchupHookForFakeProjection->expects(self::once())->method('onBeforeCatchUp');
$this->catchupHookForFakeProjection->expects(self::once())->method('onBeforeEvent');
$this->catchupHookForFakeProjection->expects(self::once())->method('onAfterEvent');
$this->catchupHookForFakeProjection->expects(self::once())->method('onAfterCatchUp')->willThrowException(
new \RuntimeException('This catchup hook is kaputt.')
);
self::assertEmpty(
$this->secondFakeProjection->getState()->findAppliedSequenceNumbers()
);
$expectedFailure = null;
try {
$this->subscriptionEngine->catchUpActive();
} catch (\Throwable $e) {
$expectedFailure = $e;
}
self::assertInstanceOf(CatchUpFailed::class, $expectedFailure);
self::assertSame($expectedFailure->getMessage(), 'Subscriber "Vendor.Package:SecondFakeProjection" failed onAfterCatchUp: This catchup hook is kaputt.');
// still the initial status
$this->expectOkayStatus('Vendor.Package:SecondFakeProjection', SubscriptionStatus::ACTIVE, SequenceNumber::none());
// must be empty because full rollback
self::assertEmpty(
$this->secondFakeProjection->getState()->findAppliedSequenceNumbers()
);
}
}