PushoverHandlerTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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->handle($this->getRecord(Logger::CRITICAL, 'test1'));
  27. fseek($this->res, 0);
  28. $content = fread($this->res, 1024);
  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\+error&timestamp=\d{10}$/', $content);
  38. }
  39. public function testWriteWithComplexTitle()
  40. {
  41. $this->createHandler('myToken', 'myUser', 'Backup finished - SQL1');
  42. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
  43. fseek($this->res, 0);
  44. $content = fread($this->res, 1024);
  45. $this->assertRegexp('/title=Backup\+finished\+-\+SQL1/', $content);
  46. }
  47. public function testWriteWithComplexMessage()
  48. {
  49. $this->createHandler();
  50. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'Backup of database "example" finished in 16 minutes.'));
  51. fseek($this->res, 0);
  52. $content = fread($this->res, 1024);
  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->createHandler();
  59. $this->handler->handle($this->getRecord(Logger::CRITICAL, $message));
  60. fseek($this->res, 0);
  61. $content = fread($this->res, 1024);
  62. $expectedMessage = substr($message, 0, 499);
  63. $this->assertRegexp('/message=' . $expectedMessage . '&title/', $content);
  64. }
  65. private function createHandler($token = 'myToken', $user = 'myUser', $title = null)
  66. {
  67. $constructArray = array($token, $user);
  68. if($title != null) {
  69. $constructArray[2] = $title;
  70. }
  71. $this->res = fopen('php://memory', 'a');
  72. $this->handler = $this->getMock(
  73. '\Monolog\Handler\PushoverHandler', array('fsockopen', 'streamSetTimeout'), $constructArray
  74. );
  75. $reflectionProperty = new \ReflectionProperty('\Monolog\Handler\SocketHandler', 'connectionString');
  76. $reflectionProperty->setAccessible(true);
  77. $reflectionProperty->setValue($this->handler, 'localhost:1234');
  78. $this->handler->expects($this->any())
  79. ->method('fsockopen')
  80. ->will($this->returnValue($this->res));
  81. $this->handler->expects($this->any())
  82. ->method('streamSetTimeout')
  83. ->will($this->returnValue(true));
  84. $this->handler->setFormatter($this->getIdentityFormatter());
  85. }
  86. }