0713f64d0b8179f5844c075658b6f8ece45f5d63
[friendica.git/.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
19          */
20         public function testGetWorkerIdZero()
21         {
22                 $logger = \Mockery::mock(LoggerInterface::class);
23                 $logger
24                         ->shouldReceive('alert')
25                         ->with('id length must be greater than 0.')
26                         ->once();
27                 new WorkerLogger($logger, 'test', 0);
28         }
29
30         /**
31          * Test the generated Uid
32          */
33         public function testGetWorkerId()
34         {
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);
40                 }
41         }
42
43         public function dataTest()
44         {
45                 return [
46                         'info' => [
47                                 'func' => 'info',
48                                 'msg' => 'the alert',
49                                 'context' => [],
50                         ],
51                         'alert' => [
52                                 'func' => 'alert',
53                                 'msg' => 'another alert',
54                                 'context' => ['test' => 'it'],
55                         ],
56                         'critical' => [
57                                 'func' => 'critical',
58                                 'msg' => 'Critical msg used',
59                                 'context' => ['test' => 'it', 'more' => 0.24545],
60                         ],
61                         'error' => [
62                                 'func' => 'error',
63                                 'msg' => 21345623,
64                                 'context' => ['test' => 'it', 'yet' => true],
65                         ],
66                         'warning' => [
67                                 'func' => 'warning',
68                                 'msg' => 'another alert' . 123523 . 324.54534 . 'test',
69                                 'context' => ['test' => 'it', 2 => 'nope'],
70                         ],
71                         'notice' => [
72                                 'func' => 'notice',
73                                 'msg' => 'Notice' . ' alert' . true . 'with' . '\'strange\'' . 1.24. 'behavior',
74                                 'context' => ['test' => 'it'],
75                         ],
76                         'debug' => [
77                                 'func' => 'debug',
78                                 'msg' => 'at last a debug',
79                                 'context' => ['test' => 'it'],
80                         ],
81                 ];
82         }
83
84         /**
85          * Test the WorkerLogger with different log calls
86          * @dataProvider dataTest
87          */
88         public function testEmergency($func, $msg, $context = [])
89         {
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']);
96                 $logger
97                         ->shouldReceive($func)
98                         ->with($msg, $testContext)
99                         ->once();
100                 $workLogger->$func($msg, $context);
101         }
102
103         /**
104          * Test the WorkerLogger with
105          */
106         public function testLog()
107         {
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']);
114                 $logger
115                         ->shouldReceive('log')
116                         ->with('debug', 'a test', $testContext)
117                         ->once();
118                 $workLogger->log('debug', 'a test', $context);
119         }
120 }