class DiffFormatterTest

Same name and namespace in other branches
  1. 10 core/tests/Drupal/Tests/Component/Diff/DiffFormatterTest.php \Drupal\Tests\Component\Diff\DiffFormatterTest
  2. 9 core/tests/Drupal/Tests/Component/Diff/DiffFormatterTest.php \Drupal\Tests\Component\Diff\DiffFormatterTest
  3. 8.9.x core/tests/Drupal/Tests/Component/Diff/DiffFormatterTest.php \Drupal\Tests\Component\Diff\DiffFormatterTest
  4. main core/tests/Drupal/Tests/Component/Diff/DiffFormatterTest.php \Drupal\Tests\Component\Diff\DiffFormatterTest

Test DiffFormatter classes.

Attributes

#[CoversClass(DiffFormatter::class)] #[Group('Diff')]

Hierarchy

  • class \Drupal\Tests\Component\Diff\DiffFormatterTest extends \PHPUnit\Framework\TestCase

Expanded class hierarchy of DiffFormatterTest

File

core/tests/Drupal/Tests/Component/Diff/DiffFormatterTest.php, line 17

Namespace

Drupal\Tests\Component\Diff
View source
class DiffFormatterTest extends TestCase {
  
  /**
   * @return array
   *   - Expected formatted diff output.
   *   - First array of text to diff.
   *   - Second array of text to diff.
   */
  public static function provideTestDiff() : array {
    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',
        ],
      ],
      // Lines are compared including their end of line markings, so lines
      // differing only by those are a change. Differ's mismatched line ending
      // warning is not part of the output.
'change, line endings differ' => [
        "1c1\n< foo\r\n\n---\n> foo\n\n",
        [
          "foo\r\n",
        ],
        [
          "foo\n",
        ],
      ],
      'change, line endings differ on multiple lines' => [
        "1,2c1,2\n< line1\r\n\n< line2\r\n\n---\n> line1\n\n> line2\n\n",
        [
          "line1\r\n",
          "line2\r\n",
        ],
        [
          "line1\n",
          "line2\n",
        ],
      ],
      'copy, line endings match' => [
        '',
        [
          "foo\r\n",
        ],
        [
          "foo\r\n",
        ],
      ],
    ];
  }
  
  /**
   * Tests whether op classes returned by DiffEngine::diff() match expectations.
   *
   * @legacy-covers ::format
   */
  public function testDiff($expected, $from, $to) : void {
    $diff = new Diff($from, $to);
    $formatter = new DiffFormatter();
    $output = $formatter->format($diff);
    $this->assertEquals($expected, $output);
  }

}

Members

Title Sort descending Modifiers Object type Summary
DiffFormatterTest::provideTestDiff public static function
DiffFormatterTest::testDiff public function Tests whether op classes returned by DiffEngine::diff() match expectations.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.