]> git.mxchange.org Git - friendica.git/blob - tests/src/Object/Log/ParsedLogLineTest.php
Merge pull request #11250 from nupplaphil/bug/redis_pw
[friendica.git] / tests / src / Object / Log / ParsedLogLineTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, 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\Object\Log;
23
24 use Friendica\Object\Log\ParsedLogLine;
25 use PHPUnit\Framework\TestCase;
26
27 /**
28  * Log parser testing class
29  */
30 class ParsedLogLineTest extends TestCase
31 {
32         public static function do_log_line($logline, $expected_data)
33         {
34                 $parsed = new ParsedLogLine(0, $logline);
35                 foreach ($expected_data as $k => $v) {
36                         self::assertSame($parsed->$k, $v, '"'.$k.'" does not match expectation');
37                 }
38         }
39
40         /**
41          * test parsing a generic log line
42          */
43         public function testGenericLogLine()
44         {
45                 self::do_log_line(
46                         '2021-05-24T15:40:01Z worker [WARNING]: Spool file does does not start with "item-" {"file":".","worker_id":"560c8b6","worker_cmd":"SpoolPost"} - {"file":"SpoolPost.php","line":40,"function":"execute","uid":"fd8c37","process_id":20846}',
47                         [
48                                 'date'    => '2021-05-24T15:40:01Z',
49                                 'context' => 'worker',
50                                 'level'   => 'WARNING',
51                                 'message' => 'Spool file does does not start with "item-"',
52                                 'data'    => '{"file":".","worker_id":"560c8b6","worker_cmd":"SpoolPost"}',
53                                 'source'  => '{"file":"SpoolPost.php","line":40,"function":"execute","uid":"fd8c37","process_id":20846}',
54                         ]
55                 );
56         }
57
58         /**
59          * test parsing a log line with empty data
60          */
61         public function testEmptyDataLogLine()
62         {
63                 self::do_log_line(
64                         '2021-05-24T15:23:58Z index [INFO]: No HTTP_SIGNATURE header [] - {"file":"HTTPSignature.php","line":476,"function":"getSigner","uid":"0a3934","process_id":14826}',
65                         [
66                                 'date'    => '2021-05-24T15:23:58Z',
67                                 'context' => 'index',
68                                 'level'   => 'INFO',
69                                 'message' => 'No HTTP_SIGNATURE header',
70                                 'data'    => null,
71                                 'source'  => '{"file":"HTTPSignature.php","line":476,"function":"getSigner","uid":"0a3934","process_id":14826}',
72                         ]
73                 );
74         }
75
76         /**
77          * test parsing a log line with various " - " in it
78          */
79         public function testTrickyDashLogLine()
80         {
81                 self::do_log_line(
82                         '2021-05-24T15:30:01Z worker [NOTICE]: Load: 0.01/20 - processes: 0/1/6 (0:0, 30:1) - maximum: 10/10 {"worker_id":"ece8fc8","worker_cmd":"Cron"} - {"file":"Worker.php","line":786,"function":"tooMuchWorkers","uid":"364d3c","process_id":20754}',
83                         [
84                                 'date'    => '2021-05-24T15:30:01Z',
85                                 'context' => 'worker',
86                                 'level'   => 'NOTICE',
87                                 'message' => 'Load: 0.01/20 - processes: 0/1/6 (0:0, 30:1) - maximum: 10/10',
88                                 'data'    => '{"worker_id":"ece8fc8","worker_cmd":"Cron"}',
89                                 'source'  => '{"file":"Worker.php","line":786,"function":"tooMuchWorkers","uid":"364d3c","process_id":20754}',
90                         ]
91                 );
92         }
93
94         /**
95          * test non conforming log line
96          */
97         public function testNonConformingLogLine()
98         {
99                 self::do_log_line(
100                         'this log line is not formatted as expected',
101                         [
102                                 'date'    => null,
103                                 'context' => null,
104                                 'level'   => null,
105                                 'message' => 'this log line is not formatted as expected',
106                                 'data'    => null,
107                                 'source'  => null,
108                         ]
109                 );
110         }
111
112         /**
113          * test missing source
114          */
115         public function testMissingSource()
116         {
117                 self::do_log_line(
118                         '2021-05-24T15:30:01Z worker [NOTICE]: Load: 0.01/20 - processes: 0/1/6 (0:0, 30:1) - maximum: 10/10 {"worker_id":"ece8fc8","worker_cmd":"Cron"}',
119                         [
120                                 'date'    => '2021-05-24T15:30:01Z',
121                                 'context' => 'worker',
122                                 'level'   => 'NOTICE',
123                                 'message' => 'Load: 0.01/20 - processes: 0/1/6 (0:0, 30:1) - maximum: 10/10',
124                                 'data'    => '{"worker_id":"ece8fc8","worker_cmd":"Cron"}',
125                                 'source'  => null,
126                         ]
127                 );
128         }
129
130         /**
131          * test missing data
132          */
133         public function testMissingData()
134         {
135                 self::do_log_line(
136                         '2021-05-24T15:30:01Z worker [NOTICE]: Load: 0.01/20 - processes: 0/1/6 (0:0, 30:1) - maximum: 10/10 - {"file":"Worker.php","line":786,"function":"tooMuchWorkers","uid":"364d3c","process_id":20754}',
137                         [
138                                 'date'    => '2021-05-24T15:30:01Z',
139                                 'context' => 'worker',
140                                 'level'   => 'NOTICE',
141                                 'message' => 'Load: 0.01/20 - processes: 0/1/6 (0:0, 30:1) - maximum: 10/10',
142                                 'data'    => null,
143                                 'source'  => '{"file":"Worker.php","line":786,"function":"tooMuchWorkers","uid":"364d3c","process_id":20754}',
144                         ]
145                 );
146         }
147
148         /**
149          * test missing data and source
150          */
151         public function testMissingDataAndSource()
152         {
153                 self::do_log_line(
154                         '2021-05-24T15:30:01Z worker [NOTICE]: Load: 0.01/20 - processes: 0/1/6 (0:0, 30:1) - maximum: 10/10',
155                         [
156                                 'date'    => '2021-05-24T15:30:01Z',
157                                 'context' => 'worker',
158                                 'level'   => 'NOTICE',
159                                 'message' => 'Load: 0.01/20 - processes: 0/1/6 (0:0, 30:1) - maximum: 10/10',
160                                 'data'    => null,
161                                 'source'  => null,
162                         ]
163                 );
164         }
165
166         /**
167          * test missing source and invalid data
168          */
169         public function testMissingSourceAndInvalidData()
170         {
171                 self::do_log_line(
172                         '2021-05-24T15:30:01Z worker [NOTICE]: Load: 0.01/20 - processes: 0/1/6 (0:0, 30:1) - maximum: 10/10 {"invalidjson {really',
173                         [
174                                 'date'    => '2021-05-24T15:30:01Z',
175                                 'context' => 'worker',
176                                 'level'   => 'NOTICE',
177                                 'message' => 'Load: 0.01/20 - processes: 0/1/6 (0:0, 30:1) - maximum: 10/10 {"invalidjson {really',
178                                 'data'    => null,
179                                 'source'  => null,
180                         ]
181                 );
182         }
183 }