diff --git a/src/Rotation.php b/src/Rotation.php index 3d94791..bd286c6 100644 --- a/src/Rotation.php +++ b/src/Rotation.php @@ -154,6 +154,10 @@ private function sucessfull(string $filenameSource, ?string $filenameRotated): v */ private function canRotate(string $filename): bool { + if (!is_file($filename)) { + return false; + } + if (!$this->fileIsValid($filename)) { $this->exception( new Exception(sprintf('the file %s not is valid.', $filename), 10) @@ -176,9 +180,9 @@ private function initProcessorFile(string $filename): void /** * check if file is valid to rotate. */ - private function fileIsValid(?string $filename): bool + private function fileIsValid(string $filename): bool { - return $filename && is_file($filename) && is_writable($filename); + return is_writable($filename); } /** diff --git a/tests/ErrorHandlerTest.php b/tests/ErrorHandlerTest.php index 9b754fa..825e684 100644 --- a/tests/ErrorHandlerTest.php +++ b/tests/ErrorHandlerTest.php @@ -13,6 +13,9 @@ public function testThrowsException() $rotation = new Rotation(); + touch(self::DIR_WORK.'/file.log'); + chmod(self::DIR_WORK.'/file.log', 0444); + $result = $rotation->rotate(self::DIR_WORK.'file.log'); $this->assertFalse($result); @@ -22,6 +25,9 @@ public function testCatchException() { $rotation = new Rotation(); + touch(self::DIR_WORK.'/file.log'); + chmod(self::DIR_WORK.'/file.log', 0444); + $result = $rotation ->catch(function (RotationFailed $exception) { $this->assertEquals( diff --git a/tests/RotationTest.php b/tests/RotationTest.php index 0d04162..5aaf5e7 100644 --- a/tests/RotationTest.php +++ b/tests/RotationTest.php @@ -2,16 +2,12 @@ namespace Cesargb\Log\Test; -use Exception; use Cesargb\Log\Rotation; -use Cesargb\Log\Test\TestCase; class RotationTest extends TestCase { - public function test_log_rotating_if_file_not_exists() + public function testLogRotatingIfFileNotExists() { - $this->expectException(Exception::class); - $rotation = new Rotation(); $result = $rotation->rotate(self::DIR_WORK.'file.log'); @@ -19,7 +15,7 @@ public function test_log_rotating_if_file_not_exists() $this->assertFalse($result); } - public function test_not_rotate_if_file_is_empty() + public function testNotRotateIfFileIsEmpty() { touch(self::DIR_WORK.'file.log'); @@ -32,7 +28,7 @@ public function test_not_rotate_if_file_is_empty() $this->assertFileDoesNotExist(self::DIR_WORK.'file.log.1'); } - public function test_option_nocompress() + public function testOptionNocompress() { file_put_contents(self::DIR_WORK.'file.log', microtime(true)); @@ -45,7 +41,7 @@ public function test_option_nocompress() $this->assertFileExists(self::DIR_WORK.'file.log.1'); } - public function test_option_compress() + public function testOptionCompress() { file_put_contents(self::DIR_WORK.'file.log', microtime(true)); @@ -56,7 +52,7 @@ public function test_option_compress() $this->assertFileExists(self::DIR_WORK.'file.log.1.gz'); } - public function test_option_files() + public function testOptionFiles() { $maxFiles = 5; @@ -74,10 +70,9 @@ public function test_option_files() } $this->assertFileDoesNotExist(self::DIR_WORK.'file.log.'.($maxFiles + 1)); - } - public function test_option_files_only_one() + public function testOptionFilesOnlyOne() { $filesToCreate = 5; @@ -95,7 +90,7 @@ public function test_option_files_only_one() $this->assertFileDoesNotExist(self::DIR_WORK.'file.log.2'); } - public function test_option_minsize() + public function testOptionMinsize() { file_put_contents(self::DIR_WORK.'file.log', microtime(true));