|
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\Target\File\FileTarget; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @group log |
|
13
|
|
|
*/ |
|
14
|
|
|
final class FileTargetTest extends TestCase |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* Tests that log directory isn't created during init process |
|
18
|
|
|
* |
|
19
|
|
|
* @see https://github.com/yiisoft/yii2/issues/15662 |
|
20
|
|
|
*/ |
|
21
|
|
|
public function testInit(): void |
|
22
|
|
|
{ |
|
23
|
|
|
FileHelper::removeDirectory(dirname($this->getLogFilePath())); |
|
24
|
|
|
$logFile = $this->getLogFilePath(); |
|
25
|
|
|
new FileTarget($logFile); |
|
26
|
|
|
self::assertFileDoesNotExist( |
|
27
|
|
|
dirname($logFile), |
|
28
|
|
|
'Log directory should not be created during init process' |
|
29
|
|
|
); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function testExportEmptyFile(): void |
|
33
|
|
|
{ |
|
34
|
|
|
FileHelper::removeDirectory(dirname($this->getLogFilePath())); |
|
35
|
|
|
|
|
36
|
|
|
$logFile = $this->getLogFilePath(); |
|
37
|
|
|
$target = new FileTarget($logFile, null, 0777, 0777); |
|
38
|
|
|
|
|
39
|
|
|
$target->export(); |
|
40
|
|
|
|
|
41
|
|
|
self::assertDirectoryExists(dirname($logFile)); |
|
42
|
|
|
self::assertFileExists($logFile); |
|
43
|
|
|
self::assertEquals("\n", file_get_contents($logFile)); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function testExportMessages(): void |
|
47
|
|
|
{ |
|
48
|
|
|
FileHelper::removeDirectory(dirname($this->getLogFilePath())); |
|
49
|
|
|
|
|
50
|
|
|
$logFile = $this->getLogFilePath(); |
|
51
|
|
|
$target = new FileTarget($logFile, null, 0777, 0777); |
|
52
|
|
|
$target->setMessages([ |
|
53
|
|
|
['level', 'text', ['category' => 'alert', 'time' => 123]] |
|
54
|
|
|
]); |
|
55
|
|
|
|
|
56
|
|
|
$target->export(); |
|
57
|
|
|
|
|
58
|
|
|
self::assertDirectoryExists(dirname($logFile)); |
|
59
|
|
|
self::assertFileExists($logFile); |
|
60
|
|
|
self::assertEquals("1970-01-01 00:02:03.000000 [level][alert] text\n", file_get_contents($logFile)); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
private function getLogFilePath(): string |
|
64
|
|
|
{ |
|
65
|
|
|
return __DIR__ . '/runtime/log/file-target-test.log'; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|