Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes problem with sending bool, int and float variable types #201

Merged
merged 2 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fixes problem with sending bool, int and float variable types
  • Loading branch information
butschster committed Jun 10, 2024
commit 3a8d41802dff2e98ddca277c7d9886259de262e1
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
6 changes: 3 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,11 @@ public function getValue(): string

public function __toString(): string
{
return $this->value;
return (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
63 changes: 27 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,53 @@
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 '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>#208</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) {
$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
Loading