3 * @copyright Copyright (C) 2010-2021, 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\Util\FileSystem;
27 use Friendica\Test\Util\VFSTrait;
28 use Friendica\Core\Logger\Type\StreamLogger;
29 use org\bovigo\vfs\vfsStream;
30 use org\bovigo\vfs\vfsStreamFile;
33 class StreamLoggerTest extends AbstractLoggerTest
47 protected function setUp(): void
53 $this->fileSystem = new FileSystem();
59 protected function getInstance($level = LogLevel::DEBUG)
61 $this->logfile = vfsStream::newFile('friendica.log')
64 $logger = new StreamLogger('test', $this->logfile->url(), $this->introspection, $this->fileSystem, $level);
72 protected function getContent()
74 return $this->logfile->getContent();
78 * Test if a stream is working
80 public function testStream()
82 $logfile = vfsStream::newFile('friendica.log')
85 $filehandler = fopen($logfile->url(), 'ab');
87 $logger = new \Friendica\Core\Logger\Type\StreamLogger('test', $filehandler, $this->introspection, $this->fileSystem);
88 $logger->emergency('working');
90 $text = $logfile->getContent();
92 self::assertLogline($text);
96 * Test if the close statement is working
98 public function testClose()
100 $logfile = vfsStream::newFile('friendica.log')
103 $logger = new StreamLogger('test', $logfile->url(), $this->introspection, $this->fileSystem);
104 $logger->emergency('working');
106 // close doesn't affect
107 $logger->emergency('working too');
109 $text = $logfile->getContent();
111 self::assertLoglineNums(2, $text);
115 * Test when a file isn't set
117 public function testNoUrl()
119 $this->expectException(LoggerArgumentException::class);
120 $this->expectExceptionMessage("Missing stream URL.");
122 $logger = new StreamLogger('test', '', $this->introspection, $this->fileSystem);
124 $logger->emergency('not working');
128 * Test when a file cannot be opened
130 public function testWrongUrl()
132 $this->expectException(LoggerException::class);
133 $this->expectExceptionMessage("Cannot create stream.");
135 $logfile = vfsStream::newFile('friendica.log')
136 ->at($this->root)->chmod(0);
138 $logger = new StreamLogger('test', $logfile->url(), $this->introspection, $this->fileSystem);
140 $logger->emergency('not working');
144 * Test when the directory cannot get created
146 public function testWrongDir()
148 $this->expectException(\UnexpectedValueException::class);
149 $this->expectExceptionMessageMatches("/Directory .* cannot get created: .* /");
151 static::markTestIncomplete('We need a platform independent way to set directory to readonly');
153 $logger = new StreamLogger('test', '/$%/wrong/directory/file.txt', $this->introspection, $this->fileSystem);
155 $logger->emergency('not working');
159 * Test when the minimum level is not valid
161 public function testWrongMinimumLevel()
163 $this->expectException(LoggerArgumentException::class);
164 $this->expectExceptionMessageMatches("/The level \".*\" is not valid./");
166 $logger = new StreamLogger('test', 'file.text', $this->introspection, $this->fileSystem, 'NOPE');
170 * Test when the minimum level is not valid
172 public function testWrongLogLevel()
174 $this->expectException(LoggerArgumentException::class);
175 $this->expectExceptionMessageMatches("/The level \".*\" is not valid./");
177 $logfile = vfsStream::newFile('friendica.log')
180 $logger = new StreamLogger('test', $logfile->url(), $this->introspection, $this->fileSystem);
182 $logger->log('NOPE', 'a test');
186 * Test when the file is null
188 public function testWrongFile()
190 $this->expectException(LoggerArgumentException::class);
191 $this->expectExceptionMessage("A stream must either be a resource or a string.");
193 $logger = new StreamLogger('test', null, $this->introspection, $this->fileSystem);
197 * Test a relative path
198 * @doesNotPerformAssertions
200 public function testRealPath()
202 static::markTestSkipped('vfsStream isn\'t compatible with chdir, so not testable.');
204 $logfile = vfsStream::newFile('friendica.log')
207 chdir($this->root->getChild('logs')->url());
209 $logger = new StreamLogger('test', '../friendica.log' , $this->introspection, $this->fileSystem);
211 $logger->info('Test');