3 * @copyright Copyright (C) 2010-2023, 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\Core\Config\Capability\IManageConfigValues;
25 use Friendica\Test\MockedTest;
26 use Friendica\Core\Logger\Util\Introspection;
27 use Mockery\MockInterface;
28 use Psr\Log\LoggerInterface;
31 abstract class AbstractLoggerTest extends MockedTest
35 const LOGLINE = '/.* \[.*]: .* {.*\"file\":\".*\".*,.*\"line\":\d*,.*\"function\":\".*\".*,.*\"uid\":\".*\".*}/';
39 const FUNC = 'myfunction';
42 * @var Introspection|MockInterface
44 protected $introspection;
46 * @var IManageConfigValues|MockInterface
51 * Returns the content of the current logger instance
55 abstract protected function getContent();
58 * Returns the current logger instance
60 * @param string $level the default loglevel
62 * @return LoggerInterface
64 abstract protected function getInstance($level = LogLevel::DEBUG);
66 protected function setUp(): void
70 $this->introspection = \Mockery::mock(Introspection::class);
71 $this->introspection->shouldReceive('getRecord')->andReturn([
74 'function' => self::FUNC
77 $this->config = \Mockery::mock(IManageConfigValues::class);
80 public function assertLogline($string)
82 self::assertMatchesRegularExpression(self::LOGLINE, $string);
85 public function assertLoglineNums($assertNum, $string)
87 self::assertEquals($assertNum, preg_match_all(self::LOGLINE, $string));
91 * Test if the logger works correctly
93 public function testNormal()
95 $logger = $this->getInstance();
96 $logger->emergency('working!');
97 $logger->alert('working too!');
98 $logger->debug('and now?');
99 $logger->notice('message', ['an' => 'context']);
101 $text = $this->getContent();
102 self::assertLogline($text);
103 self::assertLoglineNums(4, $text);
107 * Test if a log entry is correctly interpolated
109 public function testPsrInterpolate()
111 $logger = $this->getInstance();
113 $logger->emergency('A {psr} test', ['psr' => 'working']);
114 $logger->alert('An {array} test', ['array' => ['it', 'is', 'working']]);
115 $text = $this->getContent();
116 self::assertStringContainsString('A working test', $text);
117 self::assertStringContainsString('An ["it","is","working"] test', $text);
121 * Test if a log entry contains all necessary information
123 public function testContainsInformation()
125 $logger = $this->getInstance();
126 $logger->emergency('A test');
128 $text = $this->getContent();
129 self::assertStringContainsString('"file":"' . self::FILE . '"', $text);
130 self::assertStringContainsString('"line":' . self::LINE, $text);
131 self::assertStringContainsString('"function":"' . self::FUNC . '"', $text);
135 * Test if the minimum level is working
137 public function testMinimumLevel()
139 $logger = $this->getInstance(LogLevel::NOTICE);
141 $logger->emergency('working');
142 $logger->alert('working');
143 $logger->error('working');
144 $logger->warning('working');
145 $logger->notice('working');
146 $logger->info('not working');
147 $logger->debug('not working');
149 $text = $this->getContent();
151 self::assertLoglineNums(5, $text);
155 * Test with different logging data
156 * @dataProvider dataTests
158 public function testDifferentTypes($function, $message, array $context)
160 $logger = $this->getInstance();
161 $logger->$function($message, $context);
163 $text = $this->getContent();
165 self::assertLogline($text);
167 self::assertStringContainsString(@json_encode($context, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), $text);
171 * Test a message with an exception
173 public function testExceptionHandling()
175 $e = new \Exception("Test String", 123);
176 $eFollowUp = new \Exception("FollowUp", 456, $e);
178 $assertion = $eFollowUp->__toString();
180 $logger = $this->getInstance();
181 $logger->alert('test', ['e' => $eFollowUp]);
182 $text = $this->getContent();
184 self::assertLogline($text);
186 self::assertStringContainsString(@json_encode($assertion, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), $this->getContent());
189 public function testNoObjectHandling()
191 $logger = $this->getInstance();
192 $logger->alert('test', ['e' => ['test' => 'test']]);
193 $text = $this->getContent();
195 self::assertLogline($text);
197 self::assertStringContainsString('test', $this->getContent());