Skip to content

Commit ee5c5db

Browse files
compatibility for RequestStack; add basic test for SessionRequestProcessor
1 parent b8bf7b6 commit ee5c5db

File tree

3 files changed

+61
-3
lines changed

3 files changed

+61
-3
lines changed

Logger/SessionRequestProcessor.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ public function __invoke(array $record)
6868
$record['http.referer'] = $this->_server['http.referer'];
6969
$record['http.x_forwarded_for'] = $this->_server['http.x_forwarded_for'];
7070

71-
if ($this->requestStack->getMasterRequest()) {
72-
$request = $this->requestStack->getMasterRequest();
71+
if ($this->getMainRequest()) {
72+
$request = $this->getMainRequest();
7373
$context = [
7474
'request_uri' => $request->getUri(),
7575
'method' => $request->getMethod(),
@@ -94,6 +94,20 @@ public function __invoke(array $record)
9494
return $record;
9595
}
9696

97+
/**
98+
* Compatibility with Symfony <5.3
99+
* @return \Symfony\Component\HttpFoundation\Request|null
100+
*/
101+
protected function getMainRequest()
102+
{
103+
if (method_exists(RequestStack::class, 'getMainRequest')) {
104+
return $this->requestStack->getMainRequest();
105+
} else {
106+
return $this->requestStack->getMasterRequest();
107+
}
108+
}
109+
110+
97111
protected function clean($array)
98112
{
99113
$toReturn = array();
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Dayspring\LoggingBundle\Tests\Logger;
4+
5+
use Dayspring\LoggingBundle\Logger\SessionRequestProcessor;
6+
use Monolog\Handler\TestHandler;
7+
use Monolog\Logger;
8+
use PHPUnit\Framework\TestCase;
9+
use Symfony\Component\HttpFoundation\Request;
10+
use Symfony\Component\HttpFoundation\RequestStack;
11+
use Symfony\Component\HttpFoundation\Session\Session;
12+
use Symfony\Component\HttpFoundation\Session\SessionFactory;
13+
use Symfony\Component\Routing\Router;
14+
use function var_dump;
15+
16+
class SessionRequestProcessorTest extends TestCase
17+
{
18+
public function testProcessor()
19+
{
20+
$session = new Session();
21+
$requestStack = new RequestStack();
22+
$requestStack->push(Request::create('/', 'GET'));
23+
$router = $this->createPartialMock(Router::class, ['matchRequest']);
24+
$router->expects($this->once())
25+
->method('matchRequest')
26+
->willReturn(['_route' => 'test']);
27+
28+
$processor = new SessionRequestProcessor($session, $requestStack, $router);
29+
30+
$handler = new TestHandler();
31+
32+
$logger = new Logger('test', [$handler], [$processor]);
33+
34+
$logger->info('test');
35+
36+
$records = $handler->getRecords();
37+
38+
$this->assertCount(1, $records);
39+
$record = $records[0];
40+
$this->assertEquals('test', $record['context']['route']);
41+
$this->assertEquals('test', $record['context']['route_parameters']['_route']);
42+
}
43+
}

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
},
1515
"require-dev": {
1616
"php": ">=7.4",
17-
"phpunit/phpunit": "~8.5.33|^9.0"
17+
"phpunit/phpunit": "~8.5.33|^9.0",
18+
"monolog/monolog": "^1.22 || ^2.0"
1819
},
1920
"authors": [
2021
{

0 commit comments

Comments
 (0)