Passed
Push — develop ( 16fb8d...02bc0c )
by Michał
02:53
created

testCreateMultipleSimpleLogMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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