diff options
-rw-r--r-- | app/class/Logger.php | 16 | ||||
-rw-r--r-- | tests/FilesTest.php | 33 | ||||
-rw-r--r-- | tests/LoggerTest.php | 63 |
3 files changed, 102 insertions, 10 deletions
diff --git a/app/class/Logger.php b/app/class/Logger.php index 939dbf8..5d2695c 100644 --- a/app/class/Logger.php +++ b/app/class/Logger.php @@ -2,6 +2,7 @@ namespace Wcms; +use InvalidArgumentException; use Throwable; /** @@ -10,7 +11,7 @@ use Throwable; */ class Logger { - private static $file = null; + private static $file = false; private static $verbosity = 4; /** @@ -21,7 +22,18 @@ class Logger */ public static function init(string $path, int $verbosity = 4) { - self::$file = fopen($path, "a") or die("Unable to open log file!"); + if (!is_dir(dirname($path))) { + throw new InvalidArgumentException("Parent directory of '$path' does not exist."); + } + if (!is_writable(dirname($path))) { + throw new InvalidArgumentException("Parent directory of '$path' is not writable."); + } + if (is_file($path) && !is_writable($path)) { + throw new InvalidArgumentException("The logfile '$path' is not writable."); + } + self::$file = fopen($path, "a"); + if (self::$file === false) { + } self::$verbosity = $verbosity; } diff --git a/tests/FilesTest.php b/tests/FilesTest.php new file mode 100644 index 0000000..7ab66fe --- /dev/null +++ b/tests/FilesTest.php @@ -0,0 +1,33 @@ +<?php + +namespace Wcms\Tests; + +use PHPUnit\Framework\TestCase; + +/** + * This abstract test class adds 3 usefull variables for files tests: + * - $this->testdir + * - $this->notwritabledir + * - $this->notwritablefile + */ +abstract class FilesTest extends TestCase +{ + protected $testdir = 'build/test'; + protected $notwritabledir = 'build/test/notwritabledir'; + protected $notwritablefile = 'build/test/notwritablefile'; + + protected function setUp(): void + { + parent::setUp(); + if (!is_dir($this->testdir)) { + mkdir($this->testdir, 0755, true); + } + if (!file_exists($this->notwritabledir)) { + mkdir($this->notwritabledir, 0000); + } + if (!file_exists($this->notwritablefile)) { + touch($this->notwritablefile); + chmod($this->notwritablefile, 0000); + } + } +} diff --git a/tests/LoggerTest.php b/tests/LoggerTest.php index 18dcb02..95d6cae 100644 --- a/tests/LoggerTest.php +++ b/tests/LoggerTest.php @@ -3,16 +3,18 @@ namespace Wcms\Tests; use Exception; +use InvalidArgumentException; use PHPUnit\Framework\TestCase; use Throwable; use Wcms\Logger; -class LoggerTest extends TestCase +class LoggerTest extends FilesTest { - protected $logfile = 'build/w_error.log'; + protected $logfile; protected function setUp(): void { + $this->logfile = "$this->testdir/w_error.log"; parent::setUp(); if (file_exists($this->logfile)) { unlink($this->logfile); @@ -26,11 +28,56 @@ class LoggerTest extends TestCase { Logger::init($this->logfile); $this->assertFileExists($this->logfile, 'Log file has not been created.'); + $this->assertIsWritable($this->logfile); $this->assertEmpty(file_get_contents($this->logfile)); } /** * @test + */ + public function initDirNotExistTest(): void + { + $dir = 'not/existing/path'; + $file = "$dir/w_error.log"; + $this->assertDirectoryNotExists($dir); + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage("Parent directory of '$file' does not exist."); + Logger::init($file); + $this->assertFileNotExists($file); + } + + /** + * @test + */ + public function initDirNotWritableTest(): void + { + $dir = $this->notwritabledir; + $file = "$dir/w_error.log"; + $this->assertDirectoryExists($dir); + $this->assertDirectoryNotIsWritable($dir); + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage("Parent directory of '$file' is not writable."); + Logger::init($file); + $this->assertFileNotExists($file); + } + + /** + * @test + */ + public function initNotWritableTest(): void + { + $file = $this->notwritablefile; + $this->assertDirectoryExists(dirname($file)); + $this->assertDirectoryIsWritable(dirname($file)); + $this->assertNotIsWritable($file); + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage("The logfile '$file' is not writable."); + Logger::init($file); + $this->assertFileNotExists($file); + } + + /** + * @test * @dataProvider errorNotLoggedProvider */ public function errorNotLoggedTest(int $verbosity): void @@ -53,7 +100,7 @@ class LoggerTest extends TestCase { Logger::init($this->logfile, $verbosity); Logger::error($msg, ...$args); - $expected = " [ ERROR ] tests/LoggerTest.php(55) $expected\n"; + $expected = " [ ERROR ] tests/LoggerTest.php(102) $expected\n"; $this->assertEquals($expected, substr(file_get_contents($this->logfile), 25)); } @@ -91,7 +138,7 @@ class LoggerTest extends TestCase { Logger::init($this->logfile, $verbosity); Logger::warning($msg, ...$args); - $expected = " [ WARN ] tests/LoggerTest.php(93) $expected\n"; + $expected = " [ WARN ] tests/LoggerTest.php(140) $expected\n"; $this->assertEquals($expected, substr(file_get_contents($this->logfile), 25)); } @@ -128,7 +175,7 @@ class LoggerTest extends TestCase { Logger::init($this->logfile, $verbosity); Logger::info($msg, ...$args); - $expected = " [ INFO ] tests/LoggerTest.php(130) $expected\n"; + $expected = " [ INFO ] tests/LoggerTest.php(177) $expected\n"; $this->assertEquals($expected, substr(file_get_contents($this->logfile), 25)); } @@ -164,7 +211,7 @@ class LoggerTest extends TestCase { Logger::init($this->logfile, $verbosity); Logger::debug($msg, ...$args); - $expected = " [ DEBUG ] tests/LoggerTest.php(166) $expected\n"; + $expected = " [ DEBUG ] tests/LoggerTest.php(213) $expected\n"; $this->assertEquals($expected, substr(file_get_contents($this->logfile), 25)); } @@ -199,7 +246,7 @@ class LoggerTest extends TestCase { Logger::init($this->logfile, $verbosity); Logger::exception($e); - $expected = " [ ERROR ] tests/LoggerTest.php(201) $expected\n"; + $expected = " [ ERROR ] tests/LoggerTest.php(248) $expected\n"; $this->assertEquals($expected, substr(file_get_contents($this->logfile), 25)); } @@ -221,7 +268,7 @@ class LoggerTest extends TestCase Logger::init($this->logfile, 1); Logger::exception(new Exception('Error'), true); $content = file_get_contents($this->logfile); - $expected = " [ ERROR ] tests/LoggerTest.php(222) Error\n"; + $expected = " [ ERROR ] tests/LoggerTest.php(269) Error\n"; $this->assertEquals($expected, substr($content, 25, 43)); $this->assertRegExp('/(#\d+ [\w\/\.]*\(\d+\): .*\)\n)+#\d+ \{main\}\n/U', substr($content, 68)); } |