DiffEngineTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace Drupal\Tests\Component\Diff\Engine;
  3. use Drupal\Component\Diff\Engine\DiffEngine;
  4. use Drupal\Component\Diff\Engine\DiffOpAdd;
  5. use Drupal\Component\Diff\Engine\DiffOpCopy;
  6. use Drupal\Component\Diff\Engine\DiffOpChange;
  7. use Drupal\Component\Diff\Engine\DiffOpDelete;
  8. use PHPUnit\Framework\TestCase;
  9. /**
  10. * Test DiffEngine class.
  11. *
  12. * @coversDefaultClass \Drupal\Component\Diff\Engine\DiffEngine
  13. *
  14. * @group Diff
  15. */
  16. class DiffEngineTest extends TestCase {
  17. /**
  18. * @return array
  19. * - Expected output in terms of return class. A list of class names
  20. * expected to be returned by DiffEngine::diff().
  21. * - An array of strings to change from.
  22. * - An array of strings to change to.
  23. */
  24. public function provideTestDiff() {
  25. return [
  26. 'empty' => [[], [], []],
  27. 'add' => [[DiffOpAdd::class], [], ['a']],
  28. 'copy' => [[DiffOpCopy::class], ['a'], ['a']],
  29. 'change' => [[DiffOpChange::class], ['a'], ['b']],
  30. 'copy-and-change' => [
  31. [
  32. DiffOpCopy::class,
  33. DiffOpChange::class,
  34. ],
  35. ['a', 'b'],
  36. ['a', 'c'],
  37. ],
  38. 'copy-change-copy' => [
  39. [
  40. DiffOpCopy::class,
  41. DiffOpChange::class,
  42. DiffOpCopy::class,
  43. ],
  44. ['a', 'b', 'd'],
  45. ['a', 'c', 'd'],
  46. ],
  47. 'copy-change-copy-add' => [
  48. [
  49. DiffOpCopy::class,
  50. DiffOpChange::class,
  51. DiffOpCopy::class,
  52. DiffOpAdd::class,
  53. ],
  54. ['a', 'b', 'd'],
  55. ['a', 'c', 'd', 'e'],
  56. ],
  57. 'copy-delete' => [
  58. [
  59. DiffOpCopy::class,
  60. DiffOpDelete::class,
  61. ],
  62. ['a', 'b', 'd'],
  63. ['a'],
  64. ],
  65. ];
  66. }
  67. /**
  68. * Tests whether op classes returned by DiffEngine::diff() match expectations.
  69. *
  70. * @covers ::diff
  71. * @dataProvider provideTestDiff
  72. */
  73. public function testDiff($expected, $from, $to) {
  74. $diff_engine = new DiffEngine();
  75. $diff = $diff_engine->diff($from, $to);
  76. // Make sure we have the same number of results as expected.
  77. $this->assertCount(count($expected), $diff);
  78. // Make sure the diff objects match our expectations.
  79. foreach ($expected as $index => $op_class) {
  80. $this->assertEquals($op_class, get_class($diff[$index]));
  81. }
  82. }
  83. /**
  84. * Tests that two files can be successfully diffed.
  85. *
  86. * @covers ::diff
  87. */
  88. public function testDiffInfiniteLoop() {
  89. $from = explode("\n", file_get_contents(__DIR__ . '/fixtures/file1.txt'));
  90. $to = explode("\n", file_get_contents(__DIR__ . '/fixtures/file2.txt'));
  91. $diff_engine = new DiffEngine();
  92. $diff = $diff_engine->diff($from, $to);
  93. $this->assertCount(4, $diff);
  94. $this->assertEquals($diff[0], new DiffOpDelete([' - image.style.max_650x650']));
  95. $this->assertEquals($diff[1], new DiffOpCopy([' - image.style.max_325x325']));
  96. $this->assertEquals($diff[2], new DiffOpAdd([' - image.style.max_650x650', '_core:', ' default_config_hash: 3mjM9p-kQ8syzH7N8T0L9OnCJDSPvHAZoi3q6jcXJKM']));
  97. $this->assertEquals($diff[3], new DiffOpCopy(['fallback_image_style: max_325x325', '']));
  98. }
  99. }