Passed
Push — master ( 1863b6...b24285 )
by Steve
32s
created

DiffTest::testLineAdditions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 0
dl 0
loc 19
rs 9.8333
c 0
b 0
f 0
1
<?php
2
/**
3
 * (c) Steve Nebes <[email protected]>
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
declare(strict_types=1);
10
11
namespace SN\RangeDifferencer;
12
13
use PHPUnit\Framework\TestCase;
14
15
class DiffTest extends TestCase
16
{
17
    public function testLineAdditions(): void
18
    {
19
        $s1 = 'abc' . PHP_EOL . 'def' . PHP_EOL . 'xyz';
20
        $s2 = 'abc' . PHP_EOL . 'def' . PHP_EOL . '123' . PHP_EOL . 'xyz';
21
22
        $l1 = TextLineLCS::getTextLines($s1);
23
        $l2 = TextLineLCS::getTextLines($s2);
24
        $lcs = new TextLineLCS($l1, $l2);
25
        $lcs->longestCommonSubsequence();
26
27
        /** @var TextLine[][] $result */
28
        $result = $lcs->getResult();
29
30
        $this->assertSame(0, $result[0][0]->getLineNumber());
31
        $this->assertSame(0, $result[1][0]->getLineNumber());
32
        $this->assertSame(1, $result[0][1]->getLineNumber());
33
        $this->assertSame(1, $result[1][1]->getLineNumber());
34
        $this->assertSame(2, $result[0][2]->getLineNumber());
35
        $this->assertSame(3, $result[1][2]->getLineNumber());
36
    }
37
38
    public function testLineDeletion(): void
39
    {
40
        $s1 = 'abc' . PHP_EOL . 'def' . PHP_EOL . '123' . PHP_EOL . 'xyz';
41
        $s2 = 'abc' . PHP_EOL . 'def' . PHP_EOL . 'xyz';
42
43
        $l1 = TextLineLCS::getTextLines($s1);
44
        $l2 = TextLineLCS::getTextLines($s2);
45
        $lcs = new TextLineLCS($l1, $l2);
46
        $lcs->longestCommonSubsequence();
47
48
        /** @var TextLine[][] $result */
49
        $result = $lcs->getResult();
50
51
        $this->assertTrue(\count($result[0]) === \count($result[1]));
52
53
        for ($i = 0; $i < \count($result[0]); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
54
            $this->assertTrue($result[0][$i]->isSameText($result[1][$i]));
55
        }
56
57
        $this->assertSame(0, $result[0][0]->getLineNumber());
58
        $this->assertSame(1, $result[0][1]->getLineNumber());
59
        $this->assertSame(3, $result[0][2]->getLineNumber());
60
        $this->assertSame(0, $result[1][0]->getLineNumber());
61
        $this->assertSame(1, $result[1][1]->getLineNumber());
62
        $this->assertSame(2, $result[1][2]->getLineNumber());
63
    }
64
65
    public function testLineAppendEnd(): void
66
    {
67
        $s1 = 'abc' . PHP_EOL . 'def';
68
        $s2 = 'abc' . PHP_EOL . 'def' . PHP_EOL . '123';
69
70
        $l1 = TextLineLCS::getTextLines($s1);
71
        $l2 = TextLineLCS::getTextLines($s2);
72
        $lcs = new TextLineLCS($l1, $l2);
73
        $lcs->longestCommonSubsequence();
74
75
        /** @var TextLine[][] $result */
76
        $result = $lcs->getResult();
77
78
        $this->assertSame(2, \count($result[0]));
79
        $this->assertSame(2, \count($result[1]));
80
81
        for ($i = 0; $i < \count($result[0]); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
82
            $this->assertTrue($result[0][$i]->isSameText($result[1][$i]));
83
        }
84
85
        $this->assertSame(0, $result[0][0]->getLineNumber());
86
        $this->assertSame(0, $result[1][0]->getLineNumber());
87
        $this->assertSame(1, $result[0][1]->getLineNumber());
88
        $this->assertSame(1, $result[1][1]->getLineNumber());
89
    }
90
91
    public function testLineDeleteEnd(): void
92
    {
93
        $s1 = 'abc' . PHP_EOL . 'def' . PHP_EOL . '123';
94
        $s2 = 'abc' . PHP_EOL . 'def';
95
96
        $l1 = TextLineLCS::getTextLines($s1);
97
        $l2 = TextLineLCS::getTextLines($s2);
98
        $lcs = new TextLineLCS($l1, $l2);
99
        $lcs->longestCommonSubsequence();
100
101
        /** @var TextLine[][] $result */
102
        $result = $lcs->getResult();
103
104
        $this->assertSame(2, \count($result[0]));
105
        $this->assertSame(2, \count($result[1]));
106
107
        for ($i = 0; $i < \count($result[0]); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
108
            $this->assertTrue($result[0][$i]->isSameText($result[1][$i]));
109
        }
110
111
        $this->assertSame(0, $result[0][0]->getLineNumber());
112
        $this->assertSame(0, $result[1][0]->getLineNumber());
113
        $this->assertSame(1, $result[0][1]->getLineNumber());
114
        $this->assertSame(1, $result[1][1]->getLineNumber());
115
    }
116
117
    public function testLineAppendStart(): void
118
    {
119
        $s1 = 'abc' . PHP_EOL . 'def';
120
        $s2 = '123' . PHP_EOL . 'abc' . PHP_EOL . 'def';
121
122
        $l1 = TextLineLCS::getTextLines($s1);
123
        $l2 = TextLineLCS::getTextLines($s2);
124
        $lcs = new TextLineLCS($l1, $l2);
125
        $lcs->longestCommonSubsequence();
126
127
        /** @var TextLine[][] $result */
128
        $result = $lcs->getResult();
129
130
        $this->assertSame(2, \count($result[0]));
131
        $this->assertSame(2, \count($result[1]));
132
133
        for ($i = 0; $i < \count($result[0]); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
134
            $this->assertTrue($result[0][$i]->isSameText($result[1][$i]));
135
        }
136
137
        $this->assertSame(0, $result[0][0]->getLineNumber());
138
        $this->assertSame(1, $result[1][0]->getLineNumber());
139
        $this->assertSame(1, $result[0][1]->getLineNumber());
140
        $this->assertSame(2, $result[1][1]->getLineNumber());
141
    }
142
143
    public function testLineDeleteStart(): void
144
    {
145
        $s1 = '123' . PHP_EOL . 'abc' . PHP_EOL . 'def';
146
        $s2 = 'abc' . PHP_EOL . 'def';
147
148
        $l1 = TextLineLCS::getTextLines($s1);
149
        $l2 = TextLineLCS::getTextLines($s2);
150
        $lcs = new TextLineLCS($l1, $l2);
151
        $lcs->longestCommonSubsequence();
152
153
        /** @var TextLine[][] $result */
154
        $result = $lcs->getResult();
155
156
        $this->assertSame(2, \count($result[0]));
157
        $this->assertSame(2, \count($result[1]));
158
159
        for ($i = 0; $i < \count($result[0]); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
160
            $this->assertTrue($result[0][$i]->isSameText($result[1][$i]));
161
        }
162
163
        $this->assertSame(1, $result[0][0]->getLineNumber());
164
        $this->assertSame(0, $result[1][0]->getLineNumber());
165
        $this->assertSame(2, $result[0][1]->getLineNumber());
166
        $this->assertSame(1, $result[1][1]->getLineNumber());
167
    }
168
}
169