Skip to content

Commit

Permalink
Merge pull request #201 from buggregator/hotfix/var-dumper-types
Browse files Browse the repository at this point in the history
Fixes problem with sending `bool`, `int` and `float` variable types
  • Loading branch information
butschster committed Jun 10, 2024
2 parents 5ea86e4 + 47990fa commit 1bf2d85
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 41 deletions.
7 changes: 6 additions & 1 deletion app/modules/VarDumper/Application/Dump/MessageParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
10 changes: 7 additions & 3 deletions app/modules/VarDumper/Application/Dump/PrimitiveBody.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
{
public function __construct(
private string $type,
private string $value,
private mixed $value,
) {}

public function getType(): string
Expand All @@ -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;
}
}
2 changes: 1 addition & 1 deletion app/modules/VarDumper/Interfaces/TCP/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
77 changes: 41 additions & 36 deletions tests/Feature/Interfaces/TCP/VarDumper/SymfonyV7Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
<pre class=sf-dump id=sf-dump-730421088 data-indent-pad=" "><span class=sf-dump-label>Some label</span> <span class=sf-dump-note>array:1</span> [<samp data-depth=1 class=sf-dump-expanded>
"<span class=sf-dump-key>foo</span>" => "<span class=sf-dump-str>bar</span>"
</samp>]
</pre><script>Sfdump("sf-dump-730421088")</script>
$this->assertNotEmpty($data['data']['uuid']);
$this->assertNotEmpty($data['data']['timestamp']);
HTML
,
];
yield 'object' => [
(object) ['type' => 'string', 'value' => 'foo'],
'stdClass',
<<<HTML
<pre class=sf-dump id=sf-dump-730421088 data-indent-pad=" "><span class=sf-dump-label>Some label</span> {<a class=sf-dump-ref>#%s</a><samp data-depth=1 class=sf-dump-expanded>
+"<span class=sf-dump-public>type</span>": "<span class=sf-dump-str>string</span>"
+"<span class=sf-dump-public>value</span>": "<span class=sf-dump-str>foo</span>"
</samp>}
</pre><script>Sfdump("sf-dump-730421088")</script>
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(
<<<HTML
<pre class=sf-dump id=sf-dump-730421088 data-indent-pad=" "><span class=sf-dump-label>Some label</span> {<a class=sf-dump-ref>#%s</a><samp data-depth=1 class=sf-dump-expanded>
+"<span class=sf-dump-public>type</span>": "<span class=sf-dump-str>string</span>"
+"<span class=sf-dump-public>value</span>": "<span class=sf-dump-str>foo</span>"
</samp>}
</pre><script>Sfdump("sf-dump-730421088")</script>
HTML,
$objectId,
),
'type' => $type,
'value' => $expected,
'label' => 'Some label',
], $data['data']['payload']['payload']);

Expand Down

0 comments on commit 1bf2d85

Please sign in to comment.