3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Test\src\Core\Logger;
24 use Friendica\Test\MockedTest;
25 use Friendica\Util\Introspection;
26 use Mockery\MockInterface;
27 use Psr\Log\LoggerInterface;
30 abstract class AbstractLoggerTest extends MockedTest
34 const LOGLINE = '/.* \[.*\]: .* \{.*\"file\":\".*\".*,.*\"line\":\d*,.*\"function\":\".*\".*,.*\"uid\":\".*\".*}/';
38 const FUNC = 'myfunction';
41 * @var Introspection|MockInterface
43 protected $introspection;
46 * Returns the content of the current logger instance
50 abstract protected function getContent();
53 * Returns the current logger instance
55 * @param string $level the default loglevel
57 * @return LoggerInterface
59 abstract protected function getInstance($level = LogLevel::DEBUG);
61 protected function setUp(): void
65 $this->introspection = \Mockery::mock(Introspection::class);
66 $this->introspection->shouldReceive('getRecord')->andReturn([
69 'function' => self::FUNC
73 public function assertLogline($string)
75 self::assertRegExp(self::LOGLINE, $string);
78 public function assertLoglineNums($assertNum, $string)
80 self::assertEquals($assertNum, preg_match_all(self::LOGLINE, $string));
84 * Test if the logger works correctly
86 public function testNormal()
88 $logger = $this->getInstance();
89 $logger->emergency('working!');
90 $logger->alert('working too!');
91 $logger->debug('and now?');
92 $logger->notice('message', ['an' => 'context']);
94 $text = $this->getContent();
95 self::assertLogline($text);
96 self::assertLoglineNums(4, $text);
100 * Test if a log entry is correctly interpolated
102 public function testPsrInterpolate()
104 $logger = $this->getInstance();
106 $logger->emergency('A {psr} test', ['psr' => 'working']);
107 $logger->alert('An {array} test', ['array' => ['it', 'is', 'working']]);
108 $text = $this->getContent();
109 self::assertStringContainsString('A working test', $text);
110 self::assertStringContainsString('An ["it","is","working"] test', $text);
114 * Test if a log entry contains all necessary information
116 public function testContainsInformation()
118 $logger = $this->getInstance();
119 $logger->emergency('A test');
121 $text = $this->getContent();
122 self::assertStringContainsString('"file":"' . self::FILE . '"', $text);
123 self::assertStringContainsString('"line":' . self::LINE, $text);
124 self::assertStringContainsString('"function":"' . self::FUNC . '"', $text);
128 * Test if the minimum level is working
130 public function testMinimumLevel()
132 $logger = $this->getInstance(LogLevel::NOTICE);
134 $logger->emergency('working');
135 $logger->alert('working');
136 $logger->error('working');
137 $logger->warning('working');
138 $logger->notice('working');
139 $logger->info('not working');
140 $logger->debug('not working');
142 $text = $this->getContent();
144 self::assertLoglineNums(5, $text);
148 * Test with different logging data
149 * @dataProvider dataTests
151 public function testDifferentTypes($function, $message, array $context)
153 $logger = $this->getInstance();
154 $logger->$function($message, $context);
156 $text = $this->getContent();
158 self::assertLogline($text);
160 self::assertStringContainsString(@json_encode($context), $text);
164 * Test a message with an exception
166 public function testExceptionHandling()
168 $e = new \Exception("Test String", 123);
169 $eFollowUp = new \Exception("FollowUp", 456, $e);
171 $assertion = $eFollowUp->__toString();
173 $logger = $this->getInstance();
174 $logger->alert('test', ['e' => $eFollowUp]);
175 $text = $this->getContent();
177 self::assertLogline($text);
179 self::assertStringContainsString(@json_encode($assertion), $this->getContent());
182 public function testNoObjectHandling()
184 $logger = $this->getInstance();
185 $logger->alert('test', ['e' => ['test' => 'test']]);
186 $text = $this->getContent();
188 self::assertLogline($text);
190 self::assertStringContainsString('test', $this->getContent());