]> git.mxchange.org Git - friendica.git/blob - tests/src/Model/Log/ParsedLogIteratorTest.php
Add tests for ParsedLogIterator
[friendica.git] / tests / src / Model / Log / ParsedLogIteratorTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, 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\Util\ReversedFileReader;
25 use Friendica\Model\Log\ParsedLogIterator;
26
27 use PHPUnit\Framework\TestCase;
28
29
30 /**
31  * Parsed log iterator testing class
32  */
33 class ParsedLogIteratorTest extends TestCase
34 {
35         protected $pli;
36
37         public static function assertParsed($parsed, $expected_data)
38         {
39                 foreach ($expected_data as $k => $v) {
40                         self::assertSame($parsed->$k, $v, '"'.$k.'" does not match expectation');
41                 }
42         }
43
44         protected function setUp()
45         {
46                 $logfile = dirname(__DIR__) . DIRECTORY_SEPARATOR .
47                         '..' . DIRECTORY_SEPARATOR .
48                         '..' . DIRECTORY_SEPARATOR .
49                         'datasets' . DIRECTORY_SEPARATOR .
50                         'log' . DIRECTORY_SEPARATOR .
51                         'friendica.log.txt';
52
53                 $reader = new ReversedFileReader();
54                 $this->pli = new ParsedLogIterator($reader);
55                 $this->pli->open($logfile);
56         }
57
58         public function testIsIterable()
59         {
60                 self::assertIsIterable($this->pli);
61         }
62
63         public function testEverything()
64         {
65                 self::assertCount(3, iterator_to_array($this->pli, false));
66         }
67
68         public function testLimit()
69         {
70                 $this->pli->withLimit(2);
71                 self::assertCount(2, iterator_to_array($this->pli, false));
72         }
73
74         public function testFilterByLevel()
75         {
76                 $this->pli->withFilters(['level' => 'INFO']);
77                 $pls = iterator_to_array($this->pli, false);
78                 self::assertCount(1, $pls);
79                 self::assertParsed(
80                         $pls[0],
81                         [
82                                 'date'    => '2021-05-24T15:23:58Z',
83                                 'context' => 'index',
84                                 'level'   => 'INFO',
85                                 'message' => 'No HTTP_SIGNATURE header',
86                                 'data'    => null,
87                                 'source'  => '{"file":"HTTPSignature.php","line":476,"function":"getSigner","uid":"0a3934","process_id":14826}',
88                         ]
89                 );
90         }
91
92         public function testFilterByContext()
93         {
94                 $this->pli->withFilters(['context' => 'worker']);
95                 $pls = iterator_to_array($this->pli, false);
96                 self::assertCount(2, $pls);
97                 self::assertParsed(
98                         $pls[0],
99                         [
100                                 'date'    => '2021-05-24T15:40:01Z',
101                                 'context' => 'worker',
102                                 'level'   => 'WARNING',
103                                 'message' => 'Spool file does does not start with "item-"',
104                                 'data'    => '{"file":".","worker_id":"560c8b6","worker_cmd":"SpoolPost"}',
105                                 'source'  => '{"file":"SpoolPost.php","line":40,"function":"execute","uid":"fd8c37","process_id":20846}',
106                         ]
107                 );
108         }
109
110         public function testFilterCombined()
111         {
112                 $this->pli->withFilters(['level' => 'NOTICE', 'context' => 'worker']);
113                 $pls = iterator_to_array($this->pli, false);
114                 self::assertCount(1, $pls);
115                 self::assertParsed(
116                         $pls[0],
117                         [
118                                 'date'    => '2021-05-24T15:30:01Z',
119                                 'context' => 'worker',
120                                 'level'   => 'NOTICE',
121                                 'message' => 'Load: 0.01/20 - processes: 0/1/6 (0:0, 30:1) - maximum: 10/10',
122                                 'data'    => '{"worker_id":"ece8fc8","worker_cmd":"Cron"}',
123                                 'source'  => '{"file":"Worker.php","line":786,"function":"tooMuchWorkers","uid":"364d3c","process_id":20754}',
124                         ]
125                 );
126         }
127
128         public function testSearch()
129         {
130                 $this->pli->withSearch("maximum");
131                 $pls = iterator_to_array($this->pli, false);
132                 self::assertCount(1, $pls);
133                 self::assertParsed(
134                         $pls[0],
135                         [
136                                 'date'    => '2021-05-24T15:30:01Z',
137                                 'context' => 'worker',
138                                 'level'   => 'NOTICE',
139                                 'message' => 'Load: 0.01/20 - processes: 0/1/6 (0:0, 30:1) - maximum: 10/10',
140                                 'data'    => '{"worker_id":"ece8fc8","worker_cmd":"Cron"}',
141                                 'source'  => '{"file":"Worker.php","line":786,"function":"tooMuchWorkers","uid":"364d3c","process_id":20754}',
142                         ]
143                 );
144         }
145
146         public function testFilterAndSearch()
147         {
148                 $this->pli
149                         ->withFilters(['context' => 'worker'])
150                         ->withSearch("header");
151                 $pls = iterator_to_array($this->pli, false);
152                 self::assertCount(0, $pls);
153         }
154 }