Passed
Push — master ( 740e1a...88eabc )
by Daniel
16:20 queued 08:54
created

DiffTest::testDiffArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 8
rs 10
1
<?php
2
3
namespace SilverStripe\View\Tests\Parsers;
4
5
use SilverStripe\Dev\SapphireTest;
6
use SilverStripe\View\Parsers\Diff;
7
8
class DiffTest extends SapphireTest
9
{
10
11
    /**
12
     * @see https://groups.google.com/forum/#!topic/silverstripe-dev/yHcluCvuszo
13
     */
14
    public function testTableDiff()
15
    {
16
        if (!class_exists('DOMDocument')) {
17
            $this->markTestSkipped('"DOMDocument" required');
18
            return;
19
        }
20
21
        $from = "<table>
22
		<tbody>
23
			<tr class=\"blah\">
24
				<td colspan=\"2\">Row 1</td>
25
			</tr>
26
			<tr class=\"foo\">
27
				<td>Row 2</td>
28
				<td>Row 2</td>
29
			</tr>
30
			<tr>
31
				<td>Row 3</td>
32
				<td>Row 3</td>
33
			</tr>
34
			</tbody>
35
		</table>";
36
37
        $to = "<table class=\"new-class\">
38
		<tbody>
39
			<tr class=\"blah\">
40
				<td colspan=\"2\">Row 1</td>
41
			</tr>
42
			<tr class=\"foo\">
43
				<td>Row 2</td>
44
				<td>Row 2</td>
45
			</tr>
46
		</tbody>
47
		</table>";
48
49
        $expected = "<ins>" . $to . "</ins>" . "<del>" . $from . "</del>";
50
        $compare = Diff::compareHTML($from, $to);
51
52
        // Very hard to debug this way, wouldn't need to do this if PHP had an *actual* DOM parsing lib,
53
        // and not just the poor excuse that is DOMDocument
54
        $compare = preg_replace('/[\s\t\n\r]*/', '', $compare);
55
        $expected = preg_replace('/[\s\t\n\r]*/', '', $expected);
56
57
        $this->assertEquals($expected, $compare);
58
    }
59
60
    /**
61
     * @see https://github.com/silverstripe/silverstripe-framework/issues/8053
62
     */
63
    public function testLegacyEachStatement()
64
    {
65
        $sentenceOne =
66
            'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';
67
        $sentenceTwo =
68
            'Nulla porttitor, ex quis commodo pharetra, diam dui efficitur justo, eu gravida elit eros vel libero.';
69
70
        $from = "$sentenceOne $sentenceTwo";
71
        $to = "$sentenceTwo $sentenceOne";
72
73
        // We're cheating our test a little bit here, because depending on what HTML cleaner you have, you'll get
74
        // spaces added or not added around the tags.
75
        $expected = "/^ *<del>$sentenceOne<\/del> *$sentenceTwo *<ins>$sentenceOne<\/ins> *$/";
76
        $actual = Diff::compareHTML($from, $to);
77
78
        $this->assertRegExp($expected, $actual);
79
    }
80
81
    public function testDiffArray()
82
    {
83
        $from = ['Lorem', ['array here please ignore'], 'ipsum dolor'];
84
        $to = 'Lorem,ipsum';
85
        $expected = "/^Lorem,ipsum *<del>dolor<\/del> *$/";
86
        $actual = Diff::compareHTML($from, $to);
0 ignored issues
show
Bug introduced by
$from of type array<integer,array<integer,string>|string> is incompatible with the type string expected by parameter $from of SilverStripe\View\Parsers\Diff::compareHTML(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

86
        $actual = Diff::compareHTML(/** @scrutinizer ignore-type */ $from, $to);
Loading history...
87
88
        $this->assertRegExp($expected, $actual);
89
    }
90
}
91