8000 minor #34721 [MonologBridge] Add test on ServerLogHandler (jderusse) · symfony/symfony@6611ae6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6611ae6

Browse files
minor #34721 [MonologBridge] Add test on ServerLogHandler (jderusse)
This PR was merged into the 4.3 branch. Discussion ---------- [MonologBridge] Add test on ServerLogHandler | Q | A | ------------- | --- | Branch? | 5.0 | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | / | License | MIT | Doc PR | / After writing #34697 (comment) I realized that ServerLogHandler wasn't tested. Tell me if it's a BugFix and should be rebased on 4.3 Commits ------- 8c7947f Add test on ServerLogHandler
2 parents e9366b4 + 8c7947f commit 6611ae6

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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\Handler;
13+
14+
use Monolog\Formatter\JsonFormatter;
15+
use Monolog\Logger;
16+
use Monolog\Processor\ProcessIdProcessor;
17+
use PHPUnit\Framework\TestCase;
18+
use Symfony\Bridge\Monolog\Formatter\VarDumperFormatter;
19+
use Symfony\Bridge\Monolog\Handler\ServerLogHandler;
20+
use Symfony\Component\VarDumper\Cloner\Data;
21+
22+
/**
23+
* Tests the ServerLogHandler.
24+
*/
25+
class ServerLogHandlerTest extends TestCase
26+
{
27+
public function testFormatter()
28+
{
29+
$handler = new ServerLogHandler('tcp://127.0.0.1:9999');
30+
$this->assertInstanceOf(VarDumperFormatter::class, $handler->getFormatter());
31+
32+
$formatter = new JsonFormatter();
33+
$handler->setFormatter($formatter);
34+
$this->assertSame($formatter, $handler->getFormatter());
35+
}
36+
37+
public function testIsHandling()
38+
{
39+
$handler = new ServerLogHandler('tcp://127.0.0.1:9999', Logger::INFO);
40+
$this->assertFalse($handler->isHandling(['level' => Logger::DEBUG]), '->isHandling returns false when no output is set');
41+
}
42+
43+
public function testGetFormatter()
44+
{
45+
$handler = new ServerLogHandler('tcp://127.0.0.1:9999');
46+
$this->assertInstanceOf(VarDumperFormatter::class, $handler->getFormatter(),
47+
'-getFormatter returns VarDumperFormatter by default'
48+
);
49+
}
50+
51+
public function testWritingAndFormatting()
52+
{
53+
$host = 'tcp://127.0.0.1:9999';
54+
$handler = new ServerLogHandler($host, Logger::INFO, false);
55+
$handler->pushProcessor(new ProcessIdProcessor());
56+
57+
$infoRecord = [
58+
'message' => 'My info message',
59+
'context' => [],
60+
'level' => Logger::INFO,
61+
'level_name' => Logger::getLevelName(Logger::INFO),
62+
&# 10000 39;channel' => 'app',
63+
'datetime' => new \DateTime('2013-05-29 16:21:54'),
64+
'extra' => [],
65+
];
66+
67+
$socket = stream_socket_server($host, $errno, $errstr);
68+
$this->assertIsResource($socket, sprintf('Server start failed on "%s": %s %s.', $host, $errstr, $errno));
69+
70+
$this->assertTrue($handler->handle($infoRecord), 'The handler finished handling the log as bubble is false.');
71+
72+
$sockets = [(int) $socket => $socket];
73+
$write = [];
74+
75+
for ($i = 0; $i < 10; ++$i) {
76+
$read = $sockets;
77+
stream_select($read, $write, $write, null);
78+
79+
foreach ($read as $stream) {
80+
if ($socket === $stream) {
81+
$stream = stream_socket_accept($socket);
82+
$sockets[(int) $stream] = $stream;
83+
} elseif (feof($stream)) {
84+
unset($sockets[(int) $stream]);
85+
fclose($stream);
86+
} else {
87+
$message = fgets($stream);
88+
fclose($stream);
89+
90+
$record = unserialize(base64_decode($message));
91+
$this->assertIsArray($record);
92+
93+
$this->assertArrayHasKey('message', $record);
94+
$this->assertEquals('My info message', $record['message']);
95+
96+
$this->assertArrayHasKey('extra', $record);
97+
$this->assertInstanceOf(Data::class, $record['extra']);
98+
$extra = $record['extra']->getValue(true);
99+
$this->assertIsArray($extra);
100+
$this->assertArrayHasKey('process_id', $extra);
101+
$this->assertSame(getmypid(), $extra['process_id']);
102+
103+
return;
104+
}
105+
}
106+
usleep(100000);
107+
}
108+
$this->fail('Fail to read message from server');
109+
}
110+
}

0 commit comments

Comments
 (0)
0