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