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