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.

PhalApi_Logger_File_Test.php 2.6 KiB

3 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * PhpUnderControl_PhalApiLoggerFile_Test
  4. *
  5. * 针对 ../../PhalApi/Logger/File.php PhalApi_Logger_File 类的PHPUnit单元测试
  6. *
  7. * @author: dogstar 20141217
  8. */
  9. require_once dirname(__FILE__) . '/../test_env.php';
  10. if (!class_exists('PhalApi_Logger_File')) {
  11. require dirname(__FILE__) . '/../../PhalApi/Logger/File.php';
  12. }
  13. class PhpUnderControl_PhalApiLoggerFile_Test extends PHPUnit_Framework_TestCase
  14. {
  15. public $coreLoggerFile;
  16. protected function setUp()
  17. {
  18. parent::setUp();
  19. $cmd = sprintf('rm %s -rf', dirname(__FILE__) . '/Runtime');
  20. shell_exec($cmd);
  21. $this->coreLoggerFile = new PhalApi_Logger_File(dirname(__FILE__) . '/Runtime',
  22. PhalApi_Logger::LOG_LEVEL_DEBUG | PhalApi_Logger::LOG_LEVEL_INFO | PhalApi_Logger::LOG_LEVEL_ERROR);
  23. }
  24. protected function tearDown()
  25. {
  26. $cmd = sprintf('rm %s -rf', dirname(__FILE__) . '/Runtime');
  27. shell_exec($cmd);
  28. }
  29. /**
  30. * @group testLog
  31. */
  32. public function testLog()
  33. {
  34. $this->coreLoggerFile->log('debug', 'debug from log', '');
  35. $this->coreLoggerFile->log('task', 'something test for task', array('from' => 'phpunit'));
  36. $this->assertLogExists('debug from log');
  37. $this->assertLogExists('something test for task');
  38. }
  39. public function testDebug()
  40. {
  41. $this->coreLoggerFile->debug('something debug here', array('name' => 'phpunit'));
  42. $this->coreLoggerFile->debug("This
  43. should not be
  44. multi line");
  45. $this->assertLogExists('something debug here');
  46. $this->assertLogExists('This');
  47. $this->assertLogExists('should not be \n');
  48. $this->assertLogExists('multi');
  49. }
  50. public function testInfo()
  51. {
  52. $this->coreLoggerFile->info('something info here', 'phpunit');
  53. $this->coreLoggerFile->info('something info here', 2014);
  54. $this->coreLoggerFile->info('something info here', true);
  55. $this->assertLogExists('something info here');
  56. $this->assertLogExists('phpunit');
  57. $this->assertLogExists('2014');
  58. $this->assertLogExists('1');
  59. }
  60. public function testError()
  61. {
  62. $this->coreLoggerFile->error('WTF!');
  63. $this->assertLogExists('WTF!');
  64. }
  65. protected function assertLogExists($content)
  66. {
  67. $logFile = implode(DIRECTORY_SEPARATOR, array(
  68. dirname(__FILE__) . '/Runtime',
  69. 'log',
  70. date('Ym', time()),
  71. date('Ymd', time()) . '.log'
  72. ));
  73. $this->assertContains($content, file_get_contents($logFile));
  74. }
  75. }