Skip to content
Snippets Groups Projects
Commit a60e7f8d authored by Laurent's avatar Laurent
Browse files

hotline #55142 add Cata_Log as multiple targets logger

parent 2aa64ab3
Branches
Tags 7.2.4
2 merge requests!2334Master,!2133Hotline#55142 1 dtouch
Pipeline #1296 passed with stage
in 11 minutes and 6 seconds
- ticket #55142 : ajout de mécanismes d'analyse du déroulement du moissonnage des ressources numériques
\ No newline at end of file
<?php
/**
* Copyright (c) 2012-2017, Agence Française Informatique (AFI). All rights reserved.
*
* BOKEH 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.
*
* There are special exceptions to the terms and conditions of the AGPL as it
* is applied to this software (see README file).
*
* BOKEH 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 BOKEH; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
class Class_Cata_Log {
const
DEFAULT_TARGET = 'default',
ALL_TARGETS = '*';
protected $_targets = [];
public function addDefaultTarget($logger) {
return $this->addTarget(static::DEFAULT_TARGET, $logger);
}
public function addTarget($name, $logger) {
$this->_targets[$name] = $logger;
return $this;
}
public function ecrire($message, $targets = ['default']) {
return $this->_targetsDo($targets, 'ecrire', [$message]);
}
public function addInfo($message, $targets = ['default']) {
return $this->_targetsDo($targets, 'addInfo', [$message]);
}
public function addSuccess($message, $targets = ['default']) {
return $this->_targetsDo($targets, 'addSuccess', [$message]);
}
public function addError($message, $targets = ['default']) {
return $this->_targetsDo($targets, 'addError', [$message]);
}
protected function _targetsDo($targets, $method, $params) {
array_map(
function($logger) use ($method, $params)
{
call_user_func_array([$logger, $method], $params);
},
$this->_parseTargets($targets));
return $this;
}
protected function _parseTargets($targets) {
if (static::ALL_TARGETS == $targets)
return $this->_targets;
if (!is_array($targets))
$targets = [$targets];
return array_intersect_key($this->_targets,
array_combine($targets, $targets));
}
}
?>
\ No newline at end of file
<?php
/**
* Copyright (c) 2012-2017, Agence Française Informatique (AFI). All rights reserved.
*
* BOKEH 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.
*
* There are special exceptions to the terms and conditions of the AGPL as it
* is applied to this software (see README file).
*
* BOKEH 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 BOKEH; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
class Class_Cata_LogTest extends ModelTestCase {
protected $_logs;
protected function newLog($target) {
$append_log = function($message) use ($target) {
$this->_logs[$target][] = $message;
};
$mock = $this->mock();
foreach(['ecrire', 'addInfo', 'addSuccess', 'addError'] as $method)
$mock->whenCalled($method)->willDo($append_log);
return $mock;
}
public function setUp() {
parent::setUp();
$this->_logs = ['default' => [],
'error' => [],
'warning' => []];
(new Class_Cata_Log())
->addDefaultTarget($this->newLog('default'))
->addTarget('error', $this->newLog('error'))
->addTarget('warning', $this->newLog('warning'))
->ecrire('Cooking chocolate fondant')
->ecrire('Trying 2.0 version', ['warning', 'error'])
->ecrire('1.0 was buggy', 'error')
->ecrire('Starting', '*')
->addInfo('Gather ingredients', '*')
->addSuccess('250g Sugar')
->addSuccess('250g Butter')
->addError('Not enough Butter', 'default')
->addError('Go buy butter', '*');
}
public function expectedLogs() {
return
[
['default', ['Cooking chocolate fondant',
'Starting',
'Gather ingredients',
'250g Sugar',
'250g Butter',
'Not enough Butter',
'Go buy butter']],
['error', ['Trying 2.0 version',
'1.0 was buggy',
'Starting',
'Gather ingredients',
'Go buy butter']],
['warning', ['Trying 2.0 version',
'Starting',
'Gather ingredients',
'Go buy butter']]
];
}
/**
* @dataProvider expectedLogs
* @test */
public function targetShouldContainLines($target, $lines) {
$this->assertEquals($this->_logs[$target], $lines);
}
}
?>
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment