Skip to content

Not throw exception if file to rotate does not exists #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/Rotation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
}

/**
Expand Down
6 changes: 6 additions & 0 deletions tests/ErrorHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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(
Expand Down
19 changes: 7 additions & 12 deletions tests/RotationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,20 @@

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');

$this->assertFalse($result);
}

public function test_not_rotate_if_file_is_empty()
public function testNotRotateIfFileIsEmpty()
{
touch(self::DIR_WORK.'file.log');

Expand All @@ -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));

Expand All @@ -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));

Expand All @@ -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;

Expand All @@ -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;

Expand All @@ -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));

Expand Down