mirror of
https://github.com/Seldaek/monolog.git
synced 2025-08-02 11:20:31 +02:00
Tested WebProcessor
This commit is contained in:
@@ -18,6 +18,22 @@ namespace Monolog\Processor;
|
||||
*/
|
||||
class WebProcessor
|
||||
{
|
||||
protected $serverData;
|
||||
|
||||
/**
|
||||
* @param mixed $serverData array or object w/ ArrayAccess that provides access to the $_SERVER data
|
||||
*/
|
||||
public function __construct($serverData = null)
|
||||
{
|
||||
if (null === $serverData) {
|
||||
$this->serverData =& $_SERVER;
|
||||
} elseif (is_array($serverData) || $serverData instanceof \ArrayAccess) {
|
||||
$this->serverData = $serverData;
|
||||
} else {
|
||||
throw new \UnexpectedValueException('$serverData must be an array or object implementing ArrayAccess.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $record
|
||||
* @param HandlerInterface $handler
|
||||
@@ -28,9 +44,9 @@ class WebProcessor
|
||||
$record['extra'] = array_merge(
|
||||
$record['extra'],
|
||||
array(
|
||||
'url' => $_SERVER['REQUEST_URI'],
|
||||
'ip' => $_SERVER['REMOTE_ADDR'],
|
||||
'method' => $_SERVER['REQUEST_METHOD'],
|
||||
'url' => $this->serverData['REQUEST_URI'],
|
||||
'ip' => $this->serverData['REMOTE_ADDR'],
|
||||
'http_method' => $this->serverData['REQUEST_METHOD'],
|
||||
)
|
||||
);
|
||||
|
||||
|
39
tests/Monolog/Processor/WebProcessorTest.php
Normal file
39
tests/Monolog/Processor/WebProcessorTest.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Monolog package.
|
||||
*
|
||||
* (c) Jordi Boggiano <j.boggiano@seld.be>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Monolog\Processor;
|
||||
|
||||
use Monolog\TestCase;
|
||||
|
||||
class WebProcessorTest extends TestCase
|
||||
{
|
||||
public function testProcessor()
|
||||
{
|
||||
$server = array(
|
||||
'REQUEST_URI' => 'A',
|
||||
'REMOTE_ADDR' => 'B',
|
||||
'REQUEST_METHOD' => 'C',
|
||||
);
|
||||
$processor = new WebProcessor($server);
|
||||
$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']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException UnexpectedValueException
|
||||
*/
|
||||
public function testInvalidData()
|
||||
{
|
||||
new WebProcessor(new \stdClass);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user