1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\Logging\Tests; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Dev\SapphireTest; |
6
|
|
|
use SilverStripe\Logging\DetailedErrorFormatter; |
7
|
|
|
use SilverStripe\Logging\Tests\DetailedErrorFormatterTest\ErrorGenerator; |
8
|
|
|
|
9
|
|
|
class DetailedErrorFormatterTest extends SapphireTest |
10
|
|
|
{ |
11
|
|
|
public function testFormatWithException() |
12
|
|
|
{ |
13
|
|
|
$generator = new ErrorGenerator(); |
14
|
|
|
$formatter = new DetailedErrorFormatter(); |
15
|
|
|
$exception = $generator->mockException(); |
16
|
|
|
|
17
|
|
|
$output = '' . $formatter->format(['context' => [ |
18
|
|
|
'exception' => $exception, |
19
|
|
|
]]); |
20
|
|
|
|
21
|
|
|
$base = __DIR__; |
22
|
|
|
$this->assertContains('ERROR [Emergency]: Uncaught Exception: Error', $output); |
23
|
|
|
$this->assertContains("Line 32 in $base/DetailedErrorFormatterTest/ErrorGenerator.php", $output); |
24
|
|
|
$this->assertContains('* 32: throw new Exception(\'Error\');', $output); |
25
|
|
|
$this->assertContains( |
26
|
|
|
'SilverStripe\\Logging\\Tests\\DetailedErrorFormatterTest\\ErrorGenerator->mockException(4)', |
27
|
|
|
$output |
28
|
|
|
); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function testFormatWithoutException() |
32
|
|
|
{ |
33
|
|
|
$record = [ |
34
|
|
|
'code' => 401, |
35
|
|
|
'message' => 'Denied', |
36
|
|
|
'file' => 'index.php', |
37
|
|
|
'line' => 4, |
38
|
|
|
]; |
39
|
|
|
|
40
|
|
|
$formatter = new DetailedErrorFormatter(); |
41
|
|
|
$result = $formatter->format($record); |
42
|
|
|
|
43
|
|
|
$this->assertContains('ERRNO 401', $result, 'Status code was not found in trace'); |
44
|
|
|
$this->assertContains('Denied', $result, 'Message was not found in trace'); |
45
|
|
|
$this->assertContains('Line 4 in index.php', $result, 'Line or filename were not found in trace'); |
46
|
|
|
$this->assertContains(self::class, $result, 'Backtrace doesn\'t show current test class'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testFormatBatch() |
50
|
|
|
{ |
51
|
|
|
$records = [ |
52
|
|
|
[ |
53
|
|
|
'code' => 401, |
54
|
|
|
'message' => 'Denied', |
55
|
|
|
'file' => 'index.php', |
56
|
|
|
'line' => 4, |
57
|
|
|
], |
58
|
|
|
[ |
59
|
|
|
'code' => 404, |
60
|
|
|
'message' => 'Not found', |
61
|
|
|
'file' => 'admin.php', |
62
|
|
|
'line' => 7, |
63
|
|
|
], |
64
|
|
|
]; |
65
|
|
|
|
66
|
|
|
$formatter = new DetailedErrorFormatter(); |
67
|
|
|
$result = $formatter->formatBatch($records); |
68
|
|
|
|
69
|
|
|
$this->assertContains('ERRNO 401', $result, 'First status code was not found in trace'); |
70
|
|
|
$this->assertContains('ERRNO 404', $result, 'Second status code was not found in trace'); |
71
|
|
|
$this->assertContains('Denied', $result, 'First message was not found in trace'); |
72
|
|
|
$this->assertContains('Not found', $result, 'Second message was not found in trace'); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|