1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Koded\Logging\Processors; |
4
|
|
|
|
5
|
|
|
use org\bovigo\vfs\{vfsStream, vfsStreamDirectory, vfsStreamWrapper}; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
|
8
|
|
|
class FileTest extends TestCase |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var vfsStreamDirectory |
12
|
|
|
*/ |
13
|
|
|
private $dir; |
14
|
|
|
|
15
|
|
|
public function test_update() |
16
|
|
|
{ |
17
|
|
|
$subdirectory = date('Y/m'); |
18
|
|
|
$file = date('d') . '.log'; |
19
|
|
|
|
20
|
|
|
$processor = new File(['dir' => $this->dir->url()]); |
21
|
|
|
$processor->update([ |
22
|
|
|
[ |
23
|
|
|
'level' => -1, |
24
|
|
|
'levelname' => 'DEBUG', |
25
|
|
|
'message' => 'Test 1', |
26
|
|
|
'timestamp' => 1234567890 |
27
|
|
|
], |
28
|
|
|
[ |
29
|
|
|
'level' => -1, |
30
|
|
|
'levelname' => 'DEBUG', |
31
|
|
|
'message' => 'Test 2', |
32
|
|
|
'timestamp' => 1234567891 |
33
|
|
|
] |
34
|
|
|
]); |
35
|
|
|
|
36
|
|
|
$this->assertSame('', $processor->formatted()); |
37
|
|
|
$this->assertTrue($this->dir->hasChild($subdirectory)); |
38
|
|
|
|
39
|
|
|
$content = $this->dir->getChild($subdirectory . DIRECTORY_SEPARATOR . $file)->getContent(); |
40
|
|
|
$this->assertStringContainsString("1234567891 [DEBUG]: Test 2\n", $content); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function test_when_directory_does_not_exist() |
44
|
|
|
{ |
45
|
|
|
$dir = $this->dir->url() . '/nonexistent'; |
46
|
|
|
|
47
|
|
|
$this->expectException(FileProcessorException::class); |
48
|
|
|
$this->expectExceptionMessage('Log directory "' . $dir . '" must exist'); |
49
|
|
|
|
50
|
|
|
$processor = new File(['dir' => $dir]); |
51
|
|
|
$processor->update([]); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function test_when_directory_is_not_writable() |
55
|
|
|
{ |
56
|
|
|
$dir = $this->dir->url(); |
57
|
|
|
|
58
|
|
|
$this->expectException(FileProcessorException::class); |
59
|
|
|
$this->expectExceptionMessage('Log directory "' . $dir . '" must be writable'); |
60
|
|
|
|
61
|
|
|
vfsStreamWrapper::getRoot()->chmod(0400); |
62
|
|
|
$processor = new File(['dir' => $dir]); |
63
|
|
|
$processor->update([]); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
protected function setUp(): void |
67
|
|
|
{ |
68
|
|
|
$this->dir = vfsStream::setup(); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|