ParserTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php declare(strict_types=1);
  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. namespace SebastianBergmann\Diff;
  11. use PHPUnit\Framework\TestCase;
  12. use SebastianBergmann\Diff\Utils\FileUtils;
  13. /**
  14. * @covers SebastianBergmann\Diff\Parser
  15. *
  16. * @uses SebastianBergmann\Diff\Chunk
  17. * @uses SebastianBergmann\Diff\Diff
  18. * @uses SebastianBergmann\Diff\Line
  19. */
  20. final class ParserTest extends TestCase
  21. {
  22. /**
  23. * @var Parser
  24. */
  25. private $parser;
  26. protected function setUp(): void
  27. {
  28. $this->parser = new Parser;
  29. }
  30. public function testParse(): void
  31. {
  32. $content = FileUtils::getFileContent(__DIR__ . '/fixtures/patch.txt');
  33. $diffs = $this->parser->parse($content);
  34. $this->assertContainsOnlyInstancesOf(Diff::class, $diffs);
  35. $this->assertCount(1, $diffs);
  36. $chunks = $diffs[0]->getChunks();
  37. $this->assertContainsOnlyInstancesOf(Chunk::class, $chunks);
  38. $this->assertCount(1, $chunks);
  39. $this->assertSame(20, $chunks[0]->getStart());
  40. $this->assertCount(4, $chunks[0]->getLines());
  41. }
  42. public function testParseWithMultipleChunks(): void
  43. {
  44. $content = FileUtils::getFileContent(__DIR__ . '/fixtures/patch2.txt');
  45. $diffs = $this->parser->parse($content);
  46. $this->assertCount(1, $diffs);
  47. $chunks = $diffs[0]->getChunks();
  48. $this->assertCount(3, $chunks);
  49. $this->assertSame(20, $chunks[0]->getStart());
  50. $this->assertSame(320, $chunks[1]->getStart());
  51. $this->assertSame(600, $chunks[2]->getStart());
  52. $this->assertCount(5, $chunks[0]->getLines());
  53. $this->assertCount(5, $chunks[1]->getLines());
  54. $this->assertCount(4, $chunks[2]->getLines());
  55. }
  56. public function testParseWithRemovedLines(): void
  57. {
  58. $content = <<<END
  59. diff --git a/Test.txt b/Test.txt
  60. index abcdefg..abcdefh 100644
  61. --- a/Test.txt
  62. +++ b/Test.txt
  63. @@ -49,9 +49,8 @@
  64. A
  65. -B
  66. END;
  67. $diffs = $this->parser->parse($content);
  68. $this->assertContainsOnlyInstancesOf(Diff::class, $diffs);
  69. $this->assertCount(1, $diffs);
  70. $chunks = $diffs[0]->getChunks();
  71. $this->assertContainsOnlyInstancesOf(Chunk::class, $chunks);
  72. $this->assertCount(1, $chunks);
  73. $chunk = $chunks[0];
  74. $this->assertSame(49, $chunk->getStart());
  75. $this->assertSame(49, $chunk->getEnd());
  76. $this->assertSame(9, $chunk->getStartRange());
  77. $this->assertSame(8, $chunk->getEndRange());
  78. $lines = $chunk->getLines();
  79. $this->assertContainsOnlyInstancesOf(Line::class, $lines);
  80. $this->assertCount(2, $lines);
  81. /** @var Line $line */
  82. $line = $lines[0];
  83. $this->assertSame('A', $line->getContent());
  84. $this->assertSame(Line::UNCHANGED, $line->getType());
  85. $line = $lines[1];
  86. $this->assertSame('B', $line->getContent());
  87. $this->assertSame(Line::REMOVED, $line->getType());
  88. }
  89. public function testParseDiffForMulitpleFiles(): void
  90. {
  91. $content = <<<END
  92. diff --git a/Test.txt b/Test.txt
  93. index abcdefg..abcdefh 100644
  94. --- a/Test.txt
  95. +++ b/Test.txt
  96. @@ -1,3 +1,2 @@
  97. A
  98. -B
  99. diff --git a/Test123.txt b/Test123.txt
  100. index abcdefg..abcdefh 100644
  101. --- a/Test2.txt
  102. +++ b/Test2.txt
  103. @@ -1,2 +1,3 @@
  104. A
  105. +B
  106. END;
  107. $diffs = $this->parser->parse($content);
  108. $this->assertCount(2, $diffs);
  109. /** @var Diff $diff */
  110. $diff = $diffs[0];
  111. $this->assertSame('a/Test.txt', $diff->getFrom());
  112. $this->assertSame('b/Test.txt', $diff->getTo());
  113. $this->assertCount(1, $diff->getChunks());
  114. $diff = $diffs[1];
  115. $this->assertSame('a/Test2.txt', $diff->getFrom());
  116. $this->assertSame('b/Test2.txt', $diff->getTo());
  117. $this->assertCount(1, $diff->getChunks());
  118. }
  119. /**
  120. * @param string $diff
  121. * @param Diff[] $expected
  122. *
  123. * @dataProvider diffProvider
  124. */
  125. public function testParser(string $diff, array $expected): void
  126. {
  127. $result = $this->parser->parse($diff);
  128. $this->assertEquals($expected, $result);
  129. }
  130. public function diffProvider(): array
  131. {
  132. return [
  133. [
  134. "--- 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",
  135. \unserialize(FileUtils::getFileContent(__DIR__ . '/fixtures/serialized_diff.bin')),
  136. ],
  137. ];
  138. }
  139. }