]> git.mxchange.org Git - friendica.git/blob - tests/src/Core/Logger/StreamLoggerTest.php
Improved logging
[friendica.git] / tests / src / Core / Logger / StreamLoggerTest.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\LoggerArgumentException;
25 use Friendica\Core\Logger\Exception\LogLevelException;
26 use Friendica\Test\Util\VFSTrait;
27 use Friendica\Core\Logger\Type\StreamLogger;
28 use org\bovigo\vfs\vfsStream;
29 use org\bovigo\vfs\vfsStreamFile;
30 use Psr\Log\LogLevel;
31
32 class StreamLoggerTest extends AbstractLoggerTest
33 {
34         use VFSTrait;
35
36         /**
37          * @var vfsStreamFile
38          */
39         private $logfile;
40
41         protected function setUp(): void
42         {
43                 parent::setUp();
44
45                 $this->setUpVfsDir();
46         }
47
48         /**
49          * {@@inheritdoc}
50          */
51         protected function getInstance($level = LogLevel::DEBUG, $logfile = 'friendica.log')
52         {
53                 $this->logfile = vfsStream::newFile($logfile)
54                         ->at($this->root);
55
56                 $this->config->shouldReceive('get')->with('system', 'logfile')->andReturn($this->logfile->url())->once();
57                 $this->config->shouldReceive('get')->with('system', 'loglevel')->andReturn($level)->once();
58
59                 $loggerFactory = new \Friendica\Core\Logger\Factory\StreamLogger($this->introspection, 'test');
60                 return $loggerFactory->create($this->config);
61         }
62
63         /**
64          * {@inheritdoc}
65          */
66         protected function getContent()
67         {
68                 return $this->logfile->getContent();
69         }
70
71         /**
72          * Test when a file isn't set
73          */
74         public function testNoUrl()
75         {
76                 $this->expectException(LoggerArgumentException::class);
77                 $this->expectExceptionMessage(' is not a valid logfile');
78
79                 $this->config->shouldReceive('get')->with('system', 'logfile')->andReturn('')->once();
80
81                 $loggerFactory = new \Friendica\Core\Logger\Factory\StreamLogger($this->introspection, 'test');
82                 $logger = $loggerFactory->create($this->config);
83
84                 $logger->emergency('not working');
85         }
86
87         /**
88          * Test when a file cannot be opened
89          */
90         public function testWrongUrl()
91         {
92                 $this->expectException(LoggerArgumentException::class);
93
94                 $logfile = vfsStream::newFile('friendica.log')
95                         ->at($this->root)->chmod(0);
96
97                 $this->config->shouldReceive('get')->with('system', 'logfile')->andReturn($logfile->url())->once();
98
99                 $loggerFactory = new \Friendica\Core\Logger\Factory\StreamLogger($this->introspection, 'test');
100                 $logger = $loggerFactory->create($this->config);
101
102                 $logger->emergency('not working');
103         }
104
105         /**
106          * Test when the directory cannot get created
107          */
108         public function testWrongDir()
109         {
110                 $this->expectException(\UnexpectedValueException::class);
111                 $this->expectExceptionMessageMatches("/Directory .* cannot get created: .* /");
112
113                 static::markTestIncomplete('We need a platform independent way to set directory to readonly');
114
115                 $loggerFactory = new \Friendica\Core\Logger\Factory\StreamLogger($this->introspection, 'test');
116                 $logger = $loggerFactory->create($this->config);
117
118                 $logger->emergency('not working');
119         }
120
121         /**
122          * Test when the minimum level is not valid
123          */
124         public function testWrongMinimumLevel()
125         {
126                 $this->expectException(LogLevelException::class);
127                 $this->expectExceptionMessageMatches("/The level \".*\" is not valid./");
128
129                 $logger = $this->getInstance('NOPE');
130         }
131
132         /**
133          * Test when the minimum level is not valid
134          */
135         public function testWrongLogLevel()
136         {
137                 $this->expectException(LogLevelException::class);
138                 $this->expectExceptionMessageMatches("/The level \".*\" is not valid./");
139
140                 $logger = $this->getInstance('NOPE');
141
142                 $logger->log('NOPE', 'a test');
143         }
144
145         /**
146          * Test a relative path
147          * @doesNotPerformAssertions
148          */
149         public function testRealPath()
150         {
151                 static::markTestSkipped('vfsStream isn\'t compatible with chdir, so not testable.');
152
153                 $logfile = vfsStream::newFile('friendica.log')
154                                     ->at($this->root);
155
156                 chdir($this->root->getChild('logs')->url());
157
158                 $this->config->shouldReceive('get')->with('system', 'logfile')->andReturn('../friendica.log')->once();
159
160                 $logger = new StreamLogger('test', $this->config, $this->introspection, $this->fileSystem);
161
162                 $logger->info('Test');
163         }
164 }