Browse Source

Logger classes tested

master
Anton Grebnev 13 years ago
parent
commit
763fac6f40
  1. 62
      tests/logger/CliLoggerTest.php
  2. 116
      tests/logger/FileLoggerTest.php

62
tests/logger/CliLoggerTest.php

@ -0,0 +1,62 @@
<?php
/*
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage UnitTests
* @since 2011-10-06
*
* Unit tests for CliLogger class
*/
require_once dirname(__FILE__) . '/../../Registry.php';
require_once dirname(__FILE__) . '/../../Config.php';
require_once dirname(__FILE__) . '/../../logger/Logger.php';
require_once dirname(__FILE__) . '/../../logger/CliLogger.php';
class CliLoggerTest extends PHPUnit_Framework_TestCase
{
public function run(PHPUnit_Framework_TestResult $result = NULL)
{
$this->setPreserveGlobalState(false);
return parent::run($result);
}
public function setUp()
{
Config::set('Logger', array('logger' => 'CliLogger'));
}
/**
* @runInSeparateProcess
*/
public function testGetInstance()
{
$logger = Logger::getInstance();
$this->assertInstanceOf('CliLogger', $logger);
}
/**
* @runInSeparateProcess
*/
public function testLog()
{
if(!defined('DEBUG')) {
define('DEBUG', true);
}
$logger = Logger::getInstance();
ob_start();
$logger->setPid(123);
$logger->log('new msg');
$out = ob_get_clean();
$this->assertContains('<123> new msg', $out);
}
public function tearDown()
{
$conf = Config::getInstance();
$conf->offsetUnset('Logger');
}
}

116
tests/logger/FileLoggerTest.php

@ -0,0 +1,116 @@
<?php
/*
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage UnitTests
* @since 2011-10-06
*
* Unit tests for CliLogger class
*/
require_once dirname(__FILE__) . '/../../Registry.php';
require_once dirname(__FILE__) . '/../../Config.php';
require_once dirname(__FILE__) . '/../../exception/GeneralException.php';
require_once dirname(__FILE__) . '/../../logger/Logger.php';
require_once dirname(__FILE__) . '/../../logger/FileLogger.php';
require_once 'vfsStream/vfsStream.php';
class FileLoggerTest extends PHPUnit_Framework_TestCase
{
protected $conf;
public function run(PHPUnit_Framework_TestResult $result = NULL)
{
$this->setPreserveGlobalState(false);
return parent::run($result);
}
public function setUp()
{
vfsStreamWrapper::register();
$root = vfsStream::create(array());
vfsStreamWrapper::setRoot($root);
$this->conf = array('logger' => 'FileLogger', 'filepath' => vfsStream::url('root/log.txt'));
Config::set('Logger', $this->conf);
if ($root->hasChild('log.txt')) {
$root->removeChild('log.txt');
}
}
/**
* @runInSeparateProcess
*/
public function testGetInstance()
{
$logger = Logger::getInstance();
$this->assertInstanceOf('FileLogger', $logger);
}
/**
* @runInSeparateProcess
* @expectedException GeneralException
* @expectedExceptionMessage Could not open file /log.txt
*/
public function testCannotWrite()
{
if (!defined('DEBUG')) {
define('DEBUG', true);
}
$conf = array('logger' => 'FileLogger', 'filepath' => '/log.txt');
Config::set('Logger', $conf);
$logger = Logger::getInstance()->log('new msg');
$this->assertFileNotExists('log.txt');
}
/**
* @runInSeparateProcess
*/
public function testLog()
{
if (!defined('DEBUG')) {
define('DEBUG', true);
}
$this->assertFileNotExists($this->conf['filepath']);
$logger = Logger::getInstance();
$logger->setPid(123);
$logger->log('new msg');
$this->assertFileExists($this->conf['filepath']);
}
/**
* @runInSeparateProcess
*/
public function testDestruct()
{
if (!defined('DEBUG')) {
define('DEBUG', true);
}
$my_pid = posix_getpid();
$fd_command = 'lsof -n -p ' . $my_pid . ' | wc -l';
$fd_count_start = (int) `$fd_command`;
$logger = Logger::getInstance();
$logger->log('new msg');
$logger->log('new msg');
$logger->log('new msg');
$logger->log('new msg');
$logger->log('new msg');
$logger->log('new msg');
$logger->log('new msg');
$logger->log('new msg');
$logger->__destruct();
$this->assertAttributeEquals(null, 'handler', $logger);
$fd_count_end = (int) `$fd_command`;
$this->assertEquals($fd_count_start, $fd_count_end);
}
public function tearDown()
{
$conf = Config::getInstance();
$conf->offsetUnset('Logger');
if (file_exists($this->conf['filepath'])) {
unlink($this->conf['filepath']);
}
}
}
Loading…
Cancel
Save