mirror of
https://github.com/friendica/friendica
synced 2025-04-25 05:10:11 +00:00
Adding Logger Tests
This commit is contained in:
parent
7bebb03f95
commit
aee348fa02
9 changed files with 447 additions and 82 deletions
|
@ -2,126 +2,89 @@
|
|||
|
||||
namespace Friendica\Test\src\Util\Logger;
|
||||
|
||||
use Friendica\Test\MockedTest;
|
||||
use Friendica\Test\Util\VFSTrait;
|
||||
use Friendica\Util\Introspection;
|
||||
use Friendica\Util\Logger\StreamLogger;
|
||||
use Mockery\MockInterface;
|
||||
use org\bovigo\vfs\vfsStream;
|
||||
use org\bovigo\vfs\vfsStreamFile;
|
||||
use Psr\Log\LogLevel;
|
||||
|
||||
class StreamLoggerTest extends MockedTest
|
||||
class StreamLoggerTest extends AbstractLoggerTest
|
||||
{
|
||||
const LOGLINE = '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} .* \[.*\]: .* \{.*\"file\":\".*\".*,.*\"line\":\d*,.*\"function\":\".*\".*,.*\"uid\":\".*\".*,.*\"process_id\":\d*.*\}/';
|
||||
|
||||
const FILE = 'test';
|
||||
const LINE = 666;
|
||||
const FUNC = 'myfunction';
|
||||
|
||||
use VFSTrait;
|
||||
|
||||
/**
|
||||
* @var Introspection|MockInterface
|
||||
* @var StreamLogger
|
||||
*/
|
||||
private $introspection;
|
||||
private $logger;
|
||||
|
||||
/**
|
||||
* @var vfsStreamFile
|
||||
*/
|
||||
private $logfile;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->setUpVfsDir();
|
||||
|
||||
$this->introspection = \Mockery::mock(Introspection::class);
|
||||
$this->introspection->shouldReceive('getRecord')->andReturn([
|
||||
'file' => self::FILE,
|
||||
'line' => self::LINE,
|
||||
'function' => self::FUNC
|
||||
]);
|
||||
}
|
||||
|
||||
public function assertLogline($string)
|
||||
{
|
||||
$this->assertRegExp(self::LOGLINE, $string);
|
||||
}
|
||||
|
||||
public function assertLoglineNums($assertNum, $string)
|
||||
{
|
||||
$this->assertEquals($assertNum, preg_match_all(self::LOGLINE, $string));
|
||||
}
|
||||
|
||||
public function testNormal()
|
||||
{
|
||||
$logfile = vfsStream::newFile('friendica.log')
|
||||
->at($this->root);
|
||||
|
||||
$logger = new StreamLogger('test', $logfile->url(), $this->introspection);
|
||||
$logger->emergency('working!');
|
||||
$logger->alert('working too!');
|
||||
$logger->debug('and now?');
|
||||
$logger->notice('message', ['an' => 'context']);
|
||||
|
||||
$text = $logfile->getContent();
|
||||
$this->assertLogline($text);
|
||||
$this->assertLoglineNums(4, $text);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if a log entry is correctly interpolated
|
||||
* {@@inheritdoc}
|
||||
*/
|
||||
public function testPsrInterpolate()
|
||||
protected function getInstance($level = LogLevel::DEBUG)
|
||||
{
|
||||
$logfile = vfsStream::newFile('friendica.log')
|
||||
$this->logfile = vfsStream::newFile('friendica.log')
|
||||
->at($this->root);
|
||||
|
||||
$logger = new StreamLogger('test', $logfile->url(), $this->introspection);
|
||||
$this->logger = new StreamLogger('test', $this->logfile->url(), $this->introspection, $level);
|
||||
|
||||
$logger->emergency('A {psr} test', ['psr' => 'working']);
|
||||
$logger->alert('An {array} test', ['array' => ['it', 'is', 'working']]);
|
||||
$text = $logfile->getContent();
|
||||
$this->assertContains('A working test', $text);
|
||||
$this->assertContains('An ["it","is","working"] test', $text);
|
||||
return $this->logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if a log entry contains all necessary information
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function testContainsInformation()
|
||||
protected function getContent()
|
||||
{
|
||||
$logfile = vfsStream::newFile('friendica.log')
|
||||
->at($this->root);
|
||||
|
||||
$logger = new StreamLogger('test', $logfile->url(), $this->introspection);
|
||||
|
||||
$logger->emergency('A test');
|
||||
|
||||
$text = $logfile->getContent();
|
||||
$this->assertContains('"process_id":' . getmypid(), $text);
|
||||
$this->assertContains('"file":"' . self::FILE . '"', $text);
|
||||
$this->assertContains('"line":' . self::LINE, $text);
|
||||
$this->assertContains('"function":"' . self::FUNC . '"', $text);
|
||||
return $this->logfile->getContent();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if the minimum level is working
|
||||
* Test if a stream is working
|
||||
*/
|
||||
public function testMinimumLevel()
|
||||
public function testStream()
|
||||
{
|
||||
$logfile = vfsStream::newFile('friendica.log')
|
||||
->at($this->root);
|
||||
|
||||
$logger = new StreamLogger('test', $logfile->url(), $this->introspection, LogLevel::NOTICE);
|
||||
$filehandler = fopen($logfile->url(), 'ab');
|
||||
|
||||
$logger = new StreamLogger('test', $filehandler, $this->introspection);
|
||||
$logger->emergency('working');
|
||||
$logger->alert('working');
|
||||
$logger->error('working');
|
||||
$logger->warning('working');
|
||||
$logger->notice('working');
|
||||
$logger->info('not working');
|
||||
$logger->debug('not working');
|
||||
|
||||
$text = $logfile->getContent();
|
||||
|
||||
$this->assertLoglineNums(5, $text);
|
||||
$this->assertLogline($text);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if the close statement is working
|
||||
*/
|
||||
public function testClose()
|
||||
{
|
||||
$logfile = vfsStream::newFile('friendica.log')
|
||||
->at($this->root);
|
||||
|
||||
$logger = new StreamLogger('test', $logfile->url(), $this->introspection);
|
||||
$logger->emergency('working');
|
||||
$logger->close();
|
||||
// close doesn't affect
|
||||
$logger->emergency('working too');
|
||||
|
||||
$text = $logfile->getContent();
|
||||
|
||||
$this->assertLoglineNums(2, $text);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -187,4 +150,14 @@ class StreamLoggerTest extends MockedTest
|
|||
|
||||
$logger->log('NOPE', 'a test');
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue