3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Test\src\Core\Logger;
24 use Friendica\Core\Logger\Exception\LoggerArgumentException;
25 use Friendica\Core\Logger\Exception\LoggerException;
26 use Friendica\Core\Logger\Exception\LogLevelException;
27 use Friendica\Util\FileSystem;
28 use Friendica\Test\Util\VFSTrait;
29 use Friendica\Core\Logger\Type\StreamLogger;
30 use org\bovigo\vfs\vfsStream;
31 use org\bovigo\vfs\vfsStreamFile;
34 class StreamLoggerTest extends AbstractLoggerTest
48 protected function setUp(): void
54 $this->fileSystem = new FileSystem();
60 protected function getInstance($level = LogLevel::DEBUG)
62 $this->logfile = vfsStream::newFile('friendica.log')
65 $this->config->shouldReceive('get')->with('system', 'logfile')->andReturn($this->logfile->url())->once();
67 $logger = new StreamLogger('test', $this->config, $this->introspection, $this->fileSystem, $level);
75 protected function getContent()
77 return $this->logfile->getContent();
81 * Test when a file isn't set
83 public function testNoUrl()
85 $this->expectException(LoggerArgumentException::class);
86 $this->expectExceptionMessage("Missing stream URL.");
88 $this->config->shouldReceive('get')->with('system', 'logfile')->andReturn('')->once();
90 $logger = new StreamLogger('test', $this->config, $this->introspection, $this->fileSystem);
92 $logger->emergency('not working');
96 * Test when a file cannot be opened
98 public function testWrongUrl()
100 $this->expectException(LoggerArgumentException::class);
102 $logfile = vfsStream::newFile('friendica.log')
103 ->at($this->root)->chmod(0);
105 $this->config->shouldReceive('get')->with('system', 'logfile')->andReturn($logfile->url())->once();
107 $logger = new StreamLogger('test', $this->config, $this->introspection, $this->fileSystem);
109 $logger->emergency('not working');
113 * Test when the directory cannot get created
115 public function testWrongDir()
117 $this->expectException(\UnexpectedValueException::class);
118 $this->expectExceptionMessageMatches("/Directory .* cannot get created: .* /");
120 static::markTestIncomplete('We need a platform independent way to set directory to readonly');
122 $logger = new StreamLogger('test', '/$%/wrong/directory/file.txt', $this->introspection, $this->fileSystem);
124 $logger->emergency('not working');
128 * Test when the minimum level is not valid
130 public function testWrongMinimumLevel()
132 $this->expectException(LogLevelException::class);
133 $this->expectExceptionMessageMatches("/The level \".*\" is not valid./");
135 $this->config->shouldReceive('get')->with('system', 'logfile')->andReturn('file.text')->once();
137 $logger = new StreamLogger('test', $this->config, $this->introspection, $this->fileSystem, 'NOPE');
141 * Test when the minimum level is not valid
143 public function testWrongLogLevel()
145 $this->expectException(LogLevelException::class);
146 $this->expectExceptionMessageMatches("/The level \".*\" is not valid./");
148 $logfile = vfsStream::newFile('friendica.log')
151 $this->config->shouldReceive('get')->with('system', 'logfile')->andReturn($logfile->url())->once();
153 $logger = new StreamLogger('test', $this->config, $this->introspection, $this->fileSystem);
155 $logger->log('NOPE', 'a test');
159 * Test when the file is null
161 public function testWrongFile()
163 $this->expectException(LoggerArgumentException::class);
164 $this->expectExceptionMessage("A stream must either be a resource or a string.");
166 $this->config->shouldReceive('get')->with('system', 'logfile')->andReturn(null)->once();
168 $logger = new StreamLogger('test', $this->config, $this->introspection, $this->fileSystem);
172 * Test a relative path
173 * @doesNotPerformAssertions
175 public function testRealPath()
177 static::markTestSkipped('vfsStream isn\'t compatible with chdir, so not testable.');
179 $logfile = vfsStream::newFile('friendica.log')
182 chdir($this->root->getChild('logs')->url());
184 $this->config->shouldReceive('get')->with('system', 'logfile')->andReturn('../friendica.log')->once();
186 $logger = new StreamLogger('test', $this->config, $this->introspection, $this->fileSystem);
188 $logger->info('Test');