GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#9)
by Helpful
09:13
created

LoggerBridgeTest   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 844
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 14
Bugs 5 Features 3
Metric Value
wmc 30
c 14
b 5
f 3
lcom 1
cbo 1
dl 0
loc 844
rs 9.7391

28 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A tearDown() 0 6 1
A getLoggerStub() 0 4 1
A getLoggerBridge() 0 16 3
A getMockWithoutConstructor() 0 10 1
A testGetErrorReporter() 0 7 1
A testEnvReporter() 0 7 1
A testSetReserveMemory() 0 12 1
A testRegisterGlobalHandlersGeneral() 0 15 1
A testRegisterGlobalHandlersOnce() 0 23 1
B testRegisterGlobalHandlersMultiple() 0 26 1
B testRegisterGlobalHandlersAttached() 0 30 1
A testRegisterGlobalHandlersIsSuhosinRelevant() 0 22 1
B testErrorHandlerError() 0 41 1
B testErrorHandlerWarning() 0 38 1
B testErrorHandlerEnvironmentReporting() 0 44 1
B testErrorHandlerSuppression() 0 34 1
B testErrorHandlerNotLiveNoDisplay() 0 34 1
B testFatalErrorHandlerNotRegistered() 0 35 1
B testFatalErrorHandlerNotFatal() 0 40 1
A testFatalErrorHandlerNoReport() 0 58 1
A testFatalErrorHandlerReportDueToLive() 0 58 1
A testFatalErrorHandlerReport() 0 53 1
B testFatalErrorHandlerPreserveMemory() 0 41 1
B testExceptionHandler() 0 35 1
B testExceptionHandlerNoReport() 0 25 1
B testPreRequest() 0 27 1
B testPostRequest() 0 27 1
1
<?php
2
3
namespace Camspiers\LoggerBridge;
4
5
/**
6
 * Class LoggerBridgeTest
7
 */
