mirror of
https://github.com/Combodo/iTop.git
synced 2026-02-13 07:24:13 +01:00
peer review: added tests but not passed yet
This commit is contained in:
@@ -271,6 +271,8 @@ abstract class LogAPI
|
||||
// const LEVEL_ALERT = 'Alert';
|
||||
// const LEVEL_EMERGENCY = 'Emergency';
|
||||
|
||||
protected static $m_oMockMetaModelConfig = null;
|
||||
|
||||
protected static $aLevelsPriority = array(
|
||||
self::LEVEL_DEBUG => 100,
|
||||
self::LEVEL_OK => 150,
|
||||
@@ -285,6 +287,12 @@ abstract class LogAPI
|
||||
static::$m_oFileLog = new FileLog($sTargetFile);
|
||||
}
|
||||
|
||||
public static function MockStaticObjects($oFileLog, $oMetaModelConfig=null)
|
||||
{
|
||||
static::$m_oFileLog = $oFileLog;
|
||||
static::$m_oMockMetaModelConfig = $oMetaModelConfig;
|
||||
}
|
||||
|
||||
public static function Error($sMessage, $sChannel = null, $aContext = array())
|
||||
{
|
||||
static::Log(self::LEVEL_ERROR, $sMessage, $sChannel, $aContext);
|
||||
@@ -357,7 +365,7 @@ abstract class LogAPI
|
||||
*/
|
||||
private static function GetMinLogLevel($sChannel)
|
||||
{
|
||||
$oConfig = \MetaModel::GetConfig();
|
||||
$oConfig = (static::$m_oMockMetaModelConfig === null) ? static::$m_oMockMetaModelConfig : \MetaModel::GetConfig();
|
||||
if (!$oConfig instanceof Config)
|
||||
{
|
||||
return self::LEVEL_OK;
|
||||
|
||||
120
test/core/LogAPITest.php
Normal file
120
test/core/LogAPITest.php
Normal file
@@ -0,0 +1,120 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: Eric
|
||||
* Date: 31/08/2018
|
||||
* Time: 17:03
|
||||
*/
|
||||
|
||||
namespace Combodo\iTop\Test\UnitTest\Core;
|
||||
|
||||
|
||||
use Combodo\iTop\Test\UnitTest\ItopTestCase;
|
||||
|
||||
/**
|
||||
* @runTestsInSeparateProcesses
|
||||
* @preserveGlobalState disabled
|
||||
* @backupGlobals disabled
|
||||
*/
|
||||
class LogAPITest extends ItopTestCase
|
||||
{
|
||||
private $mockFileLog;
|
||||
private $oMetaModelConfig;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
$this->mockFileLog = $this->createMock('FileLog');
|
||||
$this->oMetaModelConfig = $this->createMock('Config');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
/**
|
||||
* @dataProvider LogApiProvider
|
||||
* @test
|
||||
* @backupGlobals disabled
|
||||
*/
|
||||
public function TestLogApi($oConfigObject, $sMessage, $Channel, $sExpectedLevel, $sExpectedMessage, $sExpectedChannel = '')
|
||||
{
|
||||
\IssueLog::MockStaticObjects($this->mockFileLog, $oConfigObject);
|
||||
|
||||
$this->mockFileLog->expects($this->exactly(1))
|
||||
->method($sExpectedLevel)
|
||||
->with($sExpectedMessage, $sExpectedChannel);
|
||||
|
||||
\IssueLog::Error($sMessage, $Channel);
|
||||
}
|
||||
|
||||
public function LogApiProvider()
|
||||
{
|
||||
return [
|
||||
[ $this->oMetaModelConfig, "log msg", '' , "Error", "log msg"],
|
||||
[ $this->oMetaModelConfig, "log msg", 'PoudlardChannel' , "Error", "log msg", 'PoudlardChannel'],
|
||||
[ array(), "log msg", '' , "Error", "log msg"], // Bruno?
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
/** TODISCUSS
|
||||
* @test
|
||||
* @backupGlobals disabled
|
||||
*/
|
||||
public function TestUnknownLevel()
|
||||
{
|
||||
$this->mockFileLog->expects($this->exactly(1))
|
||||
->method("Error")
|
||||
->with("invalid log level 'TotoLevel'");
|
||||
|
||||
\IssueLog::Log('TotoLevel', "log msg");
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* @dataProvider LogWithChannelLogLevelApiProvider
|
||||
* @test
|
||||
* @backupGlobals disabled
|
||||
*/
|
||||
public function TestLogWithChannelLogLevelApi($expectedCallNb, $sExpectedLevel, $ConfigReturnedObject, $bExceptionRaised=false)
|
||||
{
|
||||
$this->oMetaModelConfig
|
||||
->method("Get")
|
||||
->with('log_level_min')
|
||||
->willReturn($ConfigReturnedObject);
|
||||
|
||||
\IssueLog::MockStaticObjects($this->mockFileLog, $this->oMetaModelConfig);
|
||||
|
||||
$this->mockFileLog->expects($this->exactly($expectedCallNb))
|
||||
->method($sExpectedLevel)
|
||||
->with("log msg", "GaBuZoMeuChannel");
|
||||
|
||||
try{
|
||||
\IssueLog::Warning("log msg", "GaBuZoMeuChannel");
|
||||
if ($bExceptionRaised)
|
||||
{
|
||||
$this->fail("raised should have been raised");
|
||||
}
|
||||
}
|
||||
catch(Exception $e)
|
||||
{
|
||||
if (!$bExceptionRaised)
|
||||
{
|
||||
$this->fail("raised should NOT have been raised");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function LogWithChannelLogLevelApiProvider()
|
||||
{
|
||||
return [
|
||||
[ 0, "Ok", ''],
|
||||
[ 0, "Ok", 'TotoLevel'],
|
||||
[ 0, "Ok", array()],
|
||||
[ 0, "Ok", ["GaBuZoMeuChannel" => "TotoLevel"], true],
|
||||
[ 1, "Error", ["GaBuZoMeuChannel" => "Error"]],
|
||||
[ 0, "Info", ["GaBuZoMeuChannel" => "Info"]],
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user