3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Test\src\Object\Log;
24 use Friendica\Util\ReversedFileReader;
25 use Friendica\Model\Log\ParsedLogIterator;
27 use PHPUnit\Framework\TestCase;
30 * Parsed log iterator testing class
32 class ParsedLogIteratorTest extends TestCase
36 public static function assertParsed($parsed, $expected_data)
38 foreach ($expected_data as $k => $v) {
39 self::assertSame($parsed->$k, $v, '"'.$k.'" does not match expectation');
43 protected function setUp(): void
45 $logfile = dirname(__DIR__) . '/../../datasets/log/friendica.log.txt';
47 $reader = new ReversedFileReader();
48 $this->pli = new ParsedLogIterator($reader);
49 $this->pli->open($logfile);
52 public function testIsIterable()
54 self::assertIsIterable($this->pli);
57 public function testEverything()
59 self::assertCount(3, iterator_to_array($this->pli, false));
62 public function testLimit()
64 $this->pli->withLimit(2);
65 self::assertCount(2, iterator_to_array($this->pli, false));
68 public function testFilterByLevel()
70 $this->pli->withFilters(['level' => 'INFO']);
71 $pls = iterator_to_array($this->pli, false);
72 self::assertCount(1, $pls);
76 'date' => '2021-05-24T15:23:58Z',
79 'message' => 'No HTTP_SIGNATURE header',
81 'source' => '{"file":"HTTPSignature.php","line":476,"function":"getSigner","uid":"0a3934","process_id":14826}',
86 public function testFilterByContext()
88 $this->pli->withFilters(['context' => 'worker']);
89 $pls = iterator_to_array($this->pli, false);
90 self::assertCount(2, $pls);
94 'date' => '2021-05-24T15:40:01Z',
95 'context' => 'worker',
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}',
104 public function testFilterCombined()
106 $this->pli->withFilters(['level' => 'NOTICE', 'context' => 'worker']);
107 $pls = iterator_to_array($this->pli, false);
108 self::assertCount(1, $pls);
112 'date' => '2021-05-24T15:30:01Z',
113 'context' => 'worker',
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}',
122 public function testSearch()
124 $this->pli->withSearch("maximum");
125 $pls = iterator_to_array($this->pli, false);
126 self::assertCount(1, $pls);
130 'date' => '2021-05-24T15:30:01Z',
131 'context' => 'worker',
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}',
140 public function testFilterAndSearch()
143 ->withFilters(['context' => 'worker'])
144 ->withSearch("header");
145 $pls = iterator_to_array($this->pli, false);
146 self::assertCount(0, $pls);
149 public function testEmptyLogFile()
151 $logfile = dirname(__DIR__) . '/../../datasets/log/empty.friendica.log.txt';
153 $reader = new ReversedFileReader();
154 $pli = new ParsedLogIterator($reader);
155 $pli->open($logfile);
157 $pls = iterator_to_array($pli, false);
158 self::assertCount(0, $pls);