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\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;
32 class StreamLoggerTest extends AbstractLoggerTest
41 protected function setUp(): void
51 protected function getInstance($level = LogLevel::DEBUG, $logfile = 'friendica.log')
53 $this->logfile = vfsStream::newFile($logfile)
56 $this->config->shouldReceive('get')->with('system', 'logfile')->andReturn($this->logfile->url())->once();
57 $this->config->shouldReceive('get')->with('system', 'loglevel')->andReturn($level)->once();
59 $loggerFactory = new \Friendica\Core\Logger\Factory\StreamLogger($this->introspection, 'test');
60 return $loggerFactory->create($this->config);
66 protected function getContent()
68 return $this->logfile->getContent();
72 * Test when a file isn't set
74 public function testNoUrl()
76 $this->expectException(LoggerArgumentException::class);
77 $this->expectExceptionMessage(' is not a valid logfile');
79 $this->config->shouldReceive('get')->with('system', 'logfile')->andReturn('')->once();
81 $loggerFactory = new \Friendica\Core\Logger\Factory\StreamLogger($this->introspection, 'test');
82 $logger = $loggerFactory->create($this->config);
84 $logger->emergency('not working');
88 * Test when a file cannot be opened
90 public function testWrongUrl()
92 $this->expectException(LoggerArgumentException::class);
94 $logfile = vfsStream::newFile('friendica.log')
95 ->at($this->root)->chmod(0);
97 $this->config->shouldReceive('get')->with('system', 'logfile')->andReturn($logfile->url())->once();
99 $loggerFactory = new \Friendica\Core\Logger\Factory\StreamLogger($this->introspection, 'test');
100 $logger = $loggerFactory->create($this->config);
102 $logger->emergency('not working');
106 * Test when the directory cannot get created
108 public function testWrongDir()
110 $this->expectException(\UnexpectedValueException::class);
111 $this->expectExceptionMessageMatches("/Directory .* cannot get created: .* /");
113 static::markTestIncomplete('We need a platform independent way to set directory to readonly');
115 $loggerFactory = new \Friendica\Core\Logger\Factory\StreamLogger($this->introspection, 'test');
116 $logger = $loggerFactory->create($this->config);
118 $logger->emergency('not working');
122 * Test when the minimum level is not valid
124 public function testWrongMinimumLevel()
126 $this->expectException(LogLevelException::class);
127 $this->expectExceptionMessageMatches("/The level \".*\" is not valid./");
129 $logger = $this->getInstance('NOPE');
133 * Test when the minimum level is not valid
135 public function testWrongLogLevel()
137 $this->expectException(LogLevelException::class);
138 $this->expectExceptionMessageMatches("/The level \".*\" is not valid./");
140 $logger = $this->getInstance('NOPE');
142 $logger->log('NOPE', 'a test');
146 * Test a relative path
147 * @doesNotPerformAssertions
149 public function testRealPath()
151 static::markTestSkipped('vfsStream isn\'t compatible with chdir, so not testable.');
153 $logfile = vfsStream::newFile('friendica.log')
156 chdir($this->root->getChild('logs')->url());
158 $this->config->shouldReceive('get')->with('system', 'logfile')->andReturn('../friendica.log')->once();
160 $logger = new StreamLogger('test', $this->config, $this->introspection, $this->fileSystem);
162 $logger->info('Test');