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\Core\Logger\Type\WorkerLogger;
25 use Friendica\Test\MockedTest;
26 use Psr\Log\LoggerInterface;
28 class WorkerLoggerTest extends MockedTest
30 private function assertUid($uid, $length = 7)
32 self::assertRegExp('/^[a-zA-Z0-9]{' . $length . '}+$/', $uid);
36 * Test the a id with length zero
39 public function testGetWorkerIdZero()
41 $this->expectException(\Error::class);
43 $logger = \Mockery::mock(LoggerInterface::class);
44 new WorkerLogger($logger, 'test', 0);
48 * Test the generated Uid
50 public function testGetWorkerId()
52 $logger = \Mockery::mock(LoggerInterface::class);
53 for ($i = 1; $i < 14; $i++) {
54 $workLogger = new WorkerLogger($logger, 'test', $i);
55 $uid = $workLogger->getWorkerId();
56 self::assertUid($uid, $i);
60 public function dataTest()
70 'msg' => 'another alert',
71 'context' => ['test' => 'it'],
75 'msg' => 'Critical msg used',
76 'context' => ['test' => 'it', 'more' => 0.24545],
81 'context' => ['test' => 'it', 'yet' => true],
85 'msg' => 'another alert' . 123523 . 324.54534 . 'test',
86 'context' => ['test' => 'it', 2 => 'nope'],
90 'msg' => 'Notice' . ' alert' . true . 'with' . '\'strange\'' . 1.24. 'behavior',
91 'context' => ['test' => 'it'],
95 'msg' => 'at last a debug',
96 'context' => ['test' => 'it'],
102 * Test the WorkerLogger with different log calls
103 * @dataProvider dataTest
105 public function testEmergency($func, $msg, $context = [])
107 $logger = \Mockery::mock(LoggerInterface::class);
108 $workLogger = new WorkerLogger($logger, 'test');
109 $testContext = $context;
110 $testContext['worker_id'] = $workLogger->getWorkerId();
111 $testContext['worker_cmd'] = 'test';
112 self::assertUid($testContext['worker_id']);
114 ->shouldReceive($func)
115 ->with($msg, $testContext)
117 $workLogger->$func($msg, $context);
121 * Test the WorkerLogger with
123 public function testLog()
125 $logger = \Mockery::mock(LoggerInterface::class);
126 $workLogger = new WorkerLogger($logger, 'test');
127 $context = $testContext = ['test' => 'it'];
128 $testContext['worker_id'] = $workLogger->getWorkerId();
129 $testContext['worker_cmd'] = 'test';
130 self::assertUid($testContext['worker_id']);
132 ->shouldReceive('log')
133 ->with('debug', 'a test', $testContext)
135 $workLogger->log('debug', 'a test', $context);