|
7 | 7 | use Kirschbaum\Monitor\Facades\Monitor; |
8 | 8 |
|
9 | 9 | it('logs ccp start and end', function () { |
| 10 | + $this->setupLogMocking(); |
| 11 | + |
10 | 12 | Log::shouldReceive('info') |
11 | 13 | ->once() |
12 | 14 | ->withArgs(function ($message, $context) { |
|
29 | 31 | }); |
30 | 32 |
|
31 | 33 | it('logs ccp failure and throws', function () { |
| 34 | + $this->setupLogMocking(); |
| 35 | + |
32 | 36 | Config::set('app.debug', false); |
33 | 37 | Config::set('monitor.exception_trace.enabled', true); |
34 | 38 | Config::set('monitor.exception_trace.full_on_debug', false); |
|
52 | 56 | }); |
53 | 57 |
|
54 | 58 | it('executes onFail callback when CCP fails', function () { |
| 59 | + $this->setupLogMocking(); |
| 60 | + |
55 | 61 | Config::set('monitor.exception_trace.enabled', false); |
56 | 62 |
|
57 | 63 | $callbackExecuted = false; |
|
81 | 87 | }); |
82 | 88 |
|
83 | 89 | it('does not execute onFail callback when CCP succeeds', function () { |
| 90 | + $this->setupLogMocking(); |
| 91 | + |
84 | 92 | $callbackExecuted = false; |
85 | 93 |
|
86 | 94 | Log::shouldReceive('info')->twice(); // STARTED and ENDED |
|
96 | 104 | }); |
97 | 105 |
|
98 | 106 | it('handles onFail callback exceptions gracefully', function () { |
| 107 | + $this->setupLogMocking(); |
| 108 | + |
99 | 109 | Config::set('monitor.exception_trace.enabled', false); |
100 | 110 |
|
101 | 111 | Log::shouldReceive('info')->once(); // STARTED |
|
119 | 129 | }); |
120 | 130 |
|
121 | 131 | it('works without onFail callback (backward compatibility)', function () { |
| 132 | + $this->setupLogMocking(); |
| 133 | + |
122 | 134 | Log::shouldReceive('info')->twice(); // STARTED and ENDED |
123 | 135 |
|
124 | 136 | $result = Monitor::ccp('backward_compat_test', fn () => 'success', ['key' => 'value']); |
|
127 | 139 | }); |
128 | 140 |
|
129 | 141 | it('passes correct context data to onFail callback', function () { |
| 142 | + $this->setupLogMocking(); |
| 143 | + |
130 | 144 | Config::set('monitor.exception_trace.enabled', false); |
131 | 145 |
|
132 | 146 | $capturedContext = null; |
|
154 | 168 | }); |
155 | 169 |
|
156 | 170 | it('allows onFail callback to perform escalation actions', function () { |
| 171 | + $this->setupLogMocking(); |
| 172 | + |
157 | 173 | Config::set('monitor.exception_trace.enabled', false); |
158 | 174 |
|
159 | 175 | $alertSent = false; |
|
181 | 197 | }); |
182 | 198 |
|
183 | 199 | it('provides complete exception information to onFail callback', function () { |
| 200 | + $this->setupLogMocking(); |
| 201 | + |
184 | 202 | Config::set('monitor.exception_trace.enabled', true); |
185 | 203 | Config::set('app.debug', true); |
186 | 204 | Config::set('monitor.exception_trace.full_on_debug', true); |
|
211 | 229 | // NEW COMPREHENSIVE TESTS: |
212 | 230 |
|
213 | 231 | it('does not include exception trace when disabled', function () { |
| 232 | + $this->setupLogMocking(); |
| 233 | + |
214 | 234 | Config::set('monitor.exception_trace.enabled', false); // This tests line 67 |
215 | 235 |
|
216 | 236 | Log::shouldReceive('info')->once(); // STARTED log |
|
229 | 249 | }); |
230 | 250 |
|
231 | 251 | it('includes full trace when debug mode is enabled', function () { |
| 252 | + $this->setupLogMocking(); |
| 253 | + |
232 | 254 | Config::set('app.debug', true); |
233 | 255 | Config::set('monitor.exception_trace.enabled', true); |
234 | 256 | Config::set('monitor.exception_trace.full_on_debug', true); |
|
250 | 272 | }); |
251 | 273 |
|
252 | 274 | it('truncates trace when not in debug mode', function () { |
| 275 | + $this->setupLogMocking(); |
| 276 | + |
253 | 277 | Config::set('app.debug', false); |
254 | 278 | Config::set('monitor.exception_trace.enabled', true); |
255 | 279 | Config::set('monitor.exception_trace.full_on_debug', true); |
|
272 | 296 | }); |
273 | 297 |
|
274 | 298 | it('forces full trace when force_full_trace is enabled', function () { |
| 299 | + $this->setupLogMocking(); |
| 300 | + |
275 | 301 | Config::set('app.debug', false); // Debug off |
276 | 302 | Config::set('monitor.exception_trace.enabled', true); |
277 | 303 | Config::set('monitor.exception_trace.force_full_trace', true); // But force full trace |
|
294 | 320 | }); |
295 | 321 |
|
296 | 322 | it('includes custom context in both success and failure logs', function () { |
| 323 | + $this->setupLogMocking(); |
| 324 | + |
297 | 325 | $customContext = ['user_id' => 123, 'action' => 'test_action']; |
298 | 326 |
|
299 | 327 | Log::shouldReceive('info') |
|
319 | 347 | }); |
320 | 348 |
|
321 | 349 | it('preserves custom context in failure logs', function () { |
| 350 | + $this->setupLogMocking(); |
| 351 | + |
322 | 352 | $customContext = ['user_id' => 456, 'critical_action' => true]; |
323 | 353 |
|
324 | 354 | Config::set('monitor.exception_trace.enabled', false); // No exception details |
|
339 | 369 | }); |
340 | 370 |
|
341 | 371 | it('includes all required fields in exception trace', function () { |
| 372 | + $this->setupLogMocking(); |
| 373 | + |
342 | 374 | Config::set('monitor.exception_trace.enabled', true); |
343 | 375 |
|
344 | 376 | Log::shouldReceive('info')->once(); // STARTED log |
|
374 | 406 | // The logic should be: $isDebug = Config::boolean('app.debug') || Config::boolean('monitor.exception_trace.force_full_trace', false); |
375 | 407 | // If mutated to &&, this test will fail |
376 | 408 |
|
| 409 | + $this->setupLogMocking(); |
| 410 | + |
377 | 411 | Config::set('app.debug', false); // Debug is OFF |
378 | 412 | Config::set('monitor.exception_trace.enabled', true); |
379 | 413 | Config::set('monitor.exception_trace.force_full_trace', true); // But force is ON |
|
0 commit comments