Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e1f6536

Browse files
Add functional test for ServerDumpCommand
1 parent 272f021 commit e1f6536

File tree

3 files changed

+61
-26
lines changed

3 files changed

+61
-26
lines changed

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8585

8686
$errorIo = $io->getErrorStyle();
8787
$errorIo->title('Symfony Var Dumper Server');
88-
89-
$this->server->start();
90-
9188
$errorIo->success(sprintf('Server listening on %s', $this->server->getHost()));
9289
$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-
});
90+
$this->server->listen(
91+
function (Data $data, array $context, int $clientId) use ($descriptor, $io) {
92+
$descriptor->describe($io, $data, $context, $clientId);
93+
},
94+
$input,
95+
);
9796

9897
return 0;
9998
}

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

Lines changed: 34 additions & 18 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

@@ -32,6 +33,11 @@ class DumpServer
3233
*/
3334
private $socket;
3435

36+
/**
37+
* @var resource|null
38+
*/
39+
private $inputStream;
40+
3541
public function __construct(string $host, ?LoggerInterface $logger = null)
3642
{
3743
if (!str_contains($host, '://')) {
@@ -49,9 +55,12 @@ public function start(): void
4955
}
5056
}
5157

52-
public function listen(callable $callback): void
58+
public function listen(callable $callback, ?StreamableInputInterface $streamInput): void
5359
{
54-
if (null === $this->socket) {
60+
var_dump($streamInput instanceof StreamableInputInterface);
61+
if ($streamInput instanceof StreamableInputInterface && $stream = $streamInput->getStream()) {
62+
$this->inputStream = $stream;
63+
} elseif (null === $this->socket) {
5564
$this->start();
5665
}
5766

@@ -86,22 +95,29 @@ public function getHost(): string
8695

8796
private function getMessages(): iterable
8897
{
89-
$sockets = [(int) $this->socket => $this->socket];
90-
$write = [];
91-
92-
while (true) {
93-
$read = $sockets;
94-
stream_select($read, $write, $write, null);
95-
96-
foreach ($read as $stream) {
97-
if ($this->socket === $stream) {
98-
$stream = stream_socket_accept($this->socket);
99-
$sockets[(int) $stream] = $stream;
100-
} elseif (feof($stream)) {
101-
unset($sockets[(int) $stream]);
102-
fclose($stream);
103-
} else {
104-
yield (int) $stream => fgets($stream);
98+
if (null !== $inputStream = $this->inputStream) {
99+
while (!feof($inputStream)) {
100+
$stream = fgets($inputStream);
101+
yield (int) $stream => $stream;
102+
}
103+
} else {
104+
$sockets = [(int) $this->socket => $this->socket];
105+
$write = [];
106+
107+
while (true) {
108+
$read = $sockets;
109+
stream_select($read, $write, $write, null);
110+
111+
foreach ($read as $stream) {
112+
if ($this->socket === $stream) {
113+
$stream = stream_socket_accept($this->socket);
114+
$sockets[(int) $stream] = $stream;
115+
} elseif (feof($stream)) {
116+
unset($sockets[(int) $stream]);
117+
fclose($stream);
118+
} else {
119+
yield (int) $stream => fgets($stream);
120+
}
105121
}
106122
}
107123
}

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)