1<?php 2/* 3 * This file is part of sebastian/diff. 4 * 5 * (c) Sebastian Bergmann <sebastian@phpunit.de> 6 * 7 * For the full copyright and license information, please view the LICENSE 8 * file that was distributed with this source code. 9 */ 10 11namespace SebastianBergmann\Diff; 12 13use PHPUnit\Framework\TestCase; 14 15/** 16 * @covers SebastianBergmann\Diff\Parser 17 * 18 * @uses SebastianBergmann\Diff\Chunk 19 * @uses SebastianBergmann\Diff\Diff 20 * @uses SebastianBergmann\Diff\Line 21 */ 22class ParserTest extends TestCase 23{ 24 /** 25 * @var Parser 26 */ 27 private $parser; 28 29 protected function setUp() 30 { 31 $this->parser = new Parser; 32 } 33 34 public function testParse() 35 { 36 $content = \file_get_contents(__DIR__ . '/fixtures/patch.txt'); 37 38 $diffs = $this->parser->parse($content); 39 40 $this->assertInternalType('array', $diffs); 41 $this->assertContainsOnlyInstancesOf('SebastianBergmann\Diff\Diff', $diffs); 42 $this->assertCount(1, $diffs); 43 44 $chunks = $diffs[0]->getChunks(); 45 $this->assertInternalType('array', $chunks); 46 $this->assertContainsOnlyInstancesOf('SebastianBergmann\Diff\Chunk', $chunks); 47 48 $this->assertCount(1, $chunks); 49 50 $this->assertEquals(20, $chunks[0]->getStart()); 51 52 $this->assertCount(4, $chunks[0]->getLines()); 53 } 54 55 public function testParseWithMultipleChunks() 56 { 57 $content = \file_get_contents(__DIR__ . '/fixtures/patch2.txt'); 58 59 $diffs = $this->parser->parse($content); 60 61 $this->assertCount(1, $diffs); 62 63 $chunks = $diffs[0]->getChunks(); 64 $this->assertCount(3, $chunks); 65 66 $this->assertEquals(20, $chunks[0]->getStart()); 67 $this->assertEquals(320, $chunks[1]->getStart()); 68 $this->assertEquals(600, $chunks[2]->getStart()); 69 70 $this->assertCount(5, $chunks[0]->getLines()); 71 $this->assertCount(5, $chunks[1]->getLines()); 72 $this->assertCount(4, $chunks[2]->getLines()); 73 } 74 75 public function testParseWithRemovedLines() 76 { 77 $content = <<<A 78diff --git a/Test.txt b/Test.txt 79index abcdefg..abcdefh 100644 80--- a/Test.txt 81+++ b/Test.txt 82@@ -49,9 +49,8 @@ 83 A 84-B 85A; 86 $diffs = $this->parser->parse($content); 87 $this->assertInternalType('array', $diffs); 88 $this->assertContainsOnlyInstancesOf('SebastianBergmann\Diff\Diff', $diffs); 89 $this->assertCount(1, $diffs); 90 91 $chunks = $diffs[0]->getChunks(); 92 93 $this->assertInternalType('array', $chunks); 94 $this->assertContainsOnlyInstancesOf('SebastianBergmann\Diff\Chunk', $chunks); 95 $this->assertCount(1, $chunks); 96 97 $chunk = $chunks[0]; 98 $this->assertSame(49, $chunk->getStart()); 99 $this->assertSame(49, $chunk->getEnd()); 100 $this->assertSame(9, $chunk->getStartRange()); 101 $this->assertSame(8, $chunk->getEndRange()); 102 103 $lines = $chunk->getLines(); 104 $this->assertInternalType('array', $lines); 105 $this->assertContainsOnlyInstancesOf('SebastianBergmann\Diff\Line', $lines); 106 $this->assertCount(2, $lines); 107 108 /** @var Line $line */ 109 $line = $lines[0]; 110 $this->assertSame('A', $line->getContent()); 111 $this->assertSame(Line::UNCHANGED, $line->getType()); 112 113 $line = $lines[1]; 114 $this->assertSame('B', $line->getContent()); 115 $this->assertSame(Line::REMOVED, $line->getType()); 116 } 117 118 public function testParseDiffForMulitpleFiles() 119 { 120 $content = <<<A 121diff --git a/Test.txt b/Test.txt 122index abcdefg..abcdefh 100644 123--- a/Test.txt 124+++ b/Test.txt 125@@ -1,3 +1,2 @@ 126 A 127-B 128 129diff --git a/Test123.txt b/Test123.txt 130index abcdefg..abcdefh 100644 131--- a/Test2.txt 132+++ b/Test2.txt 133@@ -1,2 +1,3 @@ 134 A 135+B 136A; 137 $diffs = $this->parser->parse($content); 138 $this->assertCount(2, $diffs); 139 140 /** @var Diff $diff */ 141 $diff = $diffs[0]; 142 $this->assertSame('a/Test.txt', $diff->getFrom()); 143 $this->assertSame('b/Test.txt', $diff->getTo()); 144 $this->assertCount(1, $diff->getChunks()); 145 146 $diff = $diffs[1]; 147 $this->assertSame('a/Test2.txt', $diff->getFrom()); 148 $this->assertSame('b/Test2.txt', $diff->getTo()); 149 $this->assertCount(1, $diff->getChunks()); 150 } 151} 152