PushoverHandlerTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. use Monolog\Util\LocalSocket;
  14. /**
  15. * Almost all examples (expected header, titles, messages) taken from
  16. * https://www.pushover.net/api
  17. * @author Sebastian Göttschkes <sebastian.goettschkes@googlemail.com>
  18. * @see https://www.pushover.net/api
  19. */
  20. class PushoverHandlerTest extends TestCase
  21. {
  22. private $res;
  23. private $handler;
  24. public function testWriteHeader()
  25. {
  26. $this->initHandlerAndSocket();
  27. $this->handler->setHighPriorityLevel(Logger::EMERGENCY); // skip priority notifications
  28. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
  29. $content = $this->socket->getOutput();
  30. $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);
  31. return $content;
  32. }
  33. /**
  34. * @depends testWriteHeader
  35. */
  36. public function testWriteContent($content)
  37. {
  38. $this->assertRegexp('/token=myToken&user=myUser&message=test1&title=Monolog&timestamp=\d{10}$/', $content);
  39. }
  40. public function testWriteWithComplexTitle()
  41. {
  42. $this->initHandlerAndSocket('myToken', 'myUser', 'Backup finished - SQL1');
  43. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
  44. $content = $this->socket->getOutput();
  45. $this->assertRegexp('/title=Backup\+finished\+-\+SQL1/', $content);
  46. }
  47. public function testWriteWithComplexMessage()
  48. {
  49. $this->initHandlerAndSocket();
  50. $this->handler->setHighPriorityLevel(Logger::EMERGENCY); // skip priority notifications
  51. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'Backup of database "example" finished in 16 minutes.'));
  52. $content = $this->socket->getOutput();
  53. $this->assertRegexp('/message=Backup\+of\+database\+%22example%22\+finished\+in\+16\+minutes\./', $content);
  54. }
  55. public function testWriteWithTooLongMessage()
  56. {
  57. $message = str_pad('test', 520, 'a');
  58. $this->initHandlerAndSocket();
  59. $this->handler->setHighPriorityLevel(Logger::EMERGENCY); // skip priority notifications
  60. $this->handler->handle($this->getRecord(Logger::CRITICAL, $message));
  61. $content = $this->socket->getOutput();
  62. $expectedMessage = substr($message, 0, 505);
  63. $this->assertRegexp('/message=' . $expectedMessage . '&title/', $content);
  64. }
  65. public function testWriteWithHighPriority()
  66. {
  67. $this->initHandlerAndSocket();
  68. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
  69. $content = $this->socket->getOutput();
  70. $this->assertRegexp('/token=myToken&user=myUser&message=test1&title=Monolog&timestamp=\d{10}&priority=1$/', $content);
  71. }
  72. public function testWriteWithEmergencyPriority()
  73. {
  74. $this->initHandlerAndSocket();
  75. $this->handler->handle($this->getRecord(Logger::EMERGENCY, 'test1'));
  76. $content = $this->socket->getOutput();
  77. $this->assertRegexp('/token=myToken&user=myUser&message=test1&title=Monolog&timestamp=\d{10}&priority=2&retry=30&expire=25200$/', $content);
  78. }
  79. public function testWriteToMultipleUsers()
  80. {
  81. $this->markTestIncomplete('LocalSocket buffer does not support multiple-connections');
  82. $this->initHandlerAndSocket('myToken', ['userA', 'userB']);
  83. $this->handler->handle($this->getRecord(Logger::EMERGENCY, 'test1'));
  84. $content = $this->socket->getOutput();
  85. $this->assertRegexp('/token=myToken&user=userA&message=test1&title=Monolog&timestamp=\d{10}&priority=2&retry=30&expire=25200POST/', $content);
  86. $this->assertRegexp('/token=myToken&user=userB&message=test1&title=Monolog&timestamp=\d{10}&priority=2&retry=30&expire=25200$/', $content);
  87. }
  88. private function initHandlerAndSocket($token = 'myToken', $user = 'myUser', $title = 'Monolog')
  89. {
  90. $this->socket = LocalSocket::initSocket();
  91. $this->handler = new PushoverHandler($token, $user, $title);
  92. $reflectionProperty = new \ReflectionProperty('\Monolog\Handler\SocketHandler', 'connectionString');
  93. $reflectionProperty->setAccessible(true);
  94. $reflectionProperty->setValue($this->handler, '127.0.0.1:51984');
  95. $this->handler->setFormatter($this->getIdentityFormatter());
  96. }
  97. public function tearDown()
  98. {
  99. unset($this->socket, $this->handler);
  100. }
  101. }