diff options
| -rw-r--r-- | src/Drivers/FileSystemDriver.php | 24 | ||||
| -rw-r--r-- | tests/FileSystemDriverTest.php | 28 |
2 files changed, 10 insertions, 42 deletions
diff --git a/src/Drivers/FileSystemDriver.php b/src/Drivers/FileSystemDriver.php index 551b741..292ba63 100644 --- a/src/Drivers/FileSystemDriver.php +++ b/src/Drivers/FileSystemDriver.php @@ -29,27 +29,19 @@ class FileSystemDriver implements Driver $file_name = "{$date->format('Y-m-d')}.log"; $path = $this->directory . DIRECTORY_SEPARATOR . $file_name; - // If the log file does not exist, try creating one - if (!$this->exists($path)) { - throw new \RuntimeException('Log file could not be created at path: ' . $path); - } - - // We managed to get this far, but still can't write to the log file - if (!$this->isWriteable($path)) { - throw new \RuntimeException('Log file is not writeable at path: ' . $path); - } - + $this->createLogFileIfNotExist($path); $this->write($path, $serializedMessage . PHP_EOL); } - public function exists(string $path): bool + public function createLogFileIfNotExist(string $path): void { - return file_exists($path); - } + if (!file_exists($this->directory)) { + mkdir($this->directory, 0600, true); + } - public function isWriteable(string $path): bool - { - return is_writeable($path); + if (is_writable($this->directory)) { + touch($path); + } } public function write(string $path, string $data): void diff --git a/tests/FileSystemDriverTest.php b/tests/FileSystemDriverTest.php index a05b5a9..a50adcc 100644 --- a/tests/FileSystemDriverTest.php +++ b/tests/FileSystemDriverTest.php @@ -132,35 +132,11 @@ class FileSystemDriverTest extends MockeryTestCase $this->assertStringContainsString("[{$full_date}] FileSystemDriverTest.DEBUG: test", $last_line); } - public function testNoPath(): void - { - $loggr = new Loggr(new FileSystemDriver('path-does-not-exist')); - $loggr->emergency('test'); - $sep = DIRECTORY_SEPARATOR; - $date = (new DateTime())->format('Y-m-d'); - $this->assertEquals("Log file could not be created at path: path-does-not-exist{$sep}{$date}.log", $loggr->error); - } - - public function testCantWrite(): void - { - $driver = Mockery::mock(FileSystemDriver::class, [''])->makePartial(); - $driver->shouldReceive('log')->passthru(); - $driver->shouldReceive('exists')->andReturn(true); - $driver->shouldReceive('isWriteable')->andReturns(false); - $driver->shouldReceive('write')->never(); - $loggr = new Loggr($driver); - $loggr->emergency('test'); - $sep = DIRECTORY_SEPARATOR; - $date = (new DateTime())->format('Y-m-d'); - $this->assertEquals("Log file is not writeable at path: {$sep}{$date}.log", $loggr->error); - } - private function mockLogger(string &$result): Loggr { - $driver = Mockery::mock(FileSystemDriver::class, [''])->makePartial(); + $driver = Mockery::mock(FileSystemDriver::class, ['/tmp'])->makePartial(); $driver->shouldReceive('log')->passthru(); - $driver->shouldReceive('exists')->andReturn(true); - $driver->shouldReceive('isWriteable')->andReturns(true); + $driver->shouldReceive('createLogFileIfNotExist')->andReturn(true); $driver->shouldReceive('write')->withArgs(function (string $path, string $data) use (&$result) { $result .= $data; |
