Completed
Pull Request — master (#14)
by Simon
02:03
created

LogEntryTest::errorTestGetCMSFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 1
eloc 10
nc 1
nop 0
1
<?php
2
3
namespace SilverLeague\LogViewer\Tests\Model;
4
5
use SilverLeague\LogViewer\Model\LogEntry;
6
use SilverStripe\Core\Convert;
7
use SilverStripe\Dev\Debug;
8
use SilverStripe\Dev\SapphireTest;
9
10
/**
11
 * @package silverstripe-logviewer
12
 * @author  Robbie Averill <[email protected]>
13
 */
14
class LogEntryTest extends SapphireTest
15
{
16
    /**
17
     * Test that LogEntry classes can not be edited
18
     */
19
    public function testDoNotAllowEditing()
20
    {
21
        $this->assertFalse(LogEntry::create()->canEdit());
22
    }
23
24
    /**
25
     * Test the gridfield Summary method
26
     */
27
    public function testGridfieldSummary()
28
    {
29
        $shortString = 'abcdefghijklmnopqrstuvwxyz';
30
        $longString = '';
31
        // Generate a string of 350 characters. Because we don't care about the actual content
32
        // A set of a's is good enough;
33
        for ($i = 0; $i <= 350; $i++) {
34
            $longString .= 'a';
35
        }
36
        $logEntry = LogEntry::create();
37
        $logEntry->Entry = $shortString;
38
        $this->assertEquals($shortString, $logEntry->getGridfieldSummary());
39
        $logEntry->Entry = $longString;
40
        $elipsisString = substr($longString, 0, 300) . '...';
41
        $this->assertEquals($elipsisString, $logEntry->getGridfieldSummary());
42
        $this->assertEquals($longString, $logEntry->getGridfieldSummary(351));
43
    }
44
45
    public function testEntryToUl()
46
    {
47
        $entry = array(
48
            'Message' => 'Something went wrong',
49
            'DateTime' => '2016-01-22 03:14:23'
50
        );
51
        $logEntry = LogEntry::create(array(
52
            'Level' => 'INFO',
53
            'Entry' => Convert::array2json($entry)
54
        ));
55
        $result = $this->invokeMethod($logEntry, 'entryToUl', array($entry));
56
        $expectedString = '[<ul style="margin-bottom: 0"><li  class="list-unstyled"><span>Message => Something went' .
57
            ' wrong</span></li><li  class="list-unstyled"><span>DateTime => 2016-01-22 03:14:23</span></li></ul>]';
58
59
        $this->assertEquals($expectedString, $result);
60
    }
61
62
    public function testFormatLegacyEntry() {
63
        $entry = 'E_WARNING: Invalid argument supplied for foreach() {"code":2,"message":"Invalid argument supplied' .
64
            'for foreach()","file":"/data/site/docroot/logviewer/src/Model/LogEntry.php","line":89} []';
65
        $logEntry = LogEntry::create(array(
66
            'Level' => 'INFO',
67
            'Entry' => $entry
68
        ));
69
        $result = $this->invokeMethod($logEntry, 'formatLegacyEntry', array($entry));
70
        $expectedString = '<h2>Entry</h2>E_WARNING: Invalid argument supplied for foreach() => [<ul style="margin-bottom:' .
71
        ' 0"><li  class="list-unstyled"><span>code => 2</span></li><li  class="list-unstyled"><span>message =>' .
72
        ' Invalid argument suppliedfor foreach()</span></li><li  class="list-unstyled"><span>file =>' .
73
        ' /data/site/docroot/logviewer/src/Model/LogEntry.php</span></li><li  class="list-unstyled"><span>line' .
74
        " => 89</span></li></ul>]\n" .
75
        'Other => [<ul style="margin-bottom: 0"></ul>]';
76
        $this->assertEquals($expectedString, $result);
77
78
    }
0 ignored issues
show
Coding Style introduced by
Function closing brace must go on the next line following the body; found 1 blank lines before brace
Loading history...
79
80
    /**
81
     * This test throws a strange error about PageController not found
82
     * This is in the CMS Tests. It has something to do with the parent::getCMSFields
83
     */
84
    public function errorTestGetCMSFields()
85
    {
86
        $logEntry = LogEntry::create();
87
        $logEntry->Level = 'INFO';
88
        $logEntry->Entry = Convert::array2json(array('Test' => 'Message'));
89
        $fields = $logEntry->getCMSFields();
90
        $this->assertEquals(2, $fields->count());
91
        $levelField = $fields->dataFieldByName('Level');
92
        $entryField = $fields->dataFieldByName('Entry');
93
        $this->assertEquals('INFO', $levelField->Value());
94
        $this->assertContains('Test => Message', $entryField->Value());
95
    }
96
97
    /**
98
     * Call protected/private method of a class.
99
     *
100
     * @param object &$object    Instantiated object that we will run method on.
101
     * @param string $methodName Method name to call
102
     * @param array  $parameters Array of parameters to pass into method.
103
     *
104
     * @return mixed Method return.
105
     */
106
    public function invokeMethod(&$object, $methodName, array $parameters = array())
107
    {
108
        $reflection = new \ReflectionClass(get_class($object));
109
        $method = $reflection->getMethod($methodName);
110
        $method->setAccessible(true);
111
112
        return $method->invokeArgs($object, $parameters);
113
    }
114
}
115