Skip to content

Commit

Permalink
handle whoops with php7 (#28)
Browse files Browse the repository at this point in the history
* handle whoops with php7

* remove deprecated notice
  • Loading branch information
Harry Bragg authored Jun 5, 2019
1 parent 91cc36e commit 22fe31a
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 47 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"require-dev": {
"adlawson/timezone": "^1.0",
"aws/aws-sdk-php": "^2.4.9|^3.0",
"filp/whoops": "^1.0",
"filp/whoops": "^2.0",
"mindscape/raygun4php": "^1.0",
"mockery/mockery": "^0.9",
"phpunit/phpunit": "^4.8.36",
Expand Down
51 changes: 27 additions & 24 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 8 additions & 4 deletions src/Graze/Monolog/Handler/WhoopsHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,13 @@ protected function write(array $record)
{
$context = $record['context'];

if (isset($context['exception']) && $context['exception'] instanceof \Exception) {
if (isset($context['exception'])
&& (
$context['exception'] instanceof \Exception
|| (PHP_VERSION_ID > 70000 && $context['exception'] instanceof \Throwable)
)) {
$this->writeException($context['exception']);
} elseif (isset($context['file']) && $context['line']) {
} elseif (isset($context['file']) && isset($context['line'])) {
$this->writeError($record);
}
}
Expand All @@ -78,9 +82,9 @@ protected function writeError(array $record)
}

/**
* @param \Exception $exception
* @param \Exception|\Throwable $exception
*/
protected function writeException(\Exception $exception)
protected function writeException($exception)
{
$whoopsInspector = new WhoopsInspector($exception);

Expand Down
86 changes: 68 additions & 18 deletions tests/unit/src/Graze/Monolog/Handler/WhoopsHandlerTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Graze\Monolog\Handler;

use Mockery as m;
Expand All @@ -11,7 +12,7 @@ public function setUp()
if (!interface_exists('Whoops\Handler\HandlerInterface', true)) {
$this->markTestSkipped('filp/whoops not installed');
}

$this->handlerWhoops = m::mock('Whoops\Handler\HandlerInterface');
}

Expand All @@ -28,62 +29,111 @@ public function testInterface()
public function testHandleError()
{
$record = $this->getRecord(300, 'test', ['file' => 'bar', 'line' => 1]);

$formatter = m::mock('Monolog\\Formatter\\FormatterInterface');

$handlerMonolog = new WhoopsHandler($this->handlerWhoops);

$this->handlerWhoops
->shouldReceive('setInspector')
->once()
->with(m::type('Whoops\Exception\Inspector'));

$this->handlerWhoops
->shouldReceive('setRun')
->once()
->with(m::type('Whoops\Run'));

$this->handlerWhoops
->shouldReceive('setException')
->once()
->with(m::type('Whoops\Exception\ErrorException'));

$this->handlerWhoops
->shouldReceive('handle')
->once();

$handlerMonolog->handle($record);
}

public function testHandleException()
{
$exception = new \Whoops\Exception\ErrorException('foo');

$record = $this->getRecord(300, 'foo', ['exception' => $exception]);

$formatter = m::mock('Monolog\\Formatter\\FormatterInterface');

$handlerMonolog = new WhoopsHandler($this->handlerWhoops);


$this->handlerWhoops
->shouldReceive('setInspector')
->once()
->with(m::type('Whoops\Exception\Inspector'));

$this->handlerWhoops
->shouldReceive('setRun')
->once()
->with(m::type('Whoops\Run'));

$this->handlerWhoops
->shouldReceive('setException')
->once()
->with($exception);

$this->handlerWhoops
->shouldReceive('handle')
->once();

$handlerMonolog->handle($record);
}

/**
* @requires PHP 7
*/
public function testHandleThrowable()
{
$exception = new \TypeError('foo');
$record = $this->getRecord(300, 'foo', ['exception' => $exception]);
$handlerMonolog = new WhoopsHandler($this->handlerWhoops);

$this->handlerWhoops
->shouldReceive('setInspector')
->once()
->with(m::type('Whoops\Exception\Inspector'));

$this->handlerWhoops
->shouldReceive('setRun')
->once()
->with(m::type('Whoops\Run'));

$this->handlerWhoops
->shouldReceive('setException')
->once()
->with($exception);

$this->handlerWhoops
->shouldReceive('handle')
->once();

$handlerMonolog->handle($record);
}

/**
* @dataProvider nonTriggeringData
*
* @param array $context
*/
public function testContextWithNotEnoughInformationDoesNotTrigger(array $context)
{
$record = $this->getRecord(300, 'test', $context);
$handlerMonolog = new WhoopsHandler($this->handlerWhoops);
$handlerMonolog->handle($record);
}

/**
* @return array
*/
public function nonTriggeringData()
{
return [
[['file' => 'file']],
[['line' => 1]],
[[]],
[['other' => 5]],
];
}
}

0 comments on commit 22fe31a

Please sign in to comment.