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

Commit69e6ce6

Browse files
Add functional test for ServerDumpCommand
1 parent9549cc2 commit69e6ce6

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
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: 22 additions & 22 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

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

52-
publicfunctionlisten(callable$callback):void
53+
publicfunctionlisten(callable$callback, ?StreamableInputInterface$streamInput =null):void
5354
{
54-
if (null ===$this->socket) {
55+
$inputStream =null;
56+
57+
if ($streamInputinstanceof 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-
privatefunctiongetMessages():iterable
75+
/**
76+
* @param ?resource
77+
*/
78+
privatefunctiongetMessages($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
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