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;
|
|
|
|
|
2019-10-22 22:47:37 +02:00
|
|
|
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
|
|
|
|
2019-10-22 22:47:37 +02:00
|
|
|
/**
|
|
|
|
* @var Filesystem
|
|
|
|
*/
|
|
|
|
private $fileSystem;
|
|
|
|
|
2021-04-01 23:04:30 +02:00
|
|
|
protected function setUp(): void
|
2019-03-03 21:25:18 +01:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->setUpVfsDir();
|
2019-10-22 22:47:37 +02:00
|
|
|
|
2020-10-18 20:31:57 +02:00
|
|
|
$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);
|
|
|
|
|
2020-10-18 20:31:57 +02:00
|
|
|
$logger = new StreamLogger('test', $this->logfile->url(), $this->introspection, $this->fileSystem, $level);
|
2019-03-03 21:25:18 +01:00
|
|
|
|
2020-10-18 20:31:57 +02: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
|
|
|
|
2019-10-22 22:47:37 +02: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
|
|
|
|
2020-10-17 14:19:57 +02: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);
|
|
|
|
|
2019-10-22 22:47:37 +02:00
|
|
|
$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();
|
|
|
|
|
2020-10-17 14:19:57 +02:00
|
|
|
self::assertLoglineNums(2, $text);
|
2019-03-03 21:25:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test when a file isn't set
|
|
|
|
*/
|
|
|
|
public function testNoUrl()
|
|
|
|
{
|
2020-10-18 20:31:57 +02:00
|
|
|
$this->expectException(\LogicException::class);
|
|
|
|
$this->expectExceptionMessage("Missing stream URL.");
|
|
|
|
|
2019-10-22 22:47:37 +02:00
|
|
|
$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()
|
|
|
|
{
|
2020-10-18 20:31:57 +02:00
|
|
|
$this->expectException(\UnexpectedValueException::class);
|
2021-05-23 23:09:49 +02:00
|
|
|
$this->expectExceptionMessageMatches("/The stream or file .* could not be opened: .* /");
|
2020-10-18 20:31:57 +02:00
|
|
|
|
2019-03-04 08:42:08 +01:00
|
|
|
$logfile = vfsStream::newFile('friendica.log')
|
|
|
|
->at($this->root)->chmod(0);
|
|
|
|
|
2019-10-22 22:47:37 +02:00
|
|
|
$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()
|
|
|
|
{
|
2020-10-18 20:31:57 +02:00
|
|
|
$this->expectException(\UnexpectedValueException::class);
|
2021-05-23 23:09:49 +02:00
|
|
|
$this->expectExceptionMessageMatches("/Directory .* cannot get created: .* /");
|
2020-10-18 20:31:57 +02:00
|
|
|
|
|
|
|
static::markTestIncomplete('We need a platform independent way to set directory to readonly');
|
2019-09-23 15:36:16 +02:00
|
|
|
|
2019-10-22 22:47:37 +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()
|
|
|
|
{
|
2020-10-18 20:31:57 +02:00
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
2021-05-23 23:09:49 +02:00
|
|
|
$this->expectExceptionMessageMatches("/The level \".*\" is not valid./");
|
2020-10-18 20:31:57 +02:00
|
|
|
|
2019-10-22 22:47:37 +02:00
|
|
|
$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()
|
|
|
|
{
|
2020-10-18 20:31:57 +02:00
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
2021-05-23 23:09:49 +02:00
|
|
|
$this->expectExceptionMessageMatches("/The level \".*\" is not valid./");
|
2020-10-18 20:31:57 +02:00
|
|
|
|
2019-03-03 21:25:18 +01:00
|
|
|
$logfile = vfsStream::newFile('friendica.log')
|
|
|
|
->at($this->root);
|
|
|
|
|
2019-10-22 22:47:37 +02:00
|
|
|
$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()
|
|
|
|
{
|
2020-10-18 20:31:57 +02:00
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
|
|
$this->expectExceptionMessage("A stream must either be a resource or a string.");
|
|
|
|
|
2019-10-22 22:47:37 +02:00
|
|
|
$logger = new StreamLogger('test', null, $this->introspection, $this->fileSystem);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test a relative path
|
2021-05-16 23:39:03 +02:00
|
|
|
* @doesNotPerformAssertions
|
2019-10-22 22:47:37 +02:00
|
|
|
*/
|
|
|
|
public function testRealPath()
|
|
|
|
{
|
2020-10-18 20:31:57 +02:00
|
|
|
static::markTestSkipped('vfsStream isn\'t compatible with chdir, so not testable.');
|
2019-10-22 22:47:37 +02:00
|
|
|
|
|
|
|
$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
|
|
|
}
|