diff --git a/app/modules/VarDumper/Application/Dump/MessageParser.php b/app/modules/VarDumper/Application/Dump/MessageParser.php index 94a676a..d4aa1ac 100644 --- a/app/modules/VarDumper/Application/Dump/MessageParser.php +++ b/app/modules/VarDumper/Application/Dump/MessageParser.php @@ -12,10 +12,15 @@ final class MessageParser { /** * @throws \RuntimeException + * @throws InvalidPayloadException */ public function parse(string $message): ParsedPayload { - $payload = @\unserialize(\base64_decode($message), ['allowed_classes' => [Data::class, Stub::class]]); + try { + $payload = \unserialize(\base64_decode($message), ['allowed_classes' => [Data::class, Stub::class]]); + } catch (\Throwable) { + $payload = false; + } // Impossible to decode the message, give up. if (false === $payload) { diff --git a/app/modules/VarDumper/Application/Dump/PrimitiveBody.php b/app/modules/VarDumper/Application/Dump/PrimitiveBody.php index 086029a..8d22eac 100644 --- a/app/modules/VarDumper/Application/Dump/PrimitiveBody.php +++ b/app/modules/VarDumper/Application/Dump/PrimitiveBody.php @@ -8,7 +8,7 @@ { public function __construct( private string $type, - private string $value, + private mixed $value, ) {} public function getType(): string @@ -23,11 +23,15 @@ public function getValue(): string public function __toString(): string { - return $this->value; + return match (true) { + $this->value === true => '1', + $this->value === false => '0', + default => (string) $this->value, + }; } public function jsonSerialize(): string { - return $this->__toString(); + return (string) $this; } } diff --git a/app/modules/VarDumper/Interfaces/TCP/Service.php b/app/modules/VarDumper/Interfaces/TCP/Service.php index e7a124e..5dbd224 100644 --- a/app/modules/VarDumper/Interfaces/TCP/Service.php +++ b/app/modules/VarDumper/Interfaces/TCP/Service.php @@ -60,7 +60,7 @@ private function fireEvent(ParsedPayload $payload): void private function convertToPrimitive(Data $data): BodyInterface|null { - if (\in_array($data->getType(), ['string', 'boolean'])) { + if (\in_array($data->getType(), ['string', 'boolean', 'integer', 'double'])) { return new PrimitiveBody( type: $data->getType(), value: $data->getValue(), diff --git a/tests/Feature/Interfaces/TCP/VarDumper/SymfonyV7Test.php b/tests/Feature/Interfaces/TCP/VarDumper/SymfonyV7Test.php index 6edeb8b..abd7522 100644 --- a/tests/Feature/Interfaces/TCP/VarDumper/SymfonyV7Test.php +++ b/tests/Feature/Interfaces/TCP/VarDumper/SymfonyV7Test.php @@ -7,62 +7,67 @@ use App\Application\Broadcasting\Channel\EventsChannel; use Modules\VarDumper\Application\Dump\DumpIdGeneratorInterface; use Modules\VarDumper\Exception\InvalidPayloadException; +use PHPUnit\Framework\Attributes\DataProvider; use Symfony\Component\VarDumper\Caster\ReflectionCaster; use Symfony\Component\VarDumper\Cloner\VarCloner; use Tests\Feature\Interfaces\TCP\TCPTestCase; final class SymfonyV7Test extends TCPTestCase { - public function testSendDump(): void + public static function variablesDataProvider(): iterable { - $message = $this->buildPayload(var: 'foo'); - $this->handleVarDumperRequest($message); - - $this->broadcastig->assertPushed(new EventsChannel(), function (array $data) { - $this->assertSame('event.received', $data['event']); - $this->assertSame('var-dump', $data['data']['type']); - - $this->assertSame([ - 'type' => 'string', - 'value' => 'foo', - 'label' => 'Some label', - ], $data['data']['payload']['payload']); + yield 'string' => ['foo', 'string', 'foo']; + yield 'true' => [true, 'boolean', '1']; + yield 'false' => [false, 'boolean', '0']; + yield 'int' => [1, 'integer', '1']; + yield 'float' => [1.1, 'double', '1.1']; + yield 'array' => [ + ['foo' => 'bar'], + 'array', + <<<'HTML' +
Some label array:1 [
+  "foo" => "bar"
+]
+
- $this->assertNotEmpty($data['data']['uuid']); - $this->assertNotEmpty($data['data']['timestamp']); +HTML + , + ]; + yield 'object' => [ + (object) ['type' => 'string', 'value' => 'foo'], + 'stdClass', + <<Some label {#%s + +"type": "string" + +"value": "foo" +} + - return true; - }); +HTML + , + ]; } - public function testSendObjectDump(): void + #[DataProvider('variablesDataProvider')] + public function testSendDump(mixed $value, string $type, mixed $expected): void { $generator = $this->mockContainer(DumpIdGeneratorInterface::class); - $generator->shouldReceive('generate')->andReturn('sf-dump-730421088'); - $object = (object) ['type' => 'string', 'value' => 'foo']; - $message = $this->buildPayload($object); + + $message = $this->buildPayload(var: $value); $this->handleVarDumperRequest($message); - $objectId = \spl_object_id($object); - $this->broadcastig->assertPushed(new EventsChannel(), function (array $data) use ($objectId) { + if (\is_object($value)) { + $expected = \sprintf($expected, \spl_object_id($value)); + } + + $this->broadcastig->assertPushed(new EventsChannel(), function (array $data) use ($value, $type, $expected) { $this->assertSame('event.received', $data['event']); $this->assertSame('var-dump', $data['data']['type']); - $this->assertSame(null, $data['data']['project']); $this->assertSame([ - 'type' => 'stdClass', - 'value' => \sprintf( - <<Some label {#%s - +"type": "string" - +"value": "foo" -} - - -HTML, - $objectId, - ), + 'type' => $type, + 'value' => $expected, 'label' => 'Some label', ], $data['data']['payload']['payload']);