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

testFormatLegacyEntry()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 42
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 42
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 36
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
    public function testFormatLegacyEntry()
16
    {
17
        $arrayEntry = 'E_WARNING: Invalid argument supplied for foreach() {"code":2,"message":"Invalid argument ' .
18
            'supplied for foreach()","file":"/data/site/docroot/logviewer/src/Model/LogEntry.php","line":89} []';
19
20
        $result = DeprecatedLogFormatter::formatLegacyEntry($arrayEntry);
21
        $expectedJSON = 'E_WARNING: Invalid argument supplied for foreach(): <span style="color: #007700">[</span>' .
22
            '<ul style="margin-bottom: 0"><li class="list-unstyled"><span style="color: #0000BB">code</span>' .
23
            '<span style="color: #007700">: </span><span style="color: #DD0000">2</span></li>' .
24
            '<li class="list-unstyled"><span style="color: #0000BB">message</span>' .
25
            '<span style="color: #007700">: </span>' .
26
            '<span style="color: #DD0000">Invalid argument supplied for foreach()</span></li>' .
27
            '<li class="list-unstyled"><span style="color: #0000BB">file</span><span style="color: #007700">: </span>' .
28
            '<span style="color: #DD0000">/data/site/docroot/logviewer/src/Model/LogEntry.php</span></li>' .
29
            '<li class="list-unstyled"><span style="color: #0000BB">line</span><span style="color: #007700">: </span>' .
30
            '<span style="color: #DD0000">89</span></li></ul><span style="color: #007700">]</span>' .
31
            "\n" .
32
            'Other: <span style="color: #007700">[</span><ul style="margin-bottom: 0"></ul>' .
33
            '<span style="color: #007700">]</span>';
34
        $this->assertEquals($expectedJSON, $result);
35
36
        $stringEntry = 'E_WARNING: Invalid argument supplied for foreach() {"code":2,"message":"Invalid argument ' .
37
            'supplied for foreach()","file":"/data/site/docroot/logviewer/src/Model/LogEntry.php","line":89} OOPS';
38
39
        $result = DeprecatedLogFormatter::formatLegacyEntry($stringEntry);
40
        $expectedString = 'E_WARNING: Invalid argument supplied for foreach(): <span style="color: #007700">[</span>' .
41
            '<ul style="margin-bottom: 0"><li class="list-unstyled"><span style="color: #0000BB">code</span>' .
42
            '<span style="color: #007700">: </span><span style="color: #DD0000">2</span></li>' .
43
            '<li class="list-unstyled"><span style="color: #0000BB">message</span>' .
44
            '<span style="color: #007700">: </span>' .
45
            '<span style="color: #DD0000">Invalid argument supplied for foreach()</span></li>' .
46
            '<li class="list-unstyled"><span style="color: #0000BB">file</span><span style="color: #007700">: </span>' .
47
            '<span style="color: #DD0000">/data/site/docroot/logviewer/src/Model/LogEntry.php</span></li>' .
48
            '<li class="list-unstyled"><span style="color: #0000BB">line</span><span style="color: #007700">: </span>' .
49
            '<span style="color: #DD0000">89</span></li></ul><span style="color: #007700">]</span>' .
50
            "\nOther:\n OOPS";
51
        $this->assertEquals($expectedString, $result);
52
53
        $noJsonEntry = "We're making water!";
54
        $result = DeprecatedLogFormatter::formatLegacyEntry($noJsonEntry);
55
        $this->assertEquals('<p>' . $noJsonEntry . '</p>', $result);
56
    }
57
58
    public function testCreateLegacyEntry()
59
    {
60
        $entry = 'E_WARNING: Invalid argument supplied for foreach() {"code":2,"message":"Invalid argument ' .
61
            'supplied for foreach()","file":"/data/site/docroot/logviewer/src/Model/LogEntry.php","line":89} []';
62
        $entryArray = [
63
            0 =>
64
                [
65
                    0 => '{"code":2,"message":"Invalid argument supplied for foreach()","file":"/data/site/docroot/logviewer/src/Model/LogEntry.php","line":89} ',
66
                ],
67
            1 =>
68
                [
69
                    0 => '{"code":2,"message":"Invalid argument supplied for foreach()","file":"/data/site/docroot/logviewer/src/Model/LogEntry.php","line":89}',
70
                ],
71
        ];
72
        $expectedResult = 'E_WARNING: Invalid argument supplied for foreach(): <span style="color: #007700">[</span>' .
73
            '<ul style="margin-bottom: 0"><li class="list-unstyled"><span style="color: #0000BB">code</span>' .
74
            '<span style="color: #007700">: </span><span style="color: #DD0000">2</span></li>' .
75
            '<li class="list-unstyled"><span style="color: #0000BB">message</span>' .
76
            '<span style="color: #007700">: </span>' .
77
            '<span style="color: #DD0000">Invalid argument supplied for foreach()</span></li>' .
78
            '<li class="list-unstyled"><span style="color: #0000BB">file</span><span style="color: #007700">: </span>' .
79
            '<span style="color: #DD0000">/data/site/docroot/logviewer/src/Model/LogEntry.php</span></li>' .
80
            '<li class="list-unstyled"><span style="color: #0000BB">line</span><span style="color: #007700">: </span>' .
81
            '<span style="color: #DD0000">89</span></li></ul><span style="color: #007700">]</span>' .
82
            "\n" .
83
            'Other: <span style="color: #007700">[</span><ul style="margin-bottom: 0"></ul>' .
84
            '<span style="color: #007700">]</span>';
85
        $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...
86
        $result = $this->invokeMethod($formatter, 'createLegacyEntry', array($entryArray, $entry, ''));
87
        $this->assertEquals($expectedResult, $result);
88
    }
89
90
    public function testExtractValues() {
91
        $entry = 'E_WARNING: Invalid argument supplied for foreach() {"code":2,"message":"Invalid argument ' .
92
            'supplied for foreach()","file":"/data/site/docroot/logviewer/src/Model/LogEntry.php","line":89} []';
93
        $match = '{"code":2,"message":"Invalid argument supplied for foreach()","file":"/data/site/'.
94
            'docroot/logviewer/src/Model/LogEntry.php","line":89} ';
95
        $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...
96
        $result = $this->invokeMethod($formatter, 'extractValues', array($entry, $match));
97
        $expectedResult = [
98
            0 => 'E_WARNING: Invalid argument supplied for foreach() ',
99
            1 =>
100
                [
101
                    'code' => 2,
102
                    'message' => 'Invalid argument supplied for foreach()',
103
                    'file' => '/data/site/docroot/logviewer/src/Model/LogEntry.php',
104
                    'line' => 89
105
                ],
106
            2 => '[]'
107
        ];
108
        $this->assertEquals($expectedResult, $result);
109
    }
110
111
112
    /**
113
     * Call protected/private method of a class.
114
     *
115
     * @param DeprecatedLogFormatter &$object Instantiated object that we will run method on.
116
     * @param string $methodName Method name to call
117
     * @param array $parameters Array of parameters to pass into method.
118
     *
119
     * @return mixed Method return.
120
     */
121 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...
122
    {
123
        $reflection = new ReflectionClass(get_class($object));
124
        $method = $reflection->getMethod($methodName);
125
        $method->setAccessible(true);
126
127
        return $method->invokeArgs($object, $parameters);
128
    }
129
130
}