Passed
Push — fix-6460 ( 379c3e...254990 )
by Sam
09:57
created

DetailedErrorFormatterTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 39
dl 0
loc 64
rs 10
c 0
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testFormatBatch() 0 24 1
A testFormatWithException() 0 17 1
A testFormatWithoutException() 0 16 1
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