8000 Add functional test for ServerDumpCommand · symfony/symfony@69e6ce6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 69e6ce6

Browse files
Add functional test for ServerDumpCommand
1 parent 9549cc2 commit 69e6ce6

File tree

3 files changed

+61
-30
lines changed

3 files changed

+61
-30
lines changed

src/Symfony/Component/VarDumper/Command/ServerDumpCommand.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Symfony\Component\Console\Output\OutputInterface;
2222
use Symfony\Component\Console\Style\SymfonyStyle;
2323
use Symfony\Component\VarDumper\Cloner\Data;
24+
use Symfony\Component\VarDumper\Cloner\Stub;
2425
use Symfony\Component\VarDumper\Command\Descriptor\CliDescriptor;
2526
use Symfony\Component\VarDumper\Command\Descriptor\DumpDescriptorInterface;
2627
use Symfony\Component\VarDumper\Command\Descriptor\HtmlDescriptor;
@@ -85,15 +86,25 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8586

8687
$errorIo = $io->getErrorStyle();
8788
$errorIo->title('Symfony Var Dumper Server');
88-
89-
$this->server->start();
90-
9189
$errorIo->success(sprintf('Server listening on %s', $this->server->getHost()));
9290
$errorIo->comment('Quit the server with CONTROL-C.');
93-
94-
$this->server->listen(function (Data $data, array $context, int $clientId) use ($descriptor, $io) {
95-
$descriptor->describe($io, $data, $context, $clientId);
96-
});
91+
$this->server->listen(
92+
function (int $clientId, string $message) use ($descriptor, $io) {
93+
$payload = @unserialize(base64_decode($message), ['allowed_classes' => [Data::class, Stub::class]]);
94+
95+
// Impossible to decode the message, give up.
96+
if (false === $payload) {
97+
return;
98+
}
99+
100+
if (!\is_array($payload) || \count($payload) < 2 || !$payload[0] instanceof Data || !\is_array($payload[1])) {
101+
return;
102+
}
103+
[$data, $context] = $payload;
104+
$descriptor->describe($io, $data, $context, $clientId);
105+
},
106+
$input,
107+
);
97108

98109
return 0;
99110
}

src/Symfony/Component/VarDumper/Server/DumpServer.php

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\VarDumper\Server;
1313

1414
use Psr\Log\LoggerInterface;
15+
use Symfony\Component\Console\Input\StreamableInputInterface;
1516
use Symfony\Component\VarDumper\Cloner\Data;
1617
use Symfony\Component\VarDumper\Cloner\Stub;
1718

@@ -49,33 +50,20 @@ public function start(): void
4950
}
5051
}
5152

52-
public function listen(callable $callback): void
53+
public function listen(callable $callback, ?StreamableInputInterface $streamInput = null): void
5354
{
54-
if (null === $this->socket) {
55+
$inputStream = null;
56+
57+
if ($streamInput instanceof StreamableInputInterface && $stream = $streamInput->getStream()) {
58+
$inputStream = $stream;
59+
} elseif (null === $this->socket) {
5560
$this->start();
5661
}
5762

58-
foreach ($this->getMessages() as $clientId => $message) {
63+
foreach ($this->getMessages($inputStream) as $clientId => $message) {
5964
$this->logger?->info('Received a payload from client {clientId}', ['clientId' => $clientId]);
6065

61-
$payload = @unserialize(base64_decode($message), ['allowed_classes' => [Data::class, Stub::class]]);
62-
63-
// Impossible to decode the message, give up.
64-
if (false === $payload) {
65-
$this->logger?->warning('Unable to decode a message from {clientId} client.', ['clientId' => $clientId]);
66-
67-
continue;
68-
}
69-
70-
if (!\is_array($payload) || \count($payload) < 2 || !$payload[0] instanceof Data || !\is_array($payload[1])) {
71-
$this->logger?->warning('Invalid payload from {clientId} client. Expected an array of two elements (Data $data, array $context)', ['clientId' => $clientId]);
72-
73-
continue;
74-
}
75-
76-
[$data, $context] = $payload;
77-
78-
$callback($data, $context, $clientId);
66+
$callback($clientId, $message);
7967
}
8068
}
8169

@@ -84,8 +72,20 @@ public function getHost(): string
8472
return $this->host;
8573
}
8674

87-
private function getMessages(): iterable
75+
/**
76+
* @param ?resource
77+
*/
78+
private function getMessages($inputStream): iterable
8879
{
80+
if (null !== $inputStream) {
81+
while (!feof($inputStream)) {
82+
$stream = fgets($inputStream);
83+
yield (int) $stream => $stream;
84+
}
85+
86+
return;
87+
}
88+
8989
$sockets = [(int) $this->socket => $this->socket];
9090
$write = [];
9191

src/Symfony/Component/VarDumper/Tests/Command/ServerDumpCommandTest.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Console\Tester\CommandCompletionTester;
16+
use Symfony\Component\Console\Tester\CommandTester;
17+
use Symfony\Component\VarDumper\Cloner\Data;
1618
use Symfony\Component\VarDumper\Command\ServerDumpCommand;
1719
use Symfony\Component\VarDumper\Server\DumpServer;
1820

@@ -28,6 +30,24 @@ public function testComplete(array $input, array $expectedSuggestions)
2830
$this->assertSame($expectedSuggestions, $tester->complete($input));
2931
}
3032

33+
public function testServerDumpSuccess()
34+
{
35+
$command = $this->createCommand();
36+
$commandTester = new CommandTester($command);
37+
38+
$data = new Data([['my dump']]);
39+
$input = base64_encode(serialize([$data, ['timestamp' => time(), 'source' => ['name' => 'sourceName', 'line' => 222, 'file' => 'myFile']]]))."\n";
40+
41+
$commandTester->setInputs([$input]);
42+
43+
$commandTester->execute(['--format' => 'html']);
44+
45+
$commandTester->assertCommandIsSuccessful();
46+
47+
$output = $commandTester->getDisplay();
48+
$this->assertStringContainsString('my dump', $output);
49+
}
50+
3151
public static function provideCompletionSuggestions()
3252
{
3353
yield 'option --format' => [
@@ -38,6 +58,6 @@ public static function provideCompletionSuggestions()
3858

3959
private function createCommand(): ServerDumpCommand
4060
{
41-
return new ServerDumpCommand($this->createMock(DumpServer::class));
61+
return new ServerDumpCommand(new DumpServer(''));
4262
}
4363
}

0 commit comments

Comments
 (0)
0