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\Util\Logger;
24 use Friendica\Util\FileSystem;
25 use Friendica\Test\Util\VFSTrait;
26 use Friendica\Util\Logger\StreamLogger;
27 use org\bovigo\vfs\vfsStream;
28 use org\bovigo\vfs\vfsStreamFile;
31 class StreamLoggerTest extends AbstractLoggerTest
45 protected function setUp(): void
51 $this->fileSystem = new FileSystem();
57 protected function getInstance($level = LogLevel::DEBUG)
59 $this->logfile = vfsStream::newFile('friendica.log')
62 $logger = new StreamLogger('test', $this->logfile->url(), $this->introspection, $this->fileSystem, $level);
70 protected function getContent()
72 return $this->logfile->getContent();
76 * Test if a stream is working
78 public function testStream()
80 $logfile = vfsStream::newFile('friendica.log')
83 $filehandler = fopen($logfile->url(), 'ab');
85 $logger = new StreamLogger('test', $filehandler, $this->introspection, $this->fileSystem);
86 $logger->emergency('working');
88 $text = $logfile->getContent();
90 self::assertLogline($text);
94 * Test if the close statement is working
96 public function testClose()
98 $logfile = vfsStream::newFile('friendica.log')
101 $logger = new StreamLogger('test', $logfile->url(), $this->introspection, $this->fileSystem);
102 $logger->emergency('working');
104 // close doesn't affect
105 $logger->emergency('working too');
107 $text = $logfile->getContent();
109 self::assertLoglineNums(2, $text);
113 * Test when a file isn't set
115 public function testNoUrl()
117 $this->expectException(\LogicException::class);
118 $this->expectExceptionMessage("Missing stream URL.");
120 $logger = new StreamLogger('test', '', $this->introspection, $this->fileSystem);
122 $logger->emergency('not working');
126 * Test when a file cannot be opened
128 public function testWrongUrl()
130 $this->expectException(\UnexpectedValueException::class);
131 $this->expectExceptionMessageMatches("/The stream or file .* could not be opened: .* /");
133 $logfile = vfsStream::newFile('friendica.log')
134 ->at($this->root)->chmod(0);
136 $logger = new StreamLogger('test', $logfile->url(), $this->introspection, $this->fileSystem);
138 $logger->emergency('not working');
142 * Test when the directory cannot get created
144 public function testWrongDir()
146 $this->expectException(\UnexpectedValueException::class);
147 $this->expectExceptionMessageMatches("/Directory .* cannot get created: .* /");
149 static::markTestIncomplete('We need a platform independent way to set directory to readonly');
151 $logger = new StreamLogger('test', '/$%/wrong/directory/file.txt', $this->introspection, $this->fileSystem);
153 $logger->emergency('not working');
157 * Test when the minimum level is not valid
159 public function testWrongMinimumLevel()
161 $this->expectException(\InvalidArgumentException::class);
162 $this->expectExceptionMessageMatches("/The level \".*\" is not valid./");
164 $logger = new StreamLogger('test', 'file.text', $this->introspection, $this->fileSystem, 'NOPE');
168 * Test when the minimum level is not valid
170 public function testWrongLogLevel()
172 $this->expectException(\InvalidArgumentException::class);
173 $this->expectExceptionMessageMatches("/The level \".*\" is not valid./");
175 $logfile = vfsStream::newFile('friendica.log')
178 $logger = new StreamLogger('test', $logfile->url(), $this->introspection, $this->fileSystem);
180 $logger->log('NOPE', 'a test');
184 * Test when the file is null
186 public function testWrongFile()
188 $this->expectException(\InvalidArgumentException::class);
189 $this->expectExceptionMessage("A stream must either be a resource or a string.");
191 $logger = new StreamLogger('test', null, $this->introspection, $this->fileSystem);
195 * Test a relative path
196 * @doesNotPerformAssertions
198 public function testRealPath()
200 static::markTestSkipped('vfsStream isn\'t compatible with chdir, so not testable.');
202 $logfile = vfsStream::newFile('friendica.log')
205 chdir($this->root->getChild('logs')->url());
207 $logger = new StreamLogger('test', '../friendica.log' , $this->introspection, $this->fileSystem);
209 $logger->info('Test');