2019-03-03 20:25:18 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Friendica\Test\src\Util\Logger;
|
|
|
|
|
|
|
|
use Friendica\Test\Util\VFSTrait;
|
|
|
|
use Friendica\Util\Logger\StreamLogger;
|
|
|
|
use org\bovigo\vfs\vfsStream;
|
2019-03-04 22:39:14 +00:00
|
|
|
use org\bovigo\vfs\vfsStreamFile;
|
2019-03-03 20:25:18 +00:00
|
|
|
use Psr\Log\LogLevel;
|
|
|
|
|
2019-03-04 22:39:14 +00:00
|
|
|
class StreamLoggerTest extends AbstractLoggerTest
|
2019-03-03 20:25:18 +00:00
|
|
|
{
|
|
|
|
use VFSTrait;
|
|
|
|
|
|
|
|
/**
|
2019-03-04 22:39:14 +00:00
|
|
|
* @var StreamLogger
|
|
|
|
*/
|
|
|
|
private $logger;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var vfsStreamFile
|
2019-03-03 20:25:18 +00:00
|
|
|
*/
|
2019-03-04 22:39:14 +00:00
|
|
|
private $logfile;
|
2019-03-03 20:25:18 +00:00
|
|
|
|
|
|
|
protected function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->setUpVfsDir();
|
|
|
|
}
|
|
|
|
|
2019-03-04 22:39:14 +00:00
|
|
|
/**
|
|
|
|
* {@@inheritdoc}
|
|
|
|
*/
|
|
|
|
protected function getInstance($level = LogLevel::DEBUG)
|
2019-03-03 20:25:18 +00:00
|
|
|
{
|
2019-03-04 22:39:14 +00:00
|
|
|
$this->logfile = vfsStream::newFile('friendica.log')
|
2019-03-03 20:25:18 +00:00
|
|
|
->at($this->root);
|
|
|
|
|
2019-03-04 22:39:14 +00:00
|
|
|
$this->logger = new StreamLogger('test', $this->logfile->url(), $this->introspection, $level);
|
2019-03-03 20:25:18 +00:00
|
|
|
|
2019-03-04 22:39:14 +00:00
|
|
|
return $this->logger;
|
2019-03-03 20:25:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-03-04 22:39:14 +00:00
|
|
|
* {@inheritdoc}
|
2019-03-03 20:25:18 +00:00
|
|
|
*/
|
2019-03-04 22:39:14 +00:00
|
|
|
protected function getContent()
|
2019-03-03 20:25:18 +00:00
|
|
|
{
|
2019-03-04 22:39:14 +00:00
|
|
|
return $this->logfile->getContent();
|
2019-03-03 20:25:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-03-04 22:39:14 +00:00
|
|
|
* Test if a stream is working
|
2019-03-03 20:25:18 +00:00
|
|
|
*/
|
2019-03-04 22:39:14 +00:00
|
|
|
public function testStream()
|
2019-03-03 20:25:18 +00:00
|
|
|
{
|
|
|
|
$logfile = vfsStream::newFile('friendica.log')
|
|
|
|
->at($this->root);
|
|
|
|
|
2019-03-04 22:39:14 +00:00
|
|
|
$filehandler = fopen($logfile->url(), 'ab');
|
2019-03-03 20:25:18 +00:00
|
|
|
|
2019-03-04 22:39:14 +00:00
|
|
|
$logger = new StreamLogger('test', $filehandler, $this->introspection);
|
|
|
|
$logger->emergency('working');
|
2019-03-03 20:25:18 +00:00
|
|
|
|
|
|
|
$text = $logfile->getContent();
|
2019-03-04 22:39:14 +00:00
|
|
|
|
|
|
|
$this->assertLogline($text);
|
2019-03-03 20:25:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-03-04 22:39:14 +00:00
|
|
|
* Test if the close statement is working
|
2019-03-03 20:25:18 +00:00
|
|
|
*/
|
2019-03-04 22:39:14 +00:00
|
|
|
public function testClose()
|
2019-03-03 20:25:18 +00:00
|
|
|
{
|
|
|
|
$logfile = vfsStream::newFile('friendica.log')
|
|
|
|
->at($this->root);
|
|
|
|
|
2019-03-04 22:39:14 +00:00
|
|
|
$logger = new StreamLogger('test', $logfile->url(), $this->introspection);
|
2019-03-03 20:25:18 +00:00
|
|
|
$logger->emergency('working');
|
2019-03-04 22:39:14 +00:00
|
|
|
$logger->close();
|
|
|
|
// close doesn't affect
|
|
|
|
$logger->emergency('working too');
|
2019-03-03 20:25:18 +00:00
|
|
|
|
|
|
|
$text = $logfile->getContent();
|
|
|
|
|
2019-03-04 22:39:14 +00:00
|
|
|
$this->assertLoglineNums(2, $text);
|
2019-03-03 20:25:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test when a file isn't set
|
|
|
|
* @expectedException \LogicException
|
|
|
|
* @expectedExceptionMessage Missing stream URL.
|
|
|
|
*/
|
|
|
|
public function testNoUrl()
|
|
|
|
{
|
|
|
|
$logger = new StreamLogger('test', '', $this->introspection);
|
|
|
|
|
|
|
|
$logger->emergency('not working');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-03-04 07:42:08 +00:00
|
|
|
* Test when a file cannot be opened
|
2019-03-03 20:25:18 +00:00
|
|
|
* @expectedException \UnexpectedValueException
|
|
|
|
* @expectedExceptionMessageRegExp /The stream or file .* could not be opened: .* /
|
|
|
|
*/
|
|
|
|
public function testWrongUrl()
|
|
|
|
{
|
2019-03-04 07:42:08 +00:00
|
|
|
$logfile = vfsStream::newFile('friendica.log')
|
|
|
|
->at($this->root)->chmod(0);
|
|
|
|
|
|
|
|
$logger = new StreamLogger('test', $logfile->url(), $this->introspection);
|
2019-03-03 20:25:18 +00:00
|
|
|
|
|
|
|
$logger->emergency('not working');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test when the directory cannot get created
|
|
|
|
* @expectedException \UnexpectedValueException
|
|
|
|
* @expectedExceptionMessageRegExp /Directory .* cannot get created: .* /
|
|
|
|
*/
|
|
|
|
public function testWrongDir()
|
|
|
|
{
|
2019-09-23 13:23:09 +00:00
|
|
|
$logger = new StreamLogger('test', '/$%/wrong/directory/file.txt', $this->introspection);
|
2019-03-03 20:25:18 +00:00
|
|
|
|
|
|
|
$logger->emergency('not working');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test when the minimum level is not valid
|
|
|
|
* @expectedException \InvalidArgumentException
|
|
|
|
* @expectedExceptionMessageRegExp /The level ".*" is not valid./
|
|
|
|
*/
|
|
|
|
public function testWrongMinimumLevel()
|
|
|
|
{
|
|
|
|
$logger = new StreamLogger('test', 'file.text', $this->introspection, 'NOPE');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test when the minimum level is not valid
|
|
|
|
* @expectedException \InvalidArgumentException
|
|
|
|
* @expectedExceptionMessageRegExp /The level ".*" is not valid./
|
|
|
|
*/
|
|
|
|
public function testWrongLogLevel()
|
|
|
|
{
|
|
|
|
$logfile = vfsStream::newFile('friendica.log')
|
|
|
|
->at($this->root);
|
|
|
|
|
|
|
|
$logger = new StreamLogger('test', $logfile->url(), $this->introspection);
|
|
|
|
|
|
|
|
$logger->log('NOPE', 'a test');
|
|
|
|
}
|
2019-03-04 22:39:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test when the file is null
|
|
|
|
* @expectedException \InvalidArgumentException
|
|
|
|
* @expectedExceptionMessage A stream must either be a resource or a string.
|
|
|
|
*/
|
|
|
|
public function testWrongFile()
|
|
|
|
{
|
|
|
|
$logger = new StreamLogger('test', null, $this->introspection);
|
|
|
|
}
|
2019-03-03 20:25:18 +00:00
|
|
|
}
|