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.

113 lines
3.2 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. vfsStream::setup();
  29. $root = vfsStream::create(array());
  30. vfsStreamWrapper::setRoot($root);
  31. Config::set('LOGGING', 1);
  32. $this->conf = array('logger' => 'FileLogger', 'filepath' => vfsStream::url('root/log.txt'));
  33. Config::set('Logger', $this->conf);
  34. if ($root->hasChild('log.txt')) {
  35. $root->removeChild('log.txt');
  36. }
  37. }
  38. /**
  39. * @runInSeparateProcess
  40. */
  41. public function testGetInstance()
  42. {
  43. $logger = Logger::getInstance();
  44. $this->assertInstanceOf('FileLogger', $logger);
  45. }
  46. /**
  47. * @runInSeparateProcess
  48. */
  49. public function testCannotWrite()
  50. {
  51. Config::set('DEBUG', 1);
  52. $conf = array('logger' => 'FileLogger', 'filepath' => '/log.txt');
  53. Config::set('Logger', $conf);
  54. $this->setExpectedException('GeneralException', 'Could not open file /log.txt');
  55. $logger = Logger::getInstance()->log('new msg');
  56. $this->assertFileNotExists('log.txt');
  57. }
  58. /**
  59. * @runInSeparateProcess
  60. */
  61. public function testLog()
  62. {
  63. Config::set('DEBUG', 1);
  64. $this->assertFileNotExists($this->conf['filepath']);
  65. $logger = Logger::getInstance();
  66. $logger->setPid(123);
  67. $logger->log('new msg');
  68. $this->assertFileExists($this->conf['filepath']);
  69. }
  70. /**
  71. * @runInSeparateProcess
  72. */
  73. public function testDestruct()
  74. {
  75. Config::set('DEBUG', 1);
  76. $my_pid = posix_getpid();
  77. $fd_command = 'lsof -n -p ' . $my_pid . ' | wc -l';
  78. $fd_count_start = (int) `$fd_command`;
  79. $logger = Logger::getInstance();
  80. $logger->log('new msg');
  81. $logger->log('new msg');
  82. $logger->log('new msg');
  83. $logger->log('new msg');
  84. $logger->log('new msg');
  85. $logger->log('new msg');
  86. $logger->log('new msg');
  87. $logger->log('new msg');
  88. $logger->__destruct();
  89. $this->assertAttributeEquals(null, 'handler', $logger);
  90. $fd_count_end = (int) `$fd_command`;
  91. $this->assertSame($fd_count_start, $fd_count_end);
  92. }
  93. public function tearDown()
  94. {
  95. $conf = Config::getInstance();
  96. $conf->offsetUnset('Logger');
  97. if (file_exists($this->conf['filepath'])) {
  98. unlink($this->conf['filepath']);
  99. }
  100. }
  101. }