forked from thenativeweb/eventsourcingdb-client-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCloudEventTest.php
More file actions
executable file
·86 lines (70 loc) · 2.53 KB
/
CloudEventTest.php
File metadata and controls
executable file
·86 lines (70 loc) · 2.53 KB
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
<?php
declare(strict_types=1);
namespace Thenativeweb\Eventsourcingdb\Tests;
use PHPUnit\Framework\TestCase;
use RuntimeException;
use Thenativeweb\Eventsourcingdb\CloudEvent;
use Thenativeweb\Eventsourcingdb\EventCandidate;
use Thenativeweb\Eventsourcingdb\Tests\Trait\ClientTestTrait;
use Thenativeweb\Eventsourcingdb\Tests\Trait\ReflectionTestTrait;
final class CloudEventTest extends TestCase
{
use ClientTestTrait;
use ReflectionTestTrait;
public function testVerifiesTheEventHash(): void
{
$eventCandidate = new EventCandidate(
source: 'https://www.eventsourcingdb.io',
subject: '/test',
type: 'io.eventsourcingdb.test',
data: [
'value' => 23,
],
);
$writtenEvents = $this->client->writeEvents([
$eventCandidate,
]);
$this->assertCount(1, $writtenEvents);
$writtenEvent = $writtenEvents[0];
try {
$writtenEvent->verifyHash();
} catch (RuntimeException $runtimeException) {
$this->fail($runtimeException->getMessage());
}
}
public function testThrowsAnErrorIfTheEventHashIsInvalid(): void
{
$eventCandidate = new EventCandidate(
source: 'https://www.eventsourcingdb.io',
subject: '/test',
type: 'io.eventsourcingdb.test',
data: [
'value' => 23,
],
);
$writtenEvents = $this->client->writeEvents([
$eventCandidate,
]);
$this->assertCount(1, $writtenEvents);
$writtenEvent = $writtenEvents[0];
$tamperedCloudEvent = new CloudEvent(
specVersion: $writtenEvent->specVersion,
id: $writtenEvent->id,
time: $writtenEvent->time,
timeFromServer: $this->getPropertyValue($writtenEvent, 'timeFromServer'),
source: $writtenEvent->source,
subject: $writtenEvent->subject,
type: $writtenEvent->type,
dataContentType: $writtenEvent->dataContentType,
data: $writtenEvent->data,
hash: hash('sha256', 'invalid hash'),
predecessorHash: $writtenEvent->predecessorHash,
traceParent: $writtenEvent->traceParent,
traceState: $writtenEvent->traceState,
signature: $writtenEvent->signature,
);
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Failed to verify hash');
$tamperedCloudEvent->verifyHash();
}
}