3 namespace Friendica\Test\src\Util\Logger;
5 use Friendica\Util\FileSystem;
6 use Friendica\Test\Util\VFSTrait;
7 use Friendica\Util\Logger\StreamLogger;
8 use org\bovigo\vfs\vfsStream;
9 use org\bovigo\vfs\vfsStreamFile;
12 class StreamLoggerTest extends AbstractLoggerTest
31 protected function setUp()
37 $this->fileSystem = new Filesystem();
43 protected function getInstance($level = LogLevel::DEBUG)
45 $this->logfile = vfsStream::newFile('friendica.log')
48 $this->logger = new StreamLogger('test', $this->logfile->url(), $this->introspection, $this->fileSystem, $level);
56 protected function getContent()
58 return $this->logfile->getContent();
62 * Test if a stream is working
64 public function testStream()
66 $logfile = vfsStream::newFile('friendica.log')
69 $filehandler = fopen($logfile->url(), 'ab');
71 $logger = new StreamLogger('test', $filehandler, $this->introspection, $this->fileSystem);
72 $logger->emergency('working');
74 $text = $logfile->getContent();
76 $this->assertLogline($text);
80 * Test if the close statement is working
82 public function testClose()
84 $logfile = vfsStream::newFile('friendica.log')
87 $logger = new StreamLogger('test', $logfile->url(), $this->introspection, $this->fileSystem);
88 $logger->emergency('working');
90 // close doesn't affect
91 $logger->emergency('working too');
93 $text = $logfile->getContent();
95 $this->assertLoglineNums(2, $text);
99 * Test when a file isn't set
100 * @expectedException \LogicException
101 * @expectedExceptionMessage Missing stream URL.
103 public function testNoUrl()
105 $logger = new StreamLogger('test', '', $this->introspection, $this->fileSystem);
107 $logger->emergency('not working');
111 * Test when a file cannot be opened
112 * @expectedException \UnexpectedValueException
113 * @expectedExceptionMessageRegExp /The stream or file .* could not be opened: .* /
115 public function testWrongUrl()
117 $logfile = vfsStream::newFile('friendica.log')
118 ->at($this->root)->chmod(0);
120 $logger = new StreamLogger('test', $logfile->url(), $this->introspection, $this->fileSystem);
122 $logger->emergency('not working');
126 * Test when the directory cannot get created
127 * @expectedException \UnexpectedValueException
128 * @expectedExceptionMessageRegExp /Directory .* cannot get created: .* /
130 public function testWrongDir()
132 $this->markTestIncomplete('We need a platform independent way to set directory to readonly');
134 $logger = new StreamLogger('test', '/$%/wrong/directory/file.txt', $this->introspection, $this->fileSystem);
136 $logger->emergency('not working');
140 * Test when the minimum level is not valid
141 * @expectedException \InvalidArgumentException
142 * @expectedExceptionMessageRegExp /The level ".*" is not valid./
144 public function testWrongMinimumLevel()
146 $logger = new StreamLogger('test', 'file.text', $this->introspection, $this->fileSystem, 'NOPE');
150 * Test when the minimum level is not valid
151 * @expectedException \InvalidArgumentException
152 * @expectedExceptionMessageRegExp /The level ".*" is not valid./
154 public function testWrongLogLevel()
156 $logfile = vfsStream::newFile('friendica.log')
159 $logger = new StreamLogger('test', $logfile->url(), $this->introspection, $this->fileSystem);
161 $logger->log('NOPE', 'a test');
165 * Test when the file is null
166 * @expectedException \InvalidArgumentException
167 * @expectedExceptionMessage A stream must either be a resource or a string.
169 public function testWrongFile()
171 $logger = new StreamLogger('test', null, $this->introspection, $this->fileSystem);
175 * Test a relative path
177 public function testRealPath()
179 $this->markTestSkipped('vfsStream isn\'t compatible with chdir, so not testable.');
181 $logfile = vfsStream::newFile('friendica.log')
184 chdir($this->root->getChild('logs')->url());
186 $logger = new StreamLogger('test', '../friendica.log' , $this->introspection, $this->fileSystem);
188 $logger->info('Test');