unit test for Tlog. Cover all output method (trace to fatal)
This commit is contained in:
45
core/lib/Thelia/Log/Destination/TlogDestinationNull.php
Normal file
45
core/lib/Thelia/Log/Destination/TlogDestinationNull.php
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
<?php
|
||||||
|
/*************************************************************************************/
|
||||||
|
/* */
|
||||||
|
/* Thelia */
|
||||||
|
/* */
|
||||||
|
/* Copyright (c) OpenStudio */
|
||||||
|
/* email : info@thelia.net */
|
||||||
|
/* web : http://www.thelia.net */
|
||||||
|
/* */
|
||||||
|
/* This program is free software; you can redistribute it and/or modify */
|
||||||
|
/* it under the terms of the GNU General Public License as published by */
|
||||||
|
/* the Free Software Foundation; either version 3 of the License */
|
||||||
|
/* */
|
||||||
|
/* 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 General Public License for more details. */
|
||||||
|
/* */
|
||||||
|
/* You should have received a copy of the GNU General Public License */
|
||||||
|
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||||
|
/* */
|
||||||
|
/*************************************************************************************/
|
||||||
|
|
||||||
|
namespace Thelia\Log\Destination;
|
||||||
|
|
||||||
|
use Thelia\Log\AbstractTlogDestination;
|
||||||
|
|
||||||
|
class TlogDestinationNull extends AbstractTlogDestination {
|
||||||
|
|
||||||
|
public function get_titre() {
|
||||||
|
return "Trou noir";
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get_description() {
|
||||||
|
return "Cette destination ne provoque aucune sortie";
|
||||||
|
}
|
||||||
|
|
||||||
|
public function ajouter($string) {
|
||||||
|
// Rien
|
||||||
|
}
|
||||||
|
|
||||||
|
public function ecrire(&$res) {
|
||||||
|
// Rien
|
||||||
|
}
|
||||||
|
}
|
||||||
50
core/lib/Thelia/Log/Destination/TlogDestinationText.php
Normal file
50
core/lib/Thelia/Log/Destination/TlogDestinationText.php
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
<?php
|
||||||
|
/*************************************************************************************/
|
||||||
|
/* */
|
||||||
|
/* Thelia */
|
||||||
|
/* */
|
||||||
|
/* Copyright (c) OpenStudio */
|
||||||
|
/* email : info@thelia.net */
|
||||||
|
/* web : http://www.thelia.net */
|
||||||
|
/* */
|
||||||
|
/* This program is free software; you can redistribute it and/or modify */
|
||||||
|
/* it under the terms of the GNU General Public License as published by */
|
||||||
|
/* the Free Software Foundation; either version 3 of the License */
|
||||||
|
/* */
|
||||||
|
/* 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 General Public License for more details. */
|
||||||
|
/* */
|
||||||
|
/* You should have received a copy of the GNU General Public License */
|
||||||
|
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||||
|
/* */
|
||||||
|
/*************************************************************************************/
|
||||||
|
|
||||||
|
namespace Thelia\Log\Destination;
|
||||||
|
|
||||||
|
use Thelia\Log\AbstractTlogDestination;
|
||||||
|
|
||||||
|
|
||||||
|
class TlogDestinationText extends AbstractTlogDestination {
|
||||||
|
|
||||||
|
public function __construct() {
|
||||||
|
parent::__construct();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get_titre() {
|
||||||
|
return "Affichage direct dans la page, en texte brut";
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get_description() {
|
||||||
|
return "Permet d'afficher les logs directement dans la page resultat, au format texte brut.";
|
||||||
|
}
|
||||||
|
|
||||||
|
public function ajouter($texte) {
|
||||||
|
echo $texte."\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
public function ecrire(&$res) {
|
||||||
|
// Rien
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -54,7 +54,7 @@ class Tlog Implements TlogInterface
|
|||||||
|
|
||||||
// default values
|
// default values
|
||||||
const DEFAULT_LEVEL = self::DEBUG;
|
const DEFAULT_LEVEL = self::DEBUG;
|
||||||
const DEFAUT_DESTINATIONS = "Thelia\Log\Destination\TlogDestinationFile";
|
const DEFAUT_DESTINATIONS = "Thelia\Log\Destination\TlogDestinationNull";
|
||||||
const DEFAUT_PREFIXE = "#NUM: #NIVEAU [#FICHIER:#FONCTION()] {#LIGNE} #DATE #HEURE: ";
|
const DEFAUT_PREFIXE = "#NUM: #NIVEAU [#FICHIER:#FONCTION()] {#LIGNE} #DATE #HEURE: ";
|
||||||
const DEFAUT_FILES = "*";
|
const DEFAUT_FILES = "*";
|
||||||
const DEFAUT_IP = "";
|
const DEFAUT_IP = "";
|
||||||
@@ -97,7 +97,7 @@ class Tlog Implements TlogInterface
|
|||||||
// if (self::$instance == false) {
|
// if (self::$instance == false) {
|
||||||
// self::$instance = new Tlog();
|
// self::$instance = new Tlog();
|
||||||
//
|
//
|
||||||
// // On doit placer les initialisations à ce niveau pour pouvoir
|
// // On doit placer les initialisations à ce level pour pouvoir
|
||||||
// // utiliser la classe Tlog dans les classes de base (Cnx, BaseObj, etc.)
|
// // utiliser la classe Tlog dans les classes de base (Cnx, BaseObj, etc.)
|
||||||
// // Les placer dans le constructeur provoquerait une boucle
|
// // Les placer dans le constructeur provoquerait une boucle
|
||||||
// self::$instance->init();
|
// self::$instance->init();
|
||||||
@@ -111,7 +111,7 @@ class Tlog Implements TlogInterface
|
|||||||
|
|
||||||
$level = $config->read(self::VAR_LEVEL, self::DEFAULT_LEVEL);
|
$level = $config->read(self::VAR_LEVEL, self::DEFAULT_LEVEL);
|
||||||
|
|
||||||
$this->set_niveau($level);
|
$this->set_level($level);
|
||||||
|
|
||||||
$this->dir_destinations = array(
|
$this->dir_destinations = array(
|
||||||
__DIR__.'/Destination'
|
__DIR__.'/Destination'
|
||||||
@@ -141,8 +141,8 @@ class Tlog Implements TlogInterface
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function set_niveau($level) {
|
public function set_level($level) {
|
||||||
$this->niveau = $level;
|
$this->level = $level;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function set_prefixe($prefixe) {
|
public function set_prefixe($prefixe) {
|
||||||
@@ -157,7 +157,7 @@ class Tlog Implements TlogInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function set_ip($ips) {
|
public function set_ip($ips) {
|
||||||
if (! empty($ips) && ! in_array($_SERVER['REMOTE_ADDR'], explode(";", $ips))) $this->niveau = self::MUET;
|
if (! empty($ips) && ! in_array($_SERVER['REMOTE_ADDR'], explode(";", $ips))) $this->level = self::MUET;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function set_show_redirect($bool) {
|
public function set_show_redirect($bool) {
|
||||||
@@ -186,7 +186,7 @@ class Tlog Implements TlogInterface
|
|||||||
// ---------------------------
|
// ---------------------------
|
||||||
|
|
||||||
public function trace() {
|
public function trace() {
|
||||||
if ($this->niveau > self::TRACE)
|
if ($this->level > self::TRACE)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
$args = func_get_args();
|
$args = func_get_args();
|
||||||
@@ -195,7 +195,7 @@ class Tlog Implements TlogInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function debug() {
|
public function debug() {
|
||||||
if ($this->niveau > self::DEBUG)
|
if ($this->level > self::DEBUG)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
$args = func_get_args();
|
$args = func_get_args();
|
||||||
@@ -204,7 +204,7 @@ class Tlog Implements TlogInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function info() {
|
public function info() {
|
||||||
if ($this->niveau > self::INFO)
|
if ($this->level > self::INFO)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
$args = func_get_args();
|
$args = func_get_args();
|
||||||
@@ -213,7 +213,7 @@ class Tlog Implements TlogInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function warning() {
|
public function warning() {
|
||||||
if (Tlog::instance()->niveau > self::WARNING)
|
if ($this->level > self::WARNING)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
$args = func_get_args();
|
$args = func_get_args();
|
||||||
@@ -222,7 +222,7 @@ class Tlog Implements TlogInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function error() {
|
public function error() {
|
||||||
if ($this->niveau > self::ERROR)
|
if ($this->level > self::ERROR)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
$args = func_get_args();
|
$args = func_get_args();
|
||||||
@@ -231,7 +231,7 @@ class Tlog Implements TlogInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function fatal() {
|
public function fatal() {
|
||||||
if ($this->niveau > self::FATAL)
|
if ($this->level > self::FATAL)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
$args = func_get_args();
|
$args = func_get_args();
|
||||||
@@ -253,7 +253,7 @@ class Tlog Implements TlogInterface
|
|||||||
self::$ecrire_effectue = true;
|
self::$ecrire_effectue = true;
|
||||||
|
|
||||||
// Muet ? On ne fait rien
|
// Muet ? On ne fait rien
|
||||||
if ($this->niveau == self::MUET) return;
|
if ($this->level == self::MUET) return;
|
||||||
|
|
||||||
foreach($this->destinations as $dest) {
|
foreach($this->destinations as $dest) {
|
||||||
$dest->ecrire($res);
|
$dest->ecrire($res);
|
||||||
@@ -274,7 +274,7 @@ class Tlog Implements TlogInterface
|
|||||||
|
|
||||||
public function afficher_redirections($url) {
|
public function afficher_redirections($url) {
|
||||||
|
|
||||||
if ($this->niveau != self::MUET && $this->show_redirect) {
|
if ($this->level != self::MUET && $this->show_redirect) {
|
||||||
echo "
|
echo "
|
||||||
<html>
|
<html>
|
||||||
<head><title>Redirection...</title></head>
|
<head><title>Redirection...</title></head>
|
||||||
@@ -292,10 +292,10 @@ class Tlog Implements TlogInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Permet de déterminer si la trace est active, en prenant en compte
|
// Permet de déterminer si la trace est active, en prenant en compte
|
||||||
// le niveau et le filtrage par fichier
|
// le level et le filtrage par fichier
|
||||||
public function active($level) {
|
public function active($level) {
|
||||||
|
|
||||||
if ($this->niveau <= $level) {
|
if ($this->level <= $level) {
|
||||||
|
|
||||||
$origine = $this->trouver_origine();
|
$origine = $this->trouver_origine();
|
||||||
|
|
||||||
|
|||||||
187
core/lib/Thelia/Tests/Log/TlogTest.php
Normal file
187
core/lib/Thelia/Tests/Log/TlogTest.php
Normal file
@@ -0,0 +1,187 @@
|
|||||||
|
<?php
|
||||||
|
/*************************************************************************************/
|
||||||
|
/* */
|
||||||
|
/* Thelia */
|
||||||
|
/* */
|
||||||
|
/* Copyright (c) OpenStudio */
|
||||||
|
/* email : info@thelia.net */
|
||||||
|
/* web : http://www.thelia.net */
|
||||||
|
/* */
|
||||||
|
/* This program is free software; you can redistribute it and/or modify */
|
||||||
|
/* it under the terms of the GNU General Public License as published by */
|
||||||
|
/* the Free Software Foundation; either version 3 of the License */
|
||||||
|
/* */
|
||||||
|
/* 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 General Public License for more details. */
|
||||||
|
/* */
|
||||||
|
/* You should have received a copy of the GNU General Public License */
|
||||||
|
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||||
|
/* */
|
||||||
|
/*************************************************************************************/
|
||||||
|
|
||||||
|
|
||||||
|
namespace Thelia\Tests\Log;
|
||||||
|
|
||||||
|
use Thelia\Log\Tlog;
|
||||||
|
use Thelia\Core\Thelia;
|
||||||
|
|
||||||
|
class TlogTest extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
protected $logger;
|
||||||
|
|
||||||
|
protected $regex = "/(\\d)(.)(\\s+)((?:[a-z][a-z]+))(\\s+)(\\[.*?\\])(\\s+)(\\{.*?\\})(\\s+)((?:2|1)\\d{3}(?:-|\\/)(?:(?:0[1-9])|(?:1[0-2]))(?:-|\\/)(?:(?:0[1-9])|(?:[1-2][0-9])|(?:3[0-1]))(?:T|\\s)(?:(?:[0-1][0-9])|(?:2[0-3])):(?:[0-5][0-9]):(?:[0-5][0-9]))(.)(\\s+)(%s)/is";
|
||||||
|
|
||||||
|
public function setUp() {
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
$_SERVER['REMOTE_ADDR'] = '::1';
|
||||||
|
|
||||||
|
$containerMock = $this->getMock("Symfony\Component\DependencyInjection\Container", array("get", "getParameter"));
|
||||||
|
|
||||||
|
$configModel = new ConfigModel();
|
||||||
|
|
||||||
|
$containerMock->expects($this->any())
|
||||||
|
->method("get")
|
||||||
|
->will($this->returnValue($configModel));
|
||||||
|
$containerMock->expects($this->any())
|
||||||
|
->method("getParameter")
|
||||||
|
->will($this->returnValue("Thelia\Log\Tlog"));
|
||||||
|
|
||||||
|
|
||||||
|
$this->logger = new Tlog($containerMock);
|
||||||
|
|
||||||
|
$this->logger->set_destinations("Thelia\Log\Destination\TlogDestinationText");
|
||||||
|
$this->logger->set_level(Tlog::TRACE);
|
||||||
|
$this->logger->set_files("*");
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testTraceWithTraceLevel()
|
||||||
|
{
|
||||||
|
|
||||||
|
$logger = $this->logger;
|
||||||
|
$logger->set_level(Tlog::TRACE);
|
||||||
|
|
||||||
|
//"#NUM: #NIVEAU [#FICHIER:#FONCTION()] {#LIGNE} #DATE #HEURE: "
|
||||||
|
$this->expectOutputRegex(sprintf($this->regex, "foo"));
|
||||||
|
$logger->trace("foo");
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testTraceWitoutTraceLevel()
|
||||||
|
{
|
||||||
|
$logger = $this->logger;
|
||||||
|
$logger->set_level(Tlog::MUET);
|
||||||
|
|
||||||
|
$this->expectOutputRegex("/^$/");
|
||||||
|
$logger->trace("foo");
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testDebugWithDebugLevel()
|
||||||
|
{
|
||||||
|
|
||||||
|
$logger = $this->logger;
|
||||||
|
$logger->set_level(Tlog::DEBUG);
|
||||||
|
|
||||||
|
//"#NUM: #NIVEAU [#FICHIER:#FONCTION()] {#LIGNE} #DATE #HEURE: "
|
||||||
|
$this->expectOutputRegex(sprintf($this->regex, "foo"));
|
||||||
|
$logger->debug("foo");
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testDebugWitoutDebugLevel()
|
||||||
|
{
|
||||||
|
$logger = $this->logger;
|
||||||
|
$logger->set_level(Tlog::MUET);
|
||||||
|
|
||||||
|
$this->expectOutputRegex("/^$/");
|
||||||
|
$logger->debug("foo");
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testInfoWithInfoLevel()
|
||||||
|
{
|
||||||
|
|
||||||
|
$logger = $this->logger;
|
||||||
|
$logger->set_level(Tlog::INFO);
|
||||||
|
|
||||||
|
//"#NUM: #NIVEAU [#FICHIER:#FONCTION()] {#LIGNE} #DATE #HEURE: "
|
||||||
|
$this->expectOutputRegex(sprintf($this->regex, "foo"));
|
||||||
|
$logger->info("foo");
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testInfoWitoutInfoLevel()
|
||||||
|
{
|
||||||
|
$logger = $this->logger;
|
||||||
|
$logger->set_level(Tlog::MUET);
|
||||||
|
|
||||||
|
$this->expectOutputRegex("/^$/");
|
||||||
|
$logger->info("foo");
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testWarningWithWarningLevel()
|
||||||
|
{
|
||||||
|
|
||||||
|
$logger = $this->logger;
|
||||||
|
$logger->set_level(Tlog::WARNING);
|
||||||
|
|
||||||
|
//"#NUM: #NIVEAU [#FICHIER:#FONCTION()] {#LIGNE} #DATE #HEURE: "
|
||||||
|
$this->expectOutputRegex(sprintf($this->regex, "foo"));
|
||||||
|
$logger->warning("foo");
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testWarningWitoutWarningLevel()
|
||||||
|
{
|
||||||
|
$logger = $this->logger;
|
||||||
|
$logger->set_level(Tlog::MUET);
|
||||||
|
|
||||||
|
$this->expectOutputRegex("/^$/");
|
||||||
|
$logger->warning("foo");
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testErrorWithErrorLevel()
|
||||||
|
{
|
||||||
|
|
||||||
|
$logger = $this->logger;
|
||||||
|
$logger->set_level(Tlog::ERROR);
|
||||||
|
|
||||||
|
//"#NUM: #NIVEAU [#FICHIER:#FONCTION()] {#LIGNE} #DATE #HEURE: "
|
||||||
|
$this->expectOutputRegex(sprintf($this->regex, "foo"));
|
||||||
|
$logger->error("foo");
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testErrorWitoutErrorLevel()
|
||||||
|
{
|
||||||
|
$logger = $this->logger;
|
||||||
|
$logger->set_level(Tlog::MUET);
|
||||||
|
|
||||||
|
$this->expectOutputRegex("/^$/");
|
||||||
|
$logger->error("foo");
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testFatalWithFatalLevel()
|
||||||
|
{
|
||||||
|
|
||||||
|
$logger = $this->logger;
|
||||||
|
$logger->set_level(Tlog::FATAL);
|
||||||
|
|
||||||
|
//"#NUM: #NIVEAU [#FICHIER:#FONCTION()] {#LIGNE} #DATE #HEURE: "
|
||||||
|
$this->expectOutputRegex(sprintf($this->regex, "foo"));
|
||||||
|
$logger->fatal("foo");
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testFatalWitoutFatalLevel()
|
||||||
|
{
|
||||||
|
$logger = $this->logger;
|
||||||
|
$logger->set_level(Tlog::MUET);
|
||||||
|
|
||||||
|
$this->expectOutputRegex("/^$/");
|
||||||
|
$logger->fatal("foo");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class ConfigModel
|
||||||
|
{
|
||||||
|
public function __call($name, $arguments) {
|
||||||
|
return $arguments[1];
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user