3 * @copyright Copyright (C) 2010-2022, 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)
32 self::assertMatchesRegularExpression('/^[a-zA-Z0-9]{' . WorkerLogger::WORKER_ID_LENGTH . '}+$/', $uid);
35 public function dataTest()
45 'msg' => 'another alert',
46 'context' => ['test' => 'it'],
50 'msg' => 'Critical msg used',
51 'context' => ['test' => 'it', 'more' => 0.24545],
56 'context' => ['test' => 'it', 'yet' => true],
60 'msg' => 'another alert' . 123523 . 324.54534 . 'test',
61 'context' => ['test' => 'it', 2 => 'nope'],
65 'msg' => 'Notice' . ' alert' . true . 'with' . '\'strange\'' . 1.24. 'behavior',
66 'context' => ['test' => 'it'],
70 'msg' => 'at last a debug',
71 'context' => ['test' => 'it'],
77 * Test the WorkerLogger with different log calls
78 * @dataProvider dataTest
80 public function testEmergency($func, $msg, $context = [])
82 $logger = \Mockery::mock(LoggerInterface::class);
83 $workLogger = new WorkerLogger($logger);
84 $testContext = $context;
85 $testContext['worker_id'] = $workLogger->getWorkerId();
86 $testContext['worker_cmd'] = '';
87 self::assertUid($testContext['worker_id']);
89 ->shouldReceive($func)
90 ->with($msg, $testContext)
92 $workLogger->$func($msg, $context);
96 * Test the WorkerLogger with
98 public function testLog()
100 $logger = \Mockery::mock(LoggerInterface::class);
101 $workLogger = new WorkerLogger($logger);
102 $context = $testContext = ['test' => 'it'];
103 $testContext['worker_id'] = $workLogger->getWorkerId();
104 $testContext['worker_cmd'] = '';
105 self::assertUid($testContext['worker_id']);
107 ->shouldReceive('log')
108 ->with('debug', 'a test', $testContext)
110 $workLogger->log('debug', 'a test', $context);
115 * Test the WorkerLogger after setting a worker function
117 public function testChangedId()
119 $logger = \Mockery::mock(LoggerInterface::class);
120 $workLogger = new WorkerLogger($logger);
121 $context = $testContext = ['test' => 'it'];
122 $testContext['worker_id'] = $workLogger->getWorkerId();
123 $testContext['worker_cmd'] = '';
124 self::assertUid($testContext['worker_id']);
126 ->shouldReceive('log')
127 ->with('debug', 'a test', $testContext)
129 $workLogger->log('debug', 'a test', $context);
131 $workLogger->setFunctionName('testFunc');
133 self::assertNotEquals($testContext['worker_id'], $workLogger->getWorkerId());
135 $context = $testContext = ['test' => 'it'];
136 $testContext['worker_id'] = $workLogger->getWorkerId();
137 $testContext['worker_cmd'] = 'testFunc';
138 self::assertUid($testContext['worker_id']);
140 ->shouldReceive('log')
141 ->with('debug', 'a test', $testContext)
143 $workLogger->log('debug', 'a test', $context);