4 * This file is part of the Monolog package.
6 * (c) Jordi Boggiano <j.boggiano@seld.be>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Monolog\Handler;
18 * Almost all examples (expected header, titles, messages) taken from
19 * https://www.pushover.net/api
20 * @author Sebastian Göttschkes <sebastian.goettschkes@googlemail.com>
21 * @see https://www.pushover.net/api
23 class PushoverHandlerTest extends TestCase
28 public function testWriteHeader()
30 $this->createHandler();
31 $this->handler->setHighPriorityLevel(Logger::EMERGENCY); // skip priority notifications
32 $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
34 $content = fread($this->res, 1024);
36 $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);
42 * @depends testWriteHeader
44 public function testWriteContent($content)
46 $this->assertRegexp('/token=myToken&user=myUser&message=test1&title=Monolog×tamp=\d{10}$/', $content);
49 public function testWriteWithComplexTitle()
51 $this->createHandler('myToken', 'myUser', 'Backup finished - SQL1');
52 $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
54 $content = fread($this->res, 1024);
56 $this->assertRegexp('/title=Backup\+finished\+-\+SQL1/', $content);
59 public function testWriteWithComplexMessage()
61 $this->createHandler();
62 $this->handler->setHighPriorityLevel(Logger::EMERGENCY); // skip priority notifications
63 $this->handler->handle($this->getRecord(Logger::CRITICAL, 'Backup of database "example" finished in 16 minutes.'));
65 $content = fread($this->res, 1024);
67 $this->assertRegexp('/message=Backup\+of\+database\+%22example%22\+finished\+in\+16\+minutes\./', $content);
70 public function testWriteWithTooLongMessage()
72 $message = str_pad('test', 520, 'a');
73 $this->createHandler();
74 $this->handler->setHighPriorityLevel(Logger::EMERGENCY); // skip priority notifications
75 $this->handler->handle($this->getRecord(Logger::CRITICAL, $message));
77 $content = fread($this->res, 1024);
79 $expectedMessage = substr($message, 0, 505);
81 $this->assertRegexp('/message=' . $expectedMessage . '&title/', $content);
84 public function testWriteWithHighPriority()
86 $this->createHandler();
87 $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
89 $content = fread($this->res, 1024);
91 $this->assertRegexp('/token=myToken&user=myUser&message=test1&title=Monolog×tamp=\d{10}&priority=1$/', $content);
94 public function testWriteWithEmergencyPriority()
96 $this->createHandler();
97 $this->handler->handle($this->getRecord(Logger::EMERGENCY, 'test1'));
99 $content = fread($this->res, 1024);
101 $this->assertRegexp('/token=myToken&user=myUser&message=test1&title=Monolog×tamp=\d{10}&priority=2&retry=30&expire=25200$/', $content);
104 public function testWriteToMultipleUsers()
106 $this->createHandler('myToken', array('userA', 'userB'));
107 $this->handler->handle($this->getRecord(Logger::EMERGENCY, 'test1'));
108 fseek($this->res, 0);
109 $content = fread($this->res, 1024);
111 $this->assertRegexp('/token=myToken&user=userA&message=test1&title=Monolog×tamp=\d{10}&priority=2&retry=30&expire=25200POST/', $content);
112 $this->assertRegexp('/token=myToken&user=userB&message=test1&title=Monolog×tamp=\d{10}&priority=2&retry=30&expire=25200$/', $content);
115 private function createHandler($token = 'myToken', $user = 'myUser', $title = 'Monolog')
117 $constructorArgs = array($token, $user, $title);
118 $this->res = fopen('php://memory', 'a');
119 $this->handler = $this->getMock(
120 '\Monolog\Handler\PushoverHandler',
121 array('fsockopen', 'streamSetTimeout', 'closeSocket'),
125 $reflectionProperty = new \ReflectionProperty('\Monolog\Handler\SocketHandler', 'connectionString');
126 $reflectionProperty->setAccessible(true);
127 $reflectionProperty->setValue($this->handler, 'localhost:1234');
129 $this->handler->expects($this->any())
130 ->method('fsockopen')
131 ->will($this->returnValue($this->res));
132 $this->handler->expects($this->any())
133 ->method('streamSetTimeout')
134 ->will($this->returnValue(true));
135 $this->handler->expects($this->any())
136 ->method('closeSocket')
137 ->will($this->returnValue(true));
139 $this->handler->setFormatter($this->getIdentityFormatter());