-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6c6317f
commit 254d3f1
Showing
10 changed files
with
149 additions
and
294 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing\EventBus\Serializer; | ||
|
||
use Patchlevel\EventSourcing\EventBus\Message; | ||
|
||
use function base64_decode; | ||
use function base64_encode; | ||
use function serialize; | ||
use function unserialize; | ||
|
||
final class PhpNativeMessageSerializer implements MessageSerializer | ||
{ | ||
public function serialize(Message $message): string | ||
{ | ||
return base64_encode(serialize($message)); | ||
} | ||
|
||
public function deserialize(string $content): Message | ||
Check failure on line 21 in src/EventBus/Serializer/PhpNativeMessageSerializer.php GitHub Actions / Static Analysis by Psalm (locked, 8.3, ubuntu-latest)MixedInferredReturnType
|
||
{ | ||
return unserialize( | ||
Check failure on line 23 in src/EventBus/Serializer/PhpNativeMessageSerializer.php GitHub Actions / Static Analysis by Psalm (locked, 8.3, ubuntu-latest)MixedReturnStatement
Check failure on line 23 in src/EventBus/Serializer/PhpNativeMessageSerializer.php GitHub Actions / Static Analysis by PHPStan (locked, 8.3, ubuntu-latest)
|
||
base64_decode($content), | ||
['allowed_classes' => true], | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
tests/Unit/EventBus/Serializer/PhpNativeMessageSerializerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing\Tests\Unit\EventBus\Serializer; | ||
|
||
use DateTimeImmutable; | ||
use Patchlevel\EventSourcing\EventBus\Message; | ||
use Patchlevel\EventSourcing\EventBus\Serializer\PhpNativeMessageSerializer; | ||
use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileId; | ||
use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileVisited; | ||
use PHPUnit\Framework\TestCase; | ||
use Prophecy\PhpUnit\ProphecyTrait; | ||
|
||
/** @covers \Patchlevel\EventSourcing\EventBus\Serializer\PhpNativeMessageSerializer */ | ||
final class PhpNativeMessageSerializerTest extends TestCase | ||
{ | ||
use ProphecyTrait; | ||
|
||
public function testSerialize(): void | ||
{ | ||
$event = new ProfileVisited( | ||
ProfileId::fromString('foo'), | ||
); | ||
|
||
$message = Message::create($event) | ||
->withRecordedOn(new DateTimeImmutable('2020-01-01T20:00:00.000000+0100')); | ||
|
||
$nativeSerializer = new PhpNativeMessageSerializer(); | ||
|
||
$content = $nativeSerializer->serialize($message); | ||
|
||
self::assertEquals('Tzo0MToiUGF0Y2hsZXZlbFxFdmVudFNvdXJjaW5nXEV2ZW50QnVzXE1lc3NhZ2UiOjg6e3M6NTY6IgBQYXRjaGxldmVsXEV2Z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V2ZW50U291cmNpbmdcVGVzdHNcVW5pdFxGaXh0dXJlXFByb2ZpbGVJZABpZCI7czozOiJmb28iO319fQ==', $content); | ||
} | ||
|
||
public function testDeserialize(): void | ||
{ | ||
$event = new ProfileVisited( | ||
ProfileId::fromString('foo'), | ||
); | ||
|
||
$nativeSerializer = new PhpNativeMessageSerializer(); | ||
|
||
$message = $nativeSerializer->deserialize('Tzo0MToiUGF0Y2hsZXZlbFxFdmVudFNvdXJjaW5nXEV2ZW50QnVzXE1lc3NhZ2UiOjg6e3M6NTY6IgBQYXRjaGxldmVsXEV2ZW50U291cmNpbmdcRXZlbnRCdXNcTWVzc2FnZQBhZ2dyZWdhdGVOYW1lIjtOO3M6NTQ6IgBQYXRjaGxldmVsXEV2ZW50U291cmNpbmdcRXZlbnRCdXNcTWVzc2FnZQBhZ2dyZWdhdGVJZCI7TjtzOjUxOiIAUGF0Y2hsZXZlbFxFdmVudFNvdXJjaW5nXEV2ZW50QnVzXE1lc3NhZ2UAcGxheWhlYWQiO047czo1MzoiAFBhdGNobGV2ZWxcRXZlbnRTb3VyY2luZ1xFdmVudEJ1c1xNZXNzYWdlAHJlY29yZGVkT24iO086MTc6IkRhdGVUaW1lSW1tdXRhYmxlIjozOntzOjQ6ImRhdGUiO3M6MjY6IjIwMjAtMDEtMDEgMjA6MDA6MDAuMDAwMDAwIjtzOjEzOiJ0aW1lem9uZV90eXBlIjtpOjE7czo4OiJ0aW1lem9uZSI7czo2OiIrMDE6MDAiO31zOjU3OiIAUGF0Y2hsZXZlbFxFdmVudFNvdXJjaW5nXEV2ZW50QnVzXE1lc3NhZ2UAbmV3U3RyZWFtU3RhcnQiO2I6MDtzOjUxOiIAUGF0Y2hsZXZlbFxFdmVudFNvdXJjaW5nXEV2ZW50QnVzXE1lc3NhZ2UAYXJjaGl2ZWQiO2I6MDtzOjU2OiIAUGF0Y2hsZXZlbFxFdmVudFNvdXJjaW5nXEV2ZW50QnVzXE1lc3NhZ2UAY3VzdG9tSGVhZGVycyI7YTowOnt9czo0ODoiAFBhdGNobGV2ZWxcRXZlbnRTb3VyY2luZ1xFdmVudEJ1c1xNZXNzYWdlAGV2ZW50IjtPOjU4OiJQYXRjaGxldmVsXEV2ZW50U291cmNpbmdcVGVzdHNcVW5pdFxGaXh0dXJlXFByb2ZpbGVWaXNpdGVkIjoxOntzOjk6InZpc2l0b3JJZCI7Tzo1MzoiUGF0Y2hsZXZlbFxFdmVudFNvdXJjaW5nXFRlc3RzXFVuaXRcRml4dHVyZVxQcm9maWxlSWQiOjE6e3M6NTc6IgBQYXRjaGxldmVsXEV2ZW50U291cmNpbmdcVGVzdHNcVW5pdFxGaXh0dXJlXFByb2ZpbGVJZABpZCI7czozOiJmb28iO319fQ=='); | ||
|
||
self::assertEquals($event, $message->event()); | ||
self::assertEquals([ | ||
'recordedOn' => new DateTimeImmutable('2020-01-01T20:00:00.000000+0100'), | ||
'newStreamStart' => false, | ||
'archived' => false, | ||
], $message->headers()); | ||
} | ||
|
||
public function testEquals(): void | ||
{ | ||
$event = new ProfileVisited( | ||
ProfileId::fromString('foo'), | ||
); | ||
|
||
$message = Message::create($event) | ||
->withRecordedOn(new DateTimeImmutable('2020-01-01T20:00:00.000000+0100')); | ||
|
||
$nativeSerializer = new PhpNativeMessageSerializer(); | ||
|
||
$content = $nativeSerializer->serialize($message); | ||
$clonedMessage = $nativeSerializer->deserialize($content); | ||
|
||
self::assertEquals($message, $clonedMessage); | ||
} | ||
} |
Oops, something went wrong.