8000 Add functionnal test for ServerLogCommand · louismariegaborit/symfony@cbbb48f · GitHub
[go: up one dir, main page]

Skip to content

Commit cbbb48f

Browse files
Add functionnal test for ServerLogCommand
1 parent 69e6ce6 commit cbbb48f

File tree

3 files changed

+79
-42
lines changed

3 files changed

+79
-42
lines changed

src/Symfony/Bridge/Monolog/Command/ServerLogCommand.php

Lines changed: 25 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@
2020
use Symfony\Component\Console\Attribute\AsCommand;
2121
use Symfony\Component\Console\Command\Command;
2222
use Symfony\Component\Console\Exception\LogicException;
23-
use Symfony\Component\Console\Exception\RuntimeException;
2423
use Symfony\Component\Console\Input\InputInterface;
2524
use Symfony\Component\Console\Input\InputOption;
2625
use Symfony\Component\Console\Output\OutputInterface;
2726
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
27+
use Symfony\Component\VarDumper\Server\DumpServer;
2828

2929
/**
3030
* @author Grégoire Pineau <lyrixx@lyrixx.info>
@@ -34,9 +34,16 @@ class ServerLogCommand extends Command
3434
{
3535
private const BG_COLOR = ['black', 'blue', 'cyan', 'green', 'magenta', 'red', 'white', 'yellow'];
3636

37-
private ExpressionLanguage $el;
37+
private DumpServer $server;
3838
private HandlerInterface $handler;
3939

40+
public function __construct(DumpServer $server)
41+
{
42+
$this->server = $server;
43+
44+
parent::__construct();
45+
}
46+
4047
public function isEnabled(): bool
4148
{
4249
if (!class_exists(ConsoleFormatter::class)) {
@@ -78,12 +85,13 @@ protected function configure(): void
7885

7986
protected function execute(InputInterface $input, OutputInterface $output): int
8087
{
88+
$el = null;
8189
$filter = $input->getOption('filter');
8290
if ($filter) {
8391
if (!class_exists(ExpressionLanguage::class)) {
8492
throw new LogicException('Package "symfony/expression-language" is required to use the "filter" option. Try running "composer require symfony/expression-language".');
8593
}
86-
$this->el = new ExpressionLanguage();
94+
$el = new ExpressionLanguage();
8795
}
8896

8997
$this->handler = new ConsoleHandler($output, true, [
@@ -101,51 +109,27 @@ protected function execute(InputInterface $input, OutputInterface $output): int
101109
$host = 'tcp://'.$host;
102110
}
103111

104-
if (!$socket = stream_socket_server($host, $errno, $errstr)) {
105-
throw new RuntimeException(sprintf('Server start failed on "%s": ', $host).$errstr.' '.$errno);
106-
}
112+
$this->server->listen(
113+
function (int $clientId, string $message) use ($el, $filter, $output) {
114+
$record = unserialize(base64_decode($message));
107115

108-
foreach ($this->getLogs($socket) as $clientId => $message) {
109-
$record = unserialize(base64_decode($message));
110-
111-
// Impossible to decode the message, give up.
112-
if (false === $record) {
113-
continue;
114-
}
116+
// Impossible to decode the message, give up.
117+
if (false === $record) {
118+
return;
119+
}
115120

116-
if ($filter && !$this->el->evaluate($filter, $record)) {
117-
continue;
118-
}
121+
if ($filter && !$el->evaluate($filter, $record)) {
122+
return;
123+
}
119124

120-
$this->displayLog($output, $clientId, $record);
121-
}
125+
$this->displayLog($output, $clientId, $record);
126+
},
127+
$input,
128+
);
122129

123130
return 0;
124131
}
125132

126-
private function getLogs($socket): iterable
127-
{
128-
$sockets = [(int) $socket => $socket];
129-
$write = [];
130-
131-
while (true) {
132-
$read = $sockets;
133-
stream_select($read, $write, $write, null);
134-
135-
foreach ($read as $stream) {
136-
if ($socket === $stream) {
137-
$stream = stream_socket_accept($socket);
138-
$sockets[(int) $stream] = $stream;
139-
} elseif (feof($stream)) {
140-
unset($sockets[(int) $stream]);
141-
fclose($stream);
142-
} else {
143-
yield (int) $stream => fgets($stream);
144-
}
145-
}
146-
}
147-
}
148-
149133
private function displayLog(OutputInterface $output, int $clientId, array $record): void
150134
{
151135
if (isset($record['log_id'])) {
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Monolog\Tests\Command;
13+
14+
use Monolog\Level;
15+
use Monolog\LogRecord;
16+
use PHPUnit\Framework\TestCase;
17+
use Symfony\Bridge\Monolog\Command\ServerLogCommand;
18+
use Symfony\Component\Console\Tester\CommandTester;
19+
use Symfony\Component\VarDumper\Server\DumpServer;
20+
21+
class ServerLogCommandTest extends TestCase
22+
{
23+
public function testServerLogSuccess()
24+
{
25+
$command = $this->createCommand();
26+
$commandTester = new CommandTester($command);
27+
28+
$record = new LogRecord(
29+
new \DateTimeImmutable('2024-01-02 18:05'),
30+
'console',
31+
Level::Info,
32+
'test log command',
33+
);
34+
$recordFormatted = $record->toArray();
35+
$input = base64_encode(serialize($recordFormatted))."\n";
36+
37+
$commandTester->setInputs([$input]);
38+
39+
$commandTester->execute([]);
40+
41+
$commandTester->assertCommandIsSuccessful();
42+
43+
$output = $commandTester->getDisplay();
44+
$this->assertStringContainsString('18:05:00 INFO [console] test log command', $output);
45+
}
46+
47+
private function createCommand(): ServerLogCommand
48+
{
49+
$command = new ServerLogCommand(new DumpServer(''));
50+
51+
return $command;
52+
}
53+
}

src/Symfony/Bridge/Monolog/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
"require": {
1919
"php": ">=8.2",
2020
"monolog/monolog": "^3",
21+
"symfony/console": "^6.4|^7.0",
2122
"symfony/service-contracts": "^2.5|^3",
2223
"symfony/http-kernel": "^6.4|^7.0"
2324
},
2425
"require-dev": {
25-
"symfony/console": "^6.4|^7.0",
2626
"symfony/http-client": "^6.4|^7.0",
2727
"symfony/security-core": "^6.4|^7.0",
2828
"symfony/var-dumper": "^6.4|^7.0",

0 commit comments

Comments
 (0)
0