Skip to content

Commit

Permalink
remove code from isHandling, but dont throw exception on non handling (
Browse files Browse the repository at this point in the history
…#26)

* remove code from isHandling, but dont throw exception on non handling

* code standards
  • Loading branch information
Harry Bragg authored Jun 17, 2018
1 parent 1d6c1f7 commit 26ec6b4
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 123 deletions.
56 changes: 17 additions & 39 deletions src/Graze/Monolog/Handler/RaygunHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* @see http://github.com/graze/MonologExtensions/blob/master/LICENSE
* @link http://github.com/graze/MonologExtensions
*/

namespace Graze\Monolog\Handler;

use Graze\Monolog\Formatter\RaygunFormatter;
Expand All @@ -26,8 +27,8 @@ class RaygunHandler extends AbstractProcessingHandler

/**
* @param RaygunClient $client
* @param int $level
* @param bool $bubble
* @param int $level
* @param bool $bubble
*/
public function __construct(RaygunClient $client, $level = Logger::DEBUG, $bubble = true)
{
Expand All @@ -36,39 +37,17 @@ public function __construct(RaygunClient $client, $level = Logger::DEBUG, $bubbl
parent::__construct($level, $bubble);
}

/**
* {@inheritdoc}
*/
public function isHandling(array $record)
{
if (parent::isHandling($record) && isset($record['context'])) {
$context = $record['context'];

//Ensure only valid records will be handled and no InvalidArgumentException will be thrown
if ((isset($context['exception']) &&
(
$context['exception'] instanceof \Exception ||
(PHP_VERSION_ID > 70000 && $context['exception'] instanceof \Throwable)
)
) || (isset($context['file']) && isset($context['line']))
) {
return true;
}
}
return false;
}

/**
* @param array $record
*/
protected function write(array $record)
{
$context = $record['context'];

if (isset($context['exception']) &&
(
$context['exception'] instanceof \Exception ||
(PHP_VERSION_ID > 70000 && $context['exception'] instanceof \Throwable)
if (isset($context['exception'])
&& (
$context['exception'] instanceof \Exception
|| (PHP_VERSION_ID > 70000 && $context['exception'] instanceof \Throwable)
)
) {
$this->writeException(
Expand All @@ -77,25 +56,24 @@ protected function write(array $record)
$record['formatted']['custom_data'],
$record['formatted']['timestamp']
);
} elseif (isset($context['file']) && $context['line']) {
} elseif (isset($context['file']) && isset($context['line'])) {
$this->writeError(
$record['formatted'],
$record['formatted']['tags'],
$record['formatted']['custom_data'],
$record['formatted']['timestamp']
);
} else {
throw new \InvalidArgumentException('Invalid record given.');
}
// do nothing if its not an exception or an error
}

/**
* @param array $record
* @param array $tags
* @param array $customData
* @param array $record
* @param array $tags
* @param array $customData
* @param int|float $timestamp
*/
protected function writeError(array $record, array $tags = array(), array $customData = array(), $timestamp = null)
protected function writeError(array $record, array $tags = [], array $customData = [], $timestamp = null)
{
$context = $record['context'];
$this->client->SendError(
Expand All @@ -110,12 +88,12 @@ protected function writeError(array $record, array $tags = array(), array $custo
}

/**
* @param array $record
* @param array $tags
* @param array $customData
* @param array $record
* @param array $tags
* @param array $customData
* @param int|float $timestamp
*/
protected function writeException(array $record, array $tags = array(), array $customData = array(), $timestamp = null)
protected function writeException(array $record, array $tags = [], array $customData = [], $timestamp = null)
{
$this->client->SendException($record['context']['exception'], $tags, $customData, $timestamp);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Graze\Monolog\Handler;

use Mockery;
use Monolog\Logger;
use PHPUnit\Framework\TestCase;
use Raygun4php\RaygunClient;
use RuntimeException;

class RaygunHandlerIntegrationTest extends TestCase
{
/** @var Logger */
private $logger;
/** @var mixed */
private $raygun;
/** @var RaygunHandler */
private $handler;

public function setUp()
{
$this->raygun = Mockery::mock(RaygunClient::class);
$this->handler = new RaygunHandler($this->raygun, Logger::NOTICE);
$this->logger = new Logger('raygunHandlerTest', [$this->handler]);
}

public function testErrorWithExceptionTriggersLogger()
{
$exception = new RuntimeException('test exception');

$this->raygun
->shouldReceive('SendException')
->once()
->with($exception, [], [], null);

$this->logger->error('test error', ['exception' => $exception]);
}

public function testErrorWithErrorWillTriggerLogger()
{
$this->raygun
->shouldReceive('SendError')
->once()
->with(0, "test line error", __FILE__, 5, [], [], null);
$this->logger->error('test line error', ['file' => __FILE__, 'line' => 5]);
}

public function testErrorWithNoLineWillDoNothing()
{
$this->logger->error('test line error', ['file' => __FILE__]);
}

public function testErrorWithNoFileWillDoNothing()
{
$this->logger->error('test line error', ['line' => __FILE__]);
}

public function testErrorWithNeitherWillDoNothing()
{
$this->logger->error('test line error');
}
}
149 changes: 65 additions & 84 deletions tests/unit/src/Graze/Monolog/Handler/RaygunHandlerTest.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace Graze\Monolog\Handler;

use Mockery as m;
use Monolog\Logger;
use Monolog\TestCase;

class RaygunHandlerTest extends TestCase
Expand Down Expand Up @@ -35,120 +35,101 @@ public function testGetFormatter()
public function testHandleError()
{
$record = $this->getRecord(300,
'foo', array(
'foo',
[
'file' => 'bar',
'line' => 1,
)
]
);
$record['context']['tags'] = array('foo');
$record['context']['tags'] = ['foo'];
$record['context']['timestamp'] = 1234567890;
$record['extra'] = array('bar' => 'baz', 'tags' => array('bar'));
$record['extra'] = ['bar' => 'baz', 'tags' => ['bar']];
$formatted = array_merge($record,
array(
'tags' => array('foo', 'bar'),
'timestamp' => 1234567890,
'custom_data' => array('bar' => 'baz')
)
[
'tags' => ['foo', 'bar'],
'timestamp' => 1234567890,
'custom_data' => ['bar' => 'baz'],
]
);

$formatter = m::mock('Monolog\\Formatter\\FormatterInterface');
$handler = new RaygunHandler($this->client);
$handler->setFormatter($formatter);

$formatter
->shouldReceive('format')
->once()
->with($record)
->andReturn($formatted);
->shouldReceive('format')
->once()
->with($record)
->andReturn($formatted);
$this->client
->shouldReceive('SendError')
->once()
->with(0, 'foo', 'bar', 1, array('foo', 'bar'), array('bar' => 'baz'), 1234567890);
->shouldReceive('SendError')
->once()
->with(0, 'foo', 'bar', 1, ['foo', 'bar'], ['bar' => 'baz'], 1234567890);

$handler->handle($record);
}

public function testHandleException()
{
$exception = new \Exception('foo');
$record = $this->getRecord(300, 'foo', array('exception' => $exception));
$record['extra'] = array('bar' => 'baz', 'tags' => array('foo', 'bar'));
$record = $this->getRecord(300, 'foo', ['exception' => $exception]);
$record['extra'] = ['bar' => 'baz', 'tags' => ['foo', 'bar']];
$record['extra']['timestamp'] = 1234567890;
$formatted = array_merge($record,
array(
'tags' => array('foo', 'bar'),
'timestamp' => 1234567890,
'custom_data' => array('bar' => 'baz')
)
[
'tags' => ['foo', 'bar'],
'timestamp' => 1234567890,
'custom_data' => ['bar' => 'baz'],
]
);
$formatted['context']['exception'] = array(
'class' => get_class($exception),
$formatted['context']['exception'] = [
'class' => get_class($exception),
'message' => $exception->getMessage(),
'code' => $exception->getCode(),
'file' => $exception->getFile().':'.$exception->getLine(),
);
'code' => $exception->getCode(),
'file' => $exception->getFile() . ':' . $exception->getLine(),
];

$formatter = m::mock('Monolog\\Formatter\\FormatterInterface');
$handler = new RaygunHandler($this->client);
$handler->setFormatter($formatter);

$formatter
->shouldReceive('format')
->once()
->with($record)
->andReturn($formatted);
->shouldReceive('format')
->once()
->with($record)
->andReturn($formatted);
$this->client
->shouldReceive('SendException')
->once()
->with($exception, array('foo', 'bar'), array('bar' => 'baz'), 1234567890);
->shouldReceive('SendException')
->once()
->with($exception, ['foo', 'bar'], ['bar' => 'baz'], 1234567890);

$handler->handle($record);
}

/**
* @dataProvider isHandleData
*
* @param array $record
* @param bool $expected
*/
public function testIsHandling(array $record, $expected)
public function testHandleEmptyDoesNothing()
{
$handler = new RaygunHandler($this->client);

$this->assertEquals($expected, $handler->isHandling($record));
}

/**
* @return array
*/
public function isHandleData()
{
return [
[$this->getRecord(300, 'foo', ['exception' => new \Exception('foo')]), true],
[$this->getRecord(300, 'bar', ['file' => '/a/path/to/a/file', 'line' => 123]), true],
[$this->getRecord(300, 'baz', ['file' => '/a/path/to/a/file']), false],
[$this->getRecord(300, 'faz', ['line' => 456]), false],
// no context entry
$record = $this->getRecord(300, 'bar');
$record['extra'] = ['bar' => 'baz', 'tags' => ['foo', 'bar']];
$record['extra']['timestamp'] = 1234567890;
$formatted = array_merge($record,
[
[
'message' => 'foobar',
'level' => 300,
'level_name' => Logger::getLevelName(300),
'channel' => 'test',
'datetime' => \DateTime::createFromFormat('U.u', sprintf('%.6F', microtime(true))),
'extra' => [],
],
false,
],
];
}
'tags' => ['foo', 'bar'],
'timestamp' => 1234567890,
'custom_data' => ['bar' => 'baz'],
]
);

public function testHandleCallsIsHandlingFirst()
{
$formatter = m::mock('Monolog\\Formatter\\FormatterInterface');
$handler = new RaygunHandler($this->client);
$nonHandlingRecord = $this->getRecord(300, 'baz', array());
$this->assertFalse($handler->isHandling($nonHandlingRecord));
$this->assertFalse($handler->handle($nonHandlingRecord));
$handler->setFormatter($formatter);

$formatter
->shouldReceive('format')
->once()
->with($record)
->andReturn($formatted);

$handler->handle($record);
}

/**
Expand All @@ -157,14 +138,14 @@ public function testHandleCallsIsHandlingFirst()
public function testHandleThrowable()
{
$exception = new \TypeError('foo');
$record = $this->getRecord(300, 'foo', array('exception' => $exception));
$record['context']['tags'] = array('foo');
$record = $this->getRecord(300, 'foo', ['exception' => $exception]);
$record['context']['tags'] = ['foo'];
$formatted = array_merge($record,
array(
'tags' => array('foo'),
'custom_data' => array(),
'timestamp' => null,
)
[
'tags' => ['foo'],
'custom_data' => [],
'timestamp' => null,
]
);

$formatter = m::mock('Monolog\\Formatter\\FormatterInterface');
Expand All @@ -179,7 +160,7 @@ public function testHandleThrowable()
$this->client
->shouldReceive('SendException')
->once()
->with($exception, array('foo'), array(), null);
->with($exception, ['foo'], [], null);

$handler->handle($record);
}
Expand Down

0 comments on commit 26ec6b4

Please sign in to comment.