PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

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

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