Passed
Pull Request — master (#7)
by Dmitriy
01:22
created

FileRotatorTest::testRotateMaxFileSize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 16
rs 9.9332
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Log\Target\File\Tests;
6
7
use PHPUnit\Framework\TestCase;
8
use Yiisoft\Files\FileHelper;
9
use Yiisoft\Log\Logger;
10
use Yiisoft\Log\Target\File\FileRotator;
11
use Yiisoft\Log\Target\File\FileTarget;
12
13
final class FileRotatorTest extends TestCase
14
{
15
    /**
16
     * @dataProvider booleanDataProvider()
17
     * @param bool $rotateByCopy
18
     */
19
    public function testRotateByCopy(bool $rotateByCopy): void
20
    {
21
        $logFile = $this->getLogFilePath();
22
        $rotator = new FileRotator(1, 2, 777, $rotateByCopy);
23
        $fileTarget = new FileTarget($logFile, $rotator);
24
25
        $logger = new Logger([
26
            'file' => $fileTarget,
27
        ]);
28
29
        $logger->debug(str_repeat('x', 1024));
30
        $logger->flush(true);
31
        self::assertFileExists($logFile);
32
        self::assertFileDoesNotExist($logFile . '.1');
33
34
        $nonRotatedFileContent = file_get_contents($logFile);
35
36
        $logger->debug("\0");
37
        $logger->flush(true);
38
39
        self::assertFileExists($logFile);
40
        self::assertFileExists($logFile . '.1');
41
42
        self::assertEquals($nonRotatedFileContent, file_get_contents($logFile . '.1'));
43
    }
44
45
    /**
46
     * @dataProvider filesCountProvider()
47
     * @param int $filesCount
48
     */
49
    public function testRotateMaxFiles(int $filesCount): void
50
    {
51
        $logFile = $this->getLogFilePath();
52
        $rotator = new FileRotator(1, $filesCount, null, true);
53
        $fileTarget = new FileTarget($logFile, $rotator);
54
55
        $logger = new Logger([
56
            'file' => $fileTarget,
57
        ]);
58
59
        foreach (range(1, $filesCount+2) as $i) {
60
            /** @noinspection DisconnectedForeachInstructionInspection */
61
            $logger->debug(str_repeat('x', 1024));
62
            /** @noinspection DisconnectedForeachInstructionInspection */
63
            $logger->flush(true);
64
        }
65
66
        self::assertFileExists($logFile);
67
68
        foreach (range(1, $filesCount) as $counter) {
69
            $filesName = $counter !== 1 ? $logFile . '.' . ($counter - 1) : $logFile;
70
            self::assertFileExists($filesName);
71
        }
72
73
        // check that next file does not exist
74
        $filesCount++;
75
        self::assertFileDoesNotExist("{$logFile}.{$filesCount}");
76
    }
77
78
    /**
79
     * @dataProvider filesSizesProvider()
80
     * @param int $maxFileSize
81
     */
82
    public function testRotateMaxFileSize(int $maxFileSize): void
83
    {
84
        $logFile = $this->getLogFilePath();
85
        $rotator = new FileRotator($maxFileSize, 2, null, true);
86
        $fileTarget = new FileTarget($logFile, $rotator);
87
88
        $logger = new Logger([
89
            'file' => $fileTarget,
90
        ]);
91
92
        $logger->debug(str_repeat('x', $maxFileSize * 1024));
93
        $logger->flush(true);
94
95
        clearstatcache();
96
        self::assertFileExists($logFile);
97
        self::assertGreaterThan($maxFileSize, filesize($logFile) / 1024);
98
    }
99
100
    public function testDefaultMaxFileSize(): void
101
    {
102
        $rotator = new FileRotator();
103
        self::assertEquals(10240, $rotator->getMaxFileSize());
104
    }
105
106
    public function testMaxFileSizeLowerThanOne(): void
107
    {
108
        $rotator = new FileRotator(-1);
109
        self::assertEquals(1, $rotator->getMaxFileSize());
110
    }
111
112
    public function testSetMaxFileSizeLowerThanOne(): void
113
    {
114
        $rotator = new FileRotator();
115
        $rotator->setMaxFileSize(-1);
116
        self::assertEquals(1, $rotator->getMaxFileSize());
117
    }
118
119
    public function testDefaultMaxFiles(): void
120
    {
121
        $rotator = new FileRotator();
122
        self::assertEquals(5, $rotator->getMaxFiles());
123
    }
124
125
    public function testMaxFilesLowerThanOne(): void
126
    {
127
        $rotator = new FileRotator(0, -1);
128
        self::assertEquals(1, $rotator->getMaxFiles());
129
    }
130
131
    public function testSetMaxFilesLowerThanOne(): void
132
    {
133
        $rotator = new FileRotator();
134
        $rotator->setMaxFiles(-1);
135
        self::assertEquals(1, $rotator->getMaxFiles());
136
    }
137
138
    public function booleanDataProvider(): array
139
    {
140
        return [
141
            [true],
142
            [false],
143
        ];
144
    }
145
146
    public function filesCountProvider(): array
147
    {
148
        return [
149
            [1],
150
            [2],
151
            [3],
152
            [4],
153
            [10],
154
        ];
155
    }
156
157
    public function filesSizesProvider(): array
158
    {
159
        return [
160
            [2],
161
            [5],
162
            [10],
163
            [20],
164
            [100],
165
        ];
166
    }
167
168
    protected function setUp(): void
169
    {
170
        FileHelper::removeDirectory(dirname($this->getLogFilePath()));
171
        mkdir(dirname($this->getLogFilePath()), 0777, true);
172
    }
173
174
    protected function tearDown(): void
175
    {
176
        FileHelper::removeDirectory(dirname($this->getLogFilePath()));
177
    }
178
179
    private function getLogFilePath(): string
180
    {
181
        return __DIR__ . '/runtime/log/file-target-test.log';
182
    }
183
}
184