PushoverHandlerTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php declare(strict_types=1);
  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\Test\TestCase;
  12. use Monolog\Logger;
  13. /**
  14. * Almost all examples (expected header, titles, messages) taken from
  15. * https://www.pushover.net/api
  16. * @author Sebastian Göttschkes <sebastian.goettschkes@googlemail.com>
  17. * @see https://www.pushover.net/api
  18. */
  19. class PushoverHandlerTest extends TestCase
  20. {
  21. private $res;
  22. private $handler;
  23. public function testWriteHeader()
  24. {
  25. $this->initHandlerAndSocket();
  26. $this->handler->setHighPriorityLevel(Logger::EMERGENCY); // skip priority notifications
  27. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
  28. $content = $this->closeSocket();
  29. $this->assertRegexp('/POST \/1\/messages.json HTTP\/1.1\\r\\nHost: api.pushover.net\\r\\nContent-Type: application\/x-www-form-urlencoded\\r\\nContent-Length: \d{2,4}\\r\\n\\r\\n/', $content);
  30. return $content;
  31. }
  32. /**
  33. * @depends testWriteHeader
  34. */
  35. public function testWriteContent($content)
  36. {
  37. $this->assertRegexp('/token=myToken&user=myUser&message=test1&title=Monolog&timestamp=\d{10}$/', $content);
  38. }
  39. public function testWriteWithComplexTitle()
  40. {
  41. $this->initHandlerAndSocket('myToken', 'myUser', 'Backup finished - SQL1');
  42. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
  43. $content = $this->closeSocket();
  44. $this->assertRegexp('/title=Backup\+finished\+-\+SQL1/', $content);
  45. }
  46. public function testWriteWithComplexMessage()
  47. {
  48. $this->initHandlerAndSocket();
  49. $this->handler->setHighPriorityLevel(Logger::EMERGENCY); // skip priority notifications
  50. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'Backup of database "example" finished in 16 minutes.'));
  51. $content = $this->closeSocket();
  52. $this->assertRegexp('/message=Backup\+of\+database\+%22example%22\+finished\+in\+16\+minutes\./', $content);
  53. }
  54. public function testWriteWithTooLongMessage()
  55. {
  56. $message = str_pad('test', 520, 'a');
  57. $this->initHandlerAndSocket();
  58. $this->handler->setHighPriorityLevel(Logger::EMERGENCY); // skip priority notifications
  59. $this->handler->handle($this->getRecord(Logger::CRITICAL, $message));
  60. $content = $this->closeSocket();
  61. $expectedMessage = substr($message, 0, 505);
  62. $this->assertRegexp('/message=' . $expectedMessage . '&title/', $content);
  63. }
  64. public function testWriteWithHighPriority()
  65. {
  66. $this->initHandlerAndSocket();
  67. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
  68. $content = $this->closeSocket();
  69. $this->assertRegexp('/token=myToken&user=myUser&message=test1&title=Monolog&timestamp=\d{10}&priority=1$/', $content);
  70. }
  71. public function testWriteWithEmergencyPriority()
  72. {
  73. $this->initHandlerAndSocket();
  74. $this->handler->handle($this->getRecord(Logger::EMERGENCY, 'test1'));
  75. $content = $this->closeSocket();
  76. $this->assertRegexp('/token=myToken&user=myUser&message=test1&title=Monolog&timestamp=\d{10}&priority=2&retry=30&expire=25200$/', $content);
  77. }
  78. public function testWriteToMultipleUsers()
  79. {
  80. $this->initHandlerAndSocket('myToken', ['userA', 'userB']);
  81. $this->handler->handle($this->getRecord(Logger::EMERGENCY, 'test1'));
  82. $content = $this->closeSocket();
  83. $this->assertRegexp('/token=myToken&user=userA&message=test1&title=Monolog&timestamp=\d{10}&priority=2&retry=30&expire=25200POST/', $content);
  84. $this->assertRegexp('/token=myToken&user=userB&message=test1&title=Monolog&timestamp=\d{10}&priority=2&retry=30&expire=25200$/', $content);
  85. }
  86. private function initHandlerAndSocket($token = 'myToken', $user = 'myUser', $title = 'Monolog')
  87. {
  88. $tmpFile = sys_get_temp_dir().'/monolog-test-socket.php';
  89. file_put_contents($tmpFile, <<<'SCRIPT'
  90. <?php
  91. $sock = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp'));
  92. socket_bind($sock, '127.0.0.1', 51984);
  93. socket_listen($sock);
  94. while (true) {
  95. $res = socket_accept($sock);
  96. socket_set_option($res, SOL_SOCKET, SO_RCVTIMEO, array("sec" => 0, "usec" => 500));
  97. while ($read = socket_read($res, 1024)) {
  98. echo $read;
  99. }
  100. socket_close($res);
  101. }
  102. SCRIPT
  103. );
  104. $this->socket = new \Symfony\Component\Process\Process(escapeshellarg(PHP_BINARY).' '.escapeshellarg($tmpFile));
  105. $this->socket->start();
  106. $this->handler = new PushoverHandler($token, $user, $title);
  107. $reflectionProperty = new \ReflectionProperty('\Monolog\Handler\SocketHandler', 'connectionString');
  108. $reflectionProperty->setAccessible(true);
  109. $reflectionProperty->setValue($this->handler, '127.0.0.1:51984');
  110. $this->handler->setFormatter($this->getIdentityFormatter());
  111. }
  112. private function closeSocket()
  113. {
  114. $this->socket->stop();
  115. return $this->socket->getOutput();
  116. }
  117. public function tearDown()
  118. {
  119. if (isset($this->socket)) {
  120. $this->closeSocket();
  121. unset($this->socket);
  122. }
  123. }
  124. }