123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444 |
- <?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\UnifiedDiffOutputBuilder;
- /**
- * @covers SebastianBergmann\Diff\Differ
- * @covers SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder
- *
- * @uses SebastianBergmann\Diff\MemoryEfficientLongestCommonSubsequenceCalculator
- * @uses SebastianBergmann\Diff\TimeEfficientLongestCommonSubsequenceCalculator
- * @uses SebastianBergmann\Diff\Output\AbstractChunkOutputBuilder
- */
- final class DifferTest extends TestCase
- {
- /**
- * @var Differ
- */
- private $differ;
- protected function setUp(): void
- {
- $this->differ = new Differ;
- }
- /**
- * @param array $expected
- * @param array|string $from
- * @param array|string $to
- *
- * @dataProvider arrayProvider
- */
- public function testArrayRepresentationOfDiffCanBeRenderedUsingTimeEfficientLcsImplementation(array $expected, $from, $to): void
- {
- $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): void
- {
- $this->assertSame($expected, $this->differ->diff($from, $to, new TimeEfficientLongestCommonSubsequenceCalculator));
- }
- /**
- * @param array $expected
- * @param array|string $from
- * @param array|string $to
- *
- * @dataProvider arrayProvider
- */
- public function testArrayRepresentationOfDiffCanBeRenderedUsingMemoryEfficientLcsImplementation(array $expected, $from, $to): void
- {
- $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): void
- {
- $this->assertSame($expected, $this->differ->diff($from, $to, new MemoryEfficientLongestCommonSubsequenceCalculator));
- }
- public function testTypesOtherThanArrayAndStringCanBePassed(): void
- {
- $this->assertSame(
- "--- Original\n+++ New\n@@ @@\n-1\n+2\n",
- $this->differ->diff(1, 2)
- );
- }
- public function testArrayDiffs(): void
- {
- $this->assertSame(
- '--- Original
- +++ New
- @@ @@
- -one
- +two
- ',
- $this->differ->diff(['one'], ['two'])
- );
- }
- public function arrayProvider(): array
- {
- return [
- [
- [
- ['a', Differ::REMOVED],
- ['b', Differ::ADDED],
- ],
- 'a',
- 'b',
- ],
- [
- [
- ['ba', Differ::REMOVED],
- ['bc', Differ::ADDED],
- ],
- 'ba',
- 'bc',
- ],
- [
- [
- ['ab', Differ::REMOVED],
- ['cb', Differ::ADDED],
- ],
- 'ab',
- 'cb',
- ],
- [
- [
- ['abc', Differ::REMOVED],
- ['adc', Differ::ADDED],
- ],
- 'abc',
- 'adc',
- ],
- [
- [
- ['ab', Differ::REMOVED],
- ['abc', Differ::ADDED],
- ],
- 'ab',
- 'abc',
- ],
- [
- [
- ['bc', Differ::REMOVED],
- ['abc', Differ::ADDED],
- ],
- 'bc',
- 'abc',
- ],
- [
- [
- ['abc', Differ::REMOVED],
- ['abbc', Differ::ADDED],
- ],
- 'abc',
- 'abbc',
- ],
- [
- [
- ['abcdde', Differ::REMOVED],
- ['abcde', Differ::ADDED],
- ],
- 'abcdde',
- 'abcde',
- ],
- 'same start' => [
- [
- [17, Differ::OLD],
- ['b', Differ::REMOVED],
- ['d', Differ::ADDED],
- ],
- [30 => 17, 'a' => 'b'],
- [30 => 17, 'c' => 'd'],
- ],
- 'same end' => [
- [
- [1, Differ::REMOVED],
- [2, Differ::ADDED],
- ['b', Differ::OLD],
- ],
- [1 => 1, 'a' => 'b'],
- [1 => 2, 'a' => 'b'],
- ],
- 'same start (2), same end (1)' => [
- [
- [17, Differ::OLD],
- [2, Differ::OLD],
- [4, Differ::REMOVED],
- ['a', Differ::ADDED],
- [5, Differ::ADDED],
- ['x', Differ::OLD],
- ],
- [30 => 17, 1 => 2, 2 => 4, 'z' => 'x'],
- [30 => 17, 1 => 2, 3 => 'a', 2 => 5, 'z' => 'x'],
- ],
- 'same' => [
- [
- ['x', Differ::OLD],
- ],
- ['z' => 'x'],
- ['z' => 'x'],
- ],
- 'diff' => [
- [
- ['y', Differ::REMOVED],
- ['x', Differ::ADDED],
- ],
- ['x' => 'y'],
- ['z' => 'x'],
- ],
- 'diff 2' => [
- [
- ['y', Differ::REMOVED],
- ['b', Differ::REMOVED],
- ['x', Differ::ADDED],
- ['d', Differ::ADDED],
- ],
- ['x' => 'y', 'a' => 'b'],
- ['z' => 'x', 'c' => 'd'],
- ],
- 'test line diff detection' => [
- [
- [
- "#Warning: Strings contain different line endings!\n",
- Differ::DIFF_LINE_END_WARNING,
- ],
- [
- "<?php\r\n",
- Differ::REMOVED,
- ],
- [
- "<?php\n",
- Differ::ADDED,
- ],
- ],
- "<?php\r\n",
- "<?php\n",
- ],
- 'test line diff detection in array input' => [
- [
- [
- "#Warning: Strings contain different line endings!\n",
- Differ::DIFF_LINE_END_WARNING,
- ],
- [
- "<?php\r\n",
- Differ::REMOVED,
- ],
- [
- "<?php\n",
- Differ::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-A\n+A1\n B\n",
- "A\nB",
- "A1\nB",
- ],
- [
- <<<EOF
- --- Original
- +++ New
- @@ @@
- a
- -b
- +p
- c
- d
- e
- @@ @@
- g
- h
- i
- -j
- +w
- k
- EOF
- ,
- "a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk\n",
- "a\np\nc\nd\ne\nf\ng\nh\ni\nw\nk\n",
- ],
- [
- <<<EOF
- --- Original
- +++ New
- @@ @@
- -A
- +B
- 1
- 2
- 3
- EOF
- ,
- "A\n1\n2\n3\n4\n5\n6\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n",
- "B\n1\n2\n3\n4\n5\n6\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n",
- ],
- [
- "--- Original\n+++ New\n@@ @@\n #Warning: Strings contain different line endings!\n-<?php\r\n+<?php\n A\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\n",
- "a\r\n",
- "\nc\r",
- ],
- ];
- }
- public function testDiffToArrayInvalidFromType(): void
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessageRegExp('#^"from" must be an array or string\.$#');
- $this->differ->diffToArray(null, '');
- }
- public function testDiffInvalidToType(): void
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessageRegExp('#^"to" must be an array or string\.$#');
- $this->differ->diffToArray('', new \stdClass);
- }
- /**
- * @param array $expected
- * @param string $input
- *
- * @dataProvider provideSplitStringByLinesCases
- */
- public function testSplitStringByLines(array $expected, string $input): void
- {
- $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",
- ],
- ];
- }
- public function testConstructorInvalidArgInt(): void
- {
- $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(): void
- {
- $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__));
- }
- }
|