DifferTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. namespace SebastianBergmann\Diff;
  11. use SebastianBergmann\Diff\LCS\MemoryEfficientImplementation;
  12. use SebastianBergmann\Diff\LCS\TimeEfficientImplementation;
  13. use PHPUnit\Framework\TestCase;
  14. /**
  15. * @covers SebastianBergmann\Diff\Differ
  16. *
  17. * @uses SebastianBergmann\Diff\LCS\MemoryEfficientImplementation
  18. * @uses SebastianBergmann\Diff\LCS\TimeEfficientImplementation
  19. * @uses SebastianBergmann\Diff\Chunk
  20. * @uses SebastianBergmann\Diff\Diff
  21. * @uses SebastianBergmann\Diff\Line
  22. * @uses SebastianBergmann\Diff\Parser
  23. */
  24. class DifferTest extends TestCase
  25. {
  26. const REMOVED = 2;
  27. const ADDED = 1;
  28. const OLD = 0;
  29. /**
  30. * @var Differ
  31. */
  32. private $differ;
  33. protected function setUp()
  34. {
  35. $this->differ = new Differ;
  36. }
  37. /**
  38. * @param array $expected
  39. * @param string|array $from
  40. * @param string|array $to
  41. * @dataProvider arrayProvider
  42. */
  43. public function testArrayRepresentationOfDiffCanBeRenderedUsingTimeEfficientLcsImplementation(array $expected, $from, $to)
  44. {
  45. $this->assertEquals($expected, $this->differ->diffToArray($from, $to, new TimeEfficientImplementation));
  46. }
  47. /**
  48. * @param string $expected
  49. * @param string $from
  50. * @param string $to
  51. * @dataProvider textProvider
  52. */
  53. public function testTextRepresentationOfDiffCanBeRenderedUsingTimeEfficientLcsImplementation($expected, $from, $to)
  54. {
  55. $this->assertEquals($expected, $this->differ->diff($from, $to, new TimeEfficientImplementation));
  56. }
  57. /**
  58. * @param array $expected
  59. * @param string|array $from
  60. * @param string|array $to
  61. * @dataProvider arrayProvider
  62. */
  63. public function testArrayRepresentationOfDiffCanBeRenderedUsingMemoryEfficientLcsImplementation(array $expected, $from, $to)
  64. {
  65. $this->assertEquals($expected, $this->differ->diffToArray($from, $to, new MemoryEfficientImplementation));
  66. }
  67. /**
  68. * @param string $expected
  69. * @param string $from
  70. * @param string $to
  71. * @dataProvider textProvider
  72. */
  73. public function testTextRepresentationOfDiffCanBeRenderedUsingMemoryEfficientLcsImplementation($expected, $from, $to)
  74. {
  75. $this->assertEquals($expected, $this->differ->diff($from, $to, new MemoryEfficientImplementation));
  76. }
  77. public function testCustomHeaderCanBeUsed()
  78. {
  79. $differ = new Differ('CUSTOM HEADER');
  80. $this->assertEquals(
  81. "CUSTOM HEADER@@ @@\n-a\n+b\n",
  82. $differ->diff('a', 'b')
  83. );
  84. }
  85. public function testTypesOtherThanArrayAndStringCanBePassed()
  86. {
  87. $this->assertEquals(
  88. "--- Original\n+++ New\n@@ @@\n-1\n+2\n",
  89. $this->differ->diff(1, 2)
  90. );
  91. }
  92. /**
  93. * @param string $diff
  94. * @param Diff[] $expected
  95. * @dataProvider diffProvider
  96. */
  97. public function testParser($diff, array $expected)
  98. {
  99. $parser = new Parser;
  100. $result = $parser->parse($diff);
  101. $this->assertEquals($expected, $result);
  102. }
  103. public function arrayProvider()
  104. {
  105. return array(
  106. array(
  107. array(
  108. array('a', self::REMOVED),
  109. array('b', self::ADDED)
  110. ),
  111. 'a',
  112. 'b'
  113. ),
  114. array(
  115. array(
  116. array('ba', self::REMOVED),
  117. array('bc', self::ADDED)
  118. ),
  119. 'ba',
  120. 'bc'
  121. ),
  122. array(
  123. array(
  124. array('ab', self::REMOVED),
  125. array('cb', self::ADDED)
  126. ),
  127. 'ab',
  128. 'cb'
  129. ),
  130. array(
  131. array(
  132. array('abc', self::REMOVED),
  133. array('adc', self::ADDED)
  134. ),
  135. 'abc',
  136. 'adc'
  137. ),
  138. array(
  139. array(
  140. array('ab', self::REMOVED),
  141. array('abc', self::ADDED)
  142. ),
  143. 'ab',
  144. 'abc'
  145. ),
  146. array(
  147. array(
  148. array('bc', self::REMOVED),
  149. array('abc', self::ADDED)
  150. ),
  151. 'bc',
  152. 'abc'
  153. ),
  154. array(
  155. array(
  156. array('abc', self::REMOVED),
  157. array('abbc', self::ADDED)
  158. ),
  159. 'abc',
  160. 'abbc'
  161. ),
  162. array(
  163. array(
  164. array('abcdde', self::REMOVED),
  165. array('abcde', self::ADDED)
  166. ),
  167. 'abcdde',
  168. 'abcde'
  169. ),
  170. 'same start' => array(
  171. array(
  172. array(17, self::OLD),
  173. array('b', self::REMOVED),
  174. array('d', self::ADDED),
  175. ),
  176. array(30 => 17, 'a' => 'b'),
  177. array(30 => 17, 'c' => 'd'),
  178. ),
  179. 'same end' => array(
  180. array(
  181. array(1, self::REMOVED),
  182. array(2, self::ADDED),
  183. array('b', self::OLD),
  184. ),
  185. array(1 => 1, 'a' => 'b'),
  186. array(1 => 2, 'a' => 'b'),
  187. ),
  188. 'same start (2), same end (1)' => array(
  189. array(
  190. array(17, self::OLD),
  191. array(2, self::OLD),
  192. array(4, self::REMOVED),
  193. array('a', self::ADDED),
  194. array(5, self::ADDED),
  195. array('x', self::OLD),
  196. ),
  197. array(30 => 17, 1 => 2, 2 => 4, 'z' => 'x'),
  198. array(30 => 17, 1 => 2, 3 => 'a', 2 => 5, 'z' => 'x'),
  199. ),
  200. 'same' => array(
  201. array(
  202. array('x', self::OLD),
  203. ),
  204. array('z' => 'x'),
  205. array('z' => 'x'),
  206. ),
  207. 'diff' => array(
  208. array(
  209. array('y', self::REMOVED),
  210. array('x', self::ADDED),
  211. ),
  212. array('x' => 'y'),
  213. array('z' => 'x'),
  214. ),
  215. 'diff 2' => array(
  216. array(
  217. array('y', self::REMOVED),
  218. array('b', self::REMOVED),
  219. array('x', self::ADDED),
  220. array('d', self::ADDED),
  221. ),
  222. array('x' => 'y', 'a' => 'b'),
  223. array('z' => 'x', 'c' => 'd'),
  224. ),
  225. 'test line diff detection' => array(
  226. array(
  227. array(
  228. '#Warning: Strings contain different line endings!',
  229. self::OLD,
  230. ),
  231. array(
  232. '<?php',
  233. self::OLD,
  234. ),
  235. array(
  236. '',
  237. self::OLD,
  238. ),
  239. ),
  240. "<?php\r\n",
  241. "<?php\n"
  242. )
  243. );
  244. }
  245. public function textProvider()
  246. {
  247. return array(
  248. array(
  249. "--- Original\n+++ New\n@@ @@\n-a\n+b\n",
  250. 'a',
  251. 'b'
  252. ),
  253. array(
  254. "--- Original\n+++ New\n@@ @@\n-ba\n+bc\n",
  255. 'ba',
  256. 'bc'
  257. ),
  258. array(
  259. "--- Original\n+++ New\n@@ @@\n-ab\n+cb\n",
  260. 'ab',
  261. 'cb'
  262. ),
  263. array(
  264. "--- Original\n+++ New\n@@ @@\n-abc\n+adc\n",
  265. 'abc',
  266. 'adc'
  267. ),
  268. array(
  269. "--- Original\n+++ New\n@@ @@\n-ab\n+abc\n",
  270. 'ab',
  271. 'abc'
  272. ),
  273. array(
  274. "--- Original\n+++ New\n@@ @@\n-bc\n+abc\n",
  275. 'bc',
  276. 'abc'
  277. ),
  278. array(
  279. "--- Original\n+++ New\n@@ @@\n-abc\n+abbc\n",
  280. 'abc',
  281. 'abbc'
  282. ),
  283. array(
  284. "--- Original\n+++ New\n@@ @@\n-abcdde\n+abcde\n",
  285. 'abcdde',
  286. 'abcde'
  287. ),
  288. array(
  289. "--- Original\n+++ New\n@@ @@\n-A\n+A1\n B\n",
  290. "A\nB",
  291. "A1\nB",
  292. ),
  293. array(
  294. <<<EOF
  295. --- Original
  296. +++ New
  297. @@ @@
  298. a
  299. -b
  300. +p
  301. @@ @@
  302. i
  303. -j
  304. +w
  305. k
  306. EOF
  307. ,
  308. "a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk",
  309. "a\np\nc\nd\ne\nf\ng\nh\ni\nw\nk",
  310. ),
  311. array(
  312. <<<EOF
  313. --- Original
  314. +++ New
  315. @@ @@
  316. a
  317. -b
  318. +p
  319. @@ @@
  320. i
  321. -j
  322. +w
  323. k
  324. EOF
  325. ,
  326. "a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk",
  327. "a\np\nc\nd\ne\nf\ng\nh\ni\nw\nk",
  328. ),
  329. );
  330. }
  331. public function diffProvider()
  332. {
  333. $serialized_arr = <<<EOL
  334. 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:6:{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";}}}}}}
  335. EOL;
  336. return array(
  337. array(
  338. "--- 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",
  339. \unserialize($serialized_arr)
  340. )
  341. );
  342. }
  343. /**
  344. * @param string $expected
  345. * @param string $from
  346. * @param string $to
  347. * @dataProvider textForNoNonDiffLinesProvider
  348. */
  349. public function testDiffDoNotShowNonDiffLines($expected, $from, $to)
  350. {
  351. $differ = new Differ('', false);
  352. $this->assertSame($expected, $differ->diff($from, $to));
  353. }
  354. public function textForNoNonDiffLinesProvider()
  355. {
  356. return array(
  357. array(
  358. '', 'a', 'a'
  359. ),
  360. array(
  361. "-A\n+C\n",
  362. "A\n\n\nB",
  363. "C\n\n\nB",
  364. ),
  365. );
  366. }
  367. /**
  368. * @requires PHPUnit 5.7
  369. */
  370. public function testDiffToArrayInvalidFromType()
  371. {
  372. $differ = new Differ;
  373. $this->expectException('\InvalidArgumentException');
  374. $this->expectExceptionMessageRegExp('#^"from" must be an array or string\.$#');
  375. $differ->diffToArray(null, '');
  376. }
  377. /**
  378. * @requires PHPUnit 5.7
  379. */
  380. public function testDiffInvalidToType()
  381. {
  382. $differ = new Differ;
  383. $this->expectException('\InvalidArgumentException');
  384. $this->expectExceptionMessageRegExp('#^"to" must be an array or string\.$#');
  385. $differ->diffToArray('', new \stdClass);
  386. }
  387. }