3 namespace Friendica\Test\src\Util\Logger;
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;
13 class StreamLoggerTest extends MockedTest
15 const LOGLINE = '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} .* \[.*\]: .* \{.*\"file\":\".*\".*,.*\"line\":\d*,.*\"function\":\".*\".*,.*\"uid\":\".*\".*,.*\"process_id\":\d*.*\}/';
19 const FUNC = 'myfunction';
24 * @var Introspection|MockInterface
26 private $introspection;
28 protected function setUp()
34 $this->introspection = \Mockery::mock(Introspection::class);
35 $this->introspection->shouldReceive('getRecord')->andReturn([
38 'function' => self::FUNC
42 public function assertLogline($string)
44 $this->assertRegExp(self::LOGLINE, $string);
47 public function assertLoglineNums($assertNum, $string)
49 $this->assertEquals($assertNum, preg_match_all(self::LOGLINE, $string));
52 public function testNormal()
54 $logfile = vfsStream::newFile('friendica.log')
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']);
63 $text = $logfile->getContent();
64 $this->assertLogline($text);
65 $this->assertLoglineNums(4, $text);
69 * Test if a log entry is correctly interpolated
71 public function testPsrInterpolate()
73 $logfile = vfsStream::newFile('friendica.log')
76 $logger = new StreamLogger('test', $logfile->url(), $this->introspection);
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);
86 * Test if a log entry contains all necessary information
88 public function testContainsInformation()
90 $logfile = vfsStream::newFile('friendica.log')
93 $logger = new StreamLogger('test', $logfile->url(), $this->introspection);
95 $logger->emergency('A test');
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);
105 * Test if the minimum level is working
107 public function testMinimumLevel()
109 $logfile = vfsStream::newFile('friendica.log')
112 $logger = new StreamLogger('test', $logfile->url(), $this->introspection, LogLevel::NOTICE);
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');
122 $text = $logfile->getContent();
124 $this->assertLoglineNums(5, $text);
128 * Test when a file isn't set
129 * @expectedException \LogicException
130 * @expectedExceptionMessage Missing stream URL.
132 public function testNoUrl()
134 $logger = new StreamLogger('test', '', $this->introspection);
136 $logger->emergency('not working');
140 * Test when a file cannot be opened
141 * @expectedException \UnexpectedValueException
142 * @expectedExceptionMessageRegExp /The stream or file .* could not be opened: .* /
144 public function testWrongUrl()
146 $logfile = vfsStream::newFile('friendica.log')
147 ->at($this->root)->chmod(0);
149 $logger = new StreamLogger('test', $logfile->url(), $this->introspection);
151 $logger->emergency('not working');
155 * Test when the directory cannot get created
156 * @expectedException \UnexpectedValueException
157 * @expectedExceptionMessageRegExp /Directory .* cannot get created: .* /
159 public function testWrongDir()
161 $logger = new StreamLogger('test', '/a/wrong/directory/file.txt', $this->introspection);
163 $logger->emergency('not working');
167 * Test when the minimum level is not valid
168 * @expectedException \InvalidArgumentException
169 * @expectedExceptionMessageRegExp /The level ".*" is not valid./
171 public function testWrongMinimumLevel()
173 $logger = new StreamLogger('test', 'file.text', $this->introspection, 'NOPE');
177 * Test when the minimum level is not valid
178 * @expectedException \InvalidArgumentException
179 * @expectedExceptionMessageRegExp /The level ".*" is not valid./
181 public function testWrongLogLevel()
183 $logfile = vfsStream::newFile('friendica.log')
186 $logger = new StreamLogger('test', $logfile->url(), $this->introspection);
188 $logger->log('NOPE', 'a test');