1
|
|
|
<?php namespace Phprest\ErrorHandler\Handler; |
2
|
|
|
|
3
|
|
|
use ErrorException; |
4
|
|
|
use Exception; |
5
|
|
|
use Phprest\Exception\BadRequest; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
use Monolog\Handler\TestHandler; |
8
|
|
|
use Monolog\Logger; |
9
|
|
|
|
10
|
|
|
class LogTest extends TestCase |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var TestHandler |
14
|
|
|
*/ |
15
|
|
|
protected $monologHandler; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var Logger |
19
|
|
|
*/ |
20
|
|
|
protected $monolog; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var Log |
24
|
|
|
*/ |
25
|
|
|
protected $logHandler; |
26
|
|
|
|
27
|
|
|
public function setUp() |
28
|
|
|
{ |
29
|
|
|
$this->monologHandler = new TestHandler(); |
30
|
|
|
|
31
|
|
|
$this->monolog = new Logger('test', [$this->monologHandler]); |
32
|
|
|
|
33
|
|
|
$this->logHandler = new Log($this->monolog); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function testSimpleException(): void |
37
|
|
|
{ |
38
|
|
|
$this->assertFalse($this->monologHandler->hasCriticalRecords()); |
39
|
|
|
|
40
|
|
|
$this->logHandler->handle(new Exception('test exception')); |
41
|
|
|
|
42
|
|
|
$this->assertTrue($this->monologHandler->hasCriticalRecords()); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function testPhprestException(): void |
46
|
|
|
{ |
47
|
|
|
$this->assertFalse($this->monologHandler->hasCriticalRecords()); |
48
|
|
|
|
49
|
|
|
$this->logHandler->handle(new BadRequest(9, ['a detail'])); |
50
|
|
|
|
51
|
|
|
$this->assertTrue($this->monologHandler->hasCriticalRecords()); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testErrorExceptionErrorLog(): void |
55
|
|
|
{ |
56
|
|
|
$this->assertFalse($this->monologHandler->hasErrorRecords()); |
57
|
|
|
|
58
|
|
|
$this->logHandler->handle(new ErrorException('test exception', 0, E_ERROR)); |
59
|
|
|
|
60
|
|
|
$this->assertTrue($this->monologHandler->hasErrorRecords()); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testWarningExceptionErrorLog(): void |
64
|
|
|
{ |
65
|
|
|
$this->assertFalse($this->monologHandler->hasWarningRecords()); |
66
|
|
|
|
67
|
|
|
$this->logHandler->handle(new ErrorException('test exception', 0, E_WARNING)); |
68
|
|
|
|
69
|
|
|
$this->assertTrue($this->monologHandler->hasWarningRecords()); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testNoticeExceptionErrorLog(): void |
73
|
|
|
{ |
74
|
|
|
$this->assertFalse($this->monologHandler->hasNoticeRecords()); |
75
|
|
|
|
76
|
|
|
$this->logHandler->handle(new ErrorException('test exception', 0, E_NOTICE)); |
77
|
|
|
|
78
|
|
|
$this->assertTrue($this->monologHandler->hasNoticeRecords()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function testInfoExceptionErrorLog(): void |
82
|
|
|
{ |
83
|
|
|
$this->assertFalse($this->monologHandler->hasInfoRecords()); |
84
|
|
|
|
85
|
|
|
$this->logHandler->handle(new ErrorException('test exception', 0, E_STRICT)); |
86
|
|
|
|
87
|
|
|
$this->assertTrue($this->monologHandler->hasInfoRecords()); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|