MonologHandlerPushoverHandlerTest

Almost all examples (expected header, titles, messages) taken from https://www.pushover.net/api.

Defined (1)

The class is defined in the following location(s).

/vendor/monolog/monolog/tests/Monolog/Handler/PushoverHandlerTest.php  
  1. class PushoverHandlerTest extends TestCase 
  2. private $res; 
  3. private $handler; 
  4.  
  5. public function testWriteHeader() 
  6. $this->createHandler(); 
  7. $this->handler->setHighPriorityLevel(Logger::EMERGENCY); // skip priority notifications 
  8. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1')); 
  9. fseek($this->res, 0); 
  10. $content = fread($this->res, 1024); 
  11.  
  12. $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); 
  13.  
  14. return $content; 
  15.  
  16. /** 
  17. * @depends testWriteHeader 
  18. */ 
  19. public function testWriteContent($content) 
  20. $this->assertRegexp('/token=myToken&user=myUser&message=test1&title=Monolog×tamp=\d{10}$/', $content); 
  21.  
  22. public function testWriteWithComplexTitle() 
  23. $this->createHandler('myToken', 'myUser', 'Backup finished - SQL1', Logger::EMERGENCY); 
  24. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1')); 
  25. fseek($this->res, 0); 
  26. $content = fread($this->res, 1024); 
  27.  
  28. $this->assertRegexp('/title=Backup\+finished\+-\+SQL1/', $content); 
  29.  
  30. public function testWriteWithComplexMessage() 
  31. $this->createHandler(); 
  32. $this->handler->setHighPriorityLevel(Logger::EMERGENCY); // skip priority notifications 
  33. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'Backup of database "example" finished in 16 minutes.')); 
  34. fseek($this->res, 0); 
  35. $content = fread($this->res, 1024); 
  36.  
  37. $this->assertRegexp('/message=Backup\+of\+database\+%22example%22\+finished\+in\+16\+minutes\./', $content); 
  38.  
  39. public function testWriteWithTooLongMessage() 
  40. $message = str_pad('test', 520, 'a'); 
  41. $this->createHandler(); 
  42. $this->handler->setHighPriorityLevel(Logger::EMERGENCY); // skip priority notifications 
  43. $this->handler->handle($this->getRecord(Logger::CRITICAL, $message)); 
  44. fseek($this->res, 0); 
  45. $content = fread($this->res, 1024); 
  46.  
  47. $expectedMessage = substr($message, 0, 505); 
  48.  
  49. $this->assertRegexp('/message=' . $expectedMessage . '&title/', $content); 
  50.  
  51. public function testWriteWithHighPriority() 
  52. $this->createHandler(); 
  53. $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1')); 
  54. fseek($this->res, 0); 
  55. $content = fread($this->res, 1024); 
  56.  
  57. $this->assertRegexp('/token=myToken&user=myUser&message=test1&title=Monolog×tamp=\d{10}&priority=1$/', $content); 
  58.  
  59. public function testWriteWithEmergencyPriority() 
  60. $this->createHandler(); 
  61. $this->handler->handle($this->getRecord(Logger::EMERGENCY, 'test1')); 
  62. fseek($this->res, 0); 
  63. $content = fread($this->res, 1024); 
  64.  
  65. $this->assertRegexp('/token=myToken&user=myUser&message=test1&title=Monolog×tamp=\d{10}&priority=2&retry=30&expire=25200$/', $content); 
  66.  
  67. public function testWriteToMultipleUsers() 
  68. $this->createHandler('myToken', array('userA', 'userB')); 
  69. $this->handler->handle($this->getRecord(Logger::EMERGENCY, 'test1')); 
  70. fseek($this->res, 0); 
  71. $content = fread($this->res, 1024); 
  72.  
  73. $this->assertRegexp('/token=myToken&user=userA&message=test1&title=Monolog×tamp=\d{10}&priority=2&retry=30&expire=25200POST/', $content); 
  74. $this->assertRegexp('/token=myToken&user=userB&message=test1&title=Monolog×tamp=\d{10}&priority=2&retry=30&expire=25200$/', $content); 
  75.  
  76. private function createHandler($token = 'myToken', $user = 'myUser', $title = 'Monolog') 
  77. $constructorArgs = array($token, $user, $title); 
  78. $this->res = fopen('php://memory', 'a'); 
  79. $this->handler = $this->getMock( 
  80. '\Monolog\Handler\PushoverHandler',  
  81. array('fsockopen', 'streamSetTimeout', 'closeSocket'),  
  82. $constructorArgs 
  83. ); 
  84.  
  85. $reflectionProperty = new \ReflectionProperty('\Monolog\Handler\SocketHandler', 'connectionString'); 
  86. $reflectionProperty->setAccessible(true); 
  87. $reflectionProperty->setValue($this->handler, 'localhost:1234'); 
  88.  
  89. $this->handler->expects($this->any()) 
  90. ->method('fsockopen') 
  91. ->will($this->returnValue($this->res)); 
  92. $this->handler->expects($this->any()) 
  93. ->method('streamSetTimeout') 
  94. ->will($this->returnValue(true)); 
  95. $this->handler->expects($this->any()) 
  96. ->method('closeSocket') 
  97. ->will($this->returnValue(true)); 
  98.  
  99. $this->handler->setFormatter($this->getIdentityFormatter());