-
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathErrorHandlerTest.php
More file actions
160 lines (130 loc) · 4.73 KB
/
ErrorHandlerTest.php
File metadata and controls
160 lines (130 loc) · 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
declare(strict_types=1);
namespace Yiisoft\ErrorHandler\Tests;
use PHPUnit\Framework\Attributes\WithoutErrorHandler;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use RuntimeException;
use Throwable;
use Yiisoft\ErrorHandler\ErrorHandler;
use Yiisoft\ErrorHandler\Exception\ErrorException;
use Yiisoft\ErrorHandler\ThrowableRendererInterface;
final class ErrorHandlerTest extends TestCase
{
private ErrorHandler $errorHandler;
private LoggerInterface $loggerMock;
private ThrowableRendererInterface $throwableRendererMock;
protected function setUp(): void
{
$this->loggerMock = $this->createMock(LoggerInterface::class);
$this->throwableRendererMock = $this->createMock(ThrowableRendererInterface::class);
$this->errorHandler = new ErrorHandler($this->throwableRendererMock, $this->loggerMock);
$this->errorHandler->memoryReserveSize(0);
}
public function testHandleThrowableCallsDefaultRendererWithoutLogger(): void
{
$errorHandler = new ErrorHandler($this->throwableRendererMock);
$errorHandler->memoryReserveSize(0);
$throwable = new RuntimeException();
$this
->throwableRendererMock
->expects($this->once())
->method('render')
->with($throwable);
$errorHandler->handle($throwable);
}
public function testHandleThrowableCallsDefaultRendererWhenNonePassed(): void
{
$throwable = new RuntimeException();
$this
->throwableRendererMock
->expects($this->once())
->method('render')
->with($throwable);
$this->errorHandler->handle($throwable);
}
public function testHandleThrowableCallsPassedRenderer(): void
{
$throwable = new RuntimeException();
$throwableRendererMock = $this->createMock(ThrowableRendererInterface::class);
$this
->throwableRendererMock
->expects($this->never())
->method('render')
->with($throwable);
$throwableRendererMock
->expects($this->once())
->method('render')
->with($throwable);
$this->errorHandler->handle($throwable, $throwableRendererMock);
}
public function testHandleThrowableWithExposedDetailsCallsRenderVerbose(): void
{
$throwable = new RuntimeException();
$this
->throwableRendererMock
->expects($this->once())
->method('renderVerbose')
->with($throwable);
$this->errorHandler->debug();
$this->errorHandler->handle($throwable);
}
public function testHandleThrowableWithoutExposedDetailsCallsRender(): void
{
$throwable = new RuntimeException();
$this
->throwableRendererMock
->expects($this->once())
->method('render')
->with($throwable);
$this->errorHandler->debug(false);
$this->errorHandler->handle($throwable);
}
#[WithoutErrorHandler]
public function testHandleError(): void
{
$array = [];
$this->errorHandler->register();
$this->expectException(ErrorException::class);
$array['undefined'];
$this->errorHandler->unregister();
}
#[WithoutErrorHandler]
public function testHandleErrorWithCatching(): void
{
$this->errorHandler->register();
$array = ['type' => 'undefined'];
$exception = null;
try {
$array['undefined'];
} catch (Throwable $exception) {
}
$this->assertInstanceOf(ErrorException::class, $exception);
$this->assertFalse($exception::isFatalError($array));
$this->assertNull($exception->getSolution());
$backtrace = $exception->getBacktrace();
$this->assertNotEmpty($backtrace);
$this->assertSame(__FILE__, $backtrace[0]['file']);
$this->assertSame(['file', 'line'], array_keys($backtrace[0]));
$this->errorHandler->unregister();
}
#[WithoutErrorHandler]
public function testHandleTriggerErrorWithCatching(): void
{
$this->errorHandler->register();
$array = ['type' => 'undefined'];
$exception = null;
try {
trigger_error('test-error');
} catch (Throwable $exception) {
}
$this->assertInstanceOf(ErrorException::class, $exception);
$this->assertFalse($exception::isFatalError($array));
$this->assertNull($exception->getSolution());
$backtrace = $exception->getBacktrace();
$this->assertNotEmpty($backtrace);
$this->assertArrayHasKey('file', $backtrace[0]);
$this->assertSame(__FILE__, $backtrace[0]['file']);
$this->errorHandler->unregister();
}
}