bug #14964 [bugfix][MonologBridge] WebProcessor: passing $extraFields to BaseWebProcessor (MacDada)
This PR was squashed before being merged into the 2.3 branch (closes #14964).
Discussion
----------
[bugfix][MonologBridge] WebProcessor: passing $extraFields to BaseWebProcessor
| Q | A
| ------------- | ---
| Bug fix? | yes
| New feature? | no
| BC breaks? | no
| Deprecations? | no
| Tests pass? | no
| Fixed tickets | #14962
| License | MIT
| Doc PR | ~
Commits
-------
f41d1c9
[bugfix][MonologBridge] WebProcessor: passing to BaseWebProcessor
This commit is contained in:
commit
7a234429fc
@ -22,10 +22,10 @@ use Symfony\Component\HttpKernel\HttpKernelInterface;
|
|||||||
*/
|
*/
|
||||||
class WebProcessor extends BaseWebProcessor
|
class WebProcessor extends BaseWebProcessor
|
||||||
{
|
{
|
||||||
public function __construct()
|
public function __construct(array $extraFields = null)
|
||||||
{
|
{
|
||||||
// Pass an empty array as the default null value would access $_SERVER
|
// Pass an empty array as the default null value would access $_SERVER
|
||||||
parent::__construct(array());
|
parent::__construct(array(), $extraFields);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onKernelRequest(GetResponseEvent $event)
|
public function onKernelRequest(GetResponseEvent $event)
|
||||||
|
@ -19,6 +19,42 @@ use Symfony\Component\HttpKernel\HttpKernelInterface;
|
|||||||
class WebProcessorTest extends \PHPUnit_Framework_TestCase
|
class WebProcessorTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
public function testUsesRequestServerData()
|
public function testUsesRequestServerData()
|
||||||
|
{
|
||||||
|
list($event, $server) = $this->createRequestEvent();
|
||||||
|
|
||||||
|
$processor = new WebProcessor();
|
||||||
|
$processor->onKernelRequest($event);
|
||||||
|
$record = $processor($this->getRecord());
|
||||||
|
|
||||||
|
$this->assertCount(5, $record['extra']);
|
||||||
|
$this->assertEquals($server['REQUEST_URI'], $record['extra']['url']);
|
||||||
|
$this->assertEquals($server['REMOTE_ADDR'], $record['extra']['ip']);
|
||||||
|
$this->assertEquals($server['REQUEST_METHOD'], $record['extra']['http_method']);
|
||||||
|
$this->assertEquals($server['SERVER_NAME'], $record['extra']['server']);
|
||||||
|
$this->assertEquals($server['HTTP_REFERER'], $record['extra']['referrer']);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCanBeConstructedWithExtraFields()
|
||||||
|
{
|
||||||
|
if (!$this->isExtraFieldsSupported()) {
|
||||||
|
$this->markTestSkipped('WebProcessor of the installed Monolog version does not support $extraFields parameter');
|
||||||
|
}
|
||||||
|
|
||||||
|
list($event, $server) = $this->createRequestEvent();
|
||||||
|
|
||||||
|
$processor = new WebProcessor(array('url', 'referrer'));
|
||||||
|
$processor->onKernelRequest($event);
|
||||||
|
$record = $processor($this->getRecord());
|
||||||
|
|
||||||
|
$this->assertCount(2, $record['extra']);
|
||||||
|
$this->assertEquals($server['REQUEST_URI'], $record['extra']['url']);
|
||||||
|
$this->assertEquals($server['HTTP_REFERER'], $record['extra']['referrer']);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
private function createRequestEvent()
|
||||||
{
|
{
|
||||||
$server = array(
|
$server = array(
|
||||||
'REQUEST_URI' => 'A',
|
'REQUEST_URI' => 'A',
|
||||||
@ -41,15 +77,7 @@ class WebProcessorTest extends \PHPUnit_Framework_TestCase
|
|||||||
->method('getRequest')
|
->method('getRequest')
|
||||||
->will($this->returnValue($request));
|
->will($this->returnValue($request));
|
||||||
|
|
||||||
$processor = new WebProcessor();
|
return array($event, $server);
|
||||||
$processor->onKernelRequest($event);
|
|
||||||
$record = $processor($this->getRecord());
|
|
||||||
|
|
||||||
$this->assertEquals($server['REQUEST_URI'], $record['extra']['url']);
|
|
||||||
$this->assertEquals($server['REMOTE_ADDR'], $record['extra']['ip']);
|
|
||||||
$this->assertEquals($server['REQUEST_METHOD'], $record['extra']['http_method']);
|
|
||||||
$this->assertEquals($server['SERVER_NAME'], $record['extra']['server']);
|
|
||||||
$this->assertEquals($server['HTTP_REFERER'], $record['extra']['referrer']);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -58,7 +86,7 @@ class WebProcessorTest extends \PHPUnit_Framework_TestCase
|
|||||||
*
|
*
|
||||||
* @return array Record
|
* @return array Record
|
||||||
*/
|
*/
|
||||||
protected function getRecord($level = Logger::WARNING, $message = 'test')
|
private function getRecord($level = Logger::WARNING, $message = 'test')
|
||||||
{
|
{
|
||||||
return array(
|
return array(
|
||||||
'message' => $message,
|
'message' => $message,
|
||||||
@ -70,4 +98,17 @@ class WebProcessorTest extends \PHPUnit_Framework_TestCase
|
|||||||
'extra' => array(),
|
'extra' => array(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function isExtraFieldsSupported()
|
||||||
|
{
|
||||||
|
$monologWebProcessorClass = new \ReflectionClass('Monolog\Processor\WebProcessor');
|
||||||
|
|
||||||
|
foreach ($monologWebProcessorClass->getConstructor()->getParameters() as $parameter) {
|
||||||
|
if ('extraFields' === $parameter->getName()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user