friendica/tests/src/Util/Logger/StreamLoggerTest.php

191 lines
4.4 KiB
PHP
Raw Normal View History

2019-03-03 21:25:18 +01:00
<?php
namespace Friendica\Test\src\Util\Logger;
use Friendica\Util\FileSystem;
2019-03-03 21:25:18 +01:00
use Friendica\Test\Util\VFSTrait;
use Friendica\Util\Logger\StreamLogger;
use org\bovigo\vfs\vfsStream;
2019-03-04 23:39:14 +01:00
use org\bovigo\vfs\vfsStreamFile;
2019-03-03 21:25:18 +01:00
use Psr\Log\LogLevel;
2019-03-04 23:39:14 +01:00
class StreamLoggerTest extends AbstractLoggerTest
2019-03-03 21:25:18 +01:00
{
use VFSTrait;
/**
2019-03-04 23:39:14 +01:00
* @var StreamLogger
*/
private $logger;
/**
* @var vfsStreamFile
2019-03-03 21:25:18 +01:00
*/
2019-03-04 23:39:14 +01:00
private $logfile;
2019-03-03 21:25:18 +01:00
/**
* @var Filesystem
*/
private $fileSystem;
2019-03-03 21:25:18 +01:00
protected function setUp()
{
parent::setUp();
$this->setUpVfsDir();
$this->fileSystem = new Filesystem();
2019-03-03 21:25:18 +01:00
}
2019-03-04 23:39:14 +01:00
/**
* {@@inheritdoc}
*/
protected function getInstance($level = LogLevel::DEBUG)
2019-03-03 21:25:18 +01:00
{
2019-03-04 23:39:14 +01:00
$this->logfile = vfsStream::newFile('friendica.log')
2019-03-03 21:25:18 +01:00
->at($this->root);
$this->logger = new StreamLogger('test', $this->logfile->url(), $this->introspection, $this->fileSystem, $level);
2019-03-03 21:25:18 +01:00
2019-03-04 23:39:14 +01:00
return $this->logger;
2019-03-03 21:25:18 +01:00
}
/**
2019-03-04 23:39:14 +01:00
* {@inheritdoc}
2019-03-03 21:25:18 +01:00
*/
2019-03-04 23:39:14 +01:00
protected function getContent()
2019-03-03 21:25:18 +01:00
{
2019-03-04 23:39:14 +01:00
return $this->logfile->getContent();
2019-03-03 21:25:18 +01:00
}
/**
2019-03-04 23:39:14 +01:00
* Test if a stream is working
2019-03-03 21:25:18 +01:00
*/
2019-03-04 23:39:14 +01:00
public function testStream()
2019-03-03 21:25:18 +01:00
{
$logfile = vfsStream::newFile('friendica.log')
->at($this->root);
2019-03-04 23:39:14 +01:00
$filehandler = fopen($logfile->url(), 'ab');
2019-03-03 21:25:18 +01:00
$logger = new StreamLogger('test', $filehandler, $this->introspection, $this->fileSystem);
2019-03-04 23:39:14 +01:00
$logger->emergency('working');
2019-03-03 21:25:18 +01:00
$text = $logfile->getContent();
2019-03-04 23:39:14 +01:00
$this->assertLogline($text);
2019-03-03 21:25:18 +01:00
}
/**
2019-03-04 23:39:14 +01:00
* Test if the close statement is working
2019-03-03 21:25:18 +01:00
*/
2019-03-04 23:39:14 +01:00
public function testClose()
2019-03-03 21:25:18 +01:00
{
$logfile = vfsStream::newFile('friendica.log')
->at($this->root);
$logger = new StreamLogger('test', $logfile->url(), $this->introspection, $this->fileSystem);
2019-03-03 21:25:18 +01:00
$logger->emergency('working');
2019-03-04 23:39:14 +01:00
$logger->close();
// close doesn't affect
$logger->emergency('working too');
2019-03-03 21:25:18 +01:00
$text = $logfile->getContent();
2019-03-04 23:39:14 +01:00
$this->assertLoglineNums(2, $text);
2019-03-03 21:25:18 +01:00
}
/**
* Test when a file isn't set
* @expectedException \LogicException
* @expectedExceptionMessage Missing stream URL.
*/
public function testNoUrl()
{
$logger = new StreamLogger('test', '', $this->introspection, $this->fileSystem);
2019-03-03 21:25:18 +01:00
$logger->emergency('not working');
}
/**
2019-03-04 08:42:08 +01:00
* Test when a file cannot be opened
2019-03-03 21:25:18 +01:00
* @expectedException \UnexpectedValueException
* @expectedExceptionMessageRegExp /The stream or file .* could not be opened: .* /
*/
public function testWrongUrl()
{
2019-03-04 08:42:08 +01:00
$logfile = vfsStream::newFile('friendica.log')
->at($this->root)->chmod(0);
$logger = new StreamLogger('test', $logfile->url(), $this->introspection, $this->fileSystem);
2019-03-03 21:25:18 +01: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 15:36:16 +02:00
$this->markTestIncomplete('We need a platform independent way to set directory to readonly');
$logger = new StreamLogger('test', '/$%/wrong/directory/file.txt', $this->introspection, $this->fileSystem);
2019-03-03 21:25:18 +01: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, $this->fileSystem, 'NOPE');
2019-03-03 21:25:18 +01:00
}
/**
* 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, $this->fileSystem);
2019-03-03 21:25:18 +01:00
$logger->log('NOPE', 'a test');
}
2019-03-04 23:39:14 +01: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, $this->fileSystem);
}
/**
* Test a relative path
*/
public function testRealPath()
{
$this->markTestSkipped('vfsStream isn\'t compatible with chdir, so not testable.');
$logfile = vfsStream::newFile('friendica.log')
->at($this->root);
chdir($this->root->getChild('logs')->url());
$logger = new StreamLogger('test', '../friendica.log' , $this->introspection, $this->fileSystem);
$logger->info('Test');
2019-03-04 23:39:14 +01:00
}
2019-03-03 21:25:18 +01:00
}