DifferTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. /*
  3. * This file is part of the Diff package.
  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\LCS\MemoryEfficientImplementation;
  13. use SebastianBergmann\Diff\LCS\TimeEfficientImplementation;
  14. class DifferTest extends PHPUnit_Framework_TestCase
  15. {
  16. const REMOVED = 2;
  17. const ADDED = 1;
  18. const OLD = 0;
  19. /**
  20. * @var Differ
  21. */
  22. private $differ;
  23. protected function setUp()
  24. {
  25. $this->differ = new Differ;
  26. }
  27. /**
  28. * @param array $expected
  29. * @param string $from
  30. * @param string $to
  31. * @dataProvider arrayProvider
  32. * @covers SebastianBergmann\Diff\Differ::diffToArray
  33. * @covers SebastianBergmann\Diff\LCS\TimeEfficientImplementation
  34. */
  35. public function testArrayRepresentationOfDiffCanBeRenderedUsingTimeEfficientLcsImplementation(array $expected, $from, $to)
  36. {
  37. $this->assertEquals($expected, $this->differ->diffToArray($from, $to, new TimeEfficientImplementation));
  38. }
  39. /**
  40. * @param string $expected
  41. * @param string $from
  42. * @param string $to
  43. * @dataProvider textProvider
  44. * @covers SebastianBergmann\Diff\Differ::diff
  45. * @covers SebastianBergmann\Diff\LCS\TimeEfficientImplementation
  46. */
  47. public function testTextRepresentationOfDiffCanBeRenderedUsingTimeEfficientLcsImplementation($expected, $from, $to)
  48. {
  49. $this->assertEquals($expected, $this->differ->diff($from, $to, new TimeEfficientImplementation));
  50. }
  51. /**
  52. * @param array $expected
  53. * @param string $from
  54. * @param string $to
  55. * @dataProvider arrayProvider
  56. * @covers SebastianBergmann\Diff\Differ::diffToArray
  57. * @covers SebastianBergmann\Diff\LCS\MemoryEfficientImplementation
  58. */
  59. public function testArrayRepresentationOfDiffCanBeRenderedUsingMemoryEfficientLcsImplementation(array $expected, $from, $to)
  60. {
  61. $this->assertEquals($expected, $this->differ->diffToArray($from, $to, new MemoryEfficientImplementation));
  62. }
  63. /**
  64. * @param string $expected
  65. * @param string $from
  66. * @param string $to
  67. * @dataProvider textProvider
  68. * @covers SebastianBergmann\Diff\Differ::diff
  69. * @covers SebastianBergmann\Diff\LCS\MemoryEfficientImplementation
  70. */
  71. public function testTextRepresentationOfDiffCanBeRenderedUsingMemoryEfficientLcsImplementation($expected, $from, $to)
  72. {
  73. $this->assertEquals($expected, $this->differ->diff($from, $to, new MemoryEfficientImplementation));
  74. }
  75. /**
  76. * @covers SebastianBergmann\Diff\Differ::diff
  77. */
  78. public function testCustomHeaderCanBeUsed()
  79. {
  80. $differ = new Differ('CUSTOM HEADER');
  81. $this->assertEquals(
  82. "CUSTOM HEADER@@ @@\n-a\n+b\n",
  83. $differ->diff('a', 'b')
  84. );
  85. }
  86. public function testTypesOtherThanArrayAndStringCanBePassed()
  87. {
  88. $this->assertEquals(
  89. "--- Original\n+++ New\n@@ @@\n-1\n+2\n",
  90. $this->differ->diff(1, 2)
  91. );
  92. }
  93. /**
  94. * @param string $diff
  95. * @param array $expected
  96. * @dataProvider diffProvider
  97. * @covers SebastianBergmann\Diff\Parser::parse
  98. */
  99. public function testParser($diff, $expected)
  100. {
  101. $parser = new Parser;
  102. $result = $parser->parse($diff);
  103. $this->assertEquals($expected, $result);
  104. }
  105. public function arrayProvider()
  106. {
  107. return array(
  108. array(
  109. array(
  110. array('a', self::REMOVED),
  111. array('b', self::ADDED)
  112. ),
  113. 'a',
  114. 'b'
  115. ),
  116. array(
  117. array(
  118. array('ba', self::REMOVED),
  119. array('bc', self::ADDED)
  120. ),
  121. 'ba',
  122. 'bc'
  123. ),
  124. array(
  125. array(
  126. array('ab', self::REMOVED),
  127. array('cb', self::ADDED)
  128. ),
  129. 'ab',
  130. 'cb'
  131. ),
  132. array(
  133. array(
  134. array('abc', self::REMOVED),
  135. array('adc', self::ADDED)
  136. ),
  137. 'abc',
  138. 'adc'
  139. ),
  140. array(
  141. array(
  142. array('ab', self::REMOVED),
  143. array('abc', self::ADDED)
  144. ),
  145. 'ab',
  146. 'abc'
  147. ),
  148. array(
  149. array(
  150. array('bc', self::REMOVED),
  151. array('abc', self::ADDED)
  152. ),
  153. 'bc',
  154. 'abc'
  155. ),
  156. array(
  157. array(
  158. array('abc', self::REMOVED),
  159. array('abbc', self::ADDED)
  160. ),
  161. 'abc',
  162. 'abbc'
  163. ),
  164. array(
  165. array(
  166. array('abcdde', self::REMOVED),
  167. array('abcde', self::ADDED)
  168. ),
  169. 'abcdde',
  170. 'abcde'
  171. )
  172. );
  173. }
  174. public function textProvider()
  175. {
  176. return array(
  177. array(
  178. "--- Original\n+++ New\n@@ @@\n-a\n+b\n",
  179. 'a',
  180. 'b'
  181. ),
  182. array(
  183. "--- Original\n+++ New\n@@ @@\n-ba\n+bc\n",
  184. 'ba',
  185. 'bc'
  186. ),
  187. array(
  188. "--- Original\n+++ New\n@@ @@\n-ab\n+cb\n",
  189. 'ab',
  190. 'cb'
  191. ),
  192. array(
  193. "--- Original\n+++ New\n@@ @@\n-abc\n+adc\n",
  194. 'abc',
  195. 'adc'
  196. ),
  197. array(
  198. "--- Original\n+++ New\n@@ @@\n-ab\n+abc\n",
  199. 'ab',
  200. 'abc'
  201. ),
  202. array(
  203. "--- Original\n+++ New\n@@ @@\n-bc\n+abc\n",
  204. 'bc',
  205. 'abc'
  206. ),
  207. array(
  208. "--- Original\n+++ New\n@@ @@\n-abc\n+abbc\n",
  209. 'abc',
  210. 'abbc'
  211. ),
  212. array(
  213. "--- Original\n+++ New\n@@ @@\n-abcdde\n+abcde\n",
  214. 'abcdde',
  215. 'abcde'
  216. ),
  217. );
  218. }
  219. public function diffProvider()
  220. {
  221. $serialized_arr = <<<EOL
  222. 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:7:{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";}i:6;O:27:"SebastianBergmann\Diff\Line":2:{s:33:"�SebastianBergmann\Diff\Line�type";i:3;s:36:"�SebastianBergmann\Diff\Line�content";s:0:"";}}}}}}
  223. EOL;
  224. return array(
  225. array(
  226. "--- 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",
  227. unserialize($serialized_arr)
  228. )
  229. );
  230. }
  231. }