3 * This file is part of sebastian/diff.
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
11 namespace SebastianBergmann\Diff;
13 use SebastianBergmann\Diff\LCS\MemoryEfficientImplementation;
14 use SebastianBergmann\Diff\LCS\TimeEfficientImplementation;
15 use PHPUnit\Framework\TestCase;
18 * @covers SebastianBergmann\Diff\Differ
20 * @uses SebastianBergmann\Diff\LCS\MemoryEfficientImplementation
21 * @uses SebastianBergmann\Diff\LCS\TimeEfficientImplementation
22 * @uses SebastianBergmann\Diff\Chunk
23 * @uses SebastianBergmann\Diff\Diff
24 * @uses SebastianBergmann\Diff\Line
25 * @uses SebastianBergmann\Diff\Parser
27 class DifferTest extends TestCase
38 protected function setUp()
40 $this->differ = new Differ;
44 * @param array $expected
45 * @param string|array $from
46 * @param string|array $to
47 * @dataProvider arrayProvider
49 public function testArrayRepresentationOfDiffCanBeRenderedUsingTimeEfficientLcsImplementation(array $expected, $from, $to)
51 $this->assertEquals($expected, $this->differ->diffToArray($from, $to, new TimeEfficientImplementation));
55 * @param string $expected
58 * @dataProvider textProvider
60 public function testTextRepresentationOfDiffCanBeRenderedUsingTimeEfficientLcsImplementation($expected, $from, $to)
62 $this->assertEquals($expected, $this->differ->diff($from, $to, new TimeEfficientImplementation));
66 * @param array $expected
67 * @param string|array $from
68 * @param string|array $to
69 * @dataProvider arrayProvider
71 public function testArrayRepresentationOfDiffCanBeRenderedUsingMemoryEfficientLcsImplementation(array $expected, $from, $to)
73 $this->assertEquals($expected, $this->differ->diffToArray($from, $to, new MemoryEfficientImplementation));
77 * @param string $expected
80 * @dataProvider textProvider
82 public function testTextRepresentationOfDiffCanBeRenderedUsingMemoryEfficientLcsImplementation($expected, $from, $to)
84 $this->assertEquals($expected, $this->differ->diff($from, $to, new MemoryEfficientImplementation));
87 public function testCustomHeaderCanBeUsed()
89 $differ = new Differ('CUSTOM HEADER');
92 "CUSTOM HEADER@@ @@\n-a\n+b\n",
93 $differ->diff('a', 'b')
97 public function testTypesOtherThanArrayAndStringCanBePassed()
100 "--- Original\n+++ New\n@@ @@\n-1\n+2\n",
101 $this->differ->diff(1, 2)
106 * @param string $diff
107 * @param Diff[] $expected
108 * @dataProvider diffProvider
110 public function testParser($diff, array $expected)
112 $parser = new Parser;
113 $result = $parser->parse($diff);
115 $this->assertEquals($expected, $result);
118 public function arrayProvider()
123 array('a', self::REMOVED),
124 array('b', self::ADDED)
131 array('ba', self::REMOVED),
132 array('bc', self::ADDED)
139 array('ab', self::REMOVED),
140 array('cb', self::ADDED)
147 array('abc', self::REMOVED),
148 array('adc', self::ADDED)
155 array('ab', self::REMOVED),
156 array('abc', self::ADDED)
163 array('bc', self::REMOVED),
164 array('abc', self::ADDED)
171 array('abc', self::REMOVED),
172 array('abbc', self::ADDED)
179 array('abcdde', self::REMOVED),
180 array('abcde', self::ADDED)
185 'same start' => array(
187 array(17, self::OLD),
188 array('b', self::REMOVED),
189 array('d', self::ADDED),
191 array(30 => 17, 'a' => 'b'),
192 array(30 => 17, 'c' => 'd'),
196 array(1, self::REMOVED),
197 array(2, self::ADDED),
198 array('b', self::OLD),
200 array(1 => 1, 'a' => 'b'),
201 array(1 => 2, 'a' => 'b'),
203 'same start (2), same end (1)' => array(
205 array(17, self::OLD),
207 array(4, self::REMOVED),
208 array('a', self::ADDED),
209 array(5, self::ADDED),
210 array('x', self::OLD),
212 array(30 => 17, 1 => 2, 2 => 4, 'z' => 'x'),
213 array(30 => 17, 1 => 2, 3 => 'a', 2 => 5, 'z' => 'x'),
217 array('x', self::OLD),
224 array('y', self::REMOVED),
225 array('x', self::ADDED),
232 array('y', self::REMOVED),
233 array('b', self::REMOVED),
234 array('x', self::ADDED),
235 array('d', self::ADDED),
237 array('x' => 'y', 'a' => 'b'),
238 array('z' => 'x', 'c' => 'd'),
240 'test line diff detection' => array(
243 '#Warning: Strings contain different line endings!',
261 public function textProvider()
265 "--- Original\n+++ New\n@@ @@\n-a\n+b\n",
270 "--- Original\n+++ New\n@@ @@\n-ba\n+bc\n",
275 "--- Original\n+++ New\n@@ @@\n-ab\n+cb\n",
280 "--- Original\n+++ New\n@@ @@\n-abc\n+adc\n",
285 "--- Original\n+++ New\n@@ @@\n-ab\n+abc\n",
290 "--- Original\n+++ New\n@@ @@\n-bc\n+abc\n",
295 "--- Original\n+++ New\n@@ @@\n-abc\n+abbc\n",
300 "--- Original\n+++ New\n@@ @@\n-abcdde\n+abcde\n",
305 "--- Original\n+++ New\n@@ @@\n-A\n+A1\n B\n",
325 "a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk",
326 "a\np\nc\nd\ne\nf\ng\nh\ni\nw\nk",
344 "a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk",
345 "a\np\nc\nd\ne\nf\ng\nh\ni\nw\nk",
350 public function diffProvider()
352 $serialized_arr = <<<EOL
353 a:1:{i:0;O:27:"SebastianBergmann\Diff\Diff":3:{s:33:"
\0SebastianBergmann\Diff\Diff
\0from";s:7:"old.txt";s:31:"
\0SebastianBergmann\Diff\Diff
\0to";s:7:"new.txt";s:35:"
\0SebastianBergmann\Diff\Diff
\0chunks";a:3:{i:0;O:28:"SebastianBergmann\Diff\Chunk":5:{s:35:"
\0SebastianBergmann\Diff\Chunk
\0start";i:1;s:40:"
\0SebastianBergmann\Diff\Chunk
\0startRange";i:3;s:33:"
\0SebastianBergmann\Diff\Chunk
\0end";i:1;s:38:"
\0SebastianBergmann\Diff\Chunk
\0endRange";i:4;s:35:"
\0SebastianBergmann\Diff\Chunk
\0lines";a:4:{i:0;O:27:"SebastianBergmann\Diff\Line":2:{s:33:"
\0SebastianBergmann\Diff\Line
\0type";i:1;s:36:"
\0SebastianBergmann\Diff\Line
\0content";s:7:"2222111";}i:1;O:27:"SebastianBergmann\Diff\Line":2:{s:33:"
\0SebastianBergmann\Diff\Line
\0type";i:3;s:36:"
\0SebastianBergmann\Diff\Line
\0content";s:7:"1111111";}i:2;O:27:"SebastianBergmann\Diff\Line":2:{s:33:"
\0SebastianBergmann\Diff\Line
\0type";i:3;s:36:"
\0SebastianBergmann\Diff\Line
\0content";s:7:"1111111";}i:3;O:27:"SebastianBergmann\Diff\Line":2:{s:33:"
\0SebastianBergmann\Diff\Line
\0type";i:3;s:36:"
\0SebastianBergmann\Diff\Line
\0content";s:7:"1111111";}}}i:1;O:28:"SebastianBergmann\Diff\Chunk":5:{s:35:"
\0SebastianBergmann\Diff\Chunk
\0start";i:5;s:40:"
\0SebastianBergmann\Diff\Chunk
\0startRange";i:10;s:33:"
\0SebastianBergmann\Diff\Chunk
\0end";i:6;s:38:"
\0SebastianBergmann\Diff\Chunk
\0endRange";i:8;s:35:"
\0SebastianBergmann\Diff\Chunk
\0lines";a:11:{i:0;O:27:"SebastianBergmann\Diff\Line":2:{s:33:"
\0SebastianBergmann\Diff\Line
\0type";i:3;s:36:"
\0SebastianBergmann\Diff\Line
\0content";s:7:"1111111";}i:1;O:27:"SebastianBergmann\Diff\Line":2:{s:33:"
\0SebastianBergmann\Diff\Line
\0type";i:3;s:36:"
\0SebastianBergmann\Diff\Line
\0content";s:7:"1111111";}i:2;O:27:"SebastianBergmann\Diff\Line":2:{s:33:"
\0SebastianBergmann\Diff\Line
\0type";i:3;s:36:"
\0SebastianBergmann\Diff\Line
\0content";s:7:"1111111";}i:3;O:27:"SebastianBergmann\Diff\Line":2:{s:33:"
\0SebastianBergmann\Diff\Line
\0type";i:3;s:36:"
\0SebastianBergmann\Diff\Line
\0content";s:8:"+1121211";}i:4;O:27:"SebastianBergmann\Diff\Line":2:{s:33:"
\0SebastianBergmann\Diff\Line
\0type";i:3;s:36:"
\0SebastianBergmann\Diff\Line
\0content";s:7:"1111111";}i:5;O:27:"SebastianBergmann\Diff\Line":2:{s:33:"
\0SebastianBergmann\Diff\Line
\0type";i:3;s:36:"
\0SebastianBergmann\Diff\Line
\0content";s:8:"-1111111";}i:6;O:27:"SebastianBergmann\Diff\Line":2:{s:33:"
\0SebastianBergmann\Diff\Line
\0type";i:3;s:36:"
\0SebastianBergmann\Diff\Line
\0content";s:8:"-1111111";}i:7;O:27:"SebastianBergmann\Diff\Line":2:{s:33:"
\0SebastianBergmann\Diff\Line
\0type";i:3;s:36:"
\0SebastianBergmann\Diff\Line
\0content";s:8:"-2222222";}i:8;O:27:"SebastianBergmann\Diff\Line":2:{s:33:"
\0SebastianBergmann\Diff\Line
\0type";i:3;s:36:"
\0SebastianBergmann\Diff\Line
\0content";s:7:"2222222";}i:9;O:27:"SebastianBergmann\Diff\Line":2:{s:33:"
\0SebastianBergmann\Diff\Line
\0type";i:3;s:36:"
\0SebastianBergmann\Diff\Line
\0content";s:7:"2222222";}i:10;O:27:"SebastianBergmann\Diff\Line":2:{s:33:"
\0SebastianBergmann\Diff\Line
\0type";i:3;s:36:"
\0SebastianBergmann\Diff\Line
\0content";s:7:"2222222";}}}i:2;O:28:"SebastianBergmann\Diff\Chunk":5:{s:35:"
\0SebastianBergmann\Diff\Chunk
\0start";i:17;s:40:"
\0SebastianBergmann\Diff\Chunk
\0startRange";i:5;s:33:"
\0SebastianBergmann\Diff\Chunk
\0end";i:16;s:38:"
\0SebastianBergmann\Diff\Chunk
\0endRange";i:6;s:35:"
\0SebastianBergmann\Diff\Chunk
\0lines";a:6:{i:0;O:27:"SebastianBergmann\Diff\Line":2:{s:33:"
\0SebastianBergmann\Diff\Line
\0type";i:3;s:36:"
\0SebastianBergmann\Diff\Line
\0content";s:7:"2222222";}i:1;O:27:"SebastianBergmann\Diff\Line":2:{s:33:"
\0SebastianBergmann\Diff\Line
\0type";i:3;s:36:"
\0SebastianBergmann\Diff\Line
\0content";s:7:"2222222";}i:2;O:27:"SebastianBergmann\Diff\Line":2:{s:33:"
\0SebastianBergmann\Diff\Line
\0type";i:3;s:36:"
\0SebastianBergmann\Diff\Line
\0content";s:7:"2222222";}i:3;O:27:"SebastianBergmann\Diff\Line":2:{s:33:"
\0SebastianBergmann\Diff\Line
\0type";i:3;s:36:"
\0SebastianBergmann\Diff\Line
\0content";s:8:"+2122212";}i:4;O:27:"SebastianBergmann\Diff\Line":2:{s:33:"
\0SebastianBergmann\Diff\Line
\0type";i:3;s:36:"
\0SebastianBergmann\Diff\Line
\0content";s:7:"2222222";}i:5;O:27:"SebastianBergmann\Diff\Line":2:{s:33:"
\0SebastianBergmann\Diff\Line
\0type";i:3;s:36:"
\0SebastianBergmann\Diff\Line
\0content";s:7:"2222222";}}}}}}
358 "--- old.txt 2014-11-04 08:51:02.661868729 +0300\n+++ new.txt 2014-11-04 08:51:02.665868730 +0300\n@@ -1,3 +1,4 @@\n+2222111\n 1111111\n 1111111\n 1111111\n@@ -5,10 +6,8 @@\n 1111111\n 1111111\n 1111111\n +1121211\n 1111111\n -1111111\n -1111111\n -2222222\n 2222222\n 2222222\n 2222222\n@@ -17,5 +16,6 @@\n 2222222\n 2222222\n 2222222\n +2122212\n 2222222\n 2222222\n",
359 \unserialize($serialized_arr)
365 * @param string $expected
366 * @param string $from
368 * @dataProvider textForNoNonDiffLinesProvider
370 public function testDiffDoNotShowNonDiffLines($expected, $from, $to)
372 $differ = new Differ('', false);
373 $this->assertSame($expected, $differ->diff($from, $to));
376 public function textForNoNonDiffLinesProvider()
391 * @requires PHPUnit 5.7
393 public function testDiffToArrayInvalidFromType()
395 $differ = new Differ;
397 $this->expectException('\InvalidArgumentException');
398 $this->expectExceptionMessageRegExp('#^"from" must be an array or string\.$#');
400 $differ->diffToArray(null, '');
404 * @requires PHPUnit 5.7
406 public function testDiffInvalidToType()
408 $differ = new Differ;
410 $this->expectException('\InvalidArgumentException');
411 $this->expectExceptionMessageRegExp('#^"to" must be an array or string\.$#');
413 $differ->diffToArray('', new \stdClass);