ParserTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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->assertInternalType('array', $diffs);
  35. $this->assertContainsOnlyInstancesOf(Diff::class, $diffs);
  36. $this->assertCount(1, $diffs);
  37. $chunks = $diffs[0]->getChunks();
  38. $this->assertInternalType('array', $chunks);
  39. $this->assertContainsOnlyInstancesOf(Chunk::class, $chunks);
  40. $this->assertCount(1, $chunks);
  41. $this->assertSame(20, $chunks[0]->getStart());
  42. $this->assertCount(4, $chunks[0]->getLines());
  43. }
  44. public function testParseWithMultipleChunks(): void
  45. {
  46. $content = FileUtils::getFileContent(__DIR__ . '/fixtures/patch2.txt');
  47. $diffs = $this->parser->parse($content);
  48. $this->assertCount(1, $diffs);
  49. $chunks = $diffs[0]->getChunks();
  50. $this->assertCount(3, $chunks);
  51. $this->assertSame(20, $chunks[0]->getStart());
  52. $this->assertSame(320, $chunks[1]->getStart());
  53. $this->assertSame(600, $chunks[2]->getStart());
  54. $this->assertCount(5, $chunks[0]->getLines());
  55. $this->assertCount(5, $chunks[1]->getLines());
  56. $this->assertCount(4, $chunks[2]->getLines());
  57. }
  58. public function testParseWithRemovedLines(): void
  59. {
  60. $content = <<<END
  61. diff --git a/Test.txt b/Test.txt
  62. index abcdefg..abcdefh 100644
  63. --- a/Test.txt
  64. +++ b/Test.txt
  65. @@ -49,9 +49,8 @@
  66. A
  67. -B
  68. END;
  69. $diffs = $this->parser->parse($content);
  70. $this->assertInternalType('array', $diffs);
  71. $this->assertContainsOnlyInstancesOf(Diff::class, $diffs);
  72. $this->assertCount(1, $diffs);
  73. $chunks = $diffs[0]->getChunks();
  74. $this->assertInternalType('array', $chunks);
  75. $this->assertContainsOnlyInstancesOf(Chunk::class, $chunks);
  76. $this->assertCount(1, $chunks);
  77. $chunk = $chunks[0];
  78. $this->assertSame(49, $chunk->getStart());
  79. $this->assertSame(49, $chunk->getEnd());
  80. $this->assertSame(9, $chunk->getStartRange());
  81. $this->assertSame(8, $chunk->getEndRange());
  82. $lines = $chunk->getLines();
  83. $this->assertInternalType('array', $lines);
  84. $this->assertContainsOnlyInstancesOf(Line::class, $lines);
  85. $this->assertCount(2, $lines);
  86. /** @var Line $line */
  87. $line = $lines[0];
  88. $this->assertSame('A', $line->getContent());
  89. $this->assertSame(Line::UNCHANGED, $line->getType());
  90. $line = $lines[1];
  91. $this->assertSame('B', $line->getContent());
  92. $this->assertSame(Line::REMOVED, $line->getType());
  93. }
  94. public function testParseDiffForMulitpleFiles(): void
  95. {
  96. $content = <<<END
  97. diff --git a/Test.txt b/Test.txt
  98. index abcdefg..abcdefh 100644
  99. --- a/Test.txt
  100. +++ b/Test.txt
  101. @@ -1,3 +1,2 @@
  102. A
  103. -B
  104. diff --git a/Test123.txt b/Test123.txt
  105. index abcdefg..abcdefh 100644
  106. --- a/Test2.txt
  107. +++ b/Test2.txt
  108. @@ -1,2 +1,3 @@
  109. A
  110. +B
  111. END;
  112. $diffs = $this->parser->parse($content);
  113. $this->assertCount(2, $diffs);
  114. /** @var Diff $diff */
  115. $diff = $diffs[0];
  116. $this->assertSame('a/Test.txt', $diff->getFrom());
  117. $this->assertSame('b/Test.txt', $diff->getTo());
  118. $this->assertCount(1, $diff->getChunks());
  119. $diff = $diffs[1];
  120. $this->assertSame('a/Test2.txt', $diff->getFrom());
  121. $this->assertSame('b/Test2.txt', $diff->getTo());
  122. $this->assertCount(1, $diff->getChunks());
  123. }
  124. /**
  125. * @param string $diff
  126. * @param Diff[] $expected
  127. *
  128. * @dataProvider diffProvider
  129. */
  130. public function testParser(string $diff, array $expected): void
  131. {
  132. $result = $this->parser->parse($diff);
  133. $this->assertEquals($expected, $result);
  134. }
  135. public function diffProvider(): array
  136. {
  137. return [
  138. [
  139. "--- 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",
  140. \unserialize(FileUtils::getFileContent(__DIR__ . '/fixtures/serialized_diff.bin')),
  141. ],
  142. ];
  143. }
  144. }