-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExceptionTest.php
More file actions
199 lines (169 loc) · 5.02 KB
/
ExceptionTest.php
File metadata and controls
199 lines (169 loc) · 5.02 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
/**
* @author : Jakiboy
* @package : FloatPHP
* @subpackage : Classes Filesystem Component Tests
* @version : 1.5.x
* @copyright : (c) 2018 - 2025 Jihad Sinnaour <me@jihadsinnaour.com>
* @link : https://floatphp.com
* @license : MIT
*
* This file if a part of FloatPHP Framework.
*/
declare(strict_types=1);
namespace FloatPHP\Tests\Classes\Filesystem;
use PHPUnit\Framework\TestCase;
use FloatPHP\Classes\Filesystem\Exception;
/**
* Exception class tests.
*/
final class ExceptionTest extends TestCase
{
/**
* Test shutdown handler registration.
*/
public function testHandle(): void
{
$callback = function($args) {
return 'shutdown_handled';
};
$result = Exception::handle($callback, ['test']);
$this->assertTrue($result);
}
/**
* Test getting last error.
*/
public function testGetLastError(): void
{
// Clear any existing errors first
Exception::clearLastError();
// Trigger an error to test
@trigger_error('Test error', E_USER_NOTICE);
$error = Exception::getLastError();
$this->assertNotNull($error);
$this->assertIsArray($error);
$this->assertArrayHasKey('message', $error);
$this->assertEquals('Test error', $error['message']);
}
/**
* Test clearing last error.
*/
public function testClearLastError(): void
{
// Trigger an error first
@trigger_error('Test error to clear', E_USER_NOTICE);
// Clear the error
Exception::clearLastError();
// The error should be cleared
$error = Exception::getLastError();
$this->assertNull($error);
}
/**
* Test triggering user error.
*/
public function testTrigger(): void
{
// Clear any existing errors
Exception::clearLastError();
$result = @Exception::trigger('Test trigger error');
$this->assertTrue($result);
$error = Exception::getLastError();
$this->assertNotNull($error);
$this->assertEquals('Test trigger error', $error['message']);
}
/**
* Test triggering user error with custom level.
*/
public function testTriggerWithLevel(): void
{
Exception::clearLastError();
$result = @Exception::trigger('Test custom level', E_USER_WARNING);
$this->assertTrue($result);
$error = Exception::getLastError();
$this->assertNotNull($error);
$this->assertEquals(E_USER_WARNING, $error['type']);
}
/**
* Test logging user error.
*/
public function testLog(): void
{
// Test basic logging
$this->expectNotToPerformAssertions();
Exception::log('Test log message');
}
/**
* Test logging user error with type.
*/
public function testLogWithType(): void
{
$this->expectNotToPerformAssertions();
Exception::log('Test log message', 1);
}
/**
* Test logging user error with path.
*/
public function testLogWithPath(): void
{
$this->expectNotToPerformAssertions();
Exception::log('Test log message', 0, '/tmp/test.log');
}
/**
* Test logging user error with headers.
*/
public function testLogWithHeaders(): void
{
$this->expectNotToPerformAssertions();
Exception::log('Test log message', 0, null, 'Content-Type: text/plain');
}
/**
* Test exception inheritance.
*/
public function testExceptionInheritance(): void
{
$this->assertTrue(is_subclass_of(Exception::class, \Exception::class));
}
/**
* Test exception creation.
*/
public function testExceptionCreation(): void
{
$exception = new Exception('Test exception');
$this->assertInstanceOf(Exception::class, $exception);
$this->assertEquals('Test exception', $exception->getMessage());
}
/**
* Test exception with code.
*/
public function testExceptionWithCode(): void
{
$exception = new Exception('Test exception', 500);
$this->assertEquals(500, $exception->getCode());
}
/**
* Test handle with null args.
*/
public function testHandleWithNullArgs(): void
{
$callback = function() {
return 'no_args';
};
$result = Exception::handle($callback);
$this->assertTrue($result);
}
/**
* Test multiple error scenarios.
*/
public function testMultipleErrors(): void
{
Exception::clearLastError();
// First error
@Exception::trigger('First error');
$error1 = Exception::getLastError();
// Second error should override first
@Exception::trigger('Second error');
$error2 = Exception::getLastError();
$this->assertNotEquals($error1['message'], $error2['message']);
$this->assertEquals('Second error', $error2['message']);
}
}