Passed
Push — zero-is-false ( 14c39e...85b8a2 )
by Sam
06:15
created

testFormatBatch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 0
dl 0
loc 18
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\Logging\Tests;
4
5
use PHPUnit_Framework_MockObject_MockObject;
6
use SilverStripe\Control\Email\Email;
7
use SilverStripe\Control\HTTPRequest;
8
use SilverStripe\Core\Injector\Injector;
9
use SilverStripe\Dev\SapphireTest;
10
use SilverStripe\Logging\DebugViewFriendlyErrorFormatter;
11
12
class DebugViewFriendlyErrorFormatterTest extends SapphireTest
13
{
14
    protected function setUp()
15
    {
16
        parent::setUp();
17
        Email::config()->set('admin_email', '[email protected]');
18
    }
19
20
    public function testFormatPassesRecordCodeToOutput()
21
    {
22
        /** @var DebugViewFriendlyErrorFormatter|PHPUnit_Framework_MockObject_MockObject $mock */
23
        $mock = $this->getMockBuilder(DebugViewFriendlyErrorFormatter::class)
24
            ->setMethods(['output'])
25
            ->getMock();
26
27
        $mock->expects($this->once())->method('output')->with(403)->willReturn('foo');
28
        $this->assertSame('foo', $mock->format(['code' => 403]));
29
    }
30
31
    public function testFormatPassesInstanceStatusCodeToOutputWhenNotProvidedByRecord()
32
    {
33
        /** @var DebugViewFriendlyErrorFormatter|PHPUnit_Framework_MockObject_MockObject $mock */
34
        $mock = $this->getMockBuilder(DebugViewFriendlyErrorFormatter::class)
35
            ->setMethods(['output'])
36
            ->getMock();
37
38
        $mock->setStatusCode(404);
39
40
        $mock->expects($this->once())->method('output')->with(404)->willReturn('foo');
41
        $this->assertSame('foo', $mock->format(['notacode' => 'bar']));
42
    }
43
44
    public function testFormatBatch()
45
    {
46
        $records = [
47
            ['message' => 'bar'],
48
            ['open' => 'sausage'],
49
            ['horse' => 'caballo'],
50
        ];
51
52
        /** @var DebugViewFriendlyErrorFormatter|PHPUnit_Framework_MockObject_MockObject $mock */
53
        $mock = $this->getMockBuilder(DebugViewFriendlyErrorFormatter::class)
54
            ->setMethods(['format'])
55
            ->getMock();
56
57
        $mock->expects($this->exactly(3))
58
            ->method('format')
59
            ->willReturn('foo');
60
61
        $this->assertSame('foofoofoo', $mock->formatBatch($records));
62
    }
63
64
    public function testOutput()
65
    {
66
        $formatter = new DebugViewFriendlyErrorFormatter();
67
        $formatter->setTitle("There has been an error");
68
        $formatter->setBody("The website server has not been able to respond to your request");
69
70
        $expected = <<<TEXT
71
WEBSITE ERROR
72
There has been an error
73
-----------------------
74
The website server has not been able to respond to your request
75
76
Contact an administrator: testy [at] mctest [dot] face
77
78
79
TEXT
80
        ;
81
82
        $this->assertEquals($expected, $formatter->output(404));
83
    }
84
85
    public function testOutputReturnsTitleWhenRequestIsAjax()
86
    {
87
        // Mock an AJAX request
88
        Injector::inst()->registerService(new HTTPRequest('GET', '', ['ajax' => true]));
89
90
        $formatter = new DebugViewFriendlyErrorFormatter();
91
        $formatter->setTitle('The Diary of Anne Frank');
92
93
        $this->assertSame('The Diary of Anne Frank', $formatter->output(200));
94
    }
95
}
96