فهرست منبع

Fix misleading @throws tag in StreamHandler doc

The `\Exception` for non-existing and not creatable paths is thrown earliest on first write.
Not during StreamHandler instantiation, as claimed by the constructor's `@throws` tag.
Thomas Praxl 6 سال پیش
والد
کامیت
3dcb1919a3
2فایلهای تغییر یافته به همراه26 افزوده شده و 20 حذف شده
  1. 1 2
      src/Monolog/Handler/StreamHandler.php
  2. 25 18
      tests/Monolog/Handler/StreamHandlerTest.php

+ 1 - 2
src/Monolog/Handler/StreamHandler.php

@@ -32,13 +32,12 @@ class StreamHandler extends AbstractProcessingHandler
     private $dirCreated;
 
     /**
-     * @param resource|string $stream
+     * @param resource|string $stream         If a missing path can't be created, an Exception will be thrown on first write
      * @param string|int      $level          The minimum logging level at which this handler will be triggered
      * @param bool            $bubble         Whether the messages that are handled can bubble up the stack or not
      * @param int|null        $filePermission Optional file permissions (default (0644) are only for owner read/write)
      * @param bool            $useLocking     Try to lock log file before doing any writes
      *
-     * @throws \Exception                If a missing directory is not buildable
      * @throws \InvalidArgumentException If stream is not a resource or string
      */
     public function __construct($stream, $level = Logger::DEBUG, bool $bubble = true, ?int $filePermission = null, bool $useLocking = false)

+ 25 - 18
tests/Monolog/Handler/StreamHandlerTest.php

@@ -184,33 +184,40 @@ class StreamHandlerTest extends TestCase
     /**
      * @covers Monolog\Handler\StreamHandler::__construct
      * @covers Monolog\Handler\StreamHandler::write
+     * @dataProvider provideNonExistingAndNotCreatablePath
      */
-    public function testWriteNonExistingAndNotCreatablePath()
+    public function testWriteNonExistingAndNotCreatablePath($nonExistingAndNotCreatablePath)
     {
-        $this->expectException(\Exception::class);
-        $this->expectExceptionMessage('There is no existing directory at');
-
-
         if (defined('PHP_WINDOWS_VERSION_BUILD')) {
             $this->markTestSkipped('Permissions checks can not run on windows');
         }
-        $handler = new StreamHandler('/foo/bar/'.rand(0, 10000).DIRECTORY_SEPARATOR.rand(0, 10000));
-        $handler->handle($this->getRecord());
-    }
 
-    /**
-     * @covers Monolog\Handler\StreamHandler::__construct
-     * @covers Monolog\Handler\StreamHandler::write
-     */
-    public function testWriteNonExistingAndNotCreatableFileResource()
-    {
+        $handler = null;
+
+        try {
+            $handler = new StreamHandler($nonExistingAndNotCreatablePath);
+        } catch (\Exception $fail) {
+            $this->fail(
+                'A non-existing and not creatable path should throw an Exception earliest on first write.
+                 Not during instantiation.'
+            );
+        }
+
         $this->expectException(\Exception::class);
         $this->expectExceptionMessage('There is no existing directory at');
 
-        if (defined('PHP_WINDOWS_VERSION_BUILD')) {
-            $this->markTestSkipped('Permissions checks can not run on windows');
-        }
-        $handler = new StreamHandler('file:///foo/bar/'.rand(0, 10000).DIRECTORY_SEPARATOR.rand(0, 10000));
         $handler->handle($this->getRecord());
     }
+
+    public function provideNonExistingAndNotCreatablePath()
+    {
+        return [
+            '/foo/bar/…' => [
+                '/foo/bar/'.rand(0, 10000).DIRECTORY_SEPARATOR.rand(0, 10000),
+            ],
+            'file:///foo/bar/…' => [
+                'file:///foo/bar/'.rand(0, 10000).DIRECTORY_SEPARATOR.rand(0, 10000),
+            ],
+        ];
+    }
 }