]> git.mxchange.org Git - friendica.git/blob - tests/src/Core/Logger/WorkerLoggerTest.php
Improved logging
[friendica.git] / tests / src / Core / Logger / WorkerLoggerTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Test\src\Core\Logger;
23
24 use Friendica\Core\Logger\Type\WorkerLogger;
25 use Friendica\Test\MockedTest;
26 use Psr\Log\LoggerInterface;
27
28 class WorkerLoggerTest extends MockedTest
29 {
30         private function assertUid($uid)
31         {
32                 self::assertMatchesRegularExpression('/^[a-zA-Z0-9]{' . WorkerLogger::WORKER_ID_LENGTH . '}+$/', $uid);
33         }
34
35         public function dataTest()
36         {
37                 return [
38                         'info' => [
39                                 'func'    => 'info',
40                                 'msg'     => 'the alert',
41                                 'context' => [],
42                         ],
43                         'alert' => [
44                                 'func'    => 'alert',
45                                 'msg'     => 'another alert',
46                                 'context' => ['test' => 'it'],
47                         ],
48                         'critical' => [
49                                 'func'    => 'critical',
50                                 'msg'     => 'Critical msg used',
51                                 'context' => ['test' => 'it', 'more' => 0.24545],
52                         ],
53                         'error' => [
54                                 'func'    => 'error',
55                                 'msg'     => 21345623,
56                                 'context' => ['test' => 'it', 'yet' => true],
57                         ],
58                         'warning' => [
59                                 'func'    => 'warning',
60                                 'msg'     => 'another alert' . 123523 . 324.54534 . 'test',
61                                 'context' => ['test' => 'it', 2 => 'nope'],
62                         ],
63                         'notice' => [
64                                 'func'    => 'notice',
65                                 'msg'     => 'Notice' . ' alert' . true . 'with' . '\'strange\'' . 1.24. 'behavior',
66                                 'context' => ['test' => 'it'],
67                         ],
68                         'debug' => [
69                                 'func'    => 'debug',
70                                 'msg'     => 'at last a debug',
71                                 'context' => ['test' => 'it'],
72                         ],
73                 ];
74         }
75
76         /**
77          * Test the WorkerLogger with different log calls
78          * @dataProvider dataTest
79          */
80         public function testEmergency($func, $msg, $context = [])
81         {
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']);
88                 $logger
89                         ->shouldReceive($func)
90                         ->with($msg, $testContext)
91                         ->once();
92                 $workLogger->$func($msg, $context);
93         }
94
95         /**
96          * Test the WorkerLogger with
97          */
98         public function testLog()
99         {
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']);
106                 $logger
107                         ->shouldReceive('log')
108                         ->with('debug', 'a test', $testContext)
109                         ->once();
110                 $workLogger->log('debug', 'a test', $context);
111         }
112
113
114         /**
115          * Test the WorkerLogger after setting a worker function
116          */
117         public function testChangedId()
118         {
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']);
125                 $logger
126                         ->shouldReceive('log')
127                         ->with('debug', 'a test', $testContext)
128                         ->once();
129                 $workLogger->log('debug', 'a test', $context);
130
131                 $workLogger->setFunctionName('testFunc');
132
133                 self::assertNotEquals($testContext['worker_id'], $workLogger->getWorkerId());
134
135                 $context                   = $testContext                   = ['test' => 'it'];
136                 $testContext['worker_id']  = $workLogger->getWorkerId();
137                 $testContext['worker_cmd'] = 'testFunc';
138                 self::assertUid($testContext['worker_id']);
139                 $logger
140                         ->shouldReceive('log')
141                         ->with('debug', 'a test', $testContext)
142                         ->once();
143                 $workLogger->log('debug', 'a test', $context);
144         }
145 }