scratch – Blame information for rev 120
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
120 | 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\Formatter\LineFormatter; |
||
15 | use Monolog\TestCase; |
||
16 | use Monolog\Logger; |
||
17 | |||
18 | class NewRelicHandlerTest extends TestCase |
||
19 | { |
||
20 | public static $appname; |
||
21 | public static $customParameters; |
||
22 | public static $transactionName; |
||
23 | |||
24 | public function setUp() |
||
25 | { |
||
26 | self::$appname = null; |
||
27 | self::$customParameters = array(); |
||
28 | self::$transactionName = null; |
||
29 | } |
||
30 | |||
31 | /** |
||
32 | * @expectedException Monolog\Handler\MissingExtensionException |
||
33 | */ |
||
34 | public function testThehandlerThrowsAnExceptionIfTheNRExtensionIsNotLoaded() |
||
35 | { |
||
36 | $handler = new StubNewRelicHandlerWithoutExtension(); |
||
37 | $handler->handle($this->getRecord(Logger::ERROR)); |
||
38 | } |
||
39 | |||
40 | public function testThehandlerCanHandleTheRecord() |
||
41 | { |
||
42 | $handler = new StubNewRelicHandler(); |
||
43 | $handler->handle($this->getRecord(Logger::ERROR)); |
||
44 | } |
||
45 | |||
46 | public function testThehandlerCanAddContextParamsToTheNewRelicTrace() |
||
47 | { |
||
48 | $handler = new StubNewRelicHandler(); |
||
49 | $handler->handle($this->getRecord(Logger::ERROR, 'log message', array('a' => 'b'))); |
||
50 | $this->assertEquals(array('context_a' => 'b'), self::$customParameters); |
||
51 | } |
||
52 | |||
53 | public function testThehandlerCanAddExplodedContextParamsToTheNewRelicTrace() |
||
54 | { |
||
55 | $handler = new StubNewRelicHandler(Logger::ERROR, true, self::$appname, true); |
||
56 | $handler->handle($this->getRecord( |
||
57 | Logger::ERROR, |
||
58 | 'log message', |
||
59 | array('a' => array('key1' => 'value1', 'key2' => 'value2')) |
||
60 | )); |
||
61 | $this->assertEquals( |
||
62 | array('context_a_key1' => 'value1', 'context_a_key2' => 'value2'), |
||
63 | self::$customParameters |
||
64 | ); |
||
65 | } |
||
66 | |||
67 | public function testThehandlerCanAddExtraParamsToTheNewRelicTrace() |
||
68 | { |
||
69 | $record = $this->getRecord(Logger::ERROR, 'log message'); |
||
70 | $record['extra'] = array('c' => 'd'); |
||
71 | |||
72 | $handler = new StubNewRelicHandler(); |
||
73 | $handler->handle($record); |
||
74 | |||
75 | $this->assertEquals(array('extra_c' => 'd'), self::$customParameters); |
||
76 | } |
||
77 | |||
78 | public function testThehandlerCanAddExplodedExtraParamsToTheNewRelicTrace() |
||
79 | { |
||
80 | $record = $this->getRecord(Logger::ERROR, 'log message'); |
||
81 | $record['extra'] = array('c' => array('key1' => 'value1', 'key2' => 'value2')); |
||
82 | |||
83 | $handler = new StubNewRelicHandler(Logger::ERROR, true, self::$appname, true); |
||
84 | $handler->handle($record); |
||
85 | |||
86 | $this->assertEquals( |
||
87 | array('extra_c_key1' => 'value1', 'extra_c_key2' => 'value2'), |
||
88 | self::$customParameters |
||
89 | ); |
||
90 | } |
||
91 | |||
92 | public function testThehandlerCanAddExtraContextAndParamsToTheNewRelicTrace() |
||
93 | { |
||
94 | $record = $this->getRecord(Logger::ERROR, 'log message', array('a' => 'b')); |
||
95 | $record['extra'] = array('c' => 'd'); |
||
96 | |||
97 | $handler = new StubNewRelicHandler(); |
||
98 | $handler->handle($record); |
||
99 | |||
100 | $expected = array( |
||
101 | 'context_a' => 'b', |
||
102 | 'extra_c' => 'd', |
||
103 | ); |
||
104 | |||
105 | $this->assertEquals($expected, self::$customParameters); |
||
106 | } |
||
107 | |||
108 | public function testThehandlerCanHandleTheRecordsFormattedUsingTheLineFormatter() |
||
109 | { |
||
110 | $handler = new StubNewRelicHandler(); |
||
111 | $handler->setFormatter(new LineFormatter()); |
||
112 | $handler->handle($this->getRecord(Logger::ERROR)); |
||
113 | } |
||
114 | |||
115 | public function testTheAppNameIsNullByDefault() |
||
116 | { |
||
117 | $handler = new StubNewRelicHandler(); |
||
118 | $handler->handle($this->getRecord(Logger::ERROR, 'log message')); |
||
119 | |||
120 | $this->assertEquals(null, self::$appname); |
||
121 | } |
||
122 | |||
123 | public function testTheAppNameCanBeInjectedFromtheConstructor() |
||
124 | { |
||
125 | $handler = new StubNewRelicHandler(Logger::DEBUG, false, 'myAppName'); |
||
126 | $handler->handle($this->getRecord(Logger::ERROR, 'log message')); |
||
127 | |||
128 | $this->assertEquals('myAppName', self::$appname); |
||
129 | } |
||
130 | |||
131 | public function testTheAppNameCanBeOverriddenFromEachLog() |
||
132 | { |
||
133 | $handler = new StubNewRelicHandler(Logger::DEBUG, false, 'myAppName'); |
||
134 | $handler->handle($this->getRecord(Logger::ERROR, 'log message', array('appname' => 'logAppName'))); |
||
135 | |||
136 | $this->assertEquals('logAppName', self::$appname); |
||
137 | } |
||
138 | |||
139 | public function testTheTransactionNameIsNullByDefault() |
||
140 | { |
||
141 | $handler = new StubNewRelicHandler(); |
||
142 | $handler->handle($this->getRecord(Logger::ERROR, 'log message')); |
||
143 | |||
144 | $this->assertEquals(null, self::$transactionName); |
||
145 | } |
||
146 | |||
147 | public function testTheTransactionNameCanBeInjectedFromTheConstructor() |
||
148 | { |
||
149 | $handler = new StubNewRelicHandler(Logger::DEBUG, false, null, false, 'myTransaction'); |
||
150 | $handler->handle($this->getRecord(Logger::ERROR, 'log message')); |
||
151 | |||
152 | $this->assertEquals('myTransaction', self::$transactionName); |
||
153 | } |
||
154 | |||
155 | public function testTheTransactionNameCanBeOverriddenFromEachLog() |
||
156 | { |
||
157 | $handler = new StubNewRelicHandler(Logger::DEBUG, false, null, false, 'myTransaction'); |
||
158 | $handler->handle($this->getRecord(Logger::ERROR, 'log message', array('transaction_name' => 'logTransactName'))); |
||
159 | |||
160 | $this->assertEquals('logTransactName', self::$transactionName); |
||
161 | } |
||
162 | } |
||
163 | |||
164 | class StubNewRelicHandlerWithoutExtension extends NewRelicHandler |
||
165 | { |
||
166 | protected function isNewRelicEnabled() |
||
167 | { |
||
168 | return false; |
||
169 | } |
||
170 | } |
||
171 | |||
172 | class StubNewRelicHandler extends NewRelicHandler |
||
173 | { |
||
174 | protected function isNewRelicEnabled() |
||
175 | { |
||
176 | return true; |
||
177 | } |
||
178 | } |
||
179 | |||
180 | function newrelic_notice_error() |
||
181 | { |
||
182 | return true; |
||
183 | } |
||
184 | |||
185 | function newrelic_set_appname($appname) |
||
186 | { |
||
187 | return NewRelicHandlerTest::$appname = $appname; |
||
188 | } |
||
189 | |||
190 | function newrelic_name_transaction($transactionName) |
||
191 | { |
||
192 | return NewRelicHandlerTest::$transactionName = $transactionName; |
||
193 | } |
||
194 | |||
195 | function newrelic_add_custom_parameter($key, $value) |
||
196 | { |
||
197 | NewRelicHandlerTest::$customParameters[$key] = $value; |
||
198 | |||
199 | return true; |
||
200 | } |