Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0573d8d

Browse files
Add functional test for ServerDumpCommand
1 parent272f021 commit0573d8d

File tree

3 files changed

+73
-44
lines changed

3 files changed

+73
-44
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
useSymfony\Component\Console\Output\OutputInterface;
2222
useSymfony\Component\Console\Style\SymfonyStyle;
2323
useSymfony\Component\VarDumper\Cloner\Data;
24+
useSymfony\Component\VarDumper\Cloner\Stub;
2425
useSymfony\Component\VarDumper\Command\Descriptor\CliDescriptor;
2526
useSymfony\Component\VarDumper\Command\Descriptor\DumpDescriptorInterface;
2627
useSymfony\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
return0;
99110
}

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

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

1414
usePsr\Log\LoggerInterface;
15+
useSymfony\Component\Console\Input\StreamableInputInterface;
1516
useSymfony\Component\VarDumper\Cloner\Data;
1617
useSymfony\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
publicfunction__construct(string$host, ?LoggerInterface$logger =null)
3642
{
3743
if (!str_contains($host,'://')) {
@@ -49,33 +55,18 @@ public function start(): void
4955
}
5056
}
5157

52-
publicfunctionlisten(callable$callback):void
58+
publicfunctionlisten(callable$callback, ?StreamableInputInterface$streamInput):void
5359
{
54-
if (null ===$this->socket) {
60+
if ($streamInputinstanceof StreamableInputInterface &&$stream =$streamInput->getStream()) {
61+
$this->inputStream =$stream;
62+
}elseif (null ===$this->socket) {
5563
$this->start();
5664
}
5765

5866
foreach ($this->getMessages()as$clientId =>$message) {
5967
$this->logger?->info('Received a payload from client {clientId}', ['clientId' =>$clientId]);
6068

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);
69+
$callback($clientId,$message);
7970
}
8071
}
8172

@@ -86,22 +77,29 @@ public function getHost(): string
8677

8778
privatefunctiongetMessages():iterable
8879
{
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 ($readas$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);
80+
if (null !==$inputStream =$this->inputStream) {
81+
while (!feof($inputStream)) {
82+
$stream =fgets($inputStream);
83+
yield (int)$stream =>$stream;
84+
}
85+
}else {
86+
$sockets = [(int)$this->socket =>$this->socket];
87+
$write = [];
88+
89+
while (true) {
90+
$read =$sockets;
91+
stream_select($read,$write,$write,null);
92+
93+
foreach ($readas$stream) {
94+
if ($this->socket ===$stream) {
95+
$stream =stream_socket_accept($this->socket);
96+
$sockets[(int)$stream] =$stream;
97+
}elseif (feof($stream)) {
98+
unset($sockets[(int)$stream]);
99+
fclose($stream);
100+
}else {
101+
yield (int)$stream =>fgets($stream);
102+
}
105103
}
106104
}
107105
}

‎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
usePHPUnit\Framework\TestCase;
1515
useSymfony\Component\Console\Tester\CommandCompletionTester;
16+
useSymfony\Component\Console\Tester\CommandTester;
17+
useSymfony\Component\VarDumper\Cloner\Data;
1618
useSymfony\Component\VarDumper\Command\ServerDumpCommand;
1719
useSymfony\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+
publicfunctiontestServerDumpSuccess()
34+
{
35+
$command =$this->createCommand();
36+
$commandTester =newCommandTester($command);
37+
38+
$data =newData([['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
publicstaticfunctionprovideCompletionSuggestions()
3252
{
3353
yield'option --format' => [
@@ -38,6 +58,6 @@ public static function provideCompletionSuggestions()
3858

3959
privatefunctioncreateCommand():ServerDumpCommand
4060
{
41-
returnnewServerDumpCommand($this->createMock(DumpServer::class));
61+
returnnewServerDumpCommand(newDumpServer(''));
4262
}
4363
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp