Skip to content

Commit

Permalink
[8.x] Allow reporting reportable exceptions with the default logger (#…
Browse files Browse the repository at this point in the history
…33323)

* Fix Foundation exception tests

* Add test for reporting an unreportable exception

* Reporting unreported reportable exceptions
  • Loading branch information
netpok authored Jun 24, 2020
1 parent 33d5be2 commit b9cccd7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/Illuminate/Foundation/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ public function report(Throwable $e)
}

if (is_callable($reportCallable = [$e, 'report'])) {
$this->container->call($reportCallable);

return;
if ($this->container->call($reportCallable) !== false) {
return;
}
}

try {
Expand Down
30 changes: 26 additions & 4 deletions tests/Foundation/FoundationExceptionsHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Illuminate\Validation\ValidationException;
use Illuminate\Validation\Validator;
use Mockery as m;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use RuntimeException;
Expand All @@ -28,6 +29,8 @@

class FoundationExceptionsHandlerTest extends TestCase
{
use MockeryPHPUnitIntegration;

protected $config;

protected $container;
Expand Down Expand Up @@ -60,25 +63,36 @@ protected function setUp(): void

protected function tearDown(): void
{
m::close();

Container::setInstance(null);
}

public function testHandlerReportsExceptionAsContext()
{
$logger = m::mock(LoggerInterface::class);
$this->container->instance(LoggerInterface::class, $logger);
$logger->shouldReceive('error')->withArgs(['Exception message', m::hasKey('exception')]);
$logger->shouldReceive('error')->withArgs(['Exception message', m::hasKey('exception')])->once();

$this->handler->report(new RuntimeException('Exception message'));
}

public function testHandlerReportsExceptionWhenUnReportable()
{
$logger = m::mock(LoggerInterface::class);
$this->container->instance(LoggerInterface::class, $logger);
$logger->shouldReceive('error')->withArgs(['Exception message', m::hasKey('exception')])->once();

$this->handler->report(new UnReportableException('Exception message'));
}

public function testHandlerCallsReportMethodWithDependencies()
{
$reporter = m::mock(ReportingService::class);
$this->container->instance(ReportingService::class, $reporter);
$reporter->shouldReceive('send')->withArgs(['Exception message']);
$reporter->shouldReceive('send')->withArgs(['Exception message'])->once();

$logger = m::mock(LoggerInterface::class);
$this->container->instance(LoggerInterface::class, $logger);
$logger->shouldNotReceive('error');

$this->handler->report(new ReportableException('Exception message'));
}
Expand Down Expand Up @@ -226,6 +240,14 @@ public function report(ReportingService $reportingService)
}
}

class UnReportableException extends Exception
{
public function report()
{
return false;
}
}

interface ReportingService
{
public function send($message);
Expand Down

0 comments on commit b9cccd7

Please sign in to comment.