]> git.mxchange.org Git - friendica.git/commitdiff
Add tests for ParsedLogIterator
authorfabrixxm <fabrix.xm@gmail.com>
Thu, 19 Aug 2021 12:28:29 +0000 (14:28 +0200)
committerfabrixxm <fabrix.xm@gmail.com>
Thu, 19 Aug 2021 12:57:04 +0000 (14:57 +0200)
tests/datasets/log/friendica.log.txt [new file with mode: 0644]
tests/src/Model/Log/ParsedLogIteratorTest.php [new file with mode: 0644]
tests/src/Object/Log/ParsedLogTest.php

diff --git a/tests/datasets/log/friendica.log.txt b/tests/datasets/log/friendica.log.txt
new file mode 100644 (file)
index 0000000..ed695bd
--- /dev/null
@@ -0,0 +1,3 @@
+2021-05-24T15:23:58Z index [INFO]: No HTTP_SIGNATURE header [] - {"file":"HTTPSignature.php","line":476,"function":"getSigner","uid":"0a3934","process_id":14826}
+2021-05-24T15:30:01Z worker [NOTICE]: Load: 0.01/20 - processes: 0/1/6 (0:0, 30:1) - maximum: 10/10 {"worker_id":"ece8fc8","worker_cmd":"Cron"} - {"file":"Worker.php","line":786,"function":"tooMuchWorkers","uid":"364d3c","process_id":20754}
+2021-05-24T15:40:01Z worker [WARNING]: Spool file does does not start with "item-" {"file":".","worker_id":"560c8b6","worker_cmd":"SpoolPost"} - {"file":"SpoolPost.php","line":40,"function":"execute","uid":"fd8c37","process_id":20846}
diff --git a/tests/src/Model/Log/ParsedLogIteratorTest.php b/tests/src/Model/Log/ParsedLogIteratorTest.php
new file mode 100644 (file)
index 0000000..c971955
--- /dev/null
@@ -0,0 +1,154 @@
+<?php
+/**
+ * @copyright Copyright (C) 2010-2021, the Friendica project
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace Friendica\Test\src\Object\Log;
+
+use Friendica\Util\ReversedFileReader;
+use Friendica\Model\Log\ParsedLogIterator;
+
+use PHPUnit\Framework\TestCase;
+
+
+/**
+ * Parsed log iterator testing class
+ */
+class ParsedLogIteratorTest extends TestCase
+{
+       protected $pli;
+
+       public static function assertParsed($parsed, $expected_data)
+       {
+               foreach ($expected_data as $k => $v) {
+                       self::assertSame($parsed->$k, $v, '"'.$k.'" does not match expectation');
+               }
+       }
+
+       protected function setUp()
+       {
+               $logfile = dirname(__DIR__) . DIRECTORY_SEPARATOR .
+                       '..' . DIRECTORY_SEPARATOR .
+                       '..' . DIRECTORY_SEPARATOR .
+                       'datasets' . DIRECTORY_SEPARATOR .
+                       'log' . DIRECTORY_SEPARATOR .
+                       'friendica.log.txt';
+
+               $reader = new ReversedFileReader();
+               $this->pli = new ParsedLogIterator($reader);
+               $this->pli->open($logfile);
+       }
+
+       public function testIsIterable()
+       {
+               self::assertIsIterable($this->pli);
+       }
+
+       public function testEverything()
+       {
+               self::assertCount(3, iterator_to_array($this->pli, false));
+       }
+
+       public function testLimit()
+       {
+               $this->pli->withLimit(2);
+               self::assertCount(2, iterator_to_array($this->pli, false));
+       }
+
+       public function testFilterByLevel()
+       {
+               $this->pli->withFilters(['level' => 'INFO']);
+               $pls = iterator_to_array($this->pli, false);
+               self::assertCount(1, $pls);
+               self::assertParsed(
+                       $pls[0],
+                       [
+                               'date'    => '2021-05-24T15:23:58Z',
+                               'context' => 'index',
+                               'level'   => 'INFO',
+                               'message' => 'No HTTP_SIGNATURE header',
+                               'data'    => null,
+                               'source'  => '{"file":"HTTPSignature.php","line":476,"function":"getSigner","uid":"0a3934","process_id":14826}',
+                       ]
+               );
+       }
+
+       public function testFilterByContext()
+       {
+               $this->pli->withFilters(['context' => 'worker']);
+               $pls = iterator_to_array($this->pli, false);
+               self::assertCount(2, $pls);
+               self::assertParsed(
+                       $pls[0],
+                       [
+                               'date'    => '2021-05-24T15:40:01Z',
+                               'context' => 'worker',
+                               'level'   => 'WARNING',
+                               'message' => 'Spool file does does not start with "item-"',
+                               'data'    => '{"file":".","worker_id":"560c8b6","worker_cmd":"SpoolPost"}',
+                               'source'  => '{"file":"SpoolPost.php","line":40,"function":"execute","uid":"fd8c37","process_id":20846}',
+                       ]
+               );
+       }
+
+       public function testFilterCombined()
+       {
+               $this->pli->withFilters(['level' => 'NOTICE', 'context' => 'worker']);
+               $pls = iterator_to_array($this->pli, false);
+               self::assertCount(1, $pls);
+               self::assertParsed(
+                       $pls[0],
+                       [
+                               'date'    => '2021-05-24T15:30:01Z',
+                               'context' => 'worker',
+                               'level'   => 'NOTICE',
+                               'message' => 'Load: 0.01/20 - processes: 0/1/6 (0:0, 30:1) - maximum: 10/10',
+                               'data'    => '{"worker_id":"ece8fc8","worker_cmd":"Cron"}',
+                               'source'  => '{"file":"Worker.php","line":786,"function":"tooMuchWorkers","uid":"364d3c","process_id":20754}',
+                       ]
+               );
+       }
+
+       public function testSearch()
+       {
+               $this->pli->withSearch("maximum");
+               $pls = iterator_to_array($this->pli, false);
+               self::assertCount(1, $pls);
+               self::assertParsed(
+                       $pls[0],
+                       [
+                               'date'    => '2021-05-24T15:30:01Z',
+                               'context' => 'worker',
+                               'level'   => 'NOTICE',
+                               'message' => 'Load: 0.01/20 - processes: 0/1/6 (0:0, 30:1) - maximum: 10/10',
+                               'data'    => '{"worker_id":"ece8fc8","worker_cmd":"Cron"}',
+                               'source'  => '{"file":"Worker.php","line":786,"function":"tooMuchWorkers","uid":"364d3c","process_id":20754}',
+                       ]
+               );
+       }
+
+       public function testFilterAndSearch()
+       {
+               $this->pli
+                       ->withFilters(['context' => 'worker'])
+                       ->withSearch("header");
+               $pls = iterator_to_array($this->pli, false);
+               self::assertCount(0, $pls);
+       }
+}
index ab98030fc0a3bcfd04f3542384a36cc32e8d3b89..ff3d06f55be793d747e9d4d8def241b31bec3557 100644 (file)
@@ -43,11 +43,11 @@ class ParsedLogTest extends TestCase
        public function testGenericLogLine()
        {
                self::do_log_line(
-                       '2021-05-24T15:40:01Z worker [NOTICE]: Spool file does does not start with "item-" {"file":".","worker_id":"560c8b6","worker_cmd":"SpoolPost"} - {"file":"SpoolPost.php","line":40,"function":"execute","uid":"fd8c37","process_id":20846}',
+                       '2021-05-24T15:40:01Z worker [WARNING]: Spool file does does not start with "item-" {"file":".","worker_id":"560c8b6","worker_cmd":"SpoolPost"} - {"file":"SpoolPost.php","line":40,"function":"execute","uid":"fd8c37","process_id":20846}',
                        [
                                'date'    => '2021-05-24T15:40:01Z',
                                'context' => 'worker',
-                               'level'   => 'NOTICE',
+                               'level'   => 'WARNING',
                                'message' => 'Spool file does does not start with "item-"',
                                'data'    => '{"file":".","worker_id":"560c8b6","worker_cmd":"SpoolPost"}',
                                'source'  => '{"file":"SpoolPost.php","line":40,"function":"execute","uid":"fd8c37","process_id":20846}',