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
18 * @expectedException \Error
20 public function testGetWorkerIdZero()
22 $logger = \Mockery::mock(LoggerInterface::class);
23 new WorkerLogger($logger, 'test', 0);
27 * Test the generated Uid
29 public function testGetWorkerId()
31 $logger = \Mockery::mock(LoggerInterface::class);
32 for ($i = 1; $i < 14; $i++) {
33 $workLogger = new WorkerLogger($logger, 'test', $i);
34 $uid = $workLogger->getWorkerId();
35 $this->assertUid($uid, $i);
39 public function dataTest()
49 'msg' => 'another alert',
50 'context' => ['test' => 'it'],
54 'msg' => 'Critical msg used',
55 'context' => ['test' => 'it', 'more' => 0.24545],
60 'context' => ['test' => 'it', 'yet' => true],
64 'msg' => 'another alert' . 123523 . 324.54534 . 'test',
65 'context' => ['test' => 'it', 2 => 'nope'],
69 'msg' => 'Notice' . ' alert' . true . 'with' . '\'strange\'' . 1.24. 'behavior',
70 'context' => ['test' => 'it'],
74 'msg' => 'at last a debug',
75 'context' => ['test' => 'it'],
81 * Test the WorkerLogger with different log calls
82 * @dataProvider dataTest
84 public function testEmergency($func, $msg, $context = [])
86 $logger = \Mockery::mock(LoggerInterface::class);
87 $workLogger = new WorkerLogger($logger, 'test');
88 $testContext = $context;
89 $testContext['worker_id'] = $workLogger->getWorkerId();
90 $testContext['worker_cmd'] = 'test';
91 $this->assertUid($testContext['worker_id']);
93 ->shouldReceive($func)
94 ->with($msg, $testContext)
96 $workLogger->$func($msg, $context);
100 * Test the WorkerLogger with
102 public function testLog()
104 $logger = \Mockery::mock(LoggerInterface::class);
105 $workLogger = new WorkerLogger($logger, 'test');
106 $context = $testContext = ['test' => 'it'];
107 $testContext['worker_id'] = $workLogger->getWorkerId();
108 $testContext['worker_cmd'] = 'test';
109 $this->assertUid($testContext['worker_id']);
111 ->shouldReceive('log')
112 ->with('debug', 'a test', $testContext)
114 $workLogger->log('debug', 'a test', $context);