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

LogEntryTest::invokeMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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