3 namespace Friendica\Test\src\Util\Logger;
5 use Friendica\Test\MockedTest;
6 use Friendica\Util\Logger\WorkerLogger;
7 use Psr\Log\LoggerInterface;
9 class WorkerLoggerTest extends MockedTest
11 private function assertUid($uid, $length = 7)
13 $this->assertRegExp('/^[a-zA-Z0-9]{' . $length . '}+$/', $uid);
17 * Test the a id with length zero
20 public function testGetWorkerIdZero()
22 $logger = \Mockery::mock(LoggerInterface::class);
24 ->shouldReceive('alert')
25 ->with('id length must be greater than 0.')
27 new WorkerLogger($logger, 'test', 0);
31 * Test the generated Uid
33 public function testGetWorkerId()
35 $logger = \Mockery::mock(LoggerInterface::class);
36 for ($i = 1; $i < 14; $i++) {
37 $workLogger = new WorkerLogger($logger, 'test', $i);
38 $uid = $workLogger->getWorkerId();
39 $this->assertUid($uid, $i);
43 public function dataTest()
53 'msg' => 'another alert',
54 'context' => ['test' => 'it'],
58 'msg' => 'Critical msg used',
59 'context' => ['test' => 'it', 'more' => 0.24545],
64 'context' => ['test' => 'it', 'yet' => true],
68 'msg' => 'another alert' . 123523 . 324.54534 . 'test',
69 'context' => ['test' => 'it', 2 => 'nope'],
73 'msg' => 'Notice' . ' alert' . true . 'with' . '\'strange\'' . 1.24. 'behavior',
74 'context' => ['test' => 'it'],
78 'msg' => 'at last a debug',
79 'context' => ['test' => 'it'],
85 * Test the WorkerLogger with different log calls
86 * @dataProvider dataTest
88 public function testEmergency($func, $msg, $context = [])
90 $logger = \Mockery::mock(LoggerInterface::class);
91 $workLogger = new WorkerLogger($logger, 'test');
92 $testContext = $context;
93 $testContext['worker_id'] = $workLogger->getWorkerId();
94 $testContext['worker_cmd'] = 'test';
95 $this->assertUid($testContext['worker_id']);
97 ->shouldReceive($func)
98 ->with($msg, $testContext)
100 $workLogger->$func($msg, $context);
104 * Test the WorkerLogger with
106 public function testLog()
108 $logger = \Mockery::mock(LoggerInterface::class);
109 $workLogger = new WorkerLogger($logger, 'test');
110 $context = $testContext = ['test' => 'it'];
111 $testContext['worker_id'] = $workLogger->getWorkerId();
112 $testContext['worker_cmd'] = 'test';
113 $this->assertUid($testContext['worker_id']);
115 ->shouldReceive('log')
116 ->with('debug', 'a test', $testContext)
118 $workLogger->log('debug', 'a test', $context);