Completed
Pull Request — master (#14)
by Simon
01:56
created

DeprecatedLogFormatterTest::testExtractValues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
cc 1
eloc 16
nc 1
nop 0
1
<?php
2
3
namespace SilverLeague\LogViewer\Tests\Deprecated;
4
5
use ReflectionClass;
6
use SilverLeague\LogViewer\Helper\DeprecatedLogFormatter;
7
use SilverLeague\LogViewer\Tests\Helper\Invoker;
8
use SilverStripe\Dev\SapphireTest;
9
10
/**
11
 * @author Simon Erkelens <[email protected]>
12
 */
13
class DeprecatedLogFormatterTest extends SapphireTest
14
{
15
16
    /**
17
     * Check if old entries go through the entire precess neatly
18
     */
19
    public function testFormatLegacyEntry()
20
    {
21
        $arrayEntry = 'E_WARNING: Invalid argument supplied for foreach() {"code":2,"message":"Invalid argument ' .
22
            'supplied for foreach()","file":"/data/site/docroot/logviewer/src/Model/LogEntry.php","line":89} []';
23
24
        $result = DeprecatedLogFormatter::formatLegacyEntry($arrayEntry);
25
        $expectedJSON = 'E_WARNING: Invalid argument supplied for foreach(): <span style="color: #007700">[</span>' .
26
            '<ul style="margin-bottom: 0"><li class="list-unstyled"><span style="color: #0000BB">code</span>' .
27
            '<span style="color: #007700">: </span><span style="color: #DD0000">2</span></li>' .
28
            '<li class="list-unstyled"><span style="color: #0000BB">message</span>' .
29
            '<span style="color: #007700">: </span>' .
30
            '<span style="color: #DD0000">Invalid argument supplied for foreach()</span></li>' .
31
            '<li class="list-unstyled"><span style="color: #0000BB">file</span><span style="color: #007700">: </span>' .
32
            '<span style="color: #DD0000">/data/site/docroot/logviewer/src/Model/LogEntry.php</span></li>' .
33
            '<li class="list-unstyled"><span style="color: #0000BB">line</span><span style="color: #007700">: </span>' .
34
            '<span style="color: #DD0000">89</span></li></ul><span style="color: #007700">]</span>' .
35
            "\n" .
36
            'Other: <span style="color: #007700">[</span><ul style="margin-bottom: 0"></ul>' .
37
            '<span style="color: #007700">]</span>';
38
        $this->assertEquals($expectedJSON, $result);
39
40
        $stringEntry = 'E_WARNING: Invalid argument supplied for foreach() {"code":2,"message":"Invalid argument ' .
41
            'supplied for foreach()","file":"/data/site/docroot/logviewer/src/Model/LogEntry.php","line":89} OOPS';
42
43
        $result = DeprecatedLogFormatter::formatLegacyEntry($stringEntry);
44
        $expectedString = 'E_WARNING: Invalid argument supplied for foreach(): <span style="color: #007700">[</span>' .
45
            '<ul style="margin-bottom: 0"><li class="list-unstyled"><span style="color: #0000BB">code</span>' .
46
            '<span style="color: #007700">: </span><span style="color: #DD0000">2</span></li>' .
47
            '<li class="list-unstyled"><span style="color: #0000BB">message</span>' .
48
            '<span style="color: #007700">: </span>' .
49
            '<span style="color: #DD0000">Invalid argument supplied for foreach()</span></li>' .
50
            '<li class="list-unstyled"><span style="color: #0000BB">file</span><span style="color: #007700">: </span>' .
51
            '<span style="color: #DD0000">/data/site/docroot/logviewer/src/Model/LogEntry.php</span></li>' .
52
            '<li class="list-unstyled"><span style="color: #0000BB">line</span><span style="color: #007700">: </span>' .
53
            '<span style="color: #DD0000">89</span></li></ul><span style="color: #007700">]</span>' .
54
            "\nOther:\n OOPS";
55
        $this->assertEquals($expectedString, $result);
56
57
        $noJsonEntry = "We're making water!";
58
        $result = DeprecatedLogFormatter::formatLegacyEntry($noJsonEntry);
59
        $this->assertEquals('<p>' . $noJsonEntry . '</p>', $result);
60
    }
61
62
    /**
63
     * Check if the entries are created correctly from array.
64
     */
65
    public function testCreateLegacyEntry()
66
    {
67
        $entry = 'E_WARNING: Invalid argument supplied for foreach() {"code":2,"message":"Invalid argument ' .
68
            'supplied for foreach()","file":"/data/site/docroot/logviewer/src/Model/LogEntry.php","line":89} []';
69
        $entryArray = [
70
            0 =>
71
                [
72
                    0 => '{"code":2,"message":"Invalid argument supplied for foreach()","file":"/data/site/docroot/logviewer/src/Model/LogEntry.php","line":89} ',
73
                ],
74
            1 =>
75
                [
76
                    0 => '{"code":2,"message":"Invalid argument supplied for foreach()","file":"/data/site/docroot/logviewer/src/Model/LogEntry.php","line":89}',
77
                ],
78
        ];
79
        $expectedResult = 'E_WARNING: Invalid argument supplied for foreach(): <span style="color: #007700">[</span>' .
80
            '<ul style="margin-bottom: 0"><li class="list-unstyled"><span style="color: #0000BB">code</span>' .
81
            '<span style="color: #007700">: </span><span style="color: #DD0000">2</span></li>' .
82
            '<li class="list-unstyled"><span style="color: #0000BB">message</span>' .
83
            '<span style="color: #007700">: </span>' .
84
            '<span style="color: #DD0000">Invalid argument supplied for foreach()</span></li>' .
85
            '<li class="list-unstyled"><span style="color: #0000BB">file</span><span style="color: #007700">: </span>' .
86
            '<span style="color: #DD0000">/data/site/docroot/logviewer/src/Model/LogEntry.php</span></li>' .
87
            '<li class="list-unstyled"><span style="color: #0000BB">line</span><span style="color: #007700">: </span>' .
88
            '<span style="color: #DD0000">89</span></li></ul><span style="color: #007700">]</span>' .
89
            "\n" .
90
            'Other: <span style="color: #007700">[</span><ul style="margin-bottom: 0"></ul>' .
91
            '<span style="color: #007700">]</span>';
92
        $formatter = new DeprecatedLogFormatter();
0 ignored issues
show
Deprecated Code introduced by
The class SilverLeague\LogViewer\H...\DeprecatedLogFormatter has been deprecated.

This class, trait or interface has been deprecated.

Loading history...
93
        $result = Invoker::invokeMethod($formatter, 'createLegacyEntry', array($entryArray, $entry, ''));
94
        $this->assertEquals($expectedResult, $result);
95
    }
96
97
    /**
98
     * Test if the JSON data is extracted correctly
99
     */
100
    public function testExtractValues()
101
    {
102
        $entry = 'E_WARNING: Invalid argument supplied for foreach() {"code":2,"message":"Invalid argument ' .
103
            'supplied for foreach()","file":"/data/site/docroot/logviewer/src/Model/LogEntry.php","line":89} []';
104
        $match = '{"code":2,"message":"Invalid argument supplied for foreach()","file":"/data/site/' .
105
            'docroot/logviewer/src/Model/LogEntry.php","line":89} ';
106
        $formatter = new DeprecatedLogFormatter();
0 ignored issues
show
Deprecated Code introduced by
The class SilverLeague\LogViewer\H...\DeprecatedLogFormatter has been deprecated.

This class, trait or interface has been deprecated.

Loading history...
107
        $result = Invoker::invokeMethod($formatter, 'extractValues', array($entry, $match));
108
        $expectedResult = [
109
            0 => 'E_WARNING: Invalid argument supplied for foreach() ',
110
            1 =>
111
                [
112
                    'code'    => 2,
113
                    'message' => 'Invalid argument supplied for foreach()',
114
                    'file'    => '/data/site/docroot/logviewer/src/Model/LogEntry.php',
115
                    'line'    => 89
116
                ],
117
            2 => '[]'
118
        ];
119
        $this->assertEquals($expectedResult, $result);
120
    }
121
122
}