Fix tests that make use of the log wrapper

refs #5683
This commit is contained in:
Johannes Meyer 2014-02-26 11:21:23 +01:00
parent 3555e66018
commit 031c91ce4a
4 changed files with 42 additions and 215 deletions

View File

@ -27,170 +27,63 @@
*/
// {{{ICINGA_LICENSE_HEADER}}}
namespace Tests\Icinga\Application;
// @codingStandardsIgnoreStart
require_once realpath(__DIR__ . '/../../../../../library/Icinga/Test/BaseTestCase.php');
// @codingStandardsIgnoreEnd
use \Zend_Config;
use Icinga\Application\Logger;
use Icinga\Test\BaseTestCase;
/**
* Test class for Logger
*
* @backupStaticAttributes enabled
**/
// @codingStandardsIgnoreStart
require_once BaseTestCase::$libDir . '/Logger/Logger.php';
// @codingStandardsIgnoreEnd
use \Zend_Config;
use Icinga\Logger\Logger;
class LoggerTest extends BaseTestCase
{
private $tempDir;
private $logTarget;
private $debugTarget;
public function setUp()
/**
* @backupStaticAttributes enabled
*/
public function testLogfileCreation()
{
$this->tempDir = tempnam(sys_get_temp_dir(), 'icingaweb-log');
unlink($this->tempDir); // tempnam create the file automatically
if (!is_dir($this->tempDir)) {
mkdir($this->tempDir, 0755);
}
$this->debugTarget = $this->tempDir . '/debug.log';
$this->logTarget = $this->tempDir . '/application.log';
$loggingConfigurationArray = array(
'enable' => 1,
'type' => 'stream',
'verbose' => 1,
'target' => $this->logTarget,
'debug' => array(
'enable' => 1,
'type' => 'stream',
'target' => $this->debugTarget
$target = tempnam(sys_get_temp_dir(), 'log');
unlink($target);
Logger::create(
new Zend_Config(
array(
'enable' => true,
'level' => Logger::$ERROR,
'type' => 'stream',
'target' => $target
)
)
);
$loggingConfiguration = new Zend_Config($loggingConfigurationArray);
Logger::reset();
Logger::create($loggingConfiguration);
}
public function tearDown()
{
if (file_exists($this->debugTarget)) {
unlink($this->debugTarget);
}
if (file_exists($this->logTarget)) {
unlink($this->logTarget);
}
rmdir($this->tempDir);
}
private function getLogData()
{
return array(
explode(PHP_EOL, file_get_contents($this->logTarget)),
explode(PHP_EOL, file_get_contents($this->debugTarget))
);
$this->assertFileExists($target, 'Logger did not create the log file');
unlink($target);
}
/**
* Test error messages
* @backupStaticAttributes enabled
* @depends testLogfileCreation
*/
public function testLoggingErrorMessages()
{
Logger::error('test-error-1');
Logger::error('test-error-2');
$this->assertFileExists($this->logTarget);
$this->assertFileExists($this->debugTarget);
list($main, $debug) = $this->getLogData();
$this->assertCount(3, $main);
$this->assertCount(3, $debug);
$this->assertContains(' ERR (3): test-error-1', $main[0]);
$this->assertContains(' ERR (3): test-error-2', $main[1]);
$this->assertContains(' ERR (3): test-error-1', $debug[0]);
$this->assertContains(' ERR (3): test-error-2', $debug[1]);
}
/**
* Test debug log and difference between error and debug messages
*/
public function testLoggingDebugMessages()
{
Logger::debug('test-debug-1');
Logger::error('test-error-1');
Logger::debug('test-debug-2');
$this->assertFileExists($this->logTarget);
$this->assertFileExists($this->debugTarget);
list($main, $debug) = $this->getLogData();
$this->assertCount(2, $main);
$this->assertCount(4, $debug);
$this->assertContains(' ERR (3): test-error-1', $main[0]);
$this->assertContains(' DEBUG (7): test-debug-1', $debug[0]);
$this->assertContains(' ERR (3): test-error-1', $debug[1]);
$this->assertContains(' DEBUG (7): test-debug-2', $debug[2]);
}
public function testLoggingQueueIfNoWriterAvailable()
{
Logger::reset();
Logger::error('test-error-1');
Logger::debug('test-debug-1');
Logger::error('test-error-2');
list($main, $debug) = $this->getLogData();
$this->assertCount(1, $main);
$this->assertCount(1, $debug);
$this->assertTrue(Logger::hasErrorsOccurred());
$queue = Logger::getQueue();
$this->assertCount(3, $queue);
$this->assertEquals(
array(
'test-error-1',
3
),
$queue[0]
);
$this->assertEquals(
array(
'test-debug-1',
7
),
$queue[1]
);
$this->assertEquals(
array(
'test-error-2',
3
),
$queue[2]
$target = tempnam(sys_get_temp_dir(), 'log');
unlink($target);
Logger::create(
new Zend_Config(
array(
'enable' => true,
'level' => Logger::$ERROR,
'type' => 'stream',
'target' => $target
)
)
);
Logger::error('This is a test error');
$log = file_get_contents($target);
unlink($target);
$this->assertContains('This is a test error', $log, 'Log does not contain the error "This is a test error"');
}
}

View File

@ -33,13 +33,12 @@ namespace Tests\Icinga\Authentication;
require_once realpath(__DIR__ . '/../../../../../library/Icinga/Test/BaseTestCase.php');
// @codingStandardsIgnoreEnd
use Icinga\Application\Logger;
use \Icinga\Test\BaseTestCase;
// @codingStandardsIgnoreStart
require_once 'Zend/Log.php';
require_once 'Zend/Config.php';
require_once BaseTestCase::$libDir . '/Application/Logger.php';
require_once BaseTestCase::$libDir . '/Logger/Logger.php';
require_once BaseTestCase::$libDir . '/Authentication/Manager.php';
require_once BaseTestCase::$libDir . '/Authentication/Membership.php';
require_once BaseTestCase::$libDir . '/Authentication/Credential.php';

View File

@ -14,7 +14,7 @@ require_once "Zend/Session.php";
require_once "Zend/Log/Writer/Abstract.php";
require_once "Zend/Log/Writer/Stream.php";
use Icinga\Application\Logger;
use Icinga\Logger\Logger;
use Icinga\Web\Notification;
class NotificationTest extends \PHPUnit_Framework_TestCase

View File

@ -1,65 +0,0 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
/**
* This file is part of Icinga Web 2.
*
* Icinga Web 2 - Head for multiple monitoring backends.
* Copyright (C) 2013 Icinga Development Team
*
* 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 2
* of the License, or (at your option) any later version.
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* @copyright 2013 Icinga Development Team <info@icinga.org>
* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2
* @author Icinga Development Team <info@icinga.org>
*
*/
// {{{ICINGA_LICENSE_HEADER}}}
namespace Tests\Icinga\Regression;
use \Icinga\Test\BaseTestCase;
use \Icinga\Application\Logger;
use \Zend_Config;
require_once 'Zend/Log.php';
require_once 'Zend/Config.php';
require_once 'Zend/Log/Writer/Mock.php';
require_once 'Zend/Log/Writer/Null.php';
require_once 'Zend/Log/Filter/Priority.php';
require_once realpath(__DIR__ . '/../../../library/Icinga/Test/BaseTestCase.php');
require_once realpath(BaseTestCase::$libDir.'/Application/Logger.php');
/**
* Bug 4595 : "If log disabled, default target (./var/log) is not writable / no path exist"
*
* This is caused because the logger ignored the 'enable' parameter
*/
class Regression4595 extends BaseTestCase {
public function testDisableLogging()
{
$cfg = new Zend_Config(
array(
'enable' => '0',
'type' => 'mock',
'target' => 'target2'
)
);
$logger = new Logger($cfg);
$writers = $logger->getWriters();
$this->assertEquals(0, count($writers), 'Assert that loggers aren\'t registered when "enable" is set to false');
}
}