]> git.mxchange.org Git - friendica.git/blob - tests/src/Core/Logger/SyslogLoggerTest.php
Improved logging
[friendica.git] / tests / src / Core / Logger / SyslogLoggerTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Test\src\Core\Logger;
23
24 use Friendica\Core\Logger\Exception\LogLevelException;
25 use Friendica\Core\Logger\Type\SyslogLogger;
26 use Psr\Log\LogLevel;
27
28 class SyslogLoggerTest extends AbstractLoggerTest
29 {
30         /**
31          * @var SyslogLoggerWrapper
32          */
33         private $logger;
34
35         protected function setUp(): void
36         {
37                 parent::setUp();
38
39                 $this->introspection->shouldReceive('addClasses')->with([SyslogLogger::class]);
40                 $this->config->shouldReceive('get')->with('system', 'syslog_flags')->andReturn(SyslogLogger::DEFAULT_FLAGS)
41                                          ->once();
42                 $this->config->shouldReceive('get')->with('system', 'syslog_facility')
43                                          ->andReturn(SyslogLogger::DEFAULT_FACILITY)->once();
44         }
45
46         /**
47          * {@inheritdoc}
48          */
49         protected function getContent()
50         {
51                 return $this->logger->getContent();
52         }
53
54         /**
55          * {@inheritdoc}
56          */
57         protected function getInstance($level = LogLevel::DEBUG)
58         {
59                 $this->config->shouldReceive('get')->with('system', 'loglevel')->andReturn($level);
60
61                 $loggerFactory = new SyslogLoggerFactoryWrapper($this->introspection, 'test');
62                 $this->logger = $loggerFactory->create($this->config);
63
64                 return $this->logger;
65         }
66
67
68         /**
69          * Test when the minimum level is not valid
70          */
71         public function testWrongMinimumLevel()
72         {
73                 $this->expectException(LogLevelException::class);
74                 $this->expectExceptionMessageMatches("/The level \".*\" is not valid./");
75
76                 $logger = $this->getInstance('NOPE');
77         }
78
79         /**
80          * Test when the minimum level is not valid
81          */
82         public function testWrongLogLevel()
83         {
84                 $this->expectException(LogLevelException::class);
85                 $this->expectExceptionMessageMatches("/The level \".*\" is not valid./");
86
87                 $logger = $this->getInstance();
88
89                 $logger->log('NOPE', 'a test');
90         }
91
92         /**
93          * Test the close() method
94          * @doesNotPerformAssertions
95          */
96         public function testClose()
97         {
98                 $logger = $this->getInstance();
99                 $logger->emergency('test');
100                 $logger->close();
101                 // Reopened itself
102                 $logger->emergency('test');
103         }
104 }