diff --git a/src/Monolog/Handler/TestHandler.php b/src/Monolog/Handler/TestHandler.php index 3df7b6da..5686265d 100644 --- a/src/Monolog/Handler/TestHandler.php +++ b/src/Monolog/Handler/TestHandler.php @@ -86,12 +86,18 @@ class TestHandler extends AbstractProcessingHandler public function hasRecord($record, $level) { - if (is_array($record)) { - $record = $record['message']; + if (is_string($record)) { + $record = array('message' => $record); } return $this->hasRecordThatPasses(function ($rec) use ($record) { - return $rec['message'] === $record; + if ($rec['message'] !== $record['message']) { + return false; + } + if (isset($record['context']) && $rec['context'] !== $record['context']) { + return false; + } + return true; }, $level); } diff --git a/tests/Monolog/Handler/TestHandlerTest.php b/tests/Monolog/Handler/TestHandlerTest.php index db3f01ff..9214983f 100644 --- a/tests/Monolog/Handler/TestHandlerTest.php +++ b/tests/Monolog/Handler/TestHandlerTest.php @@ -54,6 +54,52 @@ class TestHandlerTest extends TestCase $this->assertEquals([$record], $records); } + public function testHandlerAssertEmptyContext() { + $handler = new TestHandler; + $record = $this->getRecord(Logger::WARNING, 'test', []); + $this->assertFalse($handler->hasWarning([ + 'message' => 'test', + 'context' => [], + ])); + + $handler->handle($record); + + $this->assertTrue($handler->hasWarning([ + 'message' => 'test', + 'context' => [], + ])); + $this->assertFalse($handler->hasWarning([ + 'message' => 'test', + 'context' => [ + 'foo' => 'bar' + ], + ])); + } + + public function testHandlerAssertNonEmptyContext() { + $handler = new TestHandler; + $record = $this->getRecord(Logger::WARNING, 'test', ['foo' => 'bar']); + $this->assertFalse($handler->hasWarning([ + 'message' => 'test', + 'context' => [ + 'foo' => 'bar' + ], + ])); + + $handler->handle($record); + + $this->assertTrue($handler->hasWarning([ + 'message' => 'test', + 'context' => [ + 'foo' => 'bar' + ], + ])); + $this->assertFalse($handler->hasWarning([ + 'message' => 'test', + 'context' => [], + ])); + } + public function methodProvider() { return [