|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tests\Unit\V2; |
| 6 | + |
| 7 | +use Tests\TestCase; |
| 8 | +use Workflow\Serializers\Serializer; |
| 9 | +use Workflow\V2\Support\CommandPayloadPreview; |
| 10 | + |
| 11 | +final class CommandPayloadPreviewTest extends TestCase |
| 12 | +{ |
| 13 | + public function testAvailableRejectsNonStringOrEmptyBlobs(): void |
| 14 | + { |
| 15 | + $this->assertFalse(CommandPayloadPreview::available(null)); |
| 16 | + $this->assertFalse(CommandPayloadPreview::available('')); |
| 17 | + $this->assertFalse(CommandPayloadPreview::available(['not', 'a', 'string'])); |
| 18 | + $this->assertTrue(CommandPayloadPreview::available('{}')); |
| 19 | + } |
| 20 | + |
| 21 | + public function testPreviewWithCodecDecodesJsonBlob(): void |
| 22 | + { |
| 23 | + $blob = Serializer::serializeWithCodec('json', ['name' => 'Taylor', 'n' => 7]); |
| 24 | + |
| 25 | + $this->assertSame( |
| 26 | + ['name' => 'Taylor', 'n' => 7], |
| 27 | + CommandPayloadPreview::previewWithCodec($blob, 'json'), |
| 28 | + ); |
| 29 | + } |
| 30 | + |
| 31 | + public function testPreviewWithCodecDecodesAvroWrappedBlob(): void |
| 32 | + { |
| 33 | + if (! class_exists(\Apache\Avro\Schema\AvroSchema::class)) { |
| 34 | + $this->markTestSkipped('apache/avro package is not installed in this environment.'); |
| 35 | + } |
| 36 | + |
| 37 | + $payload = ['name' => 'Taylor', 'count' => 3, 'tags' => ['priority', 'vip']]; |
| 38 | + $blob = Serializer::serializeWithCodec('avro', $payload); |
| 39 | + |
| 40 | + $this->assertSame( |
| 41 | + $payload, |
| 42 | + CommandPayloadPreview::previewWithCodec($blob, 'avro'), |
| 43 | + ); |
| 44 | + } |
| 45 | + |
| 46 | + public function testPreviewWithCodecFallsBackToRawBlobOnCodecMismatch(): void |
| 47 | + { |
| 48 | + if (! class_exists(\Apache\Avro\Schema\AvroSchema::class)) { |
| 49 | + $this->markTestSkipped('apache/avro package is not installed in this environment.'); |
| 50 | + } |
| 51 | + |
| 52 | + // Valid JSON bytes tagged as Avro — decode must fail safely and |
| 53 | + // return the raw blob instead of throwing. Strict mixed-codec |
| 54 | + // detection happens at ingress (PayloadEnvelopeResolver), not in |
| 55 | + // this display helper. |
| 56 | + $jsonBlob = Serializer::serializeWithCodec('json', ['hello']); |
| 57 | + |
| 58 | + $this->assertSame( |
| 59 | + $jsonBlob, |
| 60 | + CommandPayloadPreview::previewWithCodec($jsonBlob, 'avro'), |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + public function testPreviewWithCodecAcceptsLegacyCodecFqcnAliases(): void |
| 65 | + { |
| 66 | + $blob = Serializer::serializeWithCodec('workflow-serializer-y', ['a', 'b']); |
| 67 | + |
| 68 | + $this->assertSame( |
| 69 | + ['a', 'b'], |
| 70 | + CommandPayloadPreview::previewWithCodec($blob, \Workflow\Serializers\Y::class), |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + public function testPreviewWithCodecFallsThroughToLegacySniffWhenCodecNull(): void |
| 75 | + { |
| 76 | + $jsonBlob = Serializer::serializeWithCodec('json', ['legacy' => true]); |
| 77 | + |
| 78 | + $this->assertSame( |
| 79 | + ['legacy' => true], |
| 80 | + CommandPayloadPreview::previewWithCodec($jsonBlob, null), |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + public function testPreviewWithCodecReturnsNullForEmptyOrNonStringInput(): void |
| 85 | + { |
| 86 | + $this->assertNull(CommandPayloadPreview::previewWithCodec(null, 'avro')); |
| 87 | + $this->assertNull(CommandPayloadPreview::previewWithCodec('', 'avro')); |
| 88 | + $this->assertNull(CommandPayloadPreview::previewWithCodec(['x'], 'json')); |
| 89 | + } |
| 90 | + |
| 91 | + public function testPreviewWithCodecRendersAvroTypedRecordWhenSchemaContextIsSet(): void |
| 92 | + { |
| 93 | + if (! class_exists(\Apache\Avro\Schema\AvroSchema::class)) { |
| 94 | + $this->markTestSkipped('apache/avro package is not installed in this environment.'); |
| 95 | + } |
| 96 | + |
| 97 | + $schemaJson = '{"type":"record","name":"OrderPayload","namespace":"durable_workflow.test","fields":[' |
| 98 | + . '{"name":"order_id","type":"string"},' |
| 99 | + . '{"name":"amount","type":"double"},' |
| 100 | + . '{"name":"items_count","type":"int"}]}'; |
| 101 | + |
| 102 | + $schema = \Workflow\Serializers\Avro::parseSchema($schemaJson); |
| 103 | + |
| 104 | + \Workflow\Serializers\Avro::withSchema($schema); |
| 105 | + $blob = Serializer::serializeWithCodec('avro', [ |
| 106 | + 'order_id' => 'ord-42', |
| 107 | + 'amount' => 19.95, |
| 108 | + 'items_count' => 3, |
| 109 | + ]); |
| 110 | + |
| 111 | + \Workflow\Serializers\Avro::withSchema($schema); |
| 112 | + $decoded = CommandPayloadPreview::previewWithCodec($blob, 'avro'); |
| 113 | + |
| 114 | + $this->assertIsArray($decoded); |
| 115 | + $this->assertSame('ord-42', $decoded['order_id']); |
| 116 | + $this->assertSame(19.95, $decoded['amount']); |
| 117 | + $this->assertSame(3, $decoded['items_count']); |
| 118 | + } |
| 119 | +} |
0 commit comments