PushoverHandlerTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 tearDown(): void
  24. {
  25. parent::tearDown();
  26. unset($this->res);
  27. }
  28. public function testWriteHeader()
  29. {
  30. $this->createHandler();
  31. $this->handler->setHighPriorityLevel(Logger::EMERGENCY); // skip priority notifications
  32. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
  33. fseek($this->res, 0);
  34. $content = fread($this->res, 1024);
  35. $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);
  36. return $content;
  37. }
  38. /**
  39. * @depends testWriteHeader
  40. */
  41. public function testWriteContent($content)
  42. {
  43. $this->assertRegexp('/token=myToken&user=myUser&message=test1&title=Monolog&timestamp=\d{10}$/', $content);
  44. }
  45. public function testWriteWithComplexTitle()
  46. {
  47. $this->createHandler('myToken', 'myUser', 'Backup finished - SQL1');
  48. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
  49. fseek($this->res, 0);
  50. $content = fread($this->res, 1024);
  51. $this->assertRegexp('/title=Backup\+finished\+-\+SQL1/', $content);
  52. }
  53. public function testWriteWithComplexMessage()
  54. {
  55. $this->createHandler();
  56. $this->handler->setHighPriorityLevel(Logger::EMERGENCY); // skip priority notifications
  57. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'Backup of database "example" finished in 16 minutes.'));
  58. fseek($this->res, 0);
  59. $content = fread($this->res, 1024);
  60. $this->assertRegexp('/message=Backup\+of\+database\+%22example%22\+finished\+in\+16\+minutes\./', $content);
  61. }
  62. public function testWriteWithTooLongMessage()
  63. {
  64. $message = str_pad('test', 520, 'a');
  65. $this->createHandler();
  66. $this->handler->setHighPriorityLevel(Logger::EMERGENCY); // skip priority notifications
  67. $this->handler->handle($this->getRecord(Logger::CRITICAL, $message));
  68. fseek($this->res, 0);
  69. $content = fread($this->res, 1024);
  70. $expectedMessage = substr($message, 0, 505);
  71. $this->assertRegexp('/message=' . $expectedMessage . '&title/', $content);
  72. }
  73. public function testWriteWithHighPriority()
  74. {
  75. $this->createHandler();
  76. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
  77. fseek($this->res, 0);
  78. $content = fread($this->res, 1024);
  79. $this->assertRegexp('/token=myToken&user=myUser&message=test1&title=Monolog&timestamp=\d{10}&priority=1$/', $content);
  80. }
  81. public function testWriteWithEmergencyPriority()
  82. {
  83. $this->createHandler();
  84. $this->handler->handle($this->getRecord(Logger::EMERGENCY, 'test1'));
  85. fseek($this->res, 0);
  86. $content = fread($this->res, 1024);
  87. $this->assertRegexp('/token=myToken&user=myUser&message=test1&title=Monolog&timestamp=\d{10}&priority=2&retry=30&expire=25200$/', $content);
  88. }
  89. public function testWriteToMultipleUsers()
  90. {
  91. $this->createHandler('myToken', ['userA', 'userB']);
  92. $this->handler->handle($this->getRecord(Logger::EMERGENCY, 'test1'));
  93. fseek($this->res, 0);
  94. $content = fread($this->res, 1024);
  95. $this->assertRegexp('/token=myToken&user=userA&message=test1&title=Monolog&timestamp=\d{10}&priority=2&retry=30&expire=25200POST/', $content);
  96. $this->assertRegexp('/token=myToken&user=userB&message=test1&title=Monolog&timestamp=\d{10}&priority=2&retry=30&expire=25200$/', $content);
  97. }
  98. private function createHandler($token = 'myToken', $user = 'myUser', $title = 'Monolog')
  99. {
  100. $constructorArgs = [$token, $user, $title];
  101. $this->res = fopen('php://memory', 'a');
  102. $this->handler = $this->getMockBuilder('Monolog\Handler\PushoverHandler')
  103. ->setConstructorArgs($constructorArgs)
  104. ->onlyMethods(['fsockopen', 'streamSetTimeout', 'closeSocket'])
  105. ->getMock();
  106. $reflectionProperty = new \ReflectionProperty('Monolog\Handler\SocketHandler', 'connectionString');
  107. $reflectionProperty->setAccessible(true);
  108. $reflectionProperty->setValue($this->handler, 'localhost:1234');
  109. $this->handler->expects($this->any())
  110. ->method('fsockopen')
  111. ->will($this->returnValue($this->res));
  112. $this->handler->expects($this->any())
  113. ->method('streamSetTimeout')
  114. ->will($this->returnValue(true));
  115. $this->handler->expects($this->any())
  116. ->method('closeSocket')
  117. ->will($this->returnValue(true));
  118. $this->handler->setFormatter($this->getIdentityFormatter());
  119. }
  120. }