]> git.mxchange.org Git - friendica.git/blob - tests/src/Util/Logger/StreamLoggerTest.php
Rename & Testfix
[friendica.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          * Test when a file isn't set
129          * @expectedException \LogicException
130          * @expectedExceptionMessage Missing stream URL.
131          */
132         public function testNoUrl()
133         {
134                 $logger = new StreamLogger('test', '', $this->introspection);
135
136                 $logger->emergency('not working');
137         }
138
139         /**
140          * Test when a file cannot be opened
141          * @expectedException \UnexpectedValueException
142          * @expectedExceptionMessageRegExp /The stream or file .* could not be opened: .* /
143          */
144         public function testWrongUrl()
145         {
146                 $logfile = vfsStream::newFile('friendica.log')
147                         ->at($this->root)->chmod(0);
148
149                 $logger = new StreamLogger('test', $logfile->url(), $this->introspection);
150
151                 $logger->emergency('not working');
152         }
153
154         /**
155          * Test when the directory cannot get created
156          * @expectedException \UnexpectedValueException
157          * @expectedExceptionMessageRegExp /Directory .* cannot get created: .* /
158          */
159         public function testWrongDir()
160         {
161                 $logger = new StreamLogger('test', '/a/wrong/directory/file.txt', $this->introspection);
162
163                 $logger->emergency('not working');
164         }
165
166         /**
167          * Test when the minimum level is not valid
168          * @expectedException \InvalidArgumentException
169          * @expectedExceptionMessageRegExp /The level ".*" is not valid./
170          */
171         public function testWrongMinimumLevel()
172         {
173                 $logger = new StreamLogger('test', 'file.text', $this->introspection, 'NOPE');
174         }
175
176         /**
177          * Test when the minimum level is not valid
178          * @expectedException \InvalidArgumentException
179          * @expectedExceptionMessageRegExp /The level ".*" is not valid./
180          */
181         public function testWrongLogLevel()
182         {
183                 $logfile = vfsStream::newFile('friendica.log')
184                         ->at($this->root);
185
186                 $logger = new StreamLogger('test', $logfile->url(), $this->introspection);
187
188                 $logger->log('NOPE', 'a test');
189         }
190 }