From b381a973bcca4a2acb01dbe57e37bf5474fb7768 Mon Sep 17 00:00:00 2001 From: Petter Blomberg Date: Thu, 31 May 2018 15:08:33 +0200 Subject: [PATCH 1/2] Make TestHandler::hasRecord assert context, not only message --- src/Monolog/Handler/TestHandler.php | 18 ++++++--- tests/Monolog/Handler/TestHandlerTest.php | 46 +++++++++++++++++++++++ 2 files changed, 59 insertions(+), 5 deletions(-) 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( From f753c68a73e4b716f136a305434e076f1971221f Mon Sep 17 00:00:00 2001 From: Petter Blomberg Date: Thu, 31 May 2018 15:34:07 +0200 Subject: [PATCH 2/2] Make context optional in hasRecord to not break backwards compatibility --- src/Monolog/Handler/TestHandler.php | 20 +++++----- tests/Monolog/Handler/TestHandlerTest.php | 46 +++++++++++------------ 2 files changed, 32 insertions(+), 34 deletions(-) diff --git a/src/Monolog/Handler/TestHandler.php b/src/Monolog/Handler/TestHandler.php index f639594c..9fe0e03d 100644 --- a/src/Monolog/Handler/TestHandler.php +++ b/src/Monolog/Handler/TestHandler.php @@ -86,18 +86,16 @@ class TestHandler extends AbstractProcessingHandler public function hasRecord($record, $level) { + if (is_string($record)) { + $record = array('message' => $record); + } + return $this->hasRecordThatPasses(function ($rec) use ($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; - } + 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 c86e935a..a7c4fc98 100644 --- a/tests/Monolog/Handler/TestHandlerTest.php +++ b/tests/Monolog/Handler/TestHandlerTest.php @@ -56,48 +56,48 @@ class TestHandlerTest extends TestCase public function testHandlerAssertEmptyContext() { $handler = new TestHandler; - $record = $this->getRecord(Logger::WARNING, 'test', []); - $this->assertFalse($handler->hasWarning([ + $record = $this->getRecord(Logger::WARNING, 'test', array()); + $this->assertFalse($handler->hasWarning(array( 'message' => 'test', - 'context' => [], - ])); + 'context' => array(), + ))); $handler->handle($record); - $this->assertTrue($handler->hasWarning([ + $this->assertTrue($handler->hasWarning(array( 'message' => 'test', - 'context' => [], - ])); - $this->assertFalse($handler->hasWarning([ + 'context' => array(), + ))); + $this->assertFalse($handler->hasWarning(array( 'message' => 'test', - 'context' => [ + 'context' => array( 'foo' => 'bar' - ], - ])); + ), + ))); } public function testHandlerAssertNonEmptyContext() { $handler = new TestHandler; - $record = $this->getRecord(Logger::WARNING, 'test', ['foo' => 'bar']); - $this->assertFalse($handler->hasWarning([ + $record = $this->getRecord(Logger::WARNING, 'test', array('foo' => 'bar')); + $this->assertFalse($handler->hasWarning(array( 'message' => 'test', - 'context' => [ + 'context' => array( 'foo' => 'bar' - ], - ])); + ), + ))); $handler->handle($record); - $this->assertTrue($handler->hasWarning([ + $this->assertTrue($handler->hasWarning(array( 'message' => 'test', - 'context' => [ + 'context' => array( 'foo' => 'bar' - ], - ])); - $this->assertFalse($handler->hasWarning([ + ), + ))); + $this->assertFalse($handler->hasWarning(array( 'message' => 'test', - 'context' => [], - ])); + 'context' => array(), + ))); } public function methodProvider()