diff --git a/core/log.class.inc.php b/core/log.class.inc.php index cf7da8cdf..9fc2040ee 100644 --- a/core/log.class.inc.php +++ b/core/log.class.inc.php @@ -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; diff --git a/test/core/LogAPITest.php b/test/core/LogAPITest.php new file mode 100644 index 000000000..8e477c7b0 --- /dev/null +++ b/test/core/LogAPITest.php @@ -0,0 +1,120 @@ +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"]], + ]; + } + +}