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;
31 * Parsed log iterator testing class
33 class ParsedLogIteratorTest extends TestCase
37 public static function assertParsed($parsed, $expected_data)
39 foreach ($expected_data as $k => $v) {
40 self::assertSame($parsed->$k, $v, '"'.$k.'" does not match expectation');
44 protected function setUp()
46 $logfile = dirname(__DIR__) . DIRECTORY_SEPARATOR .
47 '..' . DIRECTORY_SEPARATOR .
48 '..' . DIRECTORY_SEPARATOR .
49 'datasets' . DIRECTORY_SEPARATOR .
50 'log' . DIRECTORY_SEPARATOR .
53 $reader = new ReversedFileReader();
54 $this->pli = new ParsedLogIterator($reader);
55 $this->pli->open($logfile);
58 public function testIsIterable()
60 self::assertIsIterable($this->pli);
63 public function testEverything()
65 self::assertCount(3, iterator_to_array($this->pli, false));
68 public function testLimit()
70 $this->pli->withLimit(2);
71 self::assertCount(2, iterator_to_array($this->pli, false));
74 public function testFilterByLevel()
76 $this->pli->withFilters(['level' => 'INFO']);
77 $pls = iterator_to_array($this->pli, false);
78 self::assertCount(1, $pls);
82 'date' => '2021-05-24T15:23:58Z',
85 'message' => 'No HTTP_SIGNATURE header',
87 'source' => '{"file":"HTTPSignature.php","line":476,"function":"getSigner","uid":"0a3934","process_id":14826}',
92 public function testFilterByContext()
94 $this->pli->withFilters(['context' => 'worker']);
95 $pls = iterator_to_array($this->pli, false);
96 self::assertCount(2, $pls);
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}',
110 public function testFilterCombined()
112 $this->pli->withFilters(['level' => 'NOTICE', 'context' => 'worker']);
113 $pls = iterator_to_array($this->pli, false);
114 self::assertCount(1, $pls);
118 'date' => '2021-05-24T15:30:01Z',
119 'context' => 'worker',
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}',
128 public function testSearch()
130 $this->pli->withSearch("maximum");
131 $pls = iterator_to_array($this->pli, false);
132 self::assertCount(1, $pls);
136 'date' => '2021-05-24T15:30:01Z',
137 'context' => 'worker',
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}',
146 public function testFilterAndSearch()
149 ->withFilters(['context' => 'worker'])
150 ->withSearch("header");
151 $pls = iterator_to_array($this->pli, false);
152 self::assertCount(0, $pls);