]> git.mxchange.org Git - friendica.git/blob - tests/src/Util/Logger/WorkerLoggerTest.php
Merge pull request #10104 from nupplaphil/fix/jit_caching
[friendica.git] / tests / src / Util / Logger / WorkerLoggerTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
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\Util\Logger;
23
24 use Friendica\Test\MockedTest;
25 use Friendica\Util\Logger\WorkerLogger;
26 use Psr\Log\LoggerInterface;
27
28 class WorkerLoggerTest extends MockedTest
29 {
30         private function assertUid($uid, $length = 7)
31         {
32                 self::assertRegExp('/^[a-zA-Z0-9]{' . $length . '}+$/', $uid);
33         }
34
35         /**
36          * Test the a id with length zero
37          *
38          */
39         public function testGetWorkerIdZero()
40         {
41                 $this->expectException(\Error::class);
42                 
43                 $logger = \Mockery::mock(LoggerInterface::class);
44                 new WorkerLogger($logger, 'test', 0);
45         }
46
47         /**
48          * Test the generated Uid
49          */
50         public function testGetWorkerId()
51         {
52                 $logger = \Mockery::mock(LoggerInterface::class);
53                 for ($i = 1; $i < 14; $i++) {
54                         $workLogger = new WorkerLogger($logger, 'test', $i);
55                         $uid = $workLogger->getWorkerId();
56                         self::assertUid($uid, $i);
57                 }
58         }
59
60         public function dataTest()
61         {
62                 return [
63                         'info' => [
64                                 'func' => 'info',
65                                 'msg' => 'the alert',
66                                 'context' => [],
67                         ],
68                         'alert' => [
69                                 'func' => 'alert',
70                                 'msg' => 'another alert',
71                                 'context' => ['test' => 'it'],
72                         ],
73                         'critical' => [
74                                 'func' => 'critical',
75                                 'msg' => 'Critical msg used',
76                                 'context' => ['test' => 'it', 'more' => 0.24545],
77                         ],
78                         'error' => [
79                                 'func' => 'error',
80                                 'msg' => 21345623,
81                                 'context' => ['test' => 'it', 'yet' => true],
82                         ],
83                         'warning' => [
84                                 'func' => 'warning',
85                                 'msg' => 'another alert' . 123523 . 324.54534 . 'test',
86                                 'context' => ['test' => 'it', 2 => 'nope'],
87                         ],
88                         'notice' => [
89                                 'func' => 'notice',
90                                 'msg' => 'Notice' . ' alert' . true . 'with' . '\'strange\'' . 1.24. 'behavior',
91                                 'context' => ['test' => 'it'],
92                         ],
93                         'debug' => [
94                                 'func' => 'debug',
95                                 'msg' => 'at last a debug',
96                                 'context' => ['test' => 'it'],
97                         ],
98                 ];
99         }
100
101         /**
102          * Test the WorkerLogger with different log calls
103          * @dataProvider dataTest
104          */
105         public function testEmergency($func, $msg, $context = [])
106         {
107                 $logger = \Mockery::mock(LoggerInterface::class);
108                 $workLogger = new WorkerLogger($logger, 'test');
109                 $testContext = $context;
110                 $testContext['worker_id'] = $workLogger->getWorkerId();
111                 $testContext['worker_cmd'] = 'test';
112                 self::assertUid($testContext['worker_id']);
113                 $logger
114                         ->shouldReceive($func)
115                         ->with($msg, $testContext)
116                         ->once();
117                 $workLogger->$func($msg, $context);
118         }
119
120         /**
121          * Test the WorkerLogger with
122          */
123         public function testLog()
124         {
125                 $logger = \Mockery::mock(LoggerInterface::class);
126                 $workLogger = new WorkerLogger($logger, 'test');
127                 $context = $testContext = ['test' => 'it'];
128                 $testContext['worker_id'] = $workLogger->getWorkerId();
129                 $testContext['worker_cmd'] = 'test';
130                 self::assertUid($testContext['worker_id']);
131                 $logger
132                         ->shouldReceive('log')
133                         ->with('debug', 'a test', $testContext)
134                         ->once();
135                 $workLogger->log('debug', 'a test', $context);
136         }
137 }