summaryrefslogtreecommitdiff
path: root/tests/FileSystemDriverTest.php
blob: 850f7d786f288f1eaffc6b0c75f864f233423fa4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php

namespace Asko\Loggr\Tests;

use DateTime;
use Asko\Loggr\Drivers\FileSystemDriver;
use Asko\Loggr\Loggr;
use Mockery;
use Mockery\Adapter\Phpunit\MockeryTestCase;

class FileSystemDriverTest extends MockeryTestCase
{
    /**
     * @throws \Exception
     */
    public function testEmergency(): void
    {
        $data = '';
        $loggr = $this->mockLogger($data);
        $loggr->emergency('test');
        $full_date = (new DateTime)->format('Y-m-d H:i:s');
        $this->assertStringContainsString("[{$full_date}] FileSystemDriverTest.EMERGENCY: test", $data);
    }

    /**
     * @throws \Exception
     */
    public function testAlert(): void
    {
        $data = '';
        $loggr = $this->mockLogger($data);
        $loggr->alert('test');
        $full_date = (new DateTime)->format('Y-m-d H:i:s');
        $this->assertStringContainsString("[{$full_date}] FileSystemDriverTest.ALERT: test", $data);
    }

    /**
     * @throws \Exception
     */
    public function testCritical(): void
    {
        $data = '';
        $loggr = $this->mockLogger($data);
        $loggr->critical('test');
        $full_date = (new DateTime)->format('Y-m-d H:i:s');
        $this->assertStringContainsString("[{$full_date}] FileSystemDriverTest.CRITICAL: test", $data);
    }

    /**
     * @throws \Exception
     */
    public function testError(): void
    {
        $data = '';
        $loggr = $this->mockLogger($data);
        $loggr->error('test');
        $full_date = (new DateTime)->format('Y-m-d H:i:s');
        $this->assertStringContainsString("[{$full_date}] FileSystemDriverTest.ERROR: test", $data);
    }

    /**
     * @throws \Exception
     */
    public function testWarning(): void
    {
        $data = '';
        $loggr = $this->mockLogger($data);
        $loggr->warning('test');
        $full_date = (new DateTime)->format('Y-m-d H:i:s');
        $this->assertStringContainsString("[{$full_date}] FileSystemDriverTest.WARNING: test", $data);
    }

    /**
     * @throws \Exception
     */
    public function testNotice(): void
    {
        $data = '';
        $loggr = $this->mockLogger($data);
        $loggr->notice('test');
        $full_date = (new DateTime)->format('Y-m-d H:i:s');
        $this->assertStringContainsString("[{$full_date}] FileSystemDriverTest.NOTICE: test", $data);
    }

    /**
     * @throws \Exception
     */
    public function testInfo(): void
    {
        $data = '';
        $loggr = $this->mockLogger($data);
        $loggr->info('test');
        $full_date = (new DateTime)->format('Y-m-d H:i:s');
        $this->assertStringContainsString("[{$full_date}] FileSystemDriverTest.INFO: test", $data);
    }

    /**
     * @throws \Exception
     */
    public function testDebug(): void
    {
        $data = '';
        $loggr = $this->mockLogger($data);
        $loggr->debug('test');
        $full_date = (new DateTime)->format('Y-m-d H:i:s');
        $this->assertStringContainsString("[{$full_date}] FileSystemDriverTest.DEBUG: test", $data);
    }

    /**
     * @throws \Exception
     */
    public function testMultipleEntries(): void
    {
        $data = '';
        $loggr = $this->mockLogger($data);
        $loggr->emergency('test1');
        $loggr->alert('test2');
        $loggr->critical('test3');
        $loggr->error('test4');
        $loggr->warning('test5');
        $loggr->notice('test6');
        $loggr->info('test7');
        $loggr->debug('test8');

        $lines = array_filter(explode("\n", $data), fn($line) => !empty($line));
        $this->assertCount(8, $lines);

        $first_line = $lines[0];
        $last_line = end($lines);
        $full_date = (new DateTime())->format('Y-m-d H:i:s');
        $this->assertStringContainsString("[{$full_date}] FileSystemDriverTest.EMERGENCY: test", $first_line);
        $this->assertStringContainsString("[{$full_date}] FileSystemDriverTest.DEBUG: test", $last_line);
    }

    public function testFileSystemDriverThrows(): void
    {
        $loggr = new Loggr(new FileSystemDriver('path-does-not-exist'));
        $loggr->emergency('test');
        $sep = DIRECTORY_SEPARATOR;
        $date = (new DateTime())->format('Y-m-d');
        $this->assertEquals("Log file could not be created at path: path-does-not-exist{$sep}{$date}.log", $loggr->error);
    }

    private function mockLogger(string &$result): Loggr
    {
        $driver = Mockery::mock(FileSystemDriver::class, [''])->makePartial();
        $driver->shouldReceive('log')->passthru();
        $driver->shouldReceive('exists')->andReturn(true);
        $driver->shouldReceive('isWriteable')->andReturns(true);
        $driver->shouldReceive('write')->withArgs(function ($path, $data) use (&$result) {
            $result .= $data;

            return true;
        });

        return new Loggr($driver);
    }

    protected function tearDown(): void
    {
        Mockery::close();
    }
}