Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle whoops with php7 #28

Merged
merged 2 commits into from
Jun 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]],
];
}
}