PageRenderTime 40ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/test/lib/Elastica/Test/LogTest.php

https://github.com/attilabukor/Elastica
PHP | 153 lines | 122 code | 30 blank | 1 comment | 1 complexity | d58ef084aadfaf2588d9e6f7445224c9 MD5 | raw file
  1. <?php
  2. namespace Elastica\Test;
  3. use Elastica\Client;
  4. use Elastica\Log;
  5. use Elastica\Test\Base as BaseTest;
  6. use Psr\Log\LogLevel;
  7. class LogTest extends BaseTest
  8. {
  9. private $_context = array();
  10. private $_message = 'hello world';
  11. protected function setUp()
  12. {
  13. if (!class_exists('Psr\Log\AbstractLogger')) {
  14. $this->markTestSkipped('The Psr extension is not available.');
  15. }
  16. }
  17. public function testLogInterface()
  18. {
  19. $log = new Log();
  20. $this->assertInstanceOf('Psr\Log\LoggerInterface', $log);
  21. }
  22. public function testSetLogConfigPath()
  23. {
  24. $logPath = '/tmp/php.log';
  25. $client = new Client(array('log' => $logPath));
  26. $this->assertEquals($logPath, $client->getConfig('log'));
  27. }
  28. public function testSetLogConfigEnable()
  29. {
  30. $client = new Client(array('log' => true));
  31. $this->assertTrue($client->getConfig('log'));
  32. }
  33. public function testSetLogConfigEnable1()
  34. {
  35. $client = new Client();
  36. $client->setLogger(new Log());
  37. $this->assertFalse($client->getConfig('log'));
  38. }
  39. public function testEmptyLogConfig()
  40. {
  41. $client = $this->_getClient();
  42. $this->assertEmpty($client->getConfig('log'));
  43. }
  44. public function testGetLastMessage()
  45. {
  46. $log = new Log('/tmp/php.log');
  47. $log->log(LogLevel::DEBUG, $this->_message, $this->_context);
  48. $this->_context['error_message'] = $this->_message;
  49. $message = json_encode($this->_context);
  50. $this->assertEquals($message, $log->getLastMessage());
  51. }
  52. public function testGetLastMessage2()
  53. {
  54. $client = new Client(array('log' => true));
  55. $log = new Log($client);
  56. // Set log path temp path as otherwise test fails with output
  57. $errorLog = ini_get('error_log');
  58. ini_set('error_log', sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'php.log');
  59. $this->_context['error_message'] = $this->_message;
  60. $message = json_encode($this->_context);
  61. $log->log(LogLevel::DEBUG, $this->_message, $this->_context);
  62. ini_set('error_log', $errorLog);
  63. $this->assertEquals($message, $log->getLastMessage());
  64. }
  65. public function testGetLastMessageInfo()
  66. {
  67. $log = $this->initLog();
  68. $log->info($this->_message, $this->_context);
  69. $this->assertEquals($this->getMessage(), $log->getLastMessage());
  70. }
  71. public function testGetLastMessageCritical()
  72. {
  73. $log = $this->initLog();
  74. $log->critical($this->_message, $this->_context);
  75. $this->assertEquals($this->getMessage(), $log->getLastMessage());
  76. }
  77. public function testGetLastMessageAlert()
  78. {
  79. $log = $this->initLog();
  80. $log->alert($this->_message, $this->_context);
  81. $this->assertEquals($this->getMessage(), $log->getLastMessage());
  82. }
  83. public function testGetLastMessageDebug()
  84. {
  85. $log = $this->initLog();
  86. $log->debug($this->_message, $this->_context);
  87. $this->assertEquals($this->getMessage(), $log->getLastMessage());
  88. }
  89. public function testGetLastMessageEmergency()
  90. {
  91. $log = $this->initLog();
  92. $log->emergency($this->_message, $this->_context);
  93. $this->assertEquals($this->getMessage(), $log->getLastMessage());
  94. }
  95. public function testGetLastMessageError()
  96. {
  97. $log = $this->initLog();
  98. $log->error($this->_message, $this->_context);
  99. $this->assertEquals($this->getMessage(), $log->getLastMessage());
  100. }
  101. public function testGetLastMessageNotice()
  102. {
  103. $log = $this->initLog();
  104. $log->notice($this->_message, $this->_context);
  105. $this->assertEquals($this->getMessage(), $log->getLastMessage());
  106. }
  107. public function testGetLastMessageWarning()
  108. {
  109. $log = $this->initLog();
  110. $log->warning($this->_message, $this->_context);
  111. $this->assertEquals($this->getMessage(), $log->getLastMessage());
  112. }
  113. private function initLog()
  114. {
  115. $log = new Log('/tmp/php.log');
  116. return $log;
  117. }
  118. private function getMessage()
  119. {
  120. $this->_context['error_message'] = $this->_message;
  121. return json_encode($this->_context);
  122. }
  123. }