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()
45 $logfile = dirname(__DIR__) . DIRECTORY_SEPARATOR .
46 '..' . DIRECTORY_SEPARATOR .
47 '..' . DIRECTORY_SEPARATOR .
48 'datasets' . DIRECTORY_SEPARATOR .
49 'log' . DIRECTORY_SEPARATOR .
52 $reader = new ReversedFileReader();
53 $this->pli = new ParsedLogIterator($reader);
54 $this->pli->open($logfile);
57 public function testIsIterable()
59 self::assertIsIterable($this->pli);
62 public function testEverything()
64 self::assertCount(3, iterator_to_array($this->pli, false));
67 public function testLimit()
69 $this->pli->withLimit(2);
70 self::assertCount(2, iterator_to_array($this->pli, false));
73 public function testFilterByLevel()
75 $this->pli->withFilters(['level' => 'INFO']);
76 $pls = iterator_to_array($this->pli, false);
77 self::assertCount(1, $pls);
81 'date' => '2021-05-24T15:23:58Z',
84 'message' => 'No HTTP_SIGNATURE header',
86 'source' => '{"file":"HTTPSignature.php","line":476,"function":"getSigner","uid":"0a3934","process_id":14826}',
91 public function testFilterByContext()
93 $this->pli->withFilters(['context' => 'worker']);
94 $pls = iterator_to_array($this->pli, false);
95 self::assertCount(2, $pls);
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}',
109 public function testFilterCombined()
111 $this->pli->withFilters(['level' => 'NOTICE', 'context' => 'worker']);
112 $pls = iterator_to_array($this->pli, false);
113 self::assertCount(1, $pls);
117 'date' => '2021-05-24T15:30:01Z',
118 'context' => 'worker',
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}',
127 public function testSearch()
129 $this->pli->withSearch("maximum");
130 $pls = iterator_to_array($this->pli, false);
131 self::assertCount(1, $pls);
135 'date' => '2021-05-24T15:30:01Z',
136 'context' => 'worker',
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}',
145 public function testFilterAndSearch()
148 ->withFilters(['context' => 'worker'])
149 ->withSearch("header");
150 $pls = iterator_to_array($this->pli, false);
151 self::assertCount(0, $pls);