diff --git a/src/Monolog/Handler/TestHandler.php b/src/Monolog/Handler/TestHandler.php index e39cfc66..f639594c 100644 --- a/src/Monolog/Handler/TestHandler.php +++ b/src/Monolog/Handler/TestHandler.php @@ -86,12 +86,20 @@ class TestHandler extends AbstractProcessingHandler public function hasRecord($record, $level) { - if (is_array($record)) { - $record = $record['message']; - } - return $this->hasRecordThatPasses(function ($rec) use ($record) { - return $rec['message'] === $record; + if (is_array($record)) { + if ($rec['message'] !== $record['message']) { + return false; + } + if ($rec['context'] !== $record['context']) { + return false; + } + } else { + if ($rec['message'] !== $record) { + return false; + } + } + return true; }, $level); } diff --git a/tests/Monolog/Handler/TestHandlerTest.php b/tests/Monolog/Handler/TestHandlerTest.php index bfb8d3df..c86e935a 100644 --- a/tests/Monolog/Handler/TestHandlerTest.php +++ b/tests/Monolog/Handler/TestHandlerTest.php @@ -54,6 +54,52 @@ class TestHandlerTest extends TestCase $this->assertEquals(array($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 array(