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

165 lines
4.5 KiB
PHP
Raw Permalink Normal View History

2019-03-03 21:25:18 +01:00
<?php
2020-02-09 15:45:36 +01:00
/**
* @copyright Copyright (C) 2010-2024, 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
2021-10-23 12:22:27 +02:00
namespace Friendica\Test\src\Core\Logger;
2019-03-03 21:25:18 +01:00
2021-10-23 12:22:27 +02:00
use Friendica\Core\Logger\Exception\LoggerArgumentException;
2021-10-29 08:03:59 +02:00
use Friendica\Core\Logger\Exception\LogLevelException;
2019-03-03 21:25:18 +01:00
use Friendica\Test\Util\VFSTrait;
2021-10-23 12:22:27 +02:00
use Friendica\Core\Logger\Type\StreamLogger;
2019-03-03 21:25:18 +01:00
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
protected function setUp(): void
2019-03-03 21:25:18 +01:00
{
parent::setUp();
$this->setUpVfsDir();
}
2019-03-04 23:39:14 +01:00
/**
* {@@inheritdoc}
*/
2023-07-17 01:06:11 +02:00
protected function getInstance($level = LogLevel::DEBUG, $logfile = 'friendica.log')
2019-03-03 21:25:18 +01:00
{
2023-07-17 01:06:11 +02:00
$this->logfile = vfsStream::newFile($logfile)
2019-03-03 21:25:18 +01:00
->at($this->root);
$this->config->shouldReceive('get')->with('system', 'logfile')->andReturn($this->logfile->url())->once();
2023-07-17 01:06:11 +02:00
$this->config->shouldReceive('get')->with('system', 'loglevel')->andReturn($level)->once();
2023-07-17 01:06:11 +02:00
$loggerFactory = new \Friendica\Core\Logger\Factory\StreamLogger($this->introspection, 'test');
return $loggerFactory->create($this->config);
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
}
/**
* Test when a file isn't set
*/
public function testNoUrl()
{
2021-10-23 12:22:27 +02:00
$this->expectException(LoggerArgumentException::class);
2023-07-17 01:06:11 +02:00
$this->expectExceptionMessage(' is not a valid logfile');
$this->config->shouldReceive('get')->with('system', 'logfile')->andReturn('')->once();
2023-07-17 01:06:11 +02:00
$loggerFactory = new \Friendica\Core\Logger\Factory\StreamLogger($this->introspection, 'test');
$logger = $loggerFactory->create($this->config);
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(LoggerArgumentException::class);
2019-03-04 08:42:08 +01:00
$logfile = vfsStream::newFile('friendica.log')
->at($this->root)->chmod(0);
$this->config->shouldReceive('get')->with('system', 'logfile')->andReturn($logfile->url())->once();
2023-07-17 01:06:11 +02:00
$loggerFactory = new \Friendica\Core\Logger\Factory\StreamLogger($this->introspection, 'test');
$logger = $loggerFactory->create($this->config);
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->expectExceptionMessageMatches("/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
2023-07-17 01:06:11 +02:00
$loggerFactory = new \Friendica\Core\Logger\Factory\StreamLogger($this->introspection, 'test');
$logger = $loggerFactory->create($this->config);
2019-03-03 21:25:18 +01:00
$logger->emergency('not working');
}
/**
* Test when the minimum level is not valid
*/
public function testWrongMinimumLevel()
{
2021-10-29 08:03:59 +02:00
$this->expectException(LogLevelException::class);
$this->expectExceptionMessageMatches("/The level \".*\" is not valid./");
2023-07-17 01:06:11 +02:00
$logger = $this->getInstance('NOPE');
2019-03-03 21:25:18 +01:00
}
/**
* Test when the minimum level is not valid
*/
public function testWrongLogLevel()
{
2021-10-29 08:03:59 +02:00
$this->expectException(LogLevelException::class);
$this->expectExceptionMessageMatches("/The level \".*\" is not valid./");
2023-07-17 01:06:11 +02:00
$logger = $this->getInstance('NOPE');
2019-03-03 21:25:18 +01:00
$logger->log('NOPE', 'a test');
}
2019-03-04 23:39:14 +01:00
/**
* Test a relative path
2021-05-16 23:39:03 +02:00
* @doesNotPerformAssertions
*/
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());
$this->config->shouldReceive('get')->with('system', 'logfile')->andReturn('../friendica.log')->once();
$logger = new StreamLogger('test', $this->config, $this->introspection, $this->fileSystem);
$logger->info('Test');
2019-03-04 23:39:14 +01:00
}
2019-03-03 21:25:18 +01:00
}