From 763fac6f40c2a39f967b59cb9de2eb691ffac131 Mon Sep 17 00:00:00 2001 From: Anton Grebnev Date: Mon, 21 Nov 2011 19:15:03 +0400 Subject: [PATCH] Logger classes tested --- tests/logger/CliLoggerTest.php | 62 +++++++++++++++++++++ tests/logger/FileLoggerTest.php | 116 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 178 insertions(+) create mode 100644 tests/logger/CliLoggerTest.php create mode 100644 tests/logger/FileLoggerTest.php diff --git a/tests/logger/CliLoggerTest.php b/tests/logger/CliLoggerTest.php new file mode 100644 index 0000000..6ef0673 --- /dev/null +++ b/tests/logger/CliLoggerTest.php @@ -0,0 +1,62 @@ + + * @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'); + } +} diff --git a/tests/logger/FileLoggerTest.php b/tests/logger/FileLoggerTest.php new file mode 100644 index 0000000..0d85434 --- /dev/null +++ b/tests/logger/FileLoggerTest.php @@ -0,0 +1,116 @@ + + * @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']); + } + } +}