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

211 lines
5.2 KiB
PHP
Raw Normal View History

2019-03-03 21:25:18 +01:00
<?php
2020-02-09 15:45:36 +01:00
/**
2021-03-29 08:40:20 +02:00
* @copyright Copyright (C) 2010-2021, the Friendica project
2020-02-09 15:45:36 +01:00
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
2019-03-03 21:25:18 +01:00
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 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;
protected function setUp(): void
2019-03-03 21:25:18 +01:00
{
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);
$logger = new StreamLogger('test', $this->logfile->url(), $this->introspection, $this->fileSystem, $level);
2019-03-03 21:25:18 +01:00
return $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
self::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();
self::assertLoglineNums(2, $text);
2019-03-03 21:25:18 +01:00
}
/**
* Test when a file isn't set
*/
public function testNoUrl()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage("Missing stream URL.");
$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
*/
public function testWrongUrl()
{
$this->expectException(\UnexpectedValueException::class);
$this->expectExceptionMessageRegExp("/The stream or file .* could not be opened: .* /");
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
*/
public function testWrongDir()
{
$this->expectException(\UnexpectedValueException::class);
$this->expectExceptionMessageRegExp("/Directory .* cannot get created: .* /");
static::markTestIncomplete('We need a platform independent way to set directory to readonly');
2019-09-23 15:36:16 +02:00
$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
*/
public function testWrongMinimumLevel()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessageRegExp("/The level \".*\" is not valid./");
$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
*/
public function testWrongLogLevel()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessageRegExp("/The level \".*\" is not valid./");
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->log('NOPE', 'a test');
}
2019-03-04 23:39:14 +01:00
/**
* Test when the file is null
*/
public function testWrongFile()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage("A stream must either be a resource or a string.");
$logger = new StreamLogger('test', null, $this->introspection, $this->fileSystem);
}
/**
* Test a relative path
*/
public function testRealPath()
{
static::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
}