| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* PHPUnit
|
|
|
4 |
*
|
|
|
5 |
* Copyright (c) 2002-2010, Sebastian Bergmann <sb@sebastian-bergmann.de>.
|
|
|
6 |
* All rights reserved.
|
|
|
7 |
*
|
|
|
8 |
* Redistribution and use in source and binary forms, with or without
|
|
|
9 |
* modification, are permitted provided that the following conditions
|
|
|
10 |
* are met:
|
|
|
11 |
*
|
|
|
12 |
* * Redistributions of source code must retain the above copyright
|
|
|
13 |
* notice, this list of conditions and the following disclaimer.
|
|
|
14 |
*
|
|
|
15 |
* * Redistributions in binary form must reproduce the above copyright
|
|
|
16 |
* notice, this list of conditions and the following disclaimer in
|
|
|
17 |
* the documentation and/or other materials provided with the
|
|
|
18 |
* distribution.
|
|
|
19 |
*
|
|
|
20 |
* * Neither the name of Sebastian Bergmann nor the names of his
|
|
|
21 |
* contributors may be used to endorse or promote products derived
|
|
|
22 |
* from this software without specific prior written permission.
|
|
|
23 |
*
|
|
|
24 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
25 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
26 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
27 |
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
28 |
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
29 |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
30 |
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
31 |
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
32 |
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
33 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
34 |
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
35 |
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
36 |
*
|
|
|
37 |
* @category Testing
|
|
|
38 |
* @package PHPUnit
|
|
|
39 |
* @author Sebastian Bergmann <sb@sebastian-bergmann.de>
|
|
|
40 |
* @author Kore Nordmann <mail@kore-nordmann.de>
|
|
|
41 |
* @copyright 2002-2010 Sebastian Bergmann <sb@sebastian-bergmann.de>
|
|
|
42 |
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
|
|
|
43 |
* @link http://www.phpunit.de/
|
|
|
44 |
* @since File available since Release 3.4.0
|
|
|
45 |
*/
|
|
|
46 |
|
|
|
47 |
require_once 'PHPUnit/Util/Filesystem.php';
|
|
|
48 |
require_once 'PHPUnit/Util/Filter.php';
|
|
|
49 |
|
|
|
50 |
PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
|
|
|
51 |
|
|
|
52 |
/**
|
|
|
53 |
* Diff implementation.
|
|
|
54 |
*
|
|
|
55 |
* @category Testing
|
|
|
56 |
* @package PHPUnit
|
|
|
57 |
* @author Sebastian Bergmann <sb@sebastian-bergmann.de>
|
|
|
58 |
* @author Kore Nordmann <mail@kore-nordmann.de>
|
|
|
59 |
* @copyright 2002-2010 Sebastian Bergmann <sb@sebastian-bergmann.de>
|
|
|
60 |
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
|
|
|
61 |
* @version Release: 3.4.15
|
|
|
62 |
* @link http://www.phpunit.de/
|
|
|
63 |
* @since Class available since Release 3.4.0
|
|
|
64 |
*/
|
|
|
65 |
class PHPUnit_Util_Diff
|
|
|
66 |
{
|
|
|
67 |
/**
|
|
|
68 |
* Returns the diff between two arrays or strings.
|
|
|
69 |
*
|
|
|
70 |
* @param array|string $from
|
|
|
71 |
* @param array|string $to
|
|
|
72 |
* @return string
|
|
|
73 |
*/
|
|
|
74 |
public static function diff($from, $to)
|
|
|
75 |
{
|
|
|
76 |
if (is_string($from)) {
|
|
|
77 |
$from = preg_split('(\r\n|\r|\n)', $from);
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
if (is_string($to)) {
|
|
|
81 |
$to = preg_split('(\r\n|\r|\n)', $to);
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
$buffer = "--- Expected\n+++ Actual\n";
|
|
|
85 |
$start = array();
|
|
|
86 |
$end = array();
|
|
|
87 |
$fromLength = count($from);
|
|
|
88 |
$toLength = count($to);
|
|
|
89 |
$length = min($fromLength, $toLength);
|
|
|
90 |
|
|
|
91 |
for ($i = 0; $i < $length; ++$i) {
|
|
|
92 |
if ($from[$i] === $to[$i]) {
|
|
|
93 |
$start[] = $from[$i];
|
|
|
94 |
unset($from[$i], $to[$i]);
|
|
|
95 |
} else {
|
|
|
96 |
break;
|
|
|
97 |
}
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
$length -= $i;
|
|
|
101 |
|
|
|
102 |
for ($i = 1; $i < $length; ++$i) {
|
|
|
103 |
if ($from[$fromLength - $i] === $to[$toLength - $i]) {
|
|
|
104 |
array_unshift($end, $from[$fromLength - $i]);
|
|
|
105 |
unset($from[$fromLength - $i], $to[$toLength - $i]);
|
|
|
106 |
} else {
|
|
|
107 |
break;
|
|
|
108 |
}
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
$common = self::longestCommonSubsequence(
|
|
|
112 |
array_values($from), array_values($to)
|
|
|
113 |
);
|
|
|
114 |
|
|
|
115 |
$diff = array();
|
|
|
116 |
$line = 0;
|
|
|
117 |
|
|
|
118 |
foreach ($start as $token) {
|
|
|
119 |
$diff[] = array($token, 0 /* OLD */);
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
reset($from);
|
|
|
123 |
reset($to);
|
|
|
124 |
|
|
|
125 |
foreach ($common as $token) {
|
|
|
126 |
while ((($fromToken = reset($from)) !== $token)) {
|
|
|
127 |
$diff[] = array(array_shift($from), 2 /* REMOVED */);
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
while ((($toToken = reset($to)) !== $token)) {
|
|
|
131 |
$diff[] = array(array_shift($to), 1 /* ADDED */);
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
$diff[] = array($token, 0 /* OLD */);
|
|
|
135 |
|
|
|
136 |
array_shift($from);
|
|
|
137 |
array_shift($to);
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
while (($token = array_shift($from)) !== NULL) {
|
|
|
141 |
$diff[] = array($token, 2 /* REMOVED */);
|
|
|
142 |
}
|
|
|
143 |
|
|
|
144 |
while (($token = array_shift($to)) !== NULL) {
|
|
|
145 |
$diff[] = array($token, 1 /* ADDED */);
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
foreach ($end as $token) {
|
|
|
149 |
$diff[] = array($token, 0 /* OLD */);
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
$inOld = FALSE;
|
|
|
153 |
$i = 0;
|
|
|
154 |
$old = array();
|
|
|
155 |
|
|
|
156 |
foreach ($diff as $line) {
|
|
|
157 |
if ($line[1] === 0 /* OLD */) {
|
|
|
158 |
if ($inOld === FALSE) {
|
|
|
159 |
$inOld = $i;
|
|
|
160 |
}
|
|
|
161 |
}
|
|
|
162 |
|
|
|
163 |
else if ($inOld !== FALSE) {
|
|
|
164 |
if (($i - $inOld) > 5) {
|
|
|
165 |
$old[$inOld] = $i - 1;
|
|
|
166 |
}
|
|
|
167 |
|
|
|
168 |
$inOld = FALSE;
|
|
|
169 |
}
|
|
|
170 |
|
|
|
171 |
++$i;
|
|
|
172 |
}
|
|
|
173 |
|
|
|
174 |
$start = isset($old[0]) ? $old[0] : 0;
|
|
|
175 |
$end = count($diff);
|
|
|
176 |
$i = 0;
|
|
|
177 |
|
|
|
178 |
if ($tmp = array_search($end, $old)) {
|
|
|
179 |
$end = $tmp;
|
|
|
180 |
}
|
|
|
181 |
|
|
|
182 |
$newChunk = TRUE;
|
|
|
183 |
|
|
|
184 |
for ($i = $start; $i < $end; $i++) {
|
|
|
185 |
if (isset($old[$i])) {
|
|
|
186 |
$buffer .= "\n";
|
|
|
187 |
$newChunk = TRUE;
|
|
|
188 |
$i = $old[$i];
|
|
|
189 |
}
|
|
|
190 |
|
|
|
191 |
if ($newChunk) {
|
|
|
192 |
// TODO: Implement chunk range information.
|
|
|
193 |
$buffer .= "@@ @@\n";
|
|
|
194 |
$newChunk = FALSE;
|
|
|
195 |
}
|
|
|
196 |
|
|
|
197 |
if ($diff[$i][1] === 1 /* ADDED */) {
|
|
|
198 |
$buffer .= '+' . $diff[$i][0] . "\n";
|
|
|
199 |
}
|
|
|
200 |
|
|
|
201 |
else if ($diff[$i][1] === 2 /* REMOVED */) {
|
|
|
202 |
$buffer .= '-' . $diff[$i][0] . "\n";
|
|
|
203 |
}
|
|
|
204 |
|
|
|
205 |
else {
|
|
|
206 |
$buffer .= ' ' . $diff[$i][0] . "\n";
|
|
|
207 |
}
|
|
|
208 |
}
|
|
|
209 |
|
|
|
210 |
return $buffer;
|
|
|
211 |
}
|
|
|
212 |
|
|
|
213 |
/**
|
|
|
214 |
* Calculates the longest common subsequence of two arrays.
|
|
|
215 |
*
|
|
|
216 |
* @param array $from
|
|
|
217 |
* @param array $to
|
|
|
218 |
* @return array
|
|
|
219 |
*/
|
|
|
220 |
protected static function longestCommonSubsequence(array $from, array $to)
|
|
|
221 |
{
|
|
|
222 |
$common = array();
|
|
|
223 |
$longest = 0;
|
|
|
224 |
$matrix = array();
|
|
|
225 |
$fromLength = count($from);
|
|
|
226 |
$toLength = count($to);
|
|
|
227 |
|
|
|
228 |
for ($i = 0; $i <= $fromLength; ++$i) {
|
|
|
229 |
$matrix[$i][0] = 0;
|
|
|
230 |
}
|
|
|
231 |
|
|
|
232 |
for ($j = 0; $j <= $toLength; ++$j) {
|
|
|
233 |
$matrix[0][$j] = 0;
|
|
|
234 |
}
|
|
|
235 |
|
|
|
236 |
for ($i = 1; $i <= $fromLength; ++$i) {
|
|
|
237 |
for ($j = 1; $j <= $toLength; ++$j) {
|
|
|
238 |
$matrix[$i][$j] = max(
|
|
|
239 |
$matrix[$i-1][$j],
|
|
|
240 |
$matrix[$i][$j-1],
|
|
|
241 |
$from[$i-1] === $to[$j-1] ? $matrix[$i-1][$j-1] + 1 : 0
|
|
|
242 |
);
|
|
|
243 |
}
|
|
|
244 |
}
|
|
|
245 |
|
|
|
246 |
$i = $fromLength;
|
|
|
247 |
$j = $toLength;
|
|
|
248 |
|
|
|
249 |
while ($i > 0 && $j > 0) {
|
|
|
250 |
if ($from[$i-1] === $to[$j-1]) {
|
|
|
251 |
array_unshift($common, $from[$i-1]);
|
|
|
252 |
--$i;
|
|
|
253 |
--$j;
|
|
|
254 |
}
|
|
|
255 |
|
|
|
256 |
else if ($matrix[$i][$j-1] > $matrix[$i-1][$j]) {
|
|
|
257 |
--$j;
|
|
|
258 |
}
|
|
|
259 |
|
|
|
260 |
else {
|
|
|
261 |
--$i;
|
|
|
262 |
}
|
|
|
263 |
}
|
|
|
264 |
|
|
|
265 |
return $common;
|
|
|
266 |
}
|
|
|
267 |
}
|
|
|
268 |
?>
|