forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExceptionsTest.php
More file actions
225 lines (187 loc) · 7.63 KB
/
ExceptionsTest.php
File metadata and controls
225 lines (187 loc) · 7.63 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Debug;
use App\Controllers\Home;
use CodeIgniter\Database\Exceptions\DatabaseException;
use CodeIgniter\Entity\Exceptions\CastException;
use CodeIgniter\Exceptions\ConfigException;
use CodeIgniter\Exceptions\PageNotFoundException;
use CodeIgniter\Exceptions\RuntimeException;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\ReflectionHelper;
use Config\Exceptions as ExceptionsConfig;
use ErrorException;
use PHPUnit\Framework\Attributes\Group;
/**
* @internal
*/
#[Group('Others')]
final class ExceptionsTest extends CIUnitTestCase
{
use ReflectionHelper;
private Exceptions $exception;
public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();
unset($_SERVER['CODEIGNITER_SCREAM_DEPRECATIONS']);
}
public static function tearDownAfterClass(): void
{
parent::tearDownAfterClass();
$_SERVER['CODEIGNITER_SCREAM_DEPRECATIONS'] = '1';
}
protected function setUp(): void
{
parent::setUp();
$this->exception = new Exceptions(new ExceptionsConfig());
}
public function testDeprecationsOnPhp81DoNotThrow(): void
{
$config = new ExceptionsConfig();
$config->logDeprecations = true;
$config->deprecationLogLevel = 'error';
$this->exception = new Exceptions($config);
$this->exception->initialize();
try {
$result = str_contains('foobar', null); // @phpstan-ignore argument.type (Needed for testing)
$this->assertLogContains('error', '[DEPRECATED] str_contains(): ');
} catch (ErrorException) {
$this->fail('The catch block should not be reached.');
} finally {
restore_error_handler();
restore_exception_handler();
}
}
public function testSuppressedDeprecationsAreLogged(): void
{
$config = new ExceptionsConfig();
$config->logDeprecations = true;
$config->deprecationLogLevel = 'error';
$this->exception = new Exceptions($config);
$this->exception->initialize();
@trigger_error('Hello! I am a deprecation!', E_USER_DEPRECATED);
$this->assertLogContains('error', '[DEPRECATED] Hello! I am a deprecation!');
restore_error_handler();
restore_exception_handler();
}
public function testDetermineViews(): void
{
$determineView = self::getPrivateMethodInvoker($this->exception, 'determineView');
$this->assertSame('error_404.php', $determineView(PageNotFoundException::forControllerNotFound('Foo', 'bar'), ''));
$this->assertSame('error_exception.php', $determineView(new RuntimeException('Exception'), ''));
$this->assertSame('error_404.php', $determineView(new RuntimeException('foo', 404), 'app/Views/errors/cli'));
}
public function testCollectVars(): void
{
$vars = self::getPrivateMethodInvoker($this->exception, 'collectVars')(new RuntimeException('This.'), 404);
$this->assertIsArray($vars);
$this->assertCount(7, $vars);
foreach (['title', 'type', 'code', 'message', 'file', 'line', 'trace'] as $key) {
$this->assertArrayHasKey($key, $vars);
}
}
public function testDetermineCodes(): void
{
$determineCodes = self::getPrivateMethodInvoker($this->exception, 'determineCodes');
$this->assertSame([500, EXIT_ERROR], $determineCodes(new RuntimeException('This.')));
$this->assertSame([500, EXIT_ERROR], $determineCodes(new RuntimeException('That.', 600)));
$this->assertSame([500, EXIT_ERROR], $determineCodes(new RuntimeException('There.', 404)));
$this->assertSame([500, EXIT_ERROR], $determineCodes(new RuntimeException('This.', 167)));
$this->assertSame([500, EXIT_CONFIG], $determineCodes(new ConfigException('This.')));
$this->assertSame([500, EXIT_CONFIG], $determineCodes(CastException::forInvalidInterface('This.')));
$this->assertSame([500, EXIT_DATABASE], $determineCodes(new DatabaseException('This.')));
}
public function testRenderBacktrace(): void
{
$renderer = self::getPrivateMethodInvoker(Exceptions::class, 'renderBacktrace');
$exception = new RuntimeException('This.');
$renderedBacktrace = $renderer($exception->getTrace());
$renderedBacktrace = explode("\n", $renderedBacktrace);
foreach ($renderedBacktrace as $trace) {
$this->assertMatchesRegularExpression(
'/^\s*\d* .+(?:\(\d+\))?: \S+(?:(?:\->|::)\S+)?\(.*\)$/',
$trace,
);
}
}
public function testMaskSensitiveData(): void
{
$maskSensitiveData = self::getPrivateMethodInvoker($this->exception, 'maskSensitiveData');
$trace = [
0 => [
'file' => '/var/www/CodeIgniter4/app/Controllers/Home.php',
'line' => 15,
'function' => 'f',
'class' => Home::class,
'type' => '->',
'args' => [
0 => (object) [
'password' => 'secret1',
],
1 => (object) [
'default' => [
'password' => 'secret2',
],
],
2 => [
'password' => 'secret3',
],
3 => [
'default' => ['password' => 'secret4'],
],
],
],
1 => [
'file' => '/var/www/CodeIgniter4/system/CodeIgniter.php',
'line' => 932,
'function' => 'index',
'class' => Home::class,
'type' => '->',
'args' => [
],
],
];
$keysToMask = ['password'];
$path = '';
$newTrace = $maskSensitiveData($trace, $keysToMask, $path);
$this->assertSame(['password' => '******************'], (array) $newTrace[0]['args'][0]);
$this->assertSame(['password' => '******************'], $newTrace[0]['args'][1]->default);
$this->assertSame(['password' => '******************'], $newTrace[0]['args'][2]);
$this->assertSame(['password' => '******************'], $newTrace[0]['args'][3]['default']);
}
public function testMaskSensitiveDataTraceDataKey(): void
{
$maskSensitiveData = self::getPrivateMethodInvoker($this->exception, 'maskSensitiveData');
$trace = [
0 => [
'file' => '/var/www/CodeIgniter4/app/Controllers/Home.php',
'line' => 15,
'function' => 'f',
'class' => Home::class,
'type' => '->',
'args' => [
],
],
1 => [
'file' => '/var/www/CodeIgniter4/system/CodeIgniter.php',
'line' => 932,
'function' => 'index',
'class' => Home::class,
'type' => '->',
'args' => [
],
],
];
$keysToMask = ['file'];
$path = '';
$newTrace = $maskSensitiveData($trace, $keysToMask, $path);
$this->assertSame('/var/www/CodeIgniter4/app/Controllers/Home.php', $newTrace[0]['file']);
}
}