8
class LoggerBridgeTest extends \PHPUnit_Framework_TestCase
9
{
10
    /**
11
     * Set up the error_reporting before each tests
12
     */
13
    public function setUp()
14
    {
15
        error_reporting(E_ALL);
16
    }
17
    /**
18
     *
19
     */
20
    public function tearDown()
21
    {
22
        ini_set('display_errors', true);
23
        restore_error_handler();
24
        restore_exception_handler();
25
    }
26
    /**
27
     * Helper method to return a stub of a logger interface
28
     * @return \Psr\Log\LoggerInterface
29
     */
30
    public function getLoggerStub()
31
    {
32
        return $this->getMock('Psr\Log\LoggerInterface');
33
    }
34
    /**
35
     * Helper method to return a logger bridge
36
     * @param  null                     $methods
37
     * @param  \Psr\Log\LoggerInterface $logger
0 ignored issues
show
Documentation introduced by
Should the type for parameter $logger not be \Psr\Log\LoggerInterface|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
38
     * @param  bool                     $reportErrorsWhenNotLive
39
     * @param  null                     $reserveMemory
40
     * @return LoggerBridge
41
     */
42
    public function getLoggerBridge(
43
        $methods = null,
44
        $logger = null,
45
        $reportErrorsWhenNotLive = true,
46
        $reserveMemory = null
47
    ) {
48
        return $this->getMock(
49
            __NAMESPACE__ . '\\LoggerBridge',
50
            is_array($methods) ? array_merge(array('terminate'), $methods) : array('terminate'),
51
            array(
52
                $logger ? : $this->getLoggerStub(),
53
                $reportErrorsWhenNotLive,
54
                $reserveMemory
55
            )
56
        );
57
    }
58
    /**
59
     * A helper method to get a mock that doesn't call its constructor
60
     * @param $class
61
     * @return \PHPUnit_Framework_MockObject_MockObject
62
     */
63
    protected function getMockWithoutConstructor($class)
64
    {
65
        return $this->getMock(
66
            $class,
67
            array(),
68
            array(),
69
            '',
70
            false
71
        );
72
    }
73
    /**
74
     * Test that there the default error reporter returns correctly
75
     */
76
    public function testGetErrorReporter()
77
    {
78
        $this->assertInstanceOf(
79
            __NAMESPACE__ . '\\ErrorReporter\\DebugErrorReporter',
80
            $this->getLoggerBridge()->getErrorReporter()
81
        );
82
    }
83
    /**
84
     * Test that there the default env reporter returns correctly
85
     */
86
    public function testEnvReporter()
87
    {
88
        $this->assertInstanceOf(
89
            __NAMESPACE__ . '\\EnvReporter\\DirectorEnvReporter',
90
            $this->getLoggerBridge()->getEnvReporter()
91
        );
92
    }
93
    /**
94
     * Tests that the translation of the reserve memory occurs correctly
95
     */
96
    public function testSetReserveMemory()
97
    {
98
        $bridge = $this->getLoggerBridge();
99
        $bridge->setReserveMemory(1024);
100
        $this->assertEquals(1024, $bridge->getReserveMemory());
101
        $bridge->setReserveMemory('2K');
102
        $this->assertEquals(2 * 1024, $bridge->getReserveMemory());
103
        $bridge->setReserveMemory('2M');
104
        $this->assertEquals(2 * 1024 * 1024, $bridge->getReserveMemory());
105
        $bridge->setReserveMemory('2G');
106
        $this->assertEquals(2 * 1024 * 1024 * 1024, $bridge->getReserveMemory());
107
    }
108
    /**
109
     * Tests the general function of register
110
     */
111
    public function testRegisterGlobalHandlersGeneral()
112
    {
113
        $env = $this->getMock(__NAMESPACE__ . '\\EnvReporter\\EnvReporter', array('isLive'));
114
        $bridge = $this->getLoggerBridge();
115
        $bridge->setEnvReporter($env);
116
117
        // Check that things aren't registered
118
        $this->assertNull($bridge->isRegistered());
119
120
        // Do the registration
121
        $bridge->registerGlobalHandlers();
122
123
        // Check that things registered
124
        $this->assertTrue($bridge->isRegistered());
125
    }
126
    /**
127
     * Test that the actual error handlers are registered once
128
     */
129
    public function testRegisterGlobalHandlersOnce()
130
    {
131
        $env = $this->getMock(__NAMESPACE__ . '\\EnvReporter\\EnvReporter', array('isLive'));
132
        $bridge = $this->getLoggerBridge(
133
            array(
134
                'registerErrorHandler',
135
                'registerExceptionHandler',
136
                'registerFatalErrorHandler'
137
            )
138
        );
139
        $bridge->setEnvReporter($env);
140
141
        $bridge->expects($this->once())
142
            ->method('registerErrorHandler');
143
144
        $bridge->expects($this->once())
145
            ->method('registerExceptionHandler');
146
147
        $bridge->expects($this->once())
148
            ->method('registerFatalErrorHandler');
149
150
        $bridge->registerGlobalHandlers();
151
    }
152
    /**
153
     * Test that the actual error handlers are registered the correct number of times
154
     */
155
    public function testRegisterGlobalHandlersMultiple()
156
    {
157
        $env = $this->getMock(__NAMESPACE__ . '\\EnvReporter\\EnvReporter', array('isLive'));
158
        $bridge = $this->getLoggerBridge(
159
            array(
160
                'registerErrorHandler',
161
                'registerExceptionHandler',
162
                'registerFatalErrorHandler'
163
            )
164
        );
165
        $bridge->setEnvReporter($env);
166
167
        $bridge->expects($this->exactly(2))
168
            ->method('registerErrorHandler');
169
170
        $bridge->expects($this->exactly(2))
171
            ->method('registerExceptionHandler');
172
173
        // We only expect the fatal handler to be registered once
174
        $bridge->expects($this->once())
175
            ->method('registerFatalErrorHandler');
176
177
        $bridge->registerGlobalHandlers();
178
        $bridge->deregisterGlobalHandlers();
179
        $bridge->registerGlobalHandlers();
180
    }
181
    /**
182
     * Test that the actual error handlers are attached
183
     */
184
    public function testRegisterGlobalHandlersAttached()
185
    {
186
        $env = $this->getMock(__NAMESPACE__ . '\\EnvReporter\\EnvReporter', array('isLive'));
187
        $bridge = $this->getLoggerBridge();
188
        $bridge->setEnvReporter($env);
189
190
        $bridge->registerGlobalHandlers();
191
192
        $this->assertEquals(
193
            array(
194
                $bridge,
195
                'errorHandler'
196
            ),
197
            set_error_handler(
198
                function () {
199
                }
200
            )
201
        );
202
203
        $this->assertEquals(
204
            array(
205
                $bridge,
206
                'exceptionHandler'
207
            ),
208
            set_exception_handler(
209
                function () {
210
                }
211
            )
212
        );
213
    }
214
    /**
215
     * Test that memory is reserved
216
     */
217
    public function testRegisterGlobalHandlersIsSuhosinRelevant()
218
    {
219
        $env = $this->getMock(__NAMESPACE__ . '\\EnvReporter\\EnvReporter', array('isLive'));
220
221
        $bridge = $this->getLoggerBridge(
222
            array(
223
                'isSuhosinRelevant',
224
                'ensureSuhosinMemory'
225
            )
226
        );
227
228
        $bridge->expects($this->any())
229
            ->method('isSuhosinRelevant')
230
            ->will($this->returnValue(true));
231
232
        $bridge->expects($this->once())
233
            ->method('ensureSuhosinMemory');
234
235
        $bridge->setEnvReporter($env);
236
237
        $bridge->registerGlobalHandlers();
238
    }
239
    /**
240
     *
241
     */
242
    public function testErrorHandlerError()
243
    {
244
        $bridge = $this->getLoggerBridge(
245
            null,
246
            $logger = $this->getLoggerStub()
247
        );
248
249
        $bridge->setEnvReporter(
250
            $env = $this->getMock(__NAMESPACE__ . '\\EnvReporter\\EnvReporter')
251
        );
252
253
        // isLive won't be called because reportErrorsWhenNotLive is true
254
        $env->expects($this->never())
255
            ->method('isLive')
256
            ->will($this->returnValue(true));
257
258
        $bridge->setErrorReporter(
259
            $reporter = $this->getMock(__NAMESPACE__ . '\\ErrorReporter\\ErrorReporter')
260
        );
261
262
        $reporter->expects($this->once())
263
            ->method('reportError');
264
265
        $logger->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Psr\Log\LoggerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
266
            ->method('error')
267
            ->with(
268
                $errstr = 'error string',
269
                $this->logicalAnd(
270
                    $this->contains($errfile = 'somefile'),
271
                    $this->contains($errline = 0),
272
                    $this->contains('')
273
                )
274
            );
275
276
        $bridge->errorHandler(
277
            E_USER_ERROR,
278
            $errstr,
279
            $errfile,
280
            $errline
281
        );
282
    }
283
    /**
284
     * Tests how the error handler calls warning errors
285
     */
286
    public function testErrorHandlerWarning()
287
    {
288
        $bridge = $this->getLoggerBridge(
289
            null,
290
            $logger = $this->getLoggerStub()
291
        );
292
293
        $bridge->setEnvReporter(
294
            $env = $this->getMock(__NAMESPACE__ . '\\EnvReporter\\EnvReporter')
295
        );
296
297
        // isLive won't be called because reportErrorsWhenNotLive is true
298
        $env->expects($this->never())
299
            ->method('isLive')
300
            ->will($this->returnValue(true));
301
302
        $bridge->setErrorReporter(
303
            $reporter = $this->getMock(__NAMESPACE__ . '\\ErrorReporter\\ErrorReporter')
304
        );
305
306
        $logger->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Psr\Log\LoggerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
307
            ->method('warning')
308
            ->with(
309
                $errstr = 'error string',
310
                $this->logicalAnd(
311
                    $this->contains($errfile = 'somefile'),
312
                    $this->contains($errline = 0),
313
                    $this->contains('')
314
                )
315
            );
316
317
        $bridge->errorHandler(
318
            E_USER_WARNING,
319
            $errstr,
320
            $errfile,
321
            $errline
322
        );
323
    }
324
    /**
325
     * Tests error_reporting level
326
     */
327
    public function testErrorHandlerEnvironmentReporting()
328
    {
329
        // Set error reporting type
330
        error_reporting(E_ALL & ~E_USER_WARNING);
331
332
        $bridge = $this->getLoggerBridge(
333
            null,
334
            $logger = $this->getLoggerStub()
335
        );
336
337
        $bridge->setEnvReporter(
338
            $env = $this->getMock(__NAMESPACE__ . '\\EnvReporter\\EnvReporter')
339
        );
340
341
        $env->expects($this->never())
342
            ->method('isLive');
343
344
        $bridge->setErrorReporter(
345
            $reporter = $this->getMock(__NAMESPACE__ . '\\ErrorReporter\\ErrorReporter')
346
        );
347
348
        // We expect the report to never happen because the environment reporting is
349
        // E_ALL & ~E_USER_WARNING
350
        $reporter->expects($this->never())
351
            ->method('reportError');
352
353
        $logger->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Psr\Log\LoggerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
354
            ->method('warning')
355
            ->with(
356
                $errstr = 'error string',
357
                $this->logicalAnd(
358
                    $this->contains($errfile = 'somefile'),
359
                    $this->contains($errline = 0),
360
                    $this->contains('')
361
                )
362
            );
363
364
        $bridge->errorHandler(
365
            E_USER_WARNING,
366
            $errstr,
367
            $errfile,
368
            $errline
369
        );
370
    }
371
    public function testErrorHandlerSuppression()
372
    {
373
        // Set error reporting type
374
        error_reporting(0);
375
376
        $bridge = $this->getLoggerBridge(
377
            null,
378
            $logger = $this->getLoggerStub()
379
        );
380
381
        $bridge->setEnvReporter(
382
            $env = $this->getMock(__NAMESPACE__ . '\\EnvReporter\\EnvReporter')
383
        );
384
385
        $env->expects($this->never())
386
            ->method('isLive');
387
388
        $bridge->setErrorReporter(
389
            $reporter = $this->getMock(__NAMESPACE__ . '\\ErrorReporter\\ErrorReporter')
390
        );
391
392
        $reporter->expects($this->never())
393
            ->method('reportError');
394
395
        $logger->expects($this->never())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Psr\Log\LoggerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
396
            ->method('warning');
397
398
        $bridge->errorHandler(
399
            E_USER_WARNING,
400
            '',
401
            'fake/file/path',
402
            1
403
        );
404
    }
405
    /**
406
     * Tests error reporting we not live and we request no display
407
     */
408
    public function testErrorHandlerNotLiveNoDisplay()
409
    {
410
        $bridge = $this->getLoggerBridge(
411
            null,
412
            $logger = $this->getLoggerStub(),
413
            false
414
        );
415
416
        $bridge->setEnvReporter(
417
            $env = $this->getMock(__NAMESPACE__ . '\\EnvReporter\\EnvReporter')
418
        );
419
420
        $env->expects($this->once())
421
            ->method('isLive')
422
            ->will($this->returnValue(false));
423
424
        $bridge->setErrorReporter(
425
            $reporter = $this->getMock(__NAMESPACE__ . '\\ErrorReporter\\ErrorReporter')
426
        );
427
428
        // We expect the report to never happen because we are not live && reportErrorsWhenNotLive is false
429
        $reporter->expects($this->never())
430
            ->method('reportError');
431
432
        $logger->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Psr\Log\LoggerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
433
            ->method('error');
434
435
        $bridge->errorHandler(
436
            E_USER_ERROR,
437
            '',
438
            'fake/file/path',
439
            1
440
        );
441
    }
442
443
    public function testFatalErrorHandlerNotRegistered()
444
    {
445
        $bridge = $this->getLoggerBridge(
446
            array(
447
                'isRegistered',
448
                'restoreMemory'
449
            ),
450
            $logger = $this->getLoggerStub()
451
        );
452
453
        $bridge->setEnvReporter(
454
            $env = $this->getMock(__NAMESPACE__ . '\\EnvReporter\\EnvReporter')
455
        );
456
457
        $env->expects($this->never())
458
            ->method('isLive');
459
460
        $bridge->setErrorReporter(
461
            $reporter = $this->getMock(__NAMESPACE__ . '\\ErrorReporter\\ErrorReporter')
462
        );
463
464
        $bridge->expects($this->once())
465
            ->method('isRegistered')
466
            ->will($this->returnValue(false));
467
468
        // We expect the log to never happen
469
        $logger->expects($this->never())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Psr\Log\LoggerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
470
            ->method('critical');
471
472
        // We expect the report to never happen
473
        $reporter->expects($this->never())
474
            ->method('reportError');
475
476
        $bridge->fatalHandler();
477
    }
478
479
    public function testFatalErrorHandlerNotFatal()
480
    {
481
        $bridge = $this->getLoggerBridge(
482
            array(
483
                'isRegistered',
484
                'getLastError',
485
                'restoreMemory'
486
            ),
487
            $logger = $this->getLoggerStub()
488
        );
489
490
        $bridge->setEnvReporter(
491
            $env = $this->getMock(__NAMESPACE__ . '\\EnvReporter\\EnvReporter')
492
        );
493
494
        $env->expects($this->never())
495
            ->method('isLive');
496
497
        $bridge->setErrorReporter(
498
            $reporter = $this->getMock(__NAMESPACE__ . '\\ErrorReporter\\ErrorReporter')
499
        );
500
501
        $bridge->expects($this->once())
502
            ->method('isRegistered')
503
            ->will($this->returnValue(true));
504
505
        $bridge->expects($this->once())
506
            ->method('getLastError')
507
            ->will($this->returnValue(false));
508
509
        // We expect the log to never happen
510
        $logger->expects($this->never())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Psr\Log\LoggerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
511
            ->method('critical');
512
513
        // We expect the report to never happen
514
        $reporter->expects($this->never())
515
            ->method('reportError');
516
517
        $bridge->fatalHandler();
518
    }
519
520
    public function testFatalErrorHandlerNoReport()
521
    {
522
        $bridge = $this->getLoggerBridge(
523
            array(
524
                'isRegistered',
525
                'getLastError',
526
                'restoreMemory'
527
            ),
528
            $logger = $this->getLoggerStub(),
529
            false
530
        );
531
532
        $bridge->setEnvReporter(
533
            $env = $this->getMock(__NAMESPACE__ . '\\EnvReporter\\EnvReporter')
534
        );
535
536
        $env->expects($this->once())
537
            ->method('isLive')
538
            ->will($this->returnValue(false));
539
540
        $bridge->setErrorReporter(
541
            $reporter = $this->getMock(__NAMESPACE__ . '\\ErrorReporter\\ErrorReporter')
542
        );
543
544
        $bridge->expects($this->once())
545
            ->method('isRegistered')
546
            ->will($this->returnValue(true));
547
548
        $bridge->expects($this->once())
549
            ->method('getLastError')
550
            ->will(
551
                $this->returnValue(
552
                    array(
553
                        'type'    => E_CORE_ERROR,
554
                        'message' => 'Test',
555
                        'file'    => 'file',
556
                        'line'    => 0
557
                    )
558
                )
559
            );
560
561
        // We expect the log to never happen
562
        $logger->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Psr\Log\LoggerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
563
            ->method('critical')
564
            ->with(
565
                'Test',
566
                $this->logicalAnd(
567
                    $this->contains('file'),
568
                    $this->contains('line')
569
                )
570
            );
571
572
        // We expect the report to never happen
573
        $reporter->expects($this->never())
574
            ->method('reportError');
575
576
        $bridge->fatalHandler();
577
    }
578
579
    public function testFatalErrorHandlerReportDueToLive()
580
    {
581
        $bridge = $this->getLoggerBridge(
582
            array(
583
                'isRegistered',
584
                'getLastError',
585
                'restoreMemory'
586
            ),
587
            $logger = $this->getLoggerStub(),
588
            false
589
        );
590
591
        $bridge->setEnvReporter(
592
            $env = $this->getMock(__NAMESPACE__ . '\\EnvReporter\\EnvReporter')
593
        );
594
595
        $env->expects($this->once())
596
            ->method('isLive')
597
            ->will($this->returnValue(true));
598
599
        $bridge->setErrorReporter(
600
            $reporter = $this->getMock(__NAMESPACE__ . '\\ErrorReporter\\ErrorReporter')
601
        );
602
603
        $bridge->expects($this->once())
604
            ->method('isRegistered')
605
            ->will($this->returnValue(true));
606
607
        $bridge->expects($this->once())
608
            ->method('getLastError')
609
            ->will(
610
                $this->returnValue(
611
                    array(
612
                        'type'    => E_ERROR,
613
                        'message' => 'Test',
614
                        'file'    => 'file',
615
                        'line'    => 0
616
                    )
617
                )
618
            );
619
620
        // We expect the log to never happen
621
        $logger->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Psr\Log\LoggerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
622
            ->method('critical')
623
            ->with(
624
                'Test',
625
                $this->logicalAnd(
626
                    $this->contains('file'),
627
                    $this->contains('line')
628
                )
629
            );
630
631
        // We expect the report to never happen
632
        $reporter->expects($this->once())
633
            ->method('reportError');
634
635
        $bridge->fatalHandler();
636
    }
637
638
    public function testFatalErrorHandlerReport()
639
    {
640
        $bridge = $this->getLoggerBridge(
641
            array(
642
                'isRegistered',
643
                'getLastError',
644
                'restoreMemory'
645
            ),
646
            $logger = $this->getLoggerStub()
647
        );
648
649
        $bridge->setEnvReporter(
650
            $env = $this->getMock(__NAMESPACE__ . '\\EnvReporter\\EnvReporter')
651
        );
652
653
        $env->expects($this->never())
654
            ->method('isLive');
655
656
        $bridge->setErrorReporter(
657
            $reporter = $this->getMock(__NAMESPACE__ . '\\ErrorReporter\\ErrorReporter')
658
        );
659
660
        $bridge->expects($this->once())
661
            ->method('isRegistered')
662
            ->will($this->returnValue(true));
663
664
        $bridge->expects($this->once())
665
            ->method('getLastError')
666
            ->will(
667
                $this->returnValue(
668
                    array(
669
                        'type'    => E_ERROR,
670
                        'message' => 'Test',
671
                        'file'    => 'file',
672
                        'line'    => 0
673
                    )
674
                )
675
            );
676
677
        // We expect the log to happen once
678
        $logger->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Psr\Log\LoggerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
679
            ->method('critical')
680
            ->with(
681
                'Test',
682
                $this->logicalAnd($this->contains('file'), $this->contains('line'))
683
            );
684
685
        // We expect the report to never happen
686
        $reporter->expects($this->once())
687
            ->method('reportError');
688
689
        $bridge->fatalHandler();
690
    }
691
692
    public function testFatalErrorHandlerPreserveMemory()
693
    {
694
        $bridge = $this->getLoggerBridge(
695
            array(
696
                'isRegistered',
697
                'getLastError',
698
                'changeMemoryLimit'
699
            )
700
        );
701
702
        $bridge->setEnvReporter(
703
            $env = $this->getMock(__NAMESPACE__ . '\\EnvReporter\\EnvReporter')
704
        );
705
706
        $bridge->setErrorReporter(
707
            $reporter = $this->getMock(__NAMESPACE__ . '\\ErrorReporter\\ErrorReporter')
708
        );
709
710
        $bridge->expects($this->once())
711
            ->method('isRegistered')
712
            ->will($this->returnValue(true));
713
714
        $bridge->expects($this->once())
715
            ->method('getLastError')
716
            ->will(
717
                $this->returnValue(
718
                    array(
719
                        'type'    => E_ERROR,
720
                        'message' => 'memory exhausted',
721
                        'file'    => 'file',
722
                        'line'    => 0
723
                    )
724
                )
725
            );
726
727
        $bridge->expects($this->once())
728
            ->method('changeMemoryLimit')
729
            ->with($bridge->getReserveMemory());
730
731
        $bridge->fatalHandler();
732
    }
733
734
    public function testExceptionHandler()
735
    {
736
        $bridge = $this->getLoggerBridge(
737
            null,
738
            $logger = $this->getLoggerStub()
739
        );
740
741
        $bridge->setEnvReporter(
742
            $env = $this->getMock(__NAMESPACE__ . '\\EnvReporter\\EnvReporter')
743
        );
744
745
        $env->expects($this->never())
746
            ->method('isLive');
747
748
        $bridge->setErrorReporter(
749
            $reporter = $this->getMock(__NAMESPACE__ . '\\ErrorReporter\\ErrorReporter')
750
        );
751
752
        $reporter->expects($this->once())
753
            ->method('reportError');
754
755
        $exception = new \Exception('Message');
756
757
        $logger->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Psr\Log\LoggerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
758
            ->method('error')
759
            ->with(
760
                'Uncaught Exception: Message',
761
                $this->logicalAnd(
762
                    $this->contains($exception->getFile()),
763
                    $this->contains($exception->getLine())
764
                )
765
            );
766
767
        $bridge->exceptionHandler($exception);
768
    }
769
770
    public function testExceptionHandlerNoReport()
771
    {
772
        $bridge = $this->getLoggerBridge(
773
            null,
774
            null,
775
            false
776
        );
777
778
        $bridge->setEnvReporter(
779
            $env = $this->getMock(__NAMESPACE__ . '\\EnvReporter\\EnvReporter')
780
        );
781
782
        $env->expects($this->once())
783
            ->method('isLive')
784
            ->will($this->returnValue(false));
785
786
        $bridge->setErrorReporter(
787
            $reporter = $this->getMock(__NAMESPACE__ . '\\ErrorReporter\\ErrorReporter')
788
        );
789
790
        $reporter->expects($this->never())
791
            ->method('reportError');
792
793
        $bridge->exceptionHandler(new \Exception('Message'));
794
    }
795
796
    public function testPreRequest()
797
    {
798
        $bridge = $this->getLoggerBridge(
799
            array(
800
                'registerGlobalHandlers'
801
            )
802
        );
803
804
        $bridge->setEnvReporter(
805
            $this->getMock(__NAMESPACE__ . '\\EnvReporter\\EnvReporter')
806
        );
807
808
        $bridge->setErrorReporter(
809
            $this->getMock(__NAMESPACE__ . '\\ErrorReporter\\ErrorReporter')
810
        );
811
812
        $bridge->expects($this->once())
813
            ->method('registerGlobalHandlers');
814
815
        $this->assertTrue(
816
            $bridge->preRequest(
817
                $this->getMockWithoutConstructor('SS_HTTPRequest'),
818
                $this->getMockWithoutConstructor('Session'),
819
                $this->getMockWithoutConstructor('DataModel')
820
            )
821
        );
822
    }
823
824
    public function testPostRequest()
825
    {
826
        $bridge = $this->getLoggerBridge(
827
            array(
828
                'deregisterGlobalHandlers'
829
            )
830
        );
831
832
        $bridge->setEnvReporter(
833
            $this->getMock(__NAMESPACE__ . '\\EnvReporter\\EnvReporter')
834
        );
835
836
        $bridge->setErrorReporter(
837
            $this->getMock(__NAMESPACE__ . '\\ErrorReporter\\ErrorReporter')
838
        );
839
840
        $bridge->expects($this->once())
841
            ->method('deregisterGlobalHandlers');
842
843
        $this->assertTrue(
844
            $bridge->postRequest(
845
                $this->getMockWithoutConstructor('SS_HTTPRequest'),
846
                $this->getMockWithoutConstructor('SS_HTTPResponse'),
847
                $this->getMockWithoutConstructor('DataModel')
848
            )
849
        );
850
    }
851
}
852