3 * @copyright Copyright (C) 2010-2022, 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 $logger = new StreamLogger('test', $this->logfile->url(), $this->introspection, $this->fileSystem, $level);
73 protected function getContent()
75 return $this->logfile->getContent();
79 * Test if a stream is working
81 public function testStream()
83 $logfile = vfsStream::newFile('friendica.log')
86 $filehandler = fopen($logfile->url(), 'ab');
88 $logger = new \Friendica\Core\Logger\Type\StreamLogger('test', $filehandler, $this->introspection, $this->fileSystem);
89 $logger->emergency('working');
91 $text = $logfile->getContent();
93 self::assertLogline($text);
97 * Test if the close statement is working
99 public function testClose()
101 $logfile = vfsStream::newFile('friendica.log')
104 $logger = new StreamLogger('test', $logfile->url(), $this->introspection, $this->fileSystem);
105 $logger->emergency('working');
107 // close doesn't affect
108 $logger->emergency('working too');
110 $text = $logfile->getContent();
112 self::assertLoglineNums(2, $text);
116 * Test when a file isn't set
118 public function testNoUrl()
120 $this->expectException(LoggerArgumentException::class);
121 $this->expectExceptionMessage("Missing stream URL.");
123 $logger = new StreamLogger('test', '', $this->introspection, $this->fileSystem);
125 $logger->emergency('not working');
129 * Test when a file cannot be opened
131 public function testWrongUrl()
133 $this->expectException(LoggerException::class);
134 $this->expectExceptionMessage("Cannot create stream.");
136 $logfile = vfsStream::newFile('friendica.log')
137 ->at($this->root)->chmod(0);
139 $logger = new StreamLogger('test', $logfile->url(), $this->introspection, $this->fileSystem);
141 $logger->emergency('not working');
145 * Test when the directory cannot get created
147 public function testWrongDir()
149 $this->expectException(\UnexpectedValueException::class);
150 $this->expectExceptionMessageMatches("/Directory .* cannot get created: .* /");
152 static::markTestIncomplete('We need a platform independent way to set directory to readonly');
154 $logger = new StreamLogger('test', '/$%/wrong/directory/file.txt', $this->introspection, $this->fileSystem);
156 $logger->emergency('not working');
160 * Test when the minimum level is not valid
162 public function testWrongMinimumLevel()
164 $this->expectException(LogLevelException::class);
165 $this->expectExceptionMessageMatches("/The level \".*\" is not valid./");
167 $logger = new StreamLogger('test', 'file.text', $this->introspection, $this->fileSystem, 'NOPE');
171 * Test when the minimum level is not valid
173 public function testWrongLogLevel()
175 $this->expectException(LogLevelException::class);
176 $this->expectExceptionMessageMatches("/The level \".*\" is not valid./");
178 $logfile = vfsStream::newFile('friendica.log')
181 $logger = new StreamLogger('test', $logfile->url(), $this->introspection, $this->fileSystem);
183 $logger->log('NOPE', 'a test');
187 * Test when the file is null
189 public function testWrongFile()
191 $this->expectException(LoggerArgumentException::class);
192 $this->expectExceptionMessage("A stream must either be a resource or a string.");
194 $logger = new StreamLogger('test', null, $this->introspection, $this->fileSystem);
198 * Test a relative path
199 * @doesNotPerformAssertions
201 public function testRealPath()
203 static::markTestSkipped('vfsStream isn\'t compatible with chdir, so not testable.');
205 $logfile = vfsStream::newFile('friendica.log')
208 chdir($this->root->getChild('logs')->url());
210 $logger = new StreamLogger('test', '../friendica.log' , $this->introspection, $this->fileSystem);
212 $logger->info('Test');