Passed
Branch master (8a6725)
by Michał
02:20
created

SimpleLogTest::tearDown()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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