DiffFormatterTest.php 1.34 KB
Newer Older
1 2 3 4 5 6
<?php

namespace Drupal\Tests\Component\Diff;

use Drupal\Component\Diff\Diff;
use Drupal\Component\Diff\DiffFormatter;
7
use PHPUnit\Framework\TestCase;
8 9 10 11 12 13 14 15

/**
 * Test DiffFormatter classes.
 *
 * @coversDefaultClass \Drupal\Component\Diff\DiffFormatter
 *
 * @group Diff
 */
16
class DiffFormatterTest extends TestCase {
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

  /**
   * @return array
   *   - Expected formatted diff output.
   *   - First array of text to diff.
   *   - Second array of text to diff.
   */
  public function provideTestDiff() {
    return [
      'empty' => ['', [], []],
      'add' => [
        "3a3\n> line2a\n",
        ['line1', 'line2', 'line3'],
        ['line1', 'line2', 'line2a', 'line3'],
      ],
      'delete' => [
        "3d3\n< line2a\n",
        ['line1', 'line2', 'line2a', 'line3'],
        ['line1', 'line2', 'line3'],
      ],
      'change' => [
        "3c3\n< line2a\n---\n> line2b\n",
        ['line1', 'line2', 'line2a', 'line3'],
        ['line1', 'line2', 'line2b', 'line3'],
      ],
    ];
  }

  /**
   * Tests whether op classes returned by DiffEngine::diff() match expectations.
   *
   * @covers ::format
   * @dataProvider provideTestDiff
   */
  public function testDiff($expected, $from, $to) {
    $diff = new Diff($from, $to);
    $formatter = new DiffFormatter();
    $output = $formatter->format($diff);
    $this->assertEquals($expected, $output);
  }

}