PushoverHandlerTest.php 5.1 KB

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