1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042 |
- <?php declare(strict_types=1);
- /*
- * This file is part of sebastian/diff.
- *
- * (c) Sebastian Bergmann <sebastian@phpunit.de>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace SebastianBergmann\Diff;
- use PHPUnit\Framework\TestCase;
- use SebastianBergmann\Diff\Output\AbstractChunkOutputBuilder;
- use SebastianBergmann\Diff\Output\DiffOnlyOutputBuilder;
- use SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder;
- /**
- * @covers SebastianBergmann\Diff\Differ
- * @covers SebastianBergmann\Diff\Output\AbstractChunkOutputBuilder
- * @covers SebastianBergmann\Diff\Output\DiffOnlyOutputBuilder
- * @covers SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder
- *
- * @uses SebastianBergmann\Diff\MemoryEfficientLongestCommonSubsequenceCalculator
- * @uses SebastianBergmann\Diff\TimeEfficientLongestCommonSubsequenceCalculator
- * @uses SebastianBergmann\Diff\Chunk
- * @uses SebastianBergmann\Diff\Diff
- * @uses SebastianBergmann\Diff\Line
- * @uses SebastianBergmann\Diff\Parser
- */
- final class DifferTest extends TestCase
- {
- const WARNING = 3;
- const REMOVED = 2;
- const ADDED = 1;
- const OLD = 0;
- /**
- * @var Differ
- */
- private $differ;
- protected function setUp()
- {
- $this->differ = new Differ;
- }
- /**
- * @param array $expected
- * @param string|array $from
- * @param string|array $to
- * @dataProvider arrayProvider
- */
- public function testArrayRepresentationOfDiffCanBeRenderedUsingTimeEfficientLcsImplementation(array $expected, $from, $to)
- {
- $this->assertSame($expected, $this->differ->diffToArray($from, $to, new TimeEfficientLongestCommonSubsequenceCalculator));
- }
- /**
- * @param string $expected
- * @param string $from
- * @param string $to
- * @dataProvider textProvider
- */
- public function testTextRepresentationOfDiffCanBeRenderedUsingTimeEfficientLcsImplementation(string $expected, string $from, string $to)
- {
- $this->assertSame($expected, $this->differ->diff($from, $to, new TimeEfficientLongestCommonSubsequenceCalculator));
- }
- /**
- * @param array $expected
- * @param string|array $from
- * @param string|array $to
- * @dataProvider arrayProvider
- */
- public function testArrayRepresentationOfDiffCanBeRenderedUsingMemoryEfficientLcsImplementation(array $expected, $from, $to)
- {
- $this->assertSame($expected, $this->differ->diffToArray($from, $to, new MemoryEfficientLongestCommonSubsequenceCalculator));
- }
- /**
- * @param string $expected
- * @param string $from
- * @param string $to
- * @dataProvider textProvider
- */
- public function testTextRepresentationOfDiffCanBeRenderedUsingMemoryEfficientLcsImplementation(string $expected, string $from, string $to)
- {
- $this->assertSame($expected, $this->differ->diff($from, $to, new MemoryEfficientLongestCommonSubsequenceCalculator));
- }
- /**
- * @param string $expected
- * @param string $from
- * @param string $to
- * @param string $header
- * @dataProvider headerProvider
- */
- public function testCustomHeaderCanBeUsed(string $expected, string $from, string $to, string $header)
- {
- $differ = new Differ(new UnifiedDiffOutputBuilder($header));
- $this->assertSame(
- $expected,
- $differ->diff($from, $to)
- );
- }
- public function headerProvider()
- {
- return [
- [
- "CUSTOM HEADER\n@@ @@\n-a\n+b\n",
- 'a',
- 'b',
- 'CUSTOM HEADER'
- ],
- [
- "CUSTOM HEADER\n@@ @@\n-a\n+b\n",
- 'a',
- 'b',
- "CUSTOM HEADER\n"
- ],
- [
- "CUSTOM HEADER\n\n@@ @@\n-a\n+b\n",
- 'a',
- 'b',
- "CUSTOM HEADER\n\n"
- ],
- [
- "@@ @@\n-a\n+b\n",
- 'a',
- 'b',
- ''
- ],
- ];
- }
- public function testTypesOtherThanArrayAndStringCanBePassed()
- {
- $this->assertSame(
- "--- Original\n+++ New\n@@ @@\n-1\n+2\n",
- $this->differ->diff(1, 2)
- );
- }
- /**
- * @param string $diff
- * @param Diff[] $expected
- * @dataProvider diffProvider
- */
- public function testParser(string $diff, array $expected)
- {
- $parser = new Parser;
- $result = $parser->parse($diff);
- $this->assertEquals($expected, $result);
- }
- public function arrayProvider(): array
- {
- return [
- [
- [
- ['a', self::REMOVED],
- ['b', self::ADDED]
- ],
- 'a',
- 'b'
- ],
- [
- [
- ['ba', self::REMOVED],
- ['bc', self::ADDED]
- ],
- 'ba',
- 'bc'
- ],
- [
- [
- ['ab', self::REMOVED],
- ['cb', self::ADDED]
- ],
- 'ab',
- 'cb'
- ],
- [
- [
- ['abc', self::REMOVED],
- ['adc', self::ADDED]
- ],
- 'abc',
- 'adc'
- ],
- [
- [
- ['ab', self::REMOVED],
- ['abc', self::ADDED]
- ],
- 'ab',
- 'abc'
- ],
- [
- [
- ['bc', self::REMOVED],
- ['abc', self::ADDED]
- ],
- 'bc',
- 'abc'
- ],
- [
- [
- ['abc', self::REMOVED],
- ['abbc', self::ADDED]
- ],
- 'abc',
- 'abbc'
- ],
- [
- [
- ['abcdde', self::REMOVED],
- ['abcde', self::ADDED]
- ],
- 'abcdde',
- 'abcde'
- ],
- 'same start' => [
- [
- [17, self::OLD],
- ['b', self::REMOVED],
- ['d', self::ADDED],
- ],
- [30 => 17, 'a' => 'b'],
- [30 => 17, 'c' => 'd'],
- ],
- 'same end' => [
- [
- [1, self::REMOVED],
- [2, self::ADDED],
- ['b', self::OLD],
- ],
- [1 => 1, 'a' => 'b'],
- [1 => 2, 'a' => 'b'],
- ],
- 'same start (2), same end (1)' => [
- [
- [17, self::OLD],
- [2, self::OLD],
- [4, self::REMOVED],
- ['a', self::ADDED],
- [5, self::ADDED],
- ['x', self::OLD],
- ],
- [30 => 17, 1 => 2, 2 => 4, 'z' => 'x'],
- [30 => 17, 1 => 2, 3 => 'a', 2 => 5, 'z' => 'x'],
- ],
- 'same' => [
- [
- ['x', self::OLD],
- ],
- ['z' => 'x'],
- ['z' => 'x'],
- ],
- 'diff' => [
- [
- ['y', self::REMOVED],
- ['x', self::ADDED],
- ],
- ['x' => 'y'],
- ['z' => 'x'],
- ],
- 'diff 2' => [
- [
- ['y', self::REMOVED],
- ['b', self::REMOVED],
- ['x', self::ADDED],
- ['d', self::ADDED],
- ],
- ['x' => 'y', 'a' => 'b'],
- ['z' => 'x', 'c' => 'd'],
- ],
- 'test line diff detection' => [
- [
- [
- "#Warning: Strings contain different line endings!\n",
- self::WARNING,
- ],
- [
- "<?php\r\n",
- self::REMOVED,
- ],
- [
- "<?php\n",
- self::ADDED,
- ],
- ],
- "<?php\r\n",
- "<?php\n",
- ],
- 'test line diff detection in array input' => [
- [
- [
- "#Warning: Strings contain different line endings!\n",
- self::WARNING,
- ],
- [
- "<?php\r\n",
- self::REMOVED,
- ],
- [
- "<?php\n",
- self::ADDED,
- ],
- ],
- ["<?php\r\n"],
- ["<?php\n"],
- ],
- ];
- }
- public function textProvider(): array
- {
- return [
- [
- "--- Original\n+++ New\n@@ @@\n-a\n+b\n",
- 'a',
- 'b'
- ],
- [
- "--- Original\n+++ New\n@@ @@\n-ba\n+bc\n",
- 'ba',
- 'bc'
- ],
- [
- "--- Original\n+++ New\n@@ @@\n-ab\n+cb\n",
- 'ab',
- 'cb'
- ],
- [
- "--- Original\n+++ New\n@@ @@\n-abc\n+adc\n",
- 'abc',
- 'adc'
- ],
- [
- "--- Original\n+++ New\n@@ @@\n-ab\n+abc\n",
- 'ab',
- 'abc'
- ],
- [
- "--- Original\n+++ New\n@@ @@\n-bc\n+abc\n",
- 'bc',
- 'abc'
- ],
- [
- "--- Original\n+++ New\n@@ @@\n-abc\n+abbc\n",
- 'abc',
- 'abbc'
- ],
- [
- "--- Original\n+++ New\n@@ @@\n-abcdde\n+abcde\n",
- 'abcdde',
- 'abcde'
- ],
- [
- "--- Original\n+++ New\n@@ @@\n-A\n+A1\n",
- "A\nB",
- "A1\nB",
- ],
- [
- <<<EOF
- --- Original
- +++ New
- @@ @@
- a
- -b
- +p
- @@ @@
- -j
- +w
- EOF
- ,
- "a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk",
- "a\np\nc\nd\ne\nf\ng\nh\ni\nw\nk",
- ],
- [
- <<<EOF
- --- Original
- +++ New
- @@ @@
- -A
- +B
- EOF
- ,
- "A\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1",
- "B\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1",
- ],
- [
- "--- Original\n+++ New\n@@ @@\n #Warning: Strings contain different line endings!\n-<?php\r\n+<?php\n",
- "<?php\r\nA\n",
- "<?php\nA\n",
- ],
- [
- "--- Original\n+++ New\n@@ @@\n #Warning: Strings contain different line endings!\n-a\r\n+\n+c\r",
- "a\r\n",
- "\nc\r",
- ],
- ];
- }
- public function diffProvider(): array
- {
- $serialized_arr = <<<EOL
- a:1:{i:0;O:27:"SebastianBergmann\Diff\Diff":3:{s:33:"�SebastianBergmann\Diff\Diff�from";s:7:"old.txt";s:31:"�SebastianBergmann\Diff\Diff�to";s:7:"new.txt";s:35:"�SebastianBergmann\Diff\Diff�chunks";a:3:{i:0;O:28:"SebastianBergmann\Diff\Chunk":5:{s:35:"�SebastianBergmann\Diff\Chunk�start";i:1;s:40:"�SebastianBergmann\Diff\Chunk�startRange";i:3;s:33:"�SebastianBergmann\Diff\Chunk�end";i:1;s:38:"�SebastianBergmann\Diff\Chunk�endRange";i:4;s:35:"�SebastianBergmann\Diff\Chunk�lines";a:4:{i:0;O:27:"SebastianBergmann\Diff\Line":2:{s:33:"�SebastianBergmann\Diff\Line�type";i:1;s:36:"�SebastianBergmann\Diff\Line�content";s:7:"2222111";}i:1;O:27:"SebastianBergmann\Diff\Line":2:{s:33:"�SebastianBergmann\Diff\Line�type";i:3;s:36:"�SebastianBergmann\Diff\Line�content";s:7:"1111111";}i:2;O:27:"SebastianBergmann\Diff\Line":2:{s:33:"�SebastianBergmann\Diff\Line�type";i:3;s:36:"�SebastianBergmann\Diff\Line�content";s:7:"1111111";}i:3;O:27:"SebastianBergmann\Diff\Line":2:{s:33:"�SebastianBergmann\Diff\Line�type";i:3;s:36:"�SebastianBergmann\Diff\Line�content";s:7:"1111111";}}}i:1;O:28:"SebastianBergmann\Diff\Chunk":5:{s:35:"�SebastianBergmann\Diff\Chunk�start";i:5;s:40:"�SebastianBergmann\Diff\Chunk�startRange";i:10;s:33:"�SebastianBergmann\Diff\Chunk�end";i:6;s:38:"�SebastianBergmann\Diff\Chunk�endRange";i:8;s:35:"�SebastianBergmann\Diff\Chunk�lines";a:11:{i:0;O:27:"SebastianBergmann\Diff\Line":2:{s:33:"�SebastianBergmann\Diff\Line�type";i:3;s:36:"�SebastianBergmann\Diff\Line�content";s:7:"1111111";}i:1;O:27:"SebastianBergmann\Diff\Line":2:{s:33:"�SebastianBergmann\Diff\Line�type";i:3;s:36:"�SebastianBergmann\Diff\Line�content";s:7:"1111111";}i:2;O:27:"SebastianBergmann\Diff\Line":2:{s:33:"�SebastianBergmann\Diff\Line�type";i:3;s:36:"�SebastianBergmann\Diff\Line�content";s:7:"1111111";}i:3;O:27:"SebastianBergmann\Diff\Line":2:{s:33:"�SebastianBergmann\Diff\Line�type";i:3;s:36:"�SebastianBergmann\Diff\Line�content";s:8:"+1121211";}i:4;O:27:"SebastianBergmann\Diff\Line":2:{s:33:"�SebastianBergmann\Diff\Line�type";i:3;s:36:"�SebastianBergmann\Diff\Line�content";s:7:"1111111";}i:5;O:27:"SebastianBergmann\Diff\Line":2:{s:33:"�SebastianBergmann\Diff\Line�type";i:3;s:36:"�SebastianBergmann\Diff\Line�content";s:8:"-1111111";}i:6;O:27:"SebastianBergmann\Diff\Line":2:{s:33:"�SebastianBergmann\Diff\Line�type";i:3;s:36:"�SebastianBergmann\Diff\Line�content";s:8:"-1111111";}i:7;O:27:"SebastianBergmann\Diff\Line":2:{s:33:"�SebastianBergmann\Diff\Line�type";i:3;s:36:"�SebastianBergmann\Diff\Line�content";s:8:"-2222222";}i:8;O:27:"SebastianBergmann\Diff\Line":2:{s:33:"�SebastianBergmann\Diff\Line�type";i:3;s:36:"�SebastianBergmann\Diff\Line�content";s:7:"2222222";}i:9;O:27:"SebastianBergmann\Diff\Line":2:{s:33:"�SebastianBergmann\Diff\Line�type";i:3;s:36:"�SebastianBergmann\Diff\Line�content";s:7:"2222222";}i:10;O:27:"SebastianBergmann\Diff\Line":2:{s:33:"�SebastianBergmann\Diff\Line�type";i:3;s:36:"�SebastianBergmann\Diff\Line�content";s:7:"2222222";}}}i:2;O:28:"SebastianBergmann\Diff\Chunk":5:{s:35:"�SebastianBergmann\Diff\Chunk�start";i:17;s:40:"�SebastianBergmann\Diff\Chunk�startRange";i:5;s:33:"�SebastianBergmann\Diff\Chunk�end";i:16;s:38:"�SebastianBergmann\Diff\Chunk�endRange";i:6;s:35:"�SebastianBergmann\Diff\Chunk�lines";a:6:{i:0;O:27:"SebastianBergmann\Diff\Line":2:{s:33:"�SebastianBergmann\Diff\Line�type";i:3;s:36:"�SebastianBergmann\Diff\Line�content";s:7:"2222222";}i:1;O:27:"SebastianBergmann\Diff\Line":2:{s:33:"�SebastianBergmann\Diff\Line�type";i:3;s:36:"�SebastianBergmann\Diff\Line�content";s:7:"2222222";}i:2;O:27:"SebastianBergmann\Diff\Line":2:{s:33:"�SebastianBergmann\Diff\Line�type";i:3;s:36:"�SebastianBergmann\Diff\Line�content";s:7:"2222222";}i:3;O:27:"SebastianBergmann\Diff\Line":2:{s:33:"�SebastianBergmann\Diff\Line�type";i:3;s:36:"�SebastianBergmann\Diff\Line�content";s:8:"+2122212";}i:4;O:27:"SebastianBergmann\Diff\Line":2:{s:33:"�SebastianBergmann\Diff\Line�type";i:3;s:36:"�SebastianBergmann\Diff\Line�content";s:7:"2222222";}i:5;O:27:"SebastianBergmann\Diff\Line":2:{s:33:"�SebastianBergmann\Diff\Line�type";i:3;s:36:"�SebastianBergmann\Diff\Line�content";s:7:"2222222";}}}}}}
- EOL;
- return [
- [
- "--- 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",
- \unserialize($serialized_arr)
- ]
- ];
- }
- /**
- * @param string $expected
- * @param string $from
- * @param string $to
- * @param string $header
- * @dataProvider textForNoNonDiffLinesProvider
- */
- public function testDiffDoNotShowNonDiffLines(string $expected, string $from, string $to, string $header = '')
- {
- $differ = new Differ(new DiffOnlyOutputBuilder($header));
- $this->assertSame($expected, $differ->diff($from, $to));
- }
- public function textForNoNonDiffLinesProvider(): array
- {
- return [
- [
- " #Warning: Strings contain different line endings!\n-A\r\n+B\n",
- "A\r\n",
- "B\n",
- ],
- [
- "-A\n+B\n",
- "\nA",
- "\nB"
- ],
- [
- '',
- 'a',
- 'a'
- ],
- [
- "-A\n+C\n",
- "A\n\n\nB",
- "C\n\n\nB",
- ],
- [
- "header\n",
- 'a',
- 'a',
- 'header'
- ],
- [
- "header\n",
- 'a',
- 'a',
- "header\n"
- ],
- ];
- }
- public function testDiffToArrayInvalidFromType()
- {
- $this->expectException('\InvalidArgumentException');
- $this->expectExceptionMessageRegExp('#^"from" must be an array or string\.$#');
- $this->differ->diffToArray(null, '');
- }
- public function testDiffInvalidToType()
- {
- $this->expectException('\InvalidArgumentException');
- $this->expectExceptionMessageRegExp('#^"to" must be an array or string\.$#');
- $this->differ->diffToArray('', new \stdClass);
- }
- /**
- * @param array $expected
- * @param string $from
- * @param string $to
- * @param int $lineThreshold
- * @dataProvider provideGetCommonChunks
- */
- public function testGetCommonChunks(array $expected, string $from, string $to, int $lineThreshold = 5)
- {
- $output = new class extends AbstractChunkOutputBuilder {
- public function getDiff(array $diff): string
- {
- return '';
- }
- public function getChunks(array $diff, $lineThreshold)
- {
- return $this->getCommonChunks($diff, $lineThreshold);
- }
- };
- $this->assertSame(
- $expected,
- $output->getChunks($this->differ->diffToArray($from, $to), $lineThreshold)
- );
- }
- public function provideGetCommonChunks(): array
- {
- return[
- 'same (with default threshold)' => [
- [],
- 'A',
- 'A',
- ],
- 'same (threshold 0)' => [
- [0 => 0],
- 'A',
- 'A',
- 0,
- ],
- 'empty' => [
- [],
- '',
- '',
- ],
- 'single line diff' => [
- [],
- 'A',
- 'B',
- ],
- 'below threshold I' => [
- [],
- "A\nX\nC",
- "A\nB\nC",
- ],
- 'below threshold II' => [
- [],
- "A\n\n\n\nX\nC",
- "A\n\n\n\nB\nC",
- ],
- 'below threshold III' => [
- [0 => 5],
- "A\n\n\n\n\n\nB",
- "A\n\n\n\n\n\nA",
- ],
- 'same start' => [
- [0 => 5],
- "A\n\n\n\n\n\nX\nC",
- "A\n\n\n\n\n\nB\nC",
- ],
- 'same start long' => [
- [0 => 13],
- "\n\n\n\n\n\n\n\n\n\n\n\n\n\nA",
- "\n\n\n\n\n\n\n\n\n\n\n\n\n\nB",
- ],
- 'same part in between' => [
- [2 => 8],
- "A\n\n\n\n\n\n\nX\nY\nZ\n\n",
- "B\n\n\n\n\n\n\nX\nA\nZ\n\n",
- ],
- 'same trailing' => [
- [2 => 14],
- "A\n\n\n\n\n\n\n\n\n\n\n\n\n\n",
- "B\n\n\n\n\n\n\n\n\n\n\n\n\n\n",
- ],
- 'same part in between, same trailing' => [
- [2 => 7, 10 => 15],
- "A\n\n\n\n\n\n\nA\n\n\n\n\n\n\n",
- "B\n\n\n\n\n\n\nB\n\n\n\n\n\n\n",
- ],
- 'below custom threshold I' => [
- [],
- "A\n\nB",
- "A\n\nD",
- 2
- ],
- 'custom threshold I' => [
- [0 => 1],
- "A\n\nB",
- "A\n\nD",
- 1
- ],
- 'custom threshold II' => [
- [],
- "A\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n",
- "A\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n",
- 19
- ],
- [
- [3 => 9],
- "a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk",
- "a\np\nc\nd\ne\nf\ng\nh\ni\nw\nk",
- ],
- [
- [0 => 5, 8 => 13],
- "A\nA\nA\nA\nA\nA\nX\nC\nC\nC\nC\nC\nC",
- "A\nA\nA\nA\nA\nA\nB\nC\nC\nC\nC\nC\nC",
- ],
- [
- [0 => 5, 8 => 13],
- "A\nA\nA\nA\nA\nA\nX\nC\nC\nC\nC\nC\nC\nX",
- "A\nA\nA\nA\nA\nA\nB\nC\nC\nC\nC\nC\nC\nY",
- ],
- ];
- }
- /**
- * @param array $expected
- * @param string $input
- * @dataProvider provideSplitStringByLinesCases
- */
- public function testSplitStringByLines(array $expected, string $input)
- {
- $reflection = new \ReflectionObject($this->differ);
- $method = $reflection->getMethod('splitStringByLines');
- $method->setAccessible(true);
- $this->assertSame($expected, $method->invoke($this->differ, $input));
- }
- public function provideSplitStringByLinesCases(): array
- {
- return [
- [
- [],
- ''
- ],
- [
- ['a'],
- 'a'
- ],
- [
- ["a\n"],
- "a\n"
- ],
- [
- ["a\r"],
- "a\r"
- ],
- [
- ["a\r\n"],
- "a\r\n"
- ],
- [
- ["\n"],
- "\n"
- ],
- [
- ["\r"],
- "\r"
- ],
- [
- ["\r\n"],
- "\r\n"
- ],
- [
- [
- "A\n",
- "B\n",
- "\n",
- "C\n"
- ],
- "A\nB\n\nC\n",
- ],
- [
- [
- "A\r\n",
- "B\n",
- "\n",
- "C\r"
- ],
- "A\r\nB\n\nC\r",
- ],
- [
- [
- "\n",
- "A\r\n",
- "B\n",
- "\n",
- 'C'
- ],
- "\nA\r\nB\n\nC",
- ],
- ];
- }
- /**
- * @param string $expected
- * @param string $from
- * @param string $to
- * @dataProvider provideDiffWithLineNumbers
- */
- public function testDiffWithLineNumbers($expected, $from, $to)
- {
- $differ = new Differ(new UnifiedDiffOutputBuilder("--- Original\n+++ New\n", true));
- $this->assertSame($expected, $differ->diff($from, $to));
- }
- public function provideDiffWithLineNumbers(): array
- {
- return [
- 'diff line 1 non_patch_compat' => [
- '--- Original
- +++ New
- @@ -1 +1 @@
- -AA
- +BA
- ',
- 'AA',
- 'BA',
- ],
- 'diff line +1 non_patch_compat' => [
- '--- Original
- +++ New
- @@ -1 +1,2 @@
- -AZ
- +
- +B
- ',
- 'AZ',
- "\nB",
- ],
- 'diff line -1 non_patch_compat' => [
- '--- Original
- +++ New
- @@ -1,2 +1 @@
- -
- -AF
- +B
- ',
- "\nAF",
- 'B',
- ],
- 'II non_patch_compat' => [
- '--- Original
- +++ New
- @@ -1,2 +1 @@
- -
- -
- '
- ,
- "\n\nA\n1",
- "A\n1",
- ],
- 'diff last line II - no trailing linebreak non_patch_compat' => [
- '--- Original
- +++ New
- @@ -8 +8 @@
- -E
- +B
- ',
- "A\n\n\n\n\n\n\nE",
- "A\n\n\n\n\n\n\nB",
- ],
- [
- "--- Original\n+++ New\n@@ -1,2 +1 @@\n \n-\n",
- "\n\n",
- "\n",
- ],
- 'diff line endings non_patch_compat' => [
- "--- Original\n+++ New\n@@ -1 +1 @@\n #Warning: Strings contain different line endings!\n-<?php\r\n+<?php\n",
- "<?php\r\n",
- "<?php\n",
- ],
- 'same non_patch_compat' => [
- '--- Original
- +++ New
- ',
- "AT\n",
- "AT\n",
- ],
- [
- '--- Original
- +++ New
- @@ -1 +1 @@
- -b
- +a
- ',
- "b\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n",
- "a\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"
- ],
- 'diff line @1' => [
- '--- Original
- +++ New
- @@ -1,2 +1,2 @@
- ' . '
- -AG
- +B
- ',
- "\nAG\n",
- "\nB\n",
- ],
- 'same multiple lines' => [
- '--- Original
- +++ New
- @@ -1,3 +1,3 @@
- ' . '
- ' . '
- -V
- +B
- '
- ,
- "\n\nV\nC213",
- "\n\nB\nC213",
- ],
- 'diff last line I' => [
- '--- Original
- +++ New
- @@ -8 +8 @@
- -E
- +B
- ',
- "A\n\n\n\n\n\n\nE\n",
- "A\n\n\n\n\n\n\nB\n",
- ],
- 'diff line middle' => [
- '--- Original
- +++ New
- @@ -8 +8 @@
- -X
- +Z
- ',
- "A\n\n\n\n\n\n\nX\n\n\n\n\n\n\nAY",
- "A\n\n\n\n\n\n\nZ\n\n\n\n\n\n\nAY",
- ],
- 'diff last line III' => [
- '--- Original
- +++ New
- @@ -15 +15 @@
- -A
- +B
- ',
- "A\n\n\n\n\n\n\nA\n\n\n\n\n\n\nA\n",
- "A\n\n\n\n\n\n\nA\n\n\n\n\n\n\nB\n",
- ],
- [
- '--- Original
- +++ New
- @@ -1,7 +1,7 @@
- A
- -B
- +B1
- D
- E
- EE
- F
- -G
- +G1
- ',
- "A\nB\nD\nE\nEE\nF\nG\nH",
- "A\nB1\nD\nE\nEE\nF\nG1\nH",
- ],
- [
- '--- Original
- +++ New
- @@ -1 +1,2 @@
- Z
- +
- @@ -10 +11 @@
- -i
- +x
- ',
- 'Z
- a
- b
- c
- d
- e
- f
- g
- h
- i
- j',
- 'Z
- a
- b
- c
- d
- e
- f
- g
- h
- x
- j'
- ],
- [
- '--- Original
- +++ New
- @@ -1,5 +1,3 @@
- -
- -a
- +b
- A
- -a
- -
- +b
- ',
- "\na\nA\na\n\n\nA",
- "b\nA\nb\n\nA"
- ],
- [
- <<<EOF
- --- Original
- +++ New
- @@ -1,4 +1,2 @@
- -
- -
- a
- -b
- +p
- @@ -12 +10 @@
- -j
- +w
- EOF
- ,
- "\n\na\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk",
- "a\np\nc\nd\ne\nf\ng\nh\ni\nw\nk",
- ],
- [
- '--- Original
- +++ New
- @@ -11 +11 @@
- -A
- +C
- ',
- "E\n\n\n\n\nB\n\n\n\n\nA\n\n\n\n\n\n\n\n\nD1",
- "E\n\n\n\n\nB\n\n\n\n\nC\n\n\n\n\n\n\n\n\nD1",
- ],
- [
- '--- Original
- +++ New
- @@ -8 +8 @@
- -Z
- +U
- @@ -15 +15 @@
- -X
- +V
- @@ -22 +22 @@
- -Y
- +W
- @@ -29 +29 @@
- -W
- +X
- @@ -36 +36 @@
- -V
- +Y
- @@ -43 +43 @@
- -U
- +Z
- ',
- "\n\n\n\n\n\n\nZ\n\n\n\n\n\n\nX\n\n\n\n\n\n\nY\n\n\n\n\n\n\nW\n\n\n\n\n\n\nV\n\n\n\n\n\n\nU\n",
- "\n\n\n\n\n\n\nU\n\n\n\n\n\n\nV\n\n\n\n\n\n\nW\n\n\n\n\n\n\nX\n\n\n\n\n\n\nY\n\n\n\n\n\n\nZ\n"
- ],
- [
- <<<EOF
- --- Original
- +++ New
- @@ -1,2 +1,2 @@
- a
- -b
- +p
- @@ -10 +10 @@
- -j
- +w
- EOF
- ,
- "a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk",
- "a\np\nc\nd\ne\nf\ng\nh\ni\nw\nk",
- ],
- [
- <<<EOF
- --- Original
- +++ New
- @@ -1 +1 @@
- -A
- +B
- EOF
- ,
- "A\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1",
- "B\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1",
- ],
- [
- "--- Original\n+++ New\n@@ -7 +7 @@\n-X\n+B\n",
- "A\nA\nA\nA\nA\nA\nX\nC\nC\nC\nC\nC\nC",
- "A\nA\nA\nA\nA\nA\nB\nC\nC\nC\nC\nC\nC",
- ],
- ];
- }
- public function testConstructorNull()
- {
- $this->assertAttributeInstanceOf(
- UnifiedDiffOutputBuilder::class,
- 'outputBuilder',
- new Differ(null)
- );
- }
- public function testConstructorString()
- {
- $this->assertAttributeInstanceOf(
- UnifiedDiffOutputBuilder::class,
- 'outputBuilder',
- new Differ("--- Original\n+++ New\n")
- );
- }
- public function testConstructorInvalidArgInt()
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessageRegExp('/^Expected builder to be an instance of DiffOutputBuilderInterface, <null> or a string, got integer "1"\.$/');
- new Differ(1);
- }
- public function testConstructorInvalidArgObject()
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessageRegExp('/^Expected builder to be an instance of DiffOutputBuilderInterface, <null> or a string, got instance of "SplFileInfo"\.$/');
- new Differ(new \SplFileInfo(__FILE__));
- }
- }
|