|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Unit\Event; |
| 6 | + |
| 7 | +use InvalidArgumentException; |
| 8 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 9 | +use PHPUnit\Framework\Attributes\Medium; |
| 10 | +use PHPUnit\Framework\TestCase; |
| 11 | +use RuntimeException; |
| 12 | +use Wwwision\DCBEventStore\Event\EventData; |
| 13 | + |
| 14 | +#[Medium] |
| 15 | +#[CoversClass(EventData::class)] |
| 16 | +final class EventDataTest extends TestCase |
| 17 | +{ |
| 18 | + public function test_fromString_allows_empty_string(): void |
| 19 | + { |
| 20 | + $eventData = EventData::fromString(''); |
| 21 | + self::assertSame('', $eventData->value); |
| 22 | + } |
| 23 | + |
| 24 | + public function test_jsonDecode_fails_if_data_is_no_valid_json(): void |
| 25 | + { |
| 26 | + $eventData = EventData::fromString('not json'); |
| 27 | + $this->expectException(RuntimeException::class); |
| 28 | + $this->expectExceptionMessage('Failed to JSON-decode event data: Syntax error'); |
| 29 | + $eventData->jsonDecode(); |
| 30 | + } |
| 31 | + |
| 32 | + public function test_jsonDecode_fails_if_data_is_no_json_array(): void |
| 33 | + { |
| 34 | + $eventData = EventData::fromString('true'); |
| 35 | + $this->expectException(InvalidArgumentException::class); |
| 36 | + $this->expectExceptionMessage('Expected an array. Got: boolean'); |
| 37 | + $eventData->jsonDecode(); |
| 38 | + } |
| 39 | + |
| 40 | + public function test_jsonDecode_returns_encoded_array_data(): void |
| 41 | + { |
| 42 | + $data = ['foo' => 'bar', 'bar' => ['baz' => true, 'foos' => 123.45]]; |
| 43 | + $dataJson = json_encode($data, JSON_THROW_ON_ERROR); |
| 44 | + self::assertIsString($dataJson); |
| 45 | + $result = EventData::fromString($dataJson)->jsonDecode(); |
| 46 | + self::assertSame($data, $result); |
| 47 | + } |
| 48 | + |
| 49 | + public function test_json_represents_value(): void |
| 50 | + { |
| 51 | + $eventData = EventData::fromString('input'); |
| 52 | + self::assertSame('"input"', json_encode($eventData, JSON_THROW_ON_ERROR)); |
| 53 | + } |
| 54 | +} |
0 commit comments