Added StreamLoggerTest
[friendica.git/.git] / tests / src / Util / Logger / StreamLoggerTest.php
1 <?php
2
3 namespace Friendica\Test\src\Util\Logger;
4
5 use Friendica\Test\MockedTest;
6 use Friendica\Test\Util\VFSTrait;
7 use Friendica\Util\Introspection;
8 use Friendica\Util\Logger\StreamLogger;
9 use Mockery\MockInterface;
10 use org\bovigo\vfs\vfsStream;
11 use Psr\Log\LogLevel;
12
13 class StreamLoggerTest extends MockedTest
14 {
15         const LOGLINE = '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} .* \[.*\]: .* \{.*\"file\":\".*\".*,.*\"line\":\d*,.*\"function\":\".*\".*,.*\"uid\":\".*\".*,.*\"process_id\":\d*.*\}/';
16
17         const FILE = 'test';
18         const LINE = 666;
19         const FUNC = 'myfunction';
20
21         use VFSTrait;
22
23         /**
24          * @var Introspection|MockInterface
25          */
26         private $introspection;
27
28         protected function setUp()
29         {
30                 parent::setUp();
31
32                 $this->setUpVfsDir();
33
34                 $this->introspection = \Mockery::mock(Introspection::class);
35                 $this->introspection->shouldReceive('getRecord')->andReturn([
36                         'file'     => self::FILE,
37                         'line'     => self::LINE,
38                         'function' => self::FUNC
39                 ]);
40         }
41
42         public function assertLogline($string)
43         {
44                 $this->assertRegExp(self::LOGLINE, $string);
45         }
46
47         public function assertLoglineNums($assertNum, $string)
48         {
49                 $this->assertEquals($assertNum, preg_match_all(self::LOGLINE, $string));
50         }
51
52         public function testNormal()
53         {
54                 $logfile = vfsStream::newFile('friendica.log')
55                         ->at($this->root);
56
57                 $logger = new StreamLogger('test', $logfile->url(), $this->introspection);
58                 $logger->emergency('working!');
59                 $logger->alert('working too!');
60                 $logger->debug('and now?');
61                 $logger->notice('message', ['an' => 'context']);
62
63                 $text = $logfile->getContent();
64                 $this->assertLogline($text);
65                 $this->assertLoglineNums(4, $text);
66         }
67
68         /**
69          * Test if a log entry is correctly interpolated
70          */
71         public function testPsrInterpolate()
72         {
73                 $logfile = vfsStream::newFile('friendica.log')
74                         ->at($this->root);
75
76                 $logger = new StreamLogger('test', $logfile->url(), $this->introspection);
77
78                 $logger->emergency('A {psr} test', ['psr' => 'working']);
79                 $logger->alert('An {array} test', ['array' => ['it', 'is', 'working']]);
80                 $text = $logfile->getContent();
81                 $this->assertContains('A working test', $text);
82                 $this->assertContains('An ["it","is","working"] test', $text);
83         }
84
85         /**
86          * Test if a log entry contains all necessary information
87          */
88         public function testContainsInformation()
89         {
90                 $logfile = vfsStream::newFile('friendica.log')
91                         ->at($this->root);
92
93                 $logger = new StreamLogger('test', $logfile->url(), $this->introspection);
94
95                 $logger->emergency('A test');
96
97                 $text = $logfile->getContent();
98                 $this->assertContains('"process_id":' . getmypid(), $text);
99                 $this->assertContains('"file":"' . self::FILE . '"', $text);
100                 $this->assertContains('"line":' . self::LINE, $text);
101                 $this->assertContains('"function":"' . self::FUNC . '"', $text);
102         }
103
104         /**
105          * Test if the minimum level is working
106          */
107         public function testMinimumLevel()
108         {
109                 $logfile = vfsStream::newFile('friendica.log')
110                         ->at($this->root);
111
112                 $logger = new StreamLogger('test', $logfile->url(), $this->introspection, LogLevel::NOTICE);
113
114                 $logger->emergency('working');
115                 $logger->alert('working');
116                 $logger->error('working');
117                 $logger->warning('working');
118                 $logger->notice('working');
119                 $logger->info('not working');
120                 $logger->debug('not working');
121
122                 $text = $logfile->getContent();
123
124                 $this->assertLoglineNums(5, $text);
125         }
126
127
128         /**
129          * Test if a file cannot get opened
130          * @expectedException \UnexpectedValueException
131          */
132         public function testNoFile()
133         {
134                 $logfile = vfsStream::newFile('friendica.log')
135                         ->at($this->root)
136                         ->chmod(0);
137
138                 $logger = new StreamLogger('test', $logfile->url(), $this->introspection);
139
140                 $logger->emergency('not working');
141         }
142
143         /**
144          * Test when a file isn't set
145          * @expectedException \LogicException
146          * @expectedExceptionMessage Missing stream URL.
147          */
148         public function testNoUrl()
149         {
150                 $logger = new StreamLogger('test', '', $this->introspection);
151
152                 $logger->emergency('not working');
153         }
154
155         /**
156          * Test when a file doesn't exist
157          * @expectedException \UnexpectedValueException
158          * @expectedExceptionMessageRegExp /The stream or file .* could not be opened: .* /
159          */
160         public function testWrongUrl()
161         {
162                 $logger = new StreamLogger('test', 'wrongfile', $this->introspection);
163
164                 $logger->emergency('not working');
165         }
166
167         /**
168          * Test when the directory cannot get created
169          * @expectedException \UnexpectedValueException
170          * @expectedExceptionMessageRegExp /Directory .* cannot get created: .* /
171          */
172         public function testWrongDir()
173         {
174                 $logger = new StreamLogger('test', 'a/wrong/directory/file.txt', $this->introspection);
175
176                 $logger->emergency('not working');
177         }
178
179         /**
180          * Test when the minimum level is not valid
181          * @expectedException \InvalidArgumentException
182          * @expectedExceptionMessageRegExp /The level ".*" is not valid./
183          */
184         public function testWrongMinimumLevel()
185         {
186                 $logger = new StreamLogger('test', 'file.text', $this->introspection, 'NOPE');
187         }
188
189         /**
190          * Test when the minimum level is not valid
191          * @expectedException \InvalidArgumentException
192          * @expectedExceptionMessageRegExp /The level ".*" is not valid./
193          */
194         public function testWrongLogLevel()
195         {
196                 $logfile = vfsStream::newFile('friendica.log')
197                         ->at($this->root);
198
199                 $logger = new StreamLogger('test', $logfile->url(), $this->introspection);
200
201                 $logger->log('NOPE', 'a test');
202         }
203 }