]> git.mxchange.org Git - friendica.git/commitdiff
Rename AbstractLoggerTest to LoggerTestCase
authorArt4 <art4@wlabs.de>
Sun, 15 Dec 2024 21:28:34 +0000 (21:28 +0000)
committerArt4 <art4@wlabs.de>
Sun, 15 Dec 2024 21:28:34 +0000 (21:28 +0000)
tests/LoggerDataTrait.php [new file with mode: 0644]
tests/LoggerTestCase.php [new file with mode: 0644]
tests/src/Core/Logger/AbstractLoggerTest.php [deleted file]
tests/src/Core/Logger/LoggerDataTrait.php [deleted file]
tests/src/Core/Logger/ProfilerLoggerTest.php
tests/src/Core/Logger/StreamLoggerTest.php
tests/src/Core/Logger/SyslogLoggerTest.php

diff --git a/tests/LoggerDataTrait.php b/tests/LoggerDataTrait.php
new file mode 100644 (file)
index 0000000..229a199
--- /dev/null
@@ -0,0 +1,57 @@
+<?php
+
+// Copyright (C) 2010-2024, the Friendica project
+// SPDX-FileCopyrightText: 2010-2024 the Friendica project
+//
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+namespace Friendica\Test;
+
+trait LoggerDataTrait
+{
+       public function dataTests()
+       {
+               return [
+                       'emergency' => [
+                               'function' => 'emergency',
+                               'message' => 'test',
+                               'context' => ['a' => 'context'],
+                       ],
+                       'alert' => [
+                               'function' => 'alert',
+                               'message' => 'test {test}',
+                               'context' => ['a' => 'context', 2 => 'so', 'test' => 'works'],
+                       ],
+                       'critical' => [
+                               'function' => 'critical',
+                               'message' => 'test crit 2345',
+                               'context' => ['a' => 'context', 'wit' => ['more', 'array']],
+                       ],
+                       'error' => [
+                               'function' => 'error',
+                               'message' => 2.554,
+                               'context' => [],
+                       ],
+                       'warning' => [
+                               'function' => 'warning',
+                               'message' => 'test warn',
+                               'context' => ['a' => 'context'],
+                       ],
+                       'notice' => [
+                               'function' => 'notice',
+                               'message' => 2346,
+                               'context' => ['a' => 'context'],
+                       ],
+                       'info' => [
+                               'function' => 'info',
+                               'message' => null,
+                               'context' => ['a' => 'context'],
+                       ],
+                       'debug' => [
+                               'function' => 'debug',
+                               'message' => true,
+                               'context' => ['a' => false],
+                       ],
+               ];
+       }
+}
diff --git a/tests/LoggerTestCase.php b/tests/LoggerTestCase.php
new file mode 100644 (file)
index 0000000..a31ba5d
--- /dev/null
@@ -0,0 +1,185 @@
+<?php
+
+// Copyright (C) 2010-2024, the Friendica project
+// SPDX-FileCopyrightText: 2010-2024 the Friendica project
+//
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+namespace Friendica\Test;
+
+use Friendica\Core\Config\Capability\IManageConfigValues;
+use Friendica\Core\Logger\Util\Introspection;
+use Friendica\Test\MockedTestCase;
+use Mockery\MockInterface;
+use Psr\Log\LoggerInterface;
+use Psr\Log\LogLevel;
+
+abstract class LoggerTestCase extends MockedTestCase
+{
+       use LoggerDataTrait;
+
+       const LOGLINE = '/.* \[.*]: .* {.*\"file\":\".*\".*,.*\"line\":\d*,.*\"function\":\".*\".*,.*\"uid\":\".*\".*}/';
+
+       const FILE = 'test';
+       const LINE = 666;
+       const FUNC = 'myfunction';
+
+       /**
+        * @var Introspection|MockInterface
+        */
+       protected $introspection;
+       /**
+        * @var IManageConfigValues|MockInterface
+        */
+       protected $config;
+
+       /**
+        * Returns the content of the current logger instance
+        *
+        * @return string
+        */
+       abstract protected function getContent();
+
+       /**
+        * Returns the current logger instance
+        *
+        * @param string $level the default loglevel
+        *
+        * @return LoggerInterface
+        */
+       abstract protected function getInstance($level = LogLevel::DEBUG);
+
+       protected function setUp(): void
+       {
+               parent::setUp();
+
+               $this->introspection = \Mockery::mock(Introspection::class);
+               $this->introspection->shouldReceive('getRecord')->andReturn([
+                       'file'     => self::FILE,
+                       'line'     => self::LINE,
+                       'function' => self::FUNC
+               ]);
+
+               $this->config = \Mockery::mock(IManageConfigValues::class);
+       }
+
+       public function assertLogline($string)
+       {
+               self::assertMatchesRegularExpression(self::LOGLINE, $string);
+       }
+
+       public function assertLoglineNums($assertNum, $string)
+       {
+               self::assertEquals($assertNum, preg_match_all(self::LOGLINE, $string));
+       }
+
+       /**
+        * Test if the logger works correctly
+        */
+       public function testNormal()
+       {
+               $logger = $this->getInstance();
+               $logger->emergency('working!');
+               $logger->alert('working too!');
+               $logger->debug('and now?');
+               $logger->notice('message', ['an' => 'context']);
+
+               $text = $this->getContent();
+               self::assertLogline($text);
+               self::assertLoglineNums(4, $text);
+       }
+
+       /**
+        * Test if a log entry is correctly interpolated
+        */
+       public function testPsrInterpolate()
+       {
+               $logger = $this->getInstance();
+
+               $logger->emergency('A {psr} test', ['psr' => 'working']);
+               $logger->alert('An {array} test', ['array' => ['it', 'is', 'working']]);
+               $text = $this->getContent();
+               self::assertStringContainsString('A working test', $text);
+               self::assertStringContainsString('An ["it","is","working"] test', $text);
+       }
+
+       /**
+        * Test if a log entry contains all necessary information
+        */
+       public function testContainsInformation()
+       {
+               $logger = $this->getInstance();
+               $logger->emergency('A test');
+
+               $text = $this->getContent();
+               self::assertStringContainsString('"file":"' . self::FILE . '"', $text);
+               self::assertStringContainsString('"line":' . self::LINE, $text);
+               self::assertStringContainsString('"function":"' . self::FUNC . '"', $text);
+       }
+
+       /**
+        * Test if the minimum level is working
+        */
+       public function testMinimumLevel()
+       {
+               $logger = $this->getInstance(LogLevel::NOTICE);
+
+               $logger->emergency('working');
+               $logger->alert('working');
+               $logger->error('working');
+               $logger->warning('working');
+               $logger->notice('working');
+               $logger->info('not working');
+               $logger->debug('not working');
+
+               $text = $this->getContent();
+
+               self::assertLoglineNums(5, $text);
+       }
+
+       /**
+        * Test with different logging data
+        * @dataProvider dataTests
+        */
+       public function testDifferentTypes($function, $message, array $context)
+       {
+               $logger = $this->getInstance();
+               $logger->$function($message, $context);
+
+               $text = $this->getContent();
+
+               self::assertLogline($text);
+
+               self::assertStringContainsString(@json_encode($context, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), $text);
+       }
+
+       /**
+        * Test a message with an exception
+        */
+       public function testExceptionHandling()
+       {
+               $e = new \Exception("Test String", 123);
+               $eFollowUp = new \Exception("FollowUp", 456, $e);
+
+               $assertion = $eFollowUp->__toString();
+
+               $logger = $this->getInstance();
+               $logger->alert('test', ['e' => $eFollowUp]);
+               $text = $this->getContent();
+
+               self::assertLogline($text);
+
+               self::assertStringContainsString(@json_encode($assertion, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), $this->getContent());
+       }
+
+       public function testNoObjectHandling()
+       {
+               $logger = $this->getInstance();
+               $logger->alert('test', ['e' => ['test' => 'test']]);
+               $text = $this->getContent();
+
+               self::assertLogline($text);
+
+               self::assertStringContainsString('test', $this->getContent());
+       }
+}
diff --git a/tests/src/Core/Logger/AbstractLoggerTest.php b/tests/src/Core/Logger/AbstractLoggerTest.php
deleted file mode 100644 (file)
index b1a793d..0000000
+++ /dev/null
@@ -1,185 +0,0 @@
-<?php
-
-// Copyright (C) 2010-2024, the Friendica project
-// SPDX-FileCopyrightText: 2010-2024 the Friendica project
-//
-// SPDX-License-Identifier: AGPL-3.0-or-later
-
-namespace Friendica\Test\src\Core\Logger;
-
-use Friendica\Core\Config\Capability\IManageConfigValues;
-use Friendica\Core\Logger\Util\Introspection;
-use Friendica\Test\MockedTestCase;
-use Mockery\MockInterface;
-use Psr\Log\LoggerInterface;
-use Psr\Log\LogLevel;
-
-abstract class AbstractLoggerTest extends MockedTestCase
-{
-       use LoggerDataTrait;
-
-       const LOGLINE = '/.* \[.*]: .* {.*\"file\":\".*\".*,.*\"line\":\d*,.*\"function\":\".*\".*,.*\"uid\":\".*\".*}/';
-
-       const FILE = 'test';
-       const LINE = 666;
-       const FUNC = 'myfunction';
-
-       /**
-        * @var Introspection|MockInterface
-        */
-       protected $introspection;
-       /**
-        * @var IManageConfigValues|MockInterface
-        */
-       protected $config;
-
-       /**
-        * Returns the content of the current logger instance
-        *
-        * @return string
-        */
-       abstract protected function getContent();
-
-       /**
-        * Returns the current logger instance
-        *
-        * @param string $level the default loglevel
-        *
-        * @return LoggerInterface
-        */
-       abstract protected function getInstance($level = LogLevel::DEBUG);
-
-       protected function setUp(): void
-       {
-               parent::setUp();
-
-               $this->introspection = \Mockery::mock(Introspection::class);
-               $this->introspection->shouldReceive('getRecord')->andReturn([
-                       'file'     => self::FILE,
-                       'line'     => self::LINE,
-                       'function' => self::FUNC
-               ]);
-
-               $this->config = \Mockery::mock(IManageConfigValues::class);
-       }
-
-       public function assertLogline($string)
-       {
-               self::assertMatchesRegularExpression(self::LOGLINE, $string);
-       }
-
-       public function assertLoglineNums($assertNum, $string)
-       {
-               self::assertEquals($assertNum, preg_match_all(self::LOGLINE, $string));
-       }
-
-       /**
-        * Test if the logger works correctly
-        */
-       public function testNormal()
-       {
-               $logger = $this->getInstance();
-               $logger->emergency('working!');
-               $logger->alert('working too!');
-               $logger->debug('and now?');
-               $logger->notice('message', ['an' => 'context']);
-
-               $text = $this->getContent();
-               self::assertLogline($text);
-               self::assertLoglineNums(4, $text);
-       }
-
-       /**
-        * Test if a log entry is correctly interpolated
-        */
-       public function testPsrInterpolate()
-       {
-               $logger = $this->getInstance();
-
-               $logger->emergency('A {psr} test', ['psr' => 'working']);
-               $logger->alert('An {array} test', ['array' => ['it', 'is', 'working']]);
-               $text = $this->getContent();
-               self::assertStringContainsString('A working test', $text);
-               self::assertStringContainsString('An ["it","is","working"] test', $text);
-       }
-
-       /**
-        * Test if a log entry contains all necessary information
-        */
-       public function testContainsInformation()
-       {
-               $logger = $this->getInstance();
-               $logger->emergency('A test');
-
-               $text = $this->getContent();
-               self::assertStringContainsString('"file":"' . self::FILE . '"', $text);
-               self::assertStringContainsString('"line":' . self::LINE, $text);
-               self::assertStringContainsString('"function":"' . self::FUNC . '"', $text);
-       }
-
-       /**
-        * Test if the minimum level is working
-        */
-       public function testMinimumLevel()
-       {
-               $logger = $this->getInstance(LogLevel::NOTICE);
-
-               $logger->emergency('working');
-               $logger->alert('working');
-               $logger->error('working');
-               $logger->warning('working');
-               $logger->notice('working');
-               $logger->info('not working');
-               $logger->debug('not working');
-
-               $text = $this->getContent();
-
-               self::assertLoglineNums(5, $text);
-       }
-
-       /**
-        * Test with different logging data
-        * @dataProvider dataTests
-        */
-       public function testDifferentTypes($function, $message, array $context)
-       {
-               $logger = $this->getInstance();
-               $logger->$function($message, $context);
-
-               $text = $this->getContent();
-
-               self::assertLogline($text);
-
-               self::assertStringContainsString(@json_encode($context, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), $text);
-       }
-
-       /**
-        * Test a message with an exception
-        */
-       public function testExceptionHandling()
-       {
-               $e = new \Exception("Test String", 123);
-               $eFollowUp = new \Exception("FollowUp", 456, $e);
-
-               $assertion = $eFollowUp->__toString();
-
-               $logger = $this->getInstance();
-               $logger->alert('test', ['e' => $eFollowUp]);
-               $text = $this->getContent();
-
-               self::assertLogline($text);
-
-               self::assertStringContainsString(@json_encode($assertion, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), $this->getContent());
-       }
-
-       public function testNoObjectHandling()
-       {
-               $logger = $this->getInstance();
-               $logger->alert('test', ['e' => ['test' => 'test']]);
-               $text = $this->getContent();
-
-               self::assertLogline($text);
-
-               self::assertStringContainsString('test', $this->getContent());
-       }
-}
diff --git a/tests/src/Core/Logger/LoggerDataTrait.php b/tests/src/Core/Logger/LoggerDataTrait.php
deleted file mode 100644 (file)
index dd41bf8..0000000
+++ /dev/null
@@ -1,57 +0,0 @@
-<?php
-
-// Copyright (C) 2010-2024, the Friendica project
-// SPDX-FileCopyrightText: 2010-2024 the Friendica project
-//
-// SPDX-License-Identifier: AGPL-3.0-or-later
-
-namespace Friendica\Test\src\Core\Logger;
-
-trait LoggerDataTrait
-{
-       public function dataTests()
-       {
-               return [
-                       'emergency' => [
-                               'function' => 'emergency',
-                               'message' => 'test',
-                               'context' => ['a' => 'context'],
-                       ],
-                       'alert' => [
-                               'function' => 'alert',
-                               'message' => 'test {test}',
-                               'context' => ['a' => 'context', 2 => 'so', 'test' => 'works'],
-                       ],
-                       'critical' => [
-                               'function' => 'critical',
-                               'message' => 'test crit 2345',
-                               'context' => ['a' => 'context', 'wit' => ['more', 'array']],
-                       ],
-                       'error' => [
-                               'function' => 'error',
-                               'message' => 2.554,
-                               'context' => [],
-                       ],
-                       'warning' => [
-                               'function' => 'warning',
-                               'message' => 'test warn',
-                               'context' => ['a' => 'context'],
-                       ],
-                       'notice' => [
-                               'function' => 'notice',
-                               'message' => 2346,
-                               'context' => ['a' => 'context'],
-                       ],
-                       'info' => [
-                               'function' => 'info',
-                               'message' => null,
-                               'context' => ['a' => 'context'],
-                       ],
-                       'debug' => [
-                               'function' => 'debug',
-                               'message' => true,
-                               'context' => ['a' => false],
-                       ],
-               ];
-       }
-}
index 9ae1d5b3efab71cfa8224d1644a7f981b7eb109f..f137e8c7fa297a01cef70d4f52c070d8711bec50 100644 (file)
@@ -8,6 +8,7 @@
 namespace Friendica\Test\src\Core\Logger;
 
 use Friendica\Core\Logger\Type\ProfilerLogger;
+use Friendica\Test\LoggerDataTrait;
 use Friendica\Test\MockedTestCase;
 use Friendica\Util\Profiler;
 use Mockery\MockInterface;
index 9fbb6b631e67b0c3e3904ecd279fdebfaf778cdb..5d6dc84c4465d5e23a0e75bfc8eb5a386bfbd959 100644 (file)
@@ -9,13 +9,14 @@ namespace Friendica\Test\src\Core\Logger;
 
 use Friendica\Core\Logger\Exception\LoggerArgumentException;
 use Friendica\Core\Logger\Exception\LogLevelException;
+use Friendica\Test\LoggerTestCase;
 use Friendica\Test\Util\VFSTrait;
 use Friendica\Core\Logger\Type\StreamLogger;
 use org\bovigo\vfs\vfsStream;
 use org\bovigo\vfs\vfsStreamFile;
 use Psr\Log\LogLevel;
 
-class StreamLoggerTest extends AbstractLoggerTest
+class StreamLoggerTest extends LoggerTestCase
 {
        use VFSTrait;
 
index ca3267e1657098f195b3c052b96ff93f7cc61fc2..ecdaf19e27b5bcda908db3854af975435691840f 100644 (file)
@@ -9,9 +9,10 @@ namespace Friendica\Test\src\Core\Logger;
 
 use Friendica\Core\Logger\Exception\LogLevelException;
 use Friendica\Core\Logger\Type\SyslogLogger;
+use Friendica\Test\LoggerTestCase;
 use Psr\Log\LogLevel;
 
-class SyslogLoggerTest extends AbstractLoggerTest
+class SyslogLoggerTest extends LoggerTestCase
 {
        /**
         * @var SyslogLoggerWrapper