You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
171 lines
5.8 KiB
PHP
171 lines
5.8 KiB
PHP
<?php
|
|
namespace Bsr\Utils\Logger;
|
|
|
|
use Bsr\Utils\Configuration\Configuration;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class LoggerTest extends TestCase{
|
|
|
|
private $logData = array();
|
|
private $dummyConfigFileQuiet;
|
|
private $dummyConfigFileNormal;
|
|
private $dummyConfigFileVerbose;
|
|
private $arbitraryMessage = '!some Ar.bitrary message 123alwer';
|
|
|
|
public function setUp()
|
|
{
|
|
$this->logData = array(
|
|
'ip' => '(n-a)',
|
|
'date' => date('d.m.Y H:i:s'),
|
|
'func' => '(none)',
|
|
'version' => '(none)',
|
|
'error' => ''
|
|
);
|
|
|
|
$this->dummyConfigFileQuiet = realpath(dirname(__FILE__) . '/../../../config/configuration.logger.quiet.php');
|
|
$this->dummyConfigFileNormal = realpath(dirname(__FILE__) . '/../../../config/configuration.logger.normal.php');
|
|
$this->dummyConfigFileVerbose = realpath(dirname(__FILE__) . '/../../../config/configuration.logger.verbose.php');
|
|
|
|
Configuration::setConfigFilePath($this->dummyConfigFileNormal, true);
|
|
|
|
Logger::start();
|
|
}
|
|
|
|
public function testClearEmptiesData()
|
|
{
|
|
Logger::start();
|
|
Logger::clear();
|
|
$this->assertEquals(empty(Logger::getData()), true);
|
|
}
|
|
|
|
public function testStartEmptyArrayInitializesDataDefaultKeys()
|
|
{
|
|
Logger::clear();
|
|
Logger::start(array());
|
|
$this->assertSame(array_diff_key(Logger::getData(), $this->logData), array());
|
|
}
|
|
|
|
public function testStartWithSomeArrayInitializesDataDefaultKeysPlusPassedOnes()
|
|
{
|
|
$paramArray = array(
|
|
'someOtherInfo' => array(1,2,3),
|
|
1 => 'haha'
|
|
);
|
|
Logger::clear();
|
|
Logger::start($paramArray);
|
|
$expectedDataArray = $paramArray + $this->logData;
|
|
$this->assertSame(array_diff_key(Logger::getData(),$expectedDataArray), array());
|
|
}
|
|
|
|
public function testStartWithParamArrayOverwritesDataKeyValueIfExists()
|
|
{
|
|
$overwriteDefaultValue = array('overwriteDefault', 'z', 3);
|
|
$paramArray = array('ip' => $overwriteDefaultValue);
|
|
Logger::clear();
|
|
Logger::start($paramArray);
|
|
$this->assertSame(Logger::getData()['ip'], $overwriteDefaultValue);
|
|
}
|
|
|
|
public function testInfoWithKeyParamOverwritesDataKeyValue()
|
|
{
|
|
$overwriteDefaultValue = array('overwriteDefault', 'z', 3);
|
|
Logger::clear();
|
|
Logger::start(array());
|
|
Logger::info($overwriteDefaultValue, 'ip');
|
|
$this->assertSame(Logger::getData()['ip'], $overwriteDefaultValue);
|
|
}
|
|
|
|
public function testInfoWithoutKeyParamAndIntKeysGetRenumberedAndDontOverwrite()
|
|
{
|
|
$paramArray = array(0 => 'c', 1 => 'd');
|
|
Logger::clear();
|
|
Logger::start(array(0 => 'a', 1 => 'b'));
|
|
Logger::info($paramArray);
|
|
$this->assertSame(
|
|
array_intersect(Logger::getData(), array('a', 'b', 'c', 'd')),
|
|
array('a', 'b', 'c', 'd')
|
|
);
|
|
}
|
|
|
|
public function testLogDoesNotLogMessagesAccordingToVerbosityParamWhenSmallerThanConfig()
|
|
{
|
|
Configuration::setConfigFilePath($this->dummyConfigFileNormal, true);
|
|
Logger::start();
|
|
Logger::log($this->arbitraryMessage, Logger::VERBOSE);
|
|
Logger::stop();
|
|
$this->assertSame(
|
|
strpos(Logger::getLastLogs(), $this->arbitraryMessage),
|
|
false
|
|
);
|
|
}
|
|
|
|
public function testLogLogsMessagesWhenVerbosityParamIsGreaterThanConfigVerbosity()
|
|
{
|
|
Configuration::setConfigFilePath($this->dummyConfigFileVerbose, true);
|
|
Logger::start();
|
|
Logger::log($this->arbitraryMessage, Logger::NORMAL);
|
|
Logger::stop();
|
|
$this->assertNotSame(
|
|
strpos(Logger::getLastLogs(), $this->arbitraryMessage),
|
|
false
|
|
);
|
|
}
|
|
|
|
public function testStopSavesComputesElapsedTime()
|
|
{
|
|
Logger::clear();
|
|
Logger::start();
|
|
Logger::stop();
|
|
$this->assertEquals(array_key_exists('time', Logger::getData()), true);
|
|
}
|
|
|
|
public function testStopDoesNotLogAnythingWhenConfigVerbosityIsQuiet()
|
|
{
|
|
Configuration::setConfigFilePath($this->dummyConfigFileQuiet, true);
|
|
$this->assertEquals(Configuration::get('log.verbosity'), Logger::QUIET);
|
|
$this->assertEquals(Logger::stop(), Logger::NO_FILE_LOGGED);
|
|
}
|
|
|
|
public function testStopReturnsSavedLogFileWhenConfigVerbosityIsNotQuiet()
|
|
{
|
|
Configuration::setConfigFilePath($this->dummyConfigFileNormal, true);
|
|
$this->assertNotEquals(Configuration::get('log.verbosity'), Logger::QUIET);
|
|
Logger::start();
|
|
$stopReturn = Logger::stop();
|
|
$this->assertEquals(realpath($stopReturn), realpath(Configuration::get('log.file')));
|
|
}
|
|
|
|
private function removeLogs()
|
|
{
|
|
$logsDir = realpath(dirname(Configuration::get('log.file')));
|
|
$files = glob("$logsDir/*");
|
|
foreach($files as $file){
|
|
if(is_file($file)) unlink($file);
|
|
}
|
|
}
|
|
|
|
public function testStopSavesLogFileWhenConfigVerbosityIsNotQuiet()
|
|
{
|
|
$this->removeLogs();
|
|
Configuration::setConfigFilePath($this->dummyConfigFileNormal, true);
|
|
$this->assertNotEquals(Configuration::get('log.verbosity'), Logger::QUIET);
|
|
Logger::start();
|
|
Logger::stop();
|
|
$this->assertNotEquals(Logger::NO_LOG_YET_MSG, Logger::getLastLogs());
|
|
}
|
|
|
|
public function testGetDataReturnsArray()
|
|
{
|
|
$this->assertEquals(is_array(Logger::getData()), true);
|
|
}
|
|
|
|
public function testGetLastLogsReturnsNoLogsWhenCallingItBeforeStopOnAnEmptyDir()
|
|
{
|
|
$this->removeLogs();
|
|
Configuration::setConfigFilePath($this->dummyConfigFileNormal, true);
|
|
Logger::start();
|
|
Logger::log('Some message', Logger::NORMAL);
|
|
$this->assertEquals(Logger::NO_LOG_YET_MSG, Logger::getLastLogs());
|
|
}
|
|
}
|