Skip to content

Commit

Permalink
[9.x] Fix BC break for Log feature tests (#42987)
Browse files Browse the repository at this point in the history
* fix logger tests

* update tests for custom log levels

* remove unused import

* revert an old behavior for original log levels in tests
  • Loading branch information
michael-rubel authored Jun 28, 2022
1 parent 49368f4 commit 8f1d548
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
16 changes: 8 additions & 8 deletions src/Illuminate/Foundation/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,15 +265,15 @@ public function report(Throwable $e)
$this->levels, fn ($level, $type) => $e instanceof $type, LogLevel::ERROR
);

$logger->log(
$level,
$e->getMessage(),
array_merge(
$this->exceptionContext($e),
$this->context(),
['exception' => $e]
)
$context = array_merge(
$this->exceptionContext($e),
$this->context(),
['exception' => $e]
);

method_exists($logger, $level)
? $logger->{$level}($e->getMessage(), $context)
: $logger->log($level, $e->getMessage(), $context);
}

/**
Expand Down
17 changes: 9 additions & 8 deletions tests/Foundation/FoundationExceptionsHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function testHandlerReportsExceptionAsContext()
{
$logger = m::mock(LoggerInterface::class);
$this->container->instance(LoggerInterface::class, $logger);
$logger->shouldReceive('log')->withArgs([LogLevel::ERROR, 'Exception message', m::hasKey('exception')])->once();
$logger->shouldReceive('error')->withArgs(['Exception message', m::hasKey('exception')])->once();

$this->handler->report(new RuntimeException('Exception message'));
}
Expand All @@ -83,7 +83,7 @@ public function testHandlerCallsContextMethodIfPresent()
{
$logger = m::mock(LoggerInterface::class);
$this->container->instance(LoggerInterface::class, $logger);
$logger->shouldReceive('log')->withArgs([LogLevel::ERROR, 'Exception message', m::subset(['foo' => 'bar'])])->once();
$logger->shouldReceive('error')->withArgs(['Exception message', m::subset(['foo' => 'bar'])])->once();

$this->handler->report(new ContextProvidingException('Exception message'));
}
Expand All @@ -92,7 +92,7 @@ public function testHandlerReportsExceptionWhenUnReportable()
{
$logger = m::mock(LoggerInterface::class);
$this->container->instance(LoggerInterface::class, $logger);
$logger->shouldReceive('log')->withArgs([LogLevel::ERROR, 'Exception message', m::hasKey('exception')])->once();
$logger->shouldReceive('error')->withArgs(['Exception message', m::hasKey('exception')])->once();

$this->handler->report(new UnReportableException('Exception message'));
}
Expand All @@ -101,16 +101,17 @@ public function testHandlerReportsExceptionWithCustomLogLevel()
{
$logger = m::mock(LoggerInterface::class);
$this->container->instance(LoggerInterface::class, $logger);
$logger->shouldReceive('log')->withArgs([LogLevel::CRITICAL, 'Critical message', m::hasKey('exception')])->once();
$logger->shouldReceive('log')->withArgs([LogLevel::ERROR, 'Error message', m::hasKey('exception')])->once();
$logger->shouldReceive('log')->withArgs([LogLevel::WARNING, 'Warning message', m::hasKey('exception')])->once();

$logger->shouldReceive('critical')->withArgs(['Critical message', m::hasKey('exception')])->once();
$logger->shouldReceive('error')->withArgs(['Error message', m::hasKey('exception')])->once();
$logger->shouldReceive('log')->withArgs(['custom', 'Custom message', m::hasKey('exception')])->once();

$this->handler->level(InvalidArgumentException::class, LogLevel::CRITICAL);
$this->handler->level(OutOfRangeException::class, LogLevel::WARNING);
$this->handler->level(OutOfRangeException::class, 'custom');

$this->handler->report(new InvalidArgumentException('Critical message'));
$this->handler->report(new RuntimeException('Error message'));
$this->handler->report(new OutOfRangeException('Warning message'));
$this->handler->report(new OutOfRangeException('Custom message'));
}

public function testHandlerIgnoresNotReportableExceptions()
Expand Down

0 comments on commit 8f1d548

Please sign in to comment.