You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
3.3 KiB

  1. <?php
  2. /*
  3. * @copyright NetMonsters <team@netmonsters.ru>
  4. * @link http://netmonsters.ru
  5. * @package Majestic
  6. * @subpackage UnitTests
  7. * @since 2011-10-06
  8. *
  9. * Unit tests for CliLogger class
  10. */
  11. require_once dirname(__FILE__) . '/../../Registry.php';
  12. require_once dirname(__FILE__) . '/../../Config.php';
  13. require_once dirname(__FILE__) . '/../../exception/GeneralException.php';
  14. require_once dirname(__FILE__) . '/../../logger/Logger.php';
  15. require_once dirname(__FILE__) . '/../../logger/FileLogger.php';
  16. require_once 'vfsStream/vfsStream.php';
  17. class FileLoggerTest extends PHPUnit_Framework_TestCase
  18. {
  19. protected $conf;
  20. public function run(PHPUnit_Framework_TestResult $result = NULL)
  21. {
  22. $this->setPreserveGlobalState(false);
  23. return parent::run($result);
  24. }
  25. public function setUp()
  26. {
  27. vfsStreamWrapper::register();
  28. $root = vfsStream::create(array());
  29. vfsStreamWrapper::setRoot($root);
  30. $this->conf = array('logger' => 'FileLogger', 'filepath' => vfsStream::url('root/log.txt'));
  31. Config::set('Logger', $this->conf);
  32. if ($root->hasChild('log.txt')) {
  33. $root->removeChild('log.txt');
  34. }
  35. }
  36. /**
  37. * @runInSeparateProcess
  38. */
  39. public function testGetInstance()
  40. {
  41. $logger = Logger::getInstance();
  42. $this->assertInstanceOf('FileLogger', $logger);
  43. }
  44. /**
  45. * @runInSeparateProcess
  46. * @expected Exception GeneralException
  47. * @expected ExceptionMessage Could not open file /log.txt
  48. */
  49. public function testCannotWrite()
  50. {
  51. if (!defined('DEBUG')) {
  52. define('DEBUG', true);
  53. }
  54. $conf = array('logger' => 'FileLogger', 'filepath' => '/log.txt');
  55. $this->setExpectedException('GeneralException', 'Could not open file /log.txt');
  56. Config::set('Logger', $conf);
  57. $logger = Logger::getInstance()->log('new msg');
  58. $this->assertFileNotExists('log.txt');
  59. }
  60. /**
  61. * @runInSeparateProcess
  62. */
  63. public function testLog()
  64. {
  65. if (!defined('DEBUG')) {
  66. define('DEBUG', true);
  67. }
  68. $this->assertFileNotExists($this->conf['filepath']);
  69. $logger = Logger::getInstance();
  70. $logger->setPid(123);
  71. $logger->log('new msg');
  72. $this->assertFileExists($this->conf['filepath']);
  73. }
  74. /**
  75. * @runInSeparateProcess
  76. */
  77. public function testDestruct()
  78. {
  79. if (!defined('DEBUG')) {
  80. define('DEBUG', true);
  81. }
  82. $my_pid = posix_getpid();
  83. $fd_command = 'lsof -n -p ' . $my_pid . ' | wc -l';
  84. $fd_count_start = (int) `$fd_command`;
  85. $logger = Logger::getInstance();
  86. $logger->log('new msg');
  87. $logger->log('new msg');
  88. $logger->log('new msg');
  89. $logger->log('new msg');
  90. $logger->log('new msg');
  91. $logger->log('new msg');
  92. $logger->log('new msg');
  93. $logger->log('new msg');
  94. $logger->__destruct();
  95. $this->assertAttributeEquals(null, 'handler', $logger);
  96. $fd_count_end = (int) `$fd_command`;
  97. $this->assertEquals($fd_count_start, $fd_count_end);
  98. }
  99. public function tearDown()
  100. {
  101. $conf = Config::getInstance();
  102. $conf->offsetUnset('Logger');
  103. if (file_exists($this->conf['filepath'])) {
  104. unlink($this->conf['filepath']);
  105. }
  106. }
  107. }