scratch – Blame information for rev
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
115 | office | 1 | <?php |
2 | |||
3 | /* |
||
4 | * This file is part of the Monolog package. |
||
5 | * |
||
6 | * (c) Jordi Boggiano <j.boggiano@seld.be> |
||
7 | * |
||
8 | * For the full copyright and license information, please view the LICENSE |
||
9 | * file that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | namespace Monolog\Handler; |
||
13 | |||
14 | use Monolog\TestCase; |
||
15 | use Monolog\Logger; |
||
16 | |||
17 | /** |
||
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 |
||
22 | */ |
||
23 | class PushoverHandlerTest extends TestCase |
||
24 | { |
||
25 | private $res; |
||
26 | private $handler; |
||
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 | |||
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); |
||
37 | |||
38 | return $content; |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * @depends testWriteHeader |
||
43 | */ |
||
44 | public function testWriteContent($content) |
||
45 | { |
||
46 | $this->assertRegexp('/token=myToken&user=myUser&message=test1&title=Monolog×tamp=\d{10}$/', $content); |
||
47 | } |
||
48 | |||
49 | public function testWriteWithComplexTitle() |
||
50 | { |
||
51 | $this->createHandler('myToken', 'myUser', 'Backup finished - SQL1'); |
||
52 | $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1')); |
||
53 | fseek($this->res, 0); |
||
54 | $content = fread($this->res, 1024); |
||
55 | |||
56 | $this->assertRegexp('/title=Backup\+finished\+-\+SQL1/', $content); |
||
57 | } |
||
58 | |||
59 | public function testWriteWithComplexMessage() |
||
60 | { |
||
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.')); |
||
64 | fseek($this->res, 0); |
||
65 | $content = fread($this->res, 1024); |
||
66 | |||
67 | $this->assertRegexp('/message=Backup\+of\+database\+%22example%22\+finished\+in\+16\+minutes\./', $content); |
||
68 | } |
||
69 | |||
70 | public function testWriteWithTooLongMessage() |
||
71 | { |
||
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)); |
||
76 | fseek($this->res, 0); |
||
77 | $content = fread($this->res, 1024); |
||
78 | |||
79 | $expectedMessage = substr($message, 0, 505); |
||
80 | |||
81 | $this->assertRegexp('/message=' . $expectedMessage . '&title/', $content); |
||
82 | } |
||
83 | |||
84 | public function testWriteWithHighPriority() |
||
85 | { |
||
86 | $this->createHandler(); |
||
87 | $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1')); |
||
88 | fseek($this->res, 0); |
||
89 | $content = fread($this->res, 1024); |
||
90 | |||
91 | $this->assertRegexp('/token=myToken&user=myUser&message=test1&title=Monolog×tamp=\d{10}&priority=1$/', $content); |
||
92 | } |
||
93 | |||
94 | public function testWriteWithEmergencyPriority() |
||
95 | { |
||
96 | $this->createHandler(); |
||
97 | $this->handler->handle($this->getRecord(Logger::EMERGENCY, 'test1')); |
||
98 | fseek($this->res, 0); |
||
99 | $content = fread($this->res, 1024); |
||
100 | |||
101 | $this->assertRegexp('/token=myToken&user=myUser&message=test1&title=Monolog×tamp=\d{10}&priority=2&retry=30&expire=25200$/', $content); |
||
102 | } |
||
103 | |||
104 | public function testWriteToMultipleUsers() |
||
105 | { |
||
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); |
||
110 | |||
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); |
||
113 | } |
||
114 | |||
115 | private function createHandler($token = 'myToken', $user = 'myUser', $title = 'Monolog') |
||
116 | { |
||
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'), |
||
122 | $constructorArgs |
||
123 | ); |
||
124 | |||
125 | $reflectionProperty = new \ReflectionProperty('\Monolog\Handler\SocketHandler', 'connectionString'); |
||
126 | $reflectionProperty->setAccessible(true); |
||
127 | $reflectionProperty->setValue($this->handler, 'localhost:1234'); |
||
128 | |||
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)); |
||
138 | |||
139 | $this->handler->setFormatter($this->getIdentityFormatter()); |
||
140 | } |
||
141 | } |