]> git.mxchange.org Git - friendica.git/blobdiff - tests/src/Util/Logger/AbstractLoggerTest.php
Replace `assertContains()` for string assertions
[friendica.git] / tests / src / Util / Logger / AbstractLoggerTest.php
index e0c033583765e4bf60f880082945d2be097d71d1..d008f4dc428b5c49676f4200d84f9318c683a536 100644 (file)
@@ -1,4 +1,23 @@
 <?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\Util\Logger;
 
@@ -39,7 +58,7 @@ abstract class AbstractLoggerTest extends MockedTest
         */
        abstract protected function getInstance($level = LogLevel::DEBUG);
 
-       protected function setUp()
+       protected function setUp(): void
        {
                parent::setUp();
 
@@ -53,12 +72,12 @@ abstract class AbstractLoggerTest extends MockedTest
 
        public function assertLogline($string)
        {
-               $this->assertRegExp(self::LOGLINE, $string);
+               self::assertRegExp(self::LOGLINE, $string);
        }
 
        public function assertLoglineNums($assertNum, $string)
        {
-               $this->assertEquals($assertNum, preg_match_all(self::LOGLINE, $string));
+               self::assertEquals($assertNum, preg_match_all(self::LOGLINE, $string));
        }
 
        /**
@@ -73,8 +92,8 @@ abstract class AbstractLoggerTest extends MockedTest
                $logger->notice('message', ['an' => 'context']);
 
                $text = $this->getContent();
-               $this->assertLogline($text);
-               $this->assertLoglineNums(4, $text);
+               self::assertLogline($text);
+               self::assertLoglineNums(4, $text);
        }
 
        /**
@@ -87,8 +106,8 @@ abstract class AbstractLoggerTest extends MockedTest
                $logger->emergency('A {psr} test', ['psr' => 'working']);
                $logger->alert('An {array} test', ['array' => ['it', 'is', 'working']]);
                $text = $this->getContent();
-               $this->assertContains('A working test', $text);
-               $this->assertContains('An ["it","is","working"] test', $text);
+               self::assertStringContainsString('A working test', $text);
+               self::assertStringContainsString('An ["it","is","working"] test', $text);
        }
 
        /**
@@ -100,9 +119,9 @@ abstract class AbstractLoggerTest extends MockedTest
                $logger->emergency('A test');
 
                $text = $this->getContent();
-               $this->assertContains('"file":"' . self::FILE . '"', $text);
-               $this->assertContains('"line":' . self::LINE, $text);
-               $this->assertContains('"function":"' . self::FUNC . '"', $text);
+               self::assertStringContainsString('"file":"' . self::FILE . '"', $text);
+               self::assertStringContainsString('"line":' . self::LINE, $text);
+               self::assertStringContainsString('"function":"' . self::FUNC . '"', $text);
        }
 
        /**
@@ -122,7 +141,7 @@ abstract class AbstractLoggerTest extends MockedTest
 
                $text = $this->getContent();
 
-               $this->assertLoglineNums(5, $text);
+               self::assertLoglineNums(5, $text);
        }
 
        /**
@@ -136,8 +155,38 @@ abstract class AbstractLoggerTest extends MockedTest
 
                $text = $this->getContent();
 
-               $this->assertLogline($text);
+               self::assertLogline($text);
 
-               $this->assertContains(@json_encode($context), $text);
+               self::assertStringContainsString(@json_encode($context), $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), $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());
        }
 }