diff --git a/src/Monolog/Handler/StreamHandler.php b/src/Monolog/Handler/StreamHandler.php index 0c2f8d1b..4cce3c39 100644 --- a/src/Monolog/Handler/StreamHandler.php +++ b/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 UnexpectedValueException 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) diff --git a/tests/Monolog/Handler/StreamHandlerTest.php b/tests/Monolog/Handler/StreamHandlerTest.php index f8101dba..8e0d111f 100644 --- a/tests/Monolog/Handler/StreamHandlerTest.php +++ b/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 = 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(\UnexpectedValueException::class); + $this->expectExceptionMessage('There is no existing directory at'); + $handler->handle($this->getRecord()); } - /** - * @covers Monolog\Handler\StreamHandler::__construct - * @covers Monolog\Handler\StreamHandler::write - */ - public function testWriteNonExistingAndNotCreatableFileResource() + public function provideNonExistingAndNotCreatablePath() { - $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()); + 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), + ], + ]; } }