]> git.mxchange.org Git - friendica.git/blob - tests/src/Util/Logger/WorkerLoggerTest.php
Merge remote-tracking branch 'upstream/develop' into issue-3229
[friendica.git] / tests / src / Util / Logger / WorkerLoggerTest.php
1 <?php
2
3 namespace Friendica\Test\src\Util\Logger;
4
5 use Friendica\Test\MockedTest;
6 use Friendica\Util\Logger\WorkerLogger;
7 use Psr\Log\LoggerInterface;
8
9 class WorkerLoggerTest extends MockedTest
10 {
11         private function assertUid($uid, $length = 7)
12         {
13                 $this->assertRegExp('/^[a-zA-Z0-9]{' . $length . '}+$/', $uid);
14         }
15
16         /**
17          * Test the a id with length zero
18          * @expectedException \Error
19          */
20         public function testGetWorkerIdZero()
21         {
22                 $logger = \Mockery::mock(LoggerInterface::class);
23                 new WorkerLogger($logger, 'test', 0);
24         }
25
26         /**
27          * Test the generated Uid
28          */
29         public function testGetWorkerId()
30         {
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);
36                 }
37         }
38
39         public function dataTest()
40         {
41                 return [
42                         'info' => [
43                                 'func' => 'info',
44                                 'msg' => 'the alert',
45                                 'context' => [],
46                         ],
47                         'alert' => [
48                                 'func' => 'alert',
49                                 'msg' => 'another alert',
50                                 'context' => ['test' => 'it'],
51                         ],
52                         'critical' => [
53                                 'func' => 'critical',
54                                 'msg' => 'Critical msg used',
55                                 'context' => ['test' => 'it', 'more' => 0.24545],
56                         ],
57                         'error' => [
58                                 'func' => 'error',
59                                 'msg' => 21345623,
60                                 'context' => ['test' => 'it', 'yet' => true],
61                         ],
62                         'warning' => [
63                                 'func' => 'warning',
64                                 'msg' => 'another alert' . 123523 . 324.54534 . 'test',
65                                 'context' => ['test' => 'it', 2 => 'nope'],
66                         ],
67                         'notice' => [
68                                 'func' => 'notice',
69                                 'msg' => 'Notice' . ' alert' . true . 'with' . '\'strange\'' . 1.24. 'behavior',
70                                 'context' => ['test' => 'it'],
71                         ],
72                         'debug' => [
73                                 'func' => 'debug',
74                                 'msg' => 'at last a debug',
75                                 'context' => ['test' => 'it'],
76                         ],
77                 ];
78         }
79
80         /**
81          * Test the WorkerLogger with different log calls
82          * @dataProvider dataTest
83          */
84         public function testEmergency($func, $msg, $context = [])
85         {
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']);
92                 $logger
93                         ->shouldReceive($func)
94                         ->with($msg, $testContext)
95                         ->once();
96                 $workLogger->$func($msg, $context);
97         }
98
99         /**
100          * Test the WorkerLogger with
101          */
102         public function testLog()
103         {
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']);
110                 $logger
111                         ->shouldReceive('log')
112                         ->with('debug', 'a test', $testContext)
113                         ->once();
114                 $workLogger->log('debug', 'a test', $context);
115         }
116 }