Procházet zdrojové kódy

Tested the TestHandler (WTF)

Jordi Boggiano před 14 roky
rodič
revize
7a56a1c73f

+ 10 - 7
src/Monolog/Handler/TestHandler.php

@@ -90,15 +90,18 @@ class TestHandler extends AbstractProcessingHandler
         return isset($this->recordsByLevel[Logger::DEBUG]);
     }
 
-    protected function hasRecord($record, $level = null)
+    protected function hasRecord($record, $level)
     {
-        if (null === $level) {
-            $records = $this->records;
-        } else {
-            $records = $this->recordsByLevel[$level];
+        if (!isset($this->recordsByLevel[$level])) {
+            return false;
         }
-        foreach ($records as $msg) {
-            if ($msg['message'] === $record) {
+
+        if (is_array($record)) {
+            $record = $record['message'];
+        }
+
+        foreach ($this->recordsByLevel[$level] as $rec) {
+            if ($rec['message'] === $record) {
                 return true;
             }
         }

+ 54 - 0
tests/Monolog/Handler/TestHandlerTest.php

@@ -0,0 +1,54 @@
+<?php
+
+/*
+ * This file is part of the Monolog package.
+ *
+ * (c) Jordi Boggiano <j.boggiano@seld.be>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Monolog\Handler;
+
+use Monolog\TestCase;
+use Monolog\Logger;
+
+/**
+ * @covers Monolog\Handler\TestHandler
+ */
+class TestHandlerTest extends TestCase
+{
+    /**
+     * @dataProvider methodProvider
+     */
+    public function testHandler($method, $level)
+    {
+        $handler = new TestHandler;
+        $record = $this->getRecord($level, 'test'.$method);
+        $this->assertFalse($handler->{'has'.$method}($record));
+        $this->assertFalse($handler->{'has'.$method.'Records'}());
+        $handler->handle($record);
+
+        $this->assertFalse($handler->{'has'.$method}('bar'));
+        $this->assertTrue($handler->{'has'.$method}($record));
+        $this->assertTrue($handler->{'has'.$method}('test'.$method));
+        $this->assertTrue($handler->{'has'.$method.'Records'}());
+
+        $records = $handler->getRecords();
+        unset($records[0]['formatted']);
+        $this->assertEquals(array($record), $records);
+    }
+
+    public function methodProvider()
+    {
+        return array(
+            array('Alert'   , Logger::ALERT),
+            array('Critical', Logger::CRITICAL),
+            array('Error'   , Logger::ERROR),
+            array('Warning' , Logger::WARNING),
+            array('Info'    , Logger::INFO),
+            array('Debug'   , Logger::DEBUG),
+        );
+    }
+}