Completed
Pull Request — master (#14)
by Simon
02:03
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 SilverStripe\Dev\SapphireTest;
8
9
/**
10
 * @author Simon Erkelens <[email protected]>
11
 */
12
class DeprecatedLogFormatterTest extends SapphireTest
13
{
14
15
    /**
16
     * Check if old entries go through the entire precess neatly
17
     */
18
    public function testFormatLegacyEntry()
19
    {
20
        $arrayEntry = 'E_WARNING: Invalid argument supplied for foreach() {"code":2,"message":"Invalid argument ' .
21
            'supplied for foreach()","file":"/data/site/docroot/logviewer/src/Model/LogEntry.php","line":89} []';
22
23
        $result = DeprecatedLogFormatter::formatLegacyEntry($arrayEntry);
24
        $expectedJSON = 'E_WARNING: Invalid argument supplied for foreach(): <span style="color: #007700">[</span>' .
25
            '<ul style="margin-bottom: 0"><li class="list-unstyled"><span style="color: #0000BB">code</span>' .
26
            '<span style="color: #007700">: </span><span style="color: #DD0000">2</span></li>' .
27
            '<li class="list-unstyled"><span style="color: #0000BB">message</span>' .
28
            '<span style="color: #007700">: </span>' .
29
            '<span style="color: #DD0000">Invalid argument supplied for foreach()</span></li>' .
30
            '<li class="list-unstyled"><span style="color: #0000BB">file</span><span style="color: #007700">: </span>' .
31
            '<span style="color: #DD0000">/data/site/docroot/logviewer/src/Model/LogEntry.php</span></li>' .
32
            '<li class="list-unstyled"><span style="color: #0000BB">line</span><span style="color: #007700">: </span>' .
33
            '<span style="color: #DD0000">89</span></li></ul><span style="color: #007700">]</span>' .
34
            "\n" .
35
            'Other: <span style="color: #007700">[</span><ul style="margin-bottom: 0"></ul>' .
36
            '<span style="color: #007700">]</span>';
37
        $this->assertEquals($expectedJSON, $result);
38
39
        $stringEntry = 'E_WARNING: Invalid argument supplied for foreach() {"code":2,"message":"Invalid argument ' .
40
            'supplied for foreach()","file":"/data/site/docroot/logviewer/src/Model/LogEntry.php","line":89} OOPS';
41
42
        $result = DeprecatedLogFormatter::formatLegacyEntry($stringEntry);
43
        $expectedString = 'E_WARNING: Invalid argument supplied for foreach(): <span style="color: #007700">[</span>' .
44
            '<ul style="margin-bottom: 0"><li class="list-unstyled"><span style="color: #0000BB">code</span>' .
45
            '<span style="color: #007700">: </span><span style="color: #DD0000">2</span></li>' .
46
            '<li class="list-unstyled"><span style="color: #0000BB">message</span>' .
47
            '<span style="color: #007700">: </span>' .
48
            '<span style="color: #DD0000">Invalid argument supplied for foreach()</span></li>' .
49
            '<li class="list-unstyled"><span style="color: #0000BB">file</span><span style="color: #007700">: </span>' .
50
            '<span style="color: #DD0000">/data/site/docroot/logviewer/src/Model/LogEntry.php</span></li>' .
51
            '<li class="list-unstyled"><span style="color: #0000BB">line</span><span style="color: #007700">: </span>' .
52
            '<span style="color: #DD0000">89</span></li></ul><span style="color: #007700">]</span>' .
53
            "\nOther:\n OOPS";
54
        $this->assertEquals($expectedString, $result);
55
56
        $noJsonEntry = "We're making water!";
57
        $result = DeprecatedLogFormatter::formatLegacyEntry($noJsonEntry);
58
        $this->assertEquals('<p>' . $noJsonEntry . '</p>', $result);
59
    }
60
61
    /**
62
     * Check if the entries are created correctly from array.
63
     */
64
    public function testCreateLegacyEntry()
65
    {
66
        $entry = 'E_WARNING: Invalid argument supplied for foreach() {"code":2,"message":"Invalid argument ' .
67
            'supplied for foreach()","file":"/data/site/docroot/logviewer/src/Model/LogEntry.php","line":89} []';
68
        $entryArray = [
69
            0 =>
70
                [
71
                    0 => '{"code":2,"message":"Invalid argument supplied for foreach()","file":"/data/site/docroot/logviewer/src/Model/LogEntry.php","line":89} ',
72
                ],
73
            1 =>
74
                [
75
                    0 => '{"code":2,"message":"Invalid argument supplied for foreach()","file":"/data/site/docroot/logviewer/src/Model/LogEntry.php","line":89}',
76
                ],
77
        ];
78
        $expectedResult = 'E_WARNING: Invalid argument supplied for foreach(): <span style="color: #007700">[</span>' .
79
            '<ul style="margin-bottom: 0"><li class="list-unstyled"><span style="color: #0000BB">code</span>' .
80
            '<span style="color: #007700">: </span><span style="color: #DD0000">2</span></li>' .
81
            '<li class="list-unstyled"><span style="color: #0000BB">message</span>' .
82
            '<span style="color: #007700">: </span>' .
83
            '<span style="color: #DD0000">Invalid argument supplied for foreach()</span></li>' .
84
            '<li class="list-unstyled"><span style="color: #0000BB">file</span><span style="color: #007700">: </span>' .
85
            '<span style="color: #DD0000">/data/site/docroot/logviewer/src/Model/LogEntry.php</span></li>' .
86
            '<li class="list-unstyled"><span style="color: #0000BB">line</span><span style="color: #007700">: </span>' .
87
            '<span style="color: #DD0000">89</span></li></ul><span style="color: #007700">]</span>' .
88
            "\n" .
89
            'Other: <span style="color: #007700">[</span><ul style="margin-bottom: 0"></ul>' .
90
            '<span style="color: #007700">]</span>';
91
        $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...
92
        $result = $this->invokeMethod($formatter, 'createLegacyEntry', array($entryArray, $entry, ''));
93
        $this->assertEquals($expectedResult, $result);
94
    }
95
96
    /**
97
     * Test if the JSON data is extracted correctly
98
     */
99
    public function testExtractValues()
100
    {
101
        $entry = 'E_WARNING: Invalid argument supplied for foreach() {"code":2,"message":"Invalid argument ' .
102
            'supplied for foreach()","file":"/data/site/docroot/logviewer/src/Model/LogEntry.php","line":89} []';
103
        $match = '{"code":2,"message":"Invalid argument supplied for foreach()","file":"/data/site/' .
104
            'docroot/logviewer/src/Model/LogEntry.php","line":89} ';
105
        $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...
106
        $result = $this->invokeMethod($formatter, 'extractValues', array($entry, $match));
107
        $expectedResult = [
108
            0 => 'E_WARNING: Invalid argument supplied for foreach() ',
109
            1 =>
110
                [
111
                    'code'    => 2,
112
                    'message' => 'Invalid argument supplied for foreach()',
113
                    'file'    => '/data/site/docroot/logviewer/src/Model/LogEntry.php',
114
                    'line'    => 89
115
                ],
116
            2 => '[]'
117
        ];
118
        $this->assertEquals($expectedResult, $result);
119
    }
120
121
    /**
122
     * Call protected/private method of a class.
123
     *
124
     * @param DeprecatedLogFormatter &$object Instantiated object that we will run method on.
125
     * @param string $methodName Method name to call
126
     * @param array $parameters Array of parameters to pass into method.
127
     *
128
     * @return mixed Method return.
129
     */
130 View Code Duplication
    public function invokeMethod(&$object, $methodName, array $parameters = array())
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
131
    {
132
        $reflection = new ReflectionClass(get_class($object));
133
        $method = $reflection->getMethod($methodName);
134
        $method->setAccessible(true);
135
136
        return $method->invokeArgs($object, $parameters);
137
    }
138
139
}