Просмотр исходного кода

Make context optional in hasRecord to not break backwards compatibility

Petter Blomberg 7 лет назад
Родитель
Сommit
f753c68a73
2 измененных файлов с 32 добавлено и 34 удалено
  1. 9 11
      src/Monolog/Handler/TestHandler.php
  2. 23 23
      tests/Monolog/Handler/TestHandlerTest.php

+ 9 - 11
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);

+ 23 - 23
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()