Răsfoiți Sursa

Make handlers more serializable by default by having them close() before sleeping, fixes #365

Jordi Boggiano 9 ani în urmă
părinte
comite
af7c0a7bda

+ 7 - 0
src/Monolog/Handler/Handler.php

@@ -43,4 +43,11 @@ abstract class Handler implements HandlerInterface
             // do nothing
         }
     }
+
+    public function __sleep()
+    {
+        $this->close();
+
+        return array_keys(get_object_vars($this));
+    }
 }

+ 30 - 5
tests/Monolog/Handler/StreamHandlerTest.php

@@ -51,13 +51,38 @@ class StreamHandlerTest extends TestCase
     {
         $handler = new StreamHandler('php://memory');
         $handler->handle($this->getRecord(Logger::WARNING, 'test'));
-        $streamProp = new \ReflectionProperty('Monolog\Handler\StreamHandler', 'stream');
-        $streamProp->setAccessible(true);
-        $handle = $streamProp->getValue($handler);
+        $stream = $handler->getStream();
 
-        $this->assertTrue(is_resource($handle));
+        $this->assertTrue(is_resource($stream));
         $handler->close();
-        $this->assertFalse(is_resource($handle));
+        $this->assertFalse(is_resource($stream));
+    }
+
+    /**
+     * @covers Monolog\Handler\StreamHandler::close
+     * @covers Monolog\Handler\Handler::__sleep
+     */
+    public function testSerialization()
+    {
+        $handler = new StreamHandler('php://memory');
+        $handler->handle($this->getRecord(Logger::WARNING, 'testfoo'));
+        $stream = $handler->getStream();
+
+        $this->assertTrue(is_resource($stream));
+        fseek($stream, 0);
+        $this->assertContains('testfoo', stream_get_contents($stream));
+        $serialized = serialize($handler);
+        $this->assertFalse(is_resource($stream));
+
+        $handler = unserialize($serialized);
+        $handler->handle($this->getRecord(Logger::WARNING, 'testbar'));
+        $stream = $handler->getStream();
+
+        $this->assertTrue(is_resource($stream));
+        fseek($stream, 0);
+        $contents = stream_get_contents($stream);
+        $this->assertNotContains('testfoo', $contents);
+        $this->assertContains('testbar', $contents);
     }
 
     /**