3 namespace Friendica\Test\src\Util\Logger;
5 use Friendica\Test\Util\VFSTrait;
6 use Friendica\Util\Logger\StreamLogger;
7 use org\bovigo\vfs\vfsStream;
8 use org\bovigo\vfs\vfsStreamFile;
11 class StreamLoggerTest extends AbstractLoggerTest
25 protected function setUp()
35 protected function getInstance($level = LogLevel::DEBUG)
37 $this->logfile = vfsStream::newFile('friendica.log')
40 $this->logger = new StreamLogger('test', $this->logfile->url(), $this->introspection, $level);
48 protected function getContent()
50 return $this->logfile->getContent();
54 * Test if a stream is working
56 public function testStream()
58 $logfile = vfsStream::newFile('friendica.log')
61 $filehandler = fopen($logfile->url(), 'ab');
63 $logger = new StreamLogger('test', $filehandler, $this->introspection);
64 $logger->emergency('working');
66 $text = $logfile->getContent();
68 $this->assertLogline($text);
72 * Test if the close statement is working
74 public function testClose()
76 $logfile = vfsStream::newFile('friendica.log')
79 $logger = new StreamLogger('test', $logfile->url(), $this->introspection);
80 $logger->emergency('working');
82 // close doesn't affect
83 $logger->emergency('working too');
85 $text = $logfile->getContent();
87 $this->assertLoglineNums(2, $text);
91 * Test when a file isn't set
92 * @expectedException \LogicException
93 * @expectedExceptionMessage Missing stream URL.
95 public function testNoUrl()
97 $logger = new StreamLogger('test', '', $this->introspection);
99 $logger->emergency('not working');
103 * Test when a file cannot be opened
104 * @expectedException \UnexpectedValueException
105 * @expectedExceptionMessageRegExp /The stream or file .* could not be opened: .* /
107 public function testWrongUrl()
109 $logfile = vfsStream::newFile('friendica.log')
110 ->at($this->root)->chmod(0);
112 $logger = new StreamLogger('test', $logfile->url(), $this->introspection);
114 $logger->emergency('not working');
118 * Test when the directory cannot get created
119 * @expectedException \UnexpectedValueException
120 * @expectedExceptionMessageRegExp /Directory .* cannot get created: .* /
122 public function testWrongDir()
124 $this->markTestIncomplete('We need a platform independent way to set directory to readonly');
126 $logger = new StreamLogger('test', '/$%/wrong/directory/file.txt', $this->introspection);
128 $logger->emergency('not working');
132 * Test when the minimum level is not valid
133 * @expectedException \InvalidArgumentException
134 * @expectedExceptionMessageRegExp /The level ".*" is not valid./
136 public function testWrongMinimumLevel()
138 $logger = new StreamLogger('test', 'file.text', $this->introspection, 'NOPE');
142 * Test when the minimum level is not valid
143 * @expectedException \InvalidArgumentException
144 * @expectedExceptionMessageRegExp /The level ".*" is not valid./
146 public function testWrongLogLevel()
148 $logfile = vfsStream::newFile('friendica.log')
151 $logger = new StreamLogger('test', $logfile->url(), $this->introspection);
153 $logger->log('NOPE', 'a test');
157 * Test when the file is null
158 * @expectedException \InvalidArgumentException
159 * @expectedExceptionMessage A stream must either be a resource or a string.
161 public function testWrongFile()
163 $logger = new StreamLogger('test', null, $this->introspection);