3 namespace Friendica\Test\src\Util\Logger;
5 use Friendica\Test\MockedTest;
6 use Friendica\Util\Introspection;
7 use Mockery\MockInterface;
8 use Psr\Log\LoggerInterface;
11 abstract class AbstractLoggerTest extends MockedTest
15 const LOGLINE = '/.* \[.*\]: .* \{.*\"file\":\".*\".*,.*\"line\":\d*,.*\"function\":\".*\".*,.*\"uid\":\".*\".*}/';
19 const FUNC = 'myfunction';
22 * @var Introspection|MockInterface
24 protected $introspection;
27 * Returns the content of the current logger instance
31 abstract protected function getContent();
34 * Returns the current logger instance
36 * @param string $level the default loglevel
38 * @return LoggerInterface
40 abstract protected function getInstance($level = LogLevel::DEBUG);
42 protected function setUp()
46 $this->introspection = \Mockery::mock(Introspection::class);
47 $this->introspection->shouldReceive('getRecord')->andReturn([
50 'function' => self::FUNC
54 public function assertLogline($string)
56 $this->assertRegExp(self::LOGLINE, $string);
59 public function assertLoglineNums($assertNum, $string)
61 $this->assertEquals($assertNum, preg_match_all(self::LOGLINE, $string));
65 * Test if the logger works correctly
67 public function testNormal()
69 $logger = $this->getInstance();
70 $logger->emergency('working!');
71 $logger->alert('working too!');
72 $logger->debug('and now?');
73 $logger->notice('message', ['an' => 'context']);
75 $text = $this->getContent();
76 $this->assertLogline($text);
77 $this->assertLoglineNums(4, $text);
81 * Test if a log entry is correctly interpolated
83 public function testPsrInterpolate()
85 $logger = $this->getInstance();
87 $logger->emergency('A {psr} test', ['psr' => 'working']);
88 $logger->alert('An {array} test', ['array' => ['it', 'is', 'working']]);
89 $text = $this->getContent();
90 $this->assertContains('A working test', $text);
91 $this->assertContains('An ["it","is","working"] test', $text);
95 * Test if a log entry contains all necessary information
97 public function testContainsInformation()
99 $logger = $this->getInstance();
100 $logger->emergency('A test');
102 $text = $this->getContent();
103 $this->assertContains('"file":"' . self::FILE . '"', $text);
104 $this->assertContains('"line":' . self::LINE, $text);
105 $this->assertContains('"function":"' . self::FUNC . '"', $text);
109 * Test if the minimum level is working
111 public function testMinimumLevel()
113 $logger = $this->getInstance(LogLevel::NOTICE);
115 $logger->emergency('working');
116 $logger->alert('working');
117 $logger->error('working');
118 $logger->warning('working');
119 $logger->notice('working');
120 $logger->info('not working');
121 $logger->debug('not working');
123 $text = $this->getContent();
125 $this->assertLoglineNums(5, $text);
129 * Test with different logging data
130 * @dataProvider dataTests
132 public function testDifferentTypes($function, $message, array $context)
134 $logger = $this->getInstance();
135 $logger->$function($message, $context);
137 $text = $this->getContent();
139 $this->assertLogline($text);
141 $this->assertContains(@json_encode($context), $text);