SimpleLogTest::testCreateIncorrectLogMessage()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 10
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 16
rs 9.9332
1
<?php
2
3
namespace SimpleLog\Test;
4
5
use SimpleLog\Log;
6
use SimpleLog\LogStatic;
7
use SimpleLog\Storage\File;
8
use SimpleLog\Message\DefaultInlineMessage;
9
use PHPUnit\Framework\TestCase;
10
use SimpleLog\Message\DefaultMessage;
11
12
class SimpleLogTest extends TestCase
13
{
14
    /**
15
     * name of test event log file
16
     */
17
    public const NOTICE_LOG_NAME = '/notice.log';
18
19
    /**
20
     * name of test event log file
21
     */
22
    public const WARNING_LOG_NAME = '/warning.log';
23
24
    /**
25
     * store generated log file path
26
     *
27
     * @var string
28
     */
29
    protected $logPath;
30
31
    /**
32
     * actions launched before test starts
33
     */
34
    protected function setUp(): void
35
    {
36
        $this->logPath = __DIR__ . '/log';
37
38
        $this->tearDown();
39
    }
40
41
    public function testCreateLogMessageWithIncorrectLevel(): void
42
    {
43
        $this->expectExceptionMessage("Level not defined: incorrect");
44
        $this->expectException(\Psr\Log\InvalidArgumentException::class);
45
46
        (new Log(['log_path' => $this->logPath]))
47
            ->log('incorrect', 'Some log message');
48
    }
49
50
    public function testCreateIncorrectLogMessage(): void
51
    {
52
        if (\PHP_VERSION < '8.0.0') {
53
            $this->expectExceptionMessage(
54
                'Incorrect message type. Must be string, array or object with __toString method.'
55
            );
56
            $this->expectException(\Psr\Log\InvalidArgumentException::class);
57
        } else {
58
            $this->expectExceptionMessage(
59
                'method_exists(): Argument #1 ($object_or_class) must be of type object|string, int given'
60
            );
61
            $this->expectException(\TypeError::class);
62
        }
63
64
        (new Log(['log_path' => $this->logPath]))
65
            ->makeLog(12312312);
66
    }
67
68
    /**
69
     * simple create log object and create log message in given directory
70
     */
71
    public function testCreateSimpleLogMessage(): void
72
    {
73
        $log = new Log(['log_path' => $this->logPath]);
74
75
        $this->assertFileDoesNotExist($this->logPath . self::NOTICE_LOG_NAME);
76
77
        $log->makeLog('Some log message');
78
79
        $this->assertFileExists($this->logPath . self::NOTICE_LOG_NAME);
80
81
        $content = file_get_contents($this->logPath . self::NOTICE_LOG_NAME);
82
83
        //because of different time and date of creating log file, we remove first line with date
84
        $this->assertEquals($this->getSampleContent(), \substr($content, \strpos($content, "\n") + 1));
85
    }
86
87
    /**
88
     * simple create log object and create log message in given directory
89
     */
90
    public function testCreateMultipleSimpleLogMessage(): void
91
    {
92
        $log = new Log(['log_path' => $this->logPath]);
93
94
        $this->assertFileDoesNotExist($this->logPath . self::NOTICE_LOG_NAME);
95
96
        $log->makeLog('Some log message');
97
98
        $this->assertFileExists($this->logPath . self::NOTICE_LOG_NAME);
99
100
        $log->makeLog('Some log message');
101
102
        $content = \file_get_contents($this->logPath . self::NOTICE_LOG_NAME);
103
        //because of different time and date of creating log file, we remove first line with date
104
        $this->assertEquals(
105
            $this->getSampleContent() . $this->getSampleContent(),
106
            \preg_replace('#[\d]{4}-[\d]{2}-[\d]{2} - [\d]{2}:[\d]{2}:[\d]{2}\n#', '', $content)
107
        );
108
    }
109
110
    public function testCreateSimpleLogWithOtherStorage(): void
111
    {
112
        $log = new Log([
113
            'storage' => new File(
114
                ['log_path' => $this->logPath]
115
            )
116
        ]);
117
118
        $this->assertFileDoesNotExist($this->logPath . self::NOTICE_LOG_NAME);
119
120
        $log->makeLog('Some log message');
121
122
        $this->assertFileExists($this->logPath . self::NOTICE_LOG_NAME);
123
    }
124
125
    public function testCreateSimpleLogWithOtherMessage(): void
126
    {
127
        $log = new Log([
128
            'message' => new DefaultInlineMessage(),
129
            'log_path' => $this->logPath,
130
        ]);
131
132
        $this->assertFileDoesNotExist($this->logPath . self::NOTICE_LOG_NAME);
133
134
        $log->makeLog('Some log message');
135
136
        $this->assertFileExists($this->logPath . self::NOTICE_LOG_NAME);
137
    }
138
139
    public function testGetLastMessage(): void
140
    {
141
        $log = new Log(['log_path' => $this->logPath]);
142
143
        $log->makeLog('Some log message');
144
145
        $content = $log->getLastMessage();
146
        //because of different time and date of creating log file, we remove first line with date
147
        $this->assertEquals($this->getSampleContent(), substr($content, strpos($content, "\n") + 1));
148
    }
149
150
    /**
151
     * simple create log object and create log message from array data in given directory
152
     */
153
    public function testCreateLogWithArrayMessage(): void
154
    {
155
        $log = new Log(['log_path' => $this->logPath]);
156
157
        $this->assertFileDoesNotExist($this->logPath . self::NOTICE_LOG_NAME);
158
159
        $log->makeLog(
160
            [
161
                'message key' => 'some message',
162
                'another key' => 'some another message',
163
                'no key message',
164
            ]
165
        );
166
167
        $this->assertFileExists($this->logPath . self::NOTICE_LOG_NAME);
168
169
        $content = file_get_contents($this->logPath . self::NOTICE_LOG_NAME);
170
171
        //because of different time and date of creating log file, we remove first line with date
172
        $this->assertEquals($this->getArrayMessageContent(), substr($content, strpos($content, "\n") + 1));
173
    }
174
175
    /**
176
     * simple create log object and create log message from array with sub arrays data in given directory
177
     */
178
    public function testCreateLogWithSubArrayMessage(): void
179
    {
180
        $log = new Log(['log_path' => $this->logPath]);
181
182
        $this->assertFileDoesNotExist($this->logPath . self::NOTICE_LOG_NAME);
183
184
        $log->makeLog(
185
            [
186
                'sub array' => [
187
                    'key' => 'val',
188
                    'key 2' => 'val 2',
189
                ],
190
            ]
191
        );
192
193
        $this->assertFileExists($this->logPath . self::NOTICE_LOG_NAME);
194
195
        $content = file_get_contents($this->logPath . self::NOTICE_LOG_NAME);
196
197
        //because of different time and date of creating log file, we remove first line with date
198
        //hack with remove new lines because of differences between output and stored expectation
199
        $this->assertEquals(
200
            str_replace("\n", '', $this->getSubArrayMessageContent()),
201
            str_replace("\n", '', substr($content, strpos($content, "\n") + 1))
202
        );
203
    }
204
205
    /**
206
     * test setting and getting options via specified methods
207
     */
208
    public function testCreateLogObjectWithOtherConfig(): void
209
    {
210
        $log = new Log();
211
212
        $this->assertFileDoesNotExist($this->logPath . self::WARNING_LOG_NAME);
213
214
        $log->setOption('log_path', $this->logPath)
215
            ->setOption('level', 'warning')
216
            ->makeLog('Some log message');
217
218
        $this->assertFileExists($this->logPath . self::WARNING_LOG_NAME);
219
        $this->assertEquals('warning', $log->getOption('level'));
220
        $this->assertEquals(
221
            [
222
                'log_path' => $this->logPath,
223
                'level' => 'warning',
224
                'storage' => 'SimpleLog\Storage\File',
225
                'message' => DefaultMessage::class,
226
            ],
227
            $log->getOption()
228
        );
229
    }
230
231
    /**
232
     * check static log interface
233
     */
234
    public function testCreateStaticMakeLog(): void
235
    {
236
        LogStatic::setOption('log_path', $this->logPath);
237
238
        $this->assertFileDoesNotExist($this->logPath . self::NOTICE_LOG_NAME);
239
        $this->assertEquals($this->logPath, LogStatic::getOption('log_path'));
240
241
        LogStatic::makeLog('Some log message');
242
243
        $this->assertFileExists($this->logPath . self::NOTICE_LOG_NAME);
244
    }
245
246
    /**
247
     * check static log interface
248
     */
249
    public function testCreateStaticLog(): void
250
    {
251
        LogStatic::setOption('log_path', $this->logPath);
252
253
        $this->assertFileDoesNotExist($this->logPath . self::NOTICE_LOG_NAME);
254
        $this->assertEquals($this->logPath, LogStatic::getOption('log_path'));
255
256
        LogStatic::log('notice', 'Some log message');
257
258
        $this->assertFileExists($this->logPath . self::NOTICE_LOG_NAME);
259
    }
260
261
    public function testLogWithContext(): void
262
    {
263
        $log = new Log(['log_path' => $this->logPath]);
264
265
        $this->assertFileDoesNotExist($this->logPath . self::NOTICE_LOG_NAME);
266
267
        $log->makeLog('Some log message with {context}', ['context' => 'some value']);
268
269
        $this->assertFileExists($this->logPath . self::NOTICE_LOG_NAME);
270
271
        $content = file_get_contents($this->logPath . self::NOTICE_LOG_NAME);
272
273
        //because of different time and date of creating log file, we remove first line with date
274
        $this->assertEquals(
275
            $this->getSampleContentWithContext(),
276
            substr($content, strpos($content, "\n") + 1)
277
        );
278
    }
279
280
    protected function getSampleContent(): string
281
    {
282
        return <<<EOT
283
Some log message
284
-----------------------------------------------------------
285
286
EOT;
287
    }
288
289
    protected function getSampleContentWithContext(): string
290
    {
291
        return <<<EOT
292
Some log message with some value
293
-----------------------------------------------------------
294
295
EOT;
296
    }
297
298
    protected function getArrayMessageContent(): string
299
    {
300
        return <<<EOT
301
- message key: some message
302
- another key: some another message
303
- no key message
304
-----------------------------------------------------------
305
306
EOT;
307
    }
308
309
    protected function getSubArrayMessageContent(): string
310
    {
311
        return <<<EOT
312
- sub array:
313
    - key: val
314
    - key 2: val 2
315
-----------------------------------------------------------
316
317
EOT;
318
    }
319
320
    /**
321
     * actions launched after test was finished
322
     */
323
    protected function tearDown(): void
324
    {
325
        if (\file_exists($this->logPath . self::NOTICE_LOG_NAME)) {
326
            \unlink($this->logPath . self::NOTICE_LOG_NAME);
327
        }
328
329
        if (\file_exists($this->logPath . self::WARNING_LOG_NAME)) {
330
            \unlink($this->logPath . self::WARNING_LOG_NAME);
331
        }
332
333
        if (\file_exists($this->logPath)) {
334
            \rmdir($this->logPath);
335
        }
336
    }
337
}
338