FleepHookHandlerTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. /*
  3. * This file is part of the Monolog package.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Monolog\Handler;
  11. use Monolog\Formatter\LineFormatter;
  12. use Monolog\Logger;
  13. use Monolog\TestCase;
  14. /**
  15. * Unit tests for the FleepHookHandler
  16. *
  17. * @author Ando Roots <ando@sqroot.eu>
  18. * @coversDefaultClass \Monolog\Handler\FleepHookHandler
  19. */
  20. class FleepHookHandlerTest extends TestCase
  21. {
  22. /**
  23. * Default token to use in tests
  24. */
  25. const TOKEN = '123abc';
  26. /**
  27. * @var FleepHookHandler
  28. */
  29. private $handler;
  30. /**
  31. * @var Logger
  32. */
  33. private $logger;
  34. public function setUp()
  35. {
  36. parent::setUp();
  37. if (!extension_loaded('curl')) {
  38. $this->markTestSkipped('This test requires curl extension to run');
  39. }
  40. // Create instances of the handler and logger for convenience
  41. $this->handler = new FleepHookHandler(self::TOKEN);
  42. $this->logger = new Logger('test');
  43. $this->logger->pushHandler($this->handler);
  44. }
  45. /**
  46. * @covers ::__construct
  47. */
  48. public function testConstructorSetsExpectedDefaults()
  49. {
  50. $this->assertEquals(self::TOKEN, $this->handler->getToken());
  51. $this->assertEquals(Logger::DEBUG, $this->handler->getLevel());
  52. $this->assertEquals(true, $this->handler->getBubble());
  53. }
  54. /**
  55. * @covers ::write
  56. */
  57. public function testWriteSendsFormattedMessageToFleep()
  58. {
  59. $handler = $this->mockHandler(array('send'));
  60. $message = 'theCakeIsALie';
  61. $handler->expects($this->once())
  62. ->method('send')
  63. ->with(
  64. $this->callback(
  65. function ($message) {
  66. return strstr($message, 'theCakeIsALie') && strstr($message, 'channel.ALERT');
  67. }
  68. )
  69. );
  70. $this->sendLog($handler, $message);
  71. }
  72. /**
  73. * @covers ::getDefaultFormatter
  74. */
  75. public function testHandlerUsesLineFormatterWhichIgnoresEmptyArrays()
  76. {
  77. $record = array(
  78. 'message' => 'msg',
  79. 'context' => array(),
  80. 'level' => Logger::DEBUG,
  81. 'level_name' => Logger::getLevelName(Logger::DEBUG),
  82. 'channel' => 'channel',
  83. 'datetime' => new \DateTime(),
  84. 'extra' => array(),
  85. );
  86. $expectedFormatter = new LineFormatter(null, null, true, true);
  87. $expected = $expectedFormatter->format($record);
  88. $handlerFormatter = $this->handler->getDefaultFormatter();
  89. $actual = $handlerFormatter->format($record);
  90. $this->assertEquals($expected, $actual, 'Empty context and extra arrays should not be rendered');
  91. }
  92. /**
  93. * Tests that the URL to which the message is posted is of correct format
  94. *
  95. * Example: https://fleep.io/hook/mTZG6s-XRfKdNTJtpVyVaV
  96. * @covers ::__construct
  97. */
  98. public function testFleepEndpointUrlIsConstructedCorrectly()
  99. {
  100. $handler = $this->mockHandler(array('execCurl'));
  101. $token = self::TOKEN;
  102. // Set up expectation to execCurl: receive curlOpts array where URL is correct
  103. $handler->expects($this->once())
  104. ->method('execCurl')
  105. ->with(
  106. $this->callback(
  107. function (array $curlOpts) use ($token) {
  108. return $curlOpts[CURLOPT_URL] === FleepHookHandler::HOOK_ENDPOINT . $token;
  109. }
  110. )
  111. );
  112. $this->sendLog($handler);
  113. }
  114. /**
  115. * Tests that the log message is added to the POST content, under the 'message' key
  116. *
  117. * @covers ::send
  118. */
  119. public function testSendAddsMessageToCurlOpts()
  120. {
  121. $handler = $this->mockHandler(array('execCurl'));
  122. $handler->expects($this->once())
  123. ->method('execCurl')
  124. ->with(
  125. $this->callback(
  126. function ($curlOpts) {
  127. parse_str($curlOpts[CURLOPT_POSTFIELDS], $body);
  128. return isset($body['message']) && strstr($body['message'], 'msg');
  129. }
  130. )
  131. );
  132. $this->sendLog($handler, 'msg');
  133. }
  134. /**
  135. * @covers ::addCurlOptions
  136. */
  137. public function testAddCurlOptionsLeavesUnspecifiedOptionsIntact()
  138. {
  139. $this->handler->addCurlOptions(array(CURLOPT_PROXY => 'http://localhost:3128'));
  140. $this->assertArrayHasKey(CURLOPT_POST, $this->handler->getCurlOptions(), 'addCurlOpts deleted a key!');
  141. }
  142. public function testAddCurlOptionsAddsANewCurlOption()
  143. {
  144. $proxy = 'http://localhost:3128';
  145. $this->handler->addCurlOptions(array(CURLOPT_PROXY => $proxy));
  146. $options = $this->handler->getCurlOptions();
  147. $this->assertEquals($options[CURLOPT_PROXY], $proxy);
  148. }
  149. /**
  150. * Helper method for simulating a long sending event from the logger
  151. *
  152. * @param FleepHookHandler $handler
  153. * @param string $message
  154. */
  155. private function sendLog(FleepHookHandler $handler, $message = 'test')
  156. {
  157. $logger = new Logger('channel');
  158. $logger->pushHandler($handler);
  159. $logger->addAlert($message);
  160. }
  161. /**
  162. * Helper method for constructing a new mock of FleepHookHandler
  163. *
  164. * @param array $methods
  165. * @return \PHPUnit_Framework_MockObject_MockObject
  166. */
  167. private function mockHandler(array $methods)
  168. {
  169. return $this->getMock('Monolog\Handler\FleepHookHandler', $methods, array(self::TOKEN));
  170. }
  171. }