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
90 lines (74 loc) · 3.31 KB
/
Copy pathExceptionsTest.php
File metadata and controls
90 lines (74 loc) · 3.31 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
<?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 CodeIgniter\Database\Exceptions\DatabaseException;
use CodeIgniter\Entity\Exceptions\CastException;
use CodeIgniter\Exceptions\ConfigException;
use CodeIgniter\Exceptions\PageNotFoundException;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\ReflectionHelper;
use Config\Exceptions as ExceptionsConfig;
use Config\Services;
use RuntimeException;
/**
* @internal
*/
final class ExceptionsTest extends CIUnitTestCase
{
use ReflectionHelper;
private \CodeIgniter\Debug\Exceptions $exception;
protected function setUp(): void
{
$this->exception = new Exceptions(new ExceptionsConfig(), Services::request(), Services::response());
}
public function testDetermineViews(): void
{
$determineView = $this->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 = $this->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 = $this->getPrivateMethodInvoker($this->exception, 'determineCodes');
$this->assertSame([500, EXIT__AUTO_MIN], $determineCodes(new RuntimeException('This.')));
$this->assertSame([500, EXIT_ERROR], $determineCodes(new RuntimeException('That.', 600)));
$this->assertSame([404, EXIT_ERROR], $determineCodes(new RuntimeException('There.', 404)));
$this->assertSame([167, EXIT_ERROR], $determineCodes(new RuntimeException('This.', 167)));
// @TODO This exit code should be EXIT_CONFIG.
$this->assertSame([500, 12], $determineCodes(new ConfigException('This.')));
// @TODO This exit code should be EXIT_CONFIG.
$this->assertSame([500, 9], $determineCodes(new CastException('This.')));
// @TODO This exit code should be EXIT_DATABASE.
$this->assertSame([500, 17], $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
);
}
}
}