Skip to content

Commit

Permalink
Merge pull request #7 from szoper/bugfix/missing-request-id-exception…
Browse files Browse the repository at this point in the history
…-in-monolog

FIX: MonologProcessor handles particular kind of exceptions thrown from RequestIdProviders
  • Loading branch information
snapshotpl committed Mar 23, 2016
2 parents 3cd700e + 2837632 commit f29fbac
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
10 changes: 9 additions & 1 deletion src/MonologProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace PhpMiddleware\RequestId;

use PhpMiddleware\RequestId\Exception\MissingRequestId;

final class MonologProcessor
{
const KEY = 'request_id';
Expand All @@ -15,7 +17,13 @@ public function __construct(RequestIdProviderInterface $requestIdProvider)

public function __invoke(array $record)
{
$record['extra'][self::KEY] = $this->requestIdProvider->getRequestId();
try {
$requestId = $this->requestIdProvider->getRequestId();
} catch (MissingRequestId $e) {
$requestId = null;
}

$record['extra'][self::KEY] = $requestId;

return $record;
}
Expand Down
27 changes: 21 additions & 6 deletions test/MonologProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,43 @@

namespace PhpMiddlewareTestTest\RequestId;

use PhpMiddleware\RequestId\Exception\MissingRequestId;
use PhpMiddleware\RequestId\MonologProcessor;
use PhpMiddleware\RequestId\RequestIdProviderInterface;

class MonologProcessorTest extends \PHPUnit_Framework_TestCase
{
protected $processor;
/**
* @var RequestIdProviderInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $requestIdProvider;

protected function setUp()
public function testIsRequestIdInRecord()
{
$requestIdProvider = $this->getMock(RequestIdProviderInterface::class);
$requestIdProvider->expects($this->once())->method('getRequestId')->willReturn('boo');
$this->requestIdProvider->expects($this->once())->method('getRequestId')->willReturn('boo');
$record = ['extra' => []];

$this->processor = new MonologProcessor($requestIdProvider);
$newRecord = call_user_func($this->processor, $record);

$this->assertArrayHasKey(MonologProcessor::KEY, $newRecord['extra']);
$this->assertSame('boo', $newRecord['extra'][MonologProcessor::KEY]);
}

public function testIsRequestIdInRecord()
public function testIsMissingRequestIdExceptionHandledProperly()
{
$this->requestIdProvider->expects($this->once())->method('getRequestId')->willThrowException(new MissingRequestId());
$record = ['extra' => []];

$newRecord = call_user_func($this->processor, $record);

$this->assertArrayHasKey(MonologProcessor::KEY, $newRecord['extra']);
$this->assertSame('boo', $newRecord['extra'][MonologProcessor::KEY]);
$this->assertNull($newRecord['extra'][MonologProcessor::KEY]);
}

protected function setUp()
{
$this->requestIdProvider = $this->getMock(RequestIdProviderInterface::class);
$this->processor = new MonologProcessor($this->requestIdProvider);
}
}

0 comments on commit f29fbac

Please sign in to comment.