Skip to content

Commit 5d0ff52

Browse files
Merge pull request sebastianbergmann#493 from pscheit/3.6
2 parents 6764f04 + cc354b0 commit 5d0ff52

File tree

2 files changed

+208
-61
lines changed

2 files changed

+208
-61
lines changed

PHPUnit/Util/Diff.php

Lines changed: 83 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,93 @@
6060
class PHPUnit_Util_Diff
6161
{
6262
/**
63-
* Returns the diff between two arrays or strings.
63+
* Returns the diff between two arrays or strings as string.
6464
*
6565
* @param array|string $from
6666
* @param array|string $to
6767
* @return string
6868
*/
6969
public static function diff($from, $to)
7070
{
71+
$buffer= "--- Expected\n+++ Actual\n";
72+
$diff = self::diffToArray($from,$to);
73+
74+
$inOld = FALSE;
75+
$i = 0;
76+
$old = array();
77+
78+
foreach ($diff as $line) {
79+
if ($line[1] === 0 /* OLD */) {
80+
if ($inOld === FALSE) {
81+
$inOld = $i;
82+
}
83+
}
84+
85+
else if ($inOld !== FALSE) {
86+
if (($i - $inOld) > 5) {
87+
$old[$inOld] = $i - 1;
88+
}
89+
90+
$inOld = FALSE;
91+
}
92+
93+
++$i;
94+
}
95+
96+
$start = isset($old[0]) ? $old[0] : 0;
97+
$end = count($diff);
98+
$i = 0;
99+
100+
if ($tmp = array_search($end, $old)) {
101+
$end = $tmp;
102+
}
103+
104+
$newChunk = TRUE;
105+
106+
for ($i = $start; $i < $end; $i++) {
107+
if (isset($old[$i])) {
108+
$buffer .= "\n";
109+
$newChunk = TRUE;
110+
$i = $old[$i];
111+
}
112+
113+
if ($newChunk) {
114+
$buffer .= "@@ @@\n";
115+
$newChunk = FALSE;
116+
}
117+
118+
if ($diff[$i][1] === 1 /* ADDED */) {
119+
$buffer .= '+' . $diff[$i][0] . "\n";
120+
}
121+
122+
else if ($diff[$i][1] === 2 /* REMOVED */) {
123+
$buffer .= '-' . $diff[$i][0] . "\n";
124+
}
125+
126+
else {
127+
$buffer .= ' ' . $diff[$i][0] . "\n";
128+
}
129+
}
130+
131+
return $buffer;
132+
}
133+
134+
/**
135+
* Returns the diff between two arrays or strings as array.
136+
*
137+
* every array-entry containts two elements:
138+
* - [0] => string $token
139+
* - [1] => 2|1|0
140+
*
141+
* - 2: REMOVED: $token was removed from $from
142+
* - 1: ADDED: $token was added to $from
143+
* - 0: OLD: $token is not changed in $to
144+
*
145+
* @param array|string $from
146+
* @param array|string $to
147+
* @return array
148+
*/
149+
public static function diffToArray($from, $to) {
71150
if (is_string($from)) {
72151
$from = preg_split('(\r\n|\r|\n)', $from);
73152
}
@@ -76,7 +155,7 @@ public static function diff($from, $to)
76155
$to = preg_split('(\r\n|\r|\n)', $to);
77156
}
78157

79-
$buffer = "--- Expected\n+++ Actual\n";
158+
80159
$start = array();
81160
$end = array();
82161
$fromLength = count($from);
@@ -143,65 +222,8 @@ public static function diff($from, $to)
143222
foreach ($end as $token) {
144223
$diff[] = array($token, 0 /* OLD */);
145224
}
146-
147-
$inOld = FALSE;
148-
$i = 0;
149-
$old = array();
150-
151-
foreach ($diff as $line) {
152-
if ($line[1] === 0 /* OLD */) {
153-
if ($inOld === FALSE) {
154-
$inOld = $i;
155-
}
156-
}
157-
158-
else if ($inOld !== FALSE) {
159-
if (($i - $inOld) > 5) {
160-
$old[$inOld] = $i - 1;
161-
}
162-
163-
$inOld = FALSE;
164-
}
165-
166-
++$i;
167-
}
168-
169-
$start = isset($old[0]) ? $old[0] : 0;
170-
$end = count($diff);
171-
$i = 0;
172-
173-
if ($tmp = array_search($end, $old)) {
174-
$end = $tmp;
175-
}
176-
177-
$newChunk = TRUE;
178-
179-
for ($i = $start; $i < $end; $i++) {
180-
if (isset($old[$i])) {
181-
$buffer .= "\n";
182-
$newChunk = TRUE;
183-
$i = $old[$i];
184-
}
185-
186-
if ($newChunk) {
187-
$buffer .= "@@ @@\n";
188-
$newChunk = FALSE;
189-
}
190-
191-
if ($diff[$i][1] === 1 /* ADDED */) {
192-
$buffer .= '+' . $diff[$i][0] . "\n";
193-
}
194-
195-
else if ($diff[$i][1] === 2 /* REMOVED */) {
196-
$buffer .= '-' . $diff[$i][0] . "\n";
197-
}
198-
199-
else {
200-
$buffer .= ' ' . $diff[$i][0] . "\n";
201-
}
202-
}
203-
204-
return $buffer;
225+
226+
return $diff;
205227
}
206228

207229
/**

Tests/Util/DiffTest.php

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@
5555
*/
5656
class Util_DiffTest extends PHPUnit_Framework_TestCase
5757
{
58+
59+
const REMOVED = 2;
60+
const ADDED = 1;
61+
const OLD = 0;
62+
5863
/**
5964
* @covers PHPUnit_Util_Diff::diff
6065
*/
@@ -66,6 +71,21 @@ public function testComparisonErrorMessage()
6671
);
6772
}
6873

74+
/**
75+
* @covers PHPUnit_Util_Diff::diffToArray
76+
*/
77+
public function testComparisonErrorMessage_toArray()
78+
{
79+
$diff = array();
80+
$diff[] = array('a', self::REMOVED);
81+
$diff[] = array('b', self::ADDED);
82+
83+
$this->assertEquals(
84+
$diff,
85+
PHPUnit_Util_Diff::diffToArray('a', 'b')
86+
);
87+
}
88+
6989
/**
7090
* @covers PHPUnit_Util_Diff::diff
7191
*/
@@ -77,6 +97,21 @@ public function testComparisonErrorStartSame()
7797
);
7898
}
7999

100+
/**
101+
* @covers PHPUnit_Util_Diff::diffToArray
102+
*/
103+
public function testComparisonErrorStartSame_toArray()
104+
{
105+
$diff = array();
106+
$diff[] = array('ba', self::REMOVED);
107+
$diff[] = array('bc', self::ADDED);
108+
109+
$this->assertEquals(
110+
$diff,
111+
PHPUnit_Util_Diff::diffToArray('ba', 'bc')
112+
);
113+
}
114+
80115
/**
81116
* @covers PHPUnit_Util_Diff::diff
82117
*/
@@ -88,6 +123,21 @@ public function testComparisonErrorEndSame()
88123
);
89124
}
90125

126+
/**
127+
* @covers PHPUnit_Util_Diff::diffToArray
128+
*/
129+
public function testComparisonErrorEndSame_toArray()
130+
{
131+
$diff = array();
132+
$diff[] = array('ab', self::REMOVED);
133+
$diff[] = array('cb', self::ADDED);
134+
135+
$this->assertEquals(
136+
$diff,
137+
PHPUnit_Util_Diff::diffToArray('ab', 'cb')
138+
);
139+
}
140+
91141
/**
92142
* @covers PHPUnit_Util_Diff::diff
93143
*/
@@ -99,6 +149,21 @@ public function testComparisonErrorStartAndEndSame()
99149
);
100150
}
101151

152+
/**
153+
* @covers PHPUnit_Util_Diff::diffToArray
154+
*/
155+
public function testComparisonErrorStartAndEndSame_toArray()
156+
{
157+
$diff = array();
158+
$diff[] = array('abc', self::REMOVED);
159+
$diff[] = array('adc', self::ADDED);
160+
161+
$this->assertEquals(
162+
$diff,
163+
PHPUnit_Util_Diff::diffToArray('abc', 'adc')
164+
);
165+
}
166+
102167
/**
103168
* @covers PHPUnit_Util_Diff::diff
104169
*/
@@ -110,6 +175,21 @@ public function testComparisonErrorStartSameComplete()
110175
);
111176
}
112177

178+
/**
179+
* @covers PHPUnit_Util_Diff::diffToArray
180+
*/
181+
public function testComparisonErrorStartSameComplete_toArray()
182+
{
183+
$diff = array();
184+
$diff[] = array('ab', self::REMOVED);
185+
$diff[] = array('abc', self::ADDED);
186+
187+
$this->assertEquals(
188+
$diff,
189+
PHPUnit_Util_Diff::diffToArray('ab', 'abc')
190+
);
191+
}
192+
113193
/**
114194
* @covers PHPUnit_Util_Diff::diff
115195
*/
@@ -121,6 +201,21 @@ public function testComparisonErrorEndSameComplete()
121201
);
122202
}
123203

204+
/**
205+
* @covers PHPUnit_Util_Diff::diffToArray
206+
*/
207+
public function testComparisonErrorEndSameComplete_toArray()
208+
{
209+
$diff = array();
210+
$diff[] = array('bc', self::REMOVED);
211+
$diff[] = array('abc', self::ADDED);
212+
213+
$this->assertEquals(
214+
$diff,
215+
PHPUnit_Util_Diff::diffToArray('bc', 'abc')
216+
);
217+
}
218+
124219
/**
125220
* @covers PHPUnit_Util_Diff::diff
126221
*/
@@ -132,6 +227,21 @@ public function testComparisonErrorOverlapingMatches()
132227
);
133228
}
134229

230+
/**
231+
* @covers PHPUnit_Util_Diff::diffToArray
232+
*/
233+
public function testComparisonErrorOverlapingMatches_toArray()
234+
{
235+
$diff = array();
236+
$diff[] = array('abc', self::REMOVED);
237+
$diff[] = array('abbc', self::ADDED);
238+
239+
$this->assertEquals(
240+
$diff,
241+
PHPUnit_Util_Diff::diffToArray('abc', 'abbc')
242+
);
243+
}
244+
135245
/**
136246
* @covers PHPUnit_Util_Diff::diff
137247
*/
@@ -142,4 +252,19 @@ public function testComparisonErrorOverlapingMatches2()
142252
PHPUnit_Util_Diff::diff('abcdde', 'abcde')
143253
);
144254
}
255+
256+
/**
257+
* @covers PHPUnit_Util_Diff::diffToArray
258+
*/
259+
public function testComparisonErrorOverlapingMatches2_toArray()
260+
{
261+
$diff = array();
262+
$diff[] = array('abcdde', self::REMOVED);
263+
$diff[] = array('abcde', self::ADDED);
264+
265+
$this->assertEquals(
266+
$diff,
267+
PHPUnit_Util_Diff::diffToArray('abcdde', 'abcde')
268+
);
269+
}
145270
}

0 commit comments

Comments
 (0)