Some text more text';
$to = 'Other text more text';
$diff = HtmlDiff::compareHtml($from, $to);
$this->assertEquals('Some Other text more text', $diff, false);
// Note that the end result here isn't perfect (there are new spaces where there weren't before)...
// If we make improvements later on that keep only the original spaces, that would be preferred.
// This test is more here to protect against any unexpected changes to the spacing, so that we can make an intentional
// decision as to whether those changes are desirable.
$diff = HtmlDiff::compareHtml($from, $to, true);
$this->assertEquals('<span> Some Other text </span> <span> more text </span>', $diff, true);
}
/**
* The underlying SebastianBergmann\Diff\Differ class has a special constant for end of line differences
* but we shouldn't ever encounter that because of the way we're modifying the values before passing them
* in to that class.
*/
public function testEndOfLineDoesntNeedHandling()
{
$from = 'some change' . "\r";
$to = 'some change' . "\n";
// If we encounter an end-of-line difference, these should throw exceptions
$this->assertSame('some change', HtmlDiff::compareHtml($from, $to, true));
$this->assertSame('some change', HtmlDiff::compareHtml($from, $to));
$this->assertSame('some change', HtmlDiff::compareHtml($to, $from, true));
$this->assertSame('some change', HtmlDiff::compareHtml($to, $from));
// Ensure that this test is valid and that those changes would include an end-of-line warning
// in a direct call to the underlying differ
$differ = new Differ();
$expected = [
[
'#Warning: Strings contain different line endings!' . "\n",
Differ::DIFF_LINE_END_WARNING,
],
[
$from,
Differ::REMOVED,
],
[
$to,
Differ::ADDED,
],
];
$this->assertSame($expected, $differ->diffToArray($from, $to));
}
public function provideCompareHtml(): array
{
return [
[
'from' => '
Some text
', 'to' => 'Other text
', 'escape' => false, 'expected' => 'SomeOther text
Some text
', 'to' => 'Other text', 'escape' => false, 'expected' => 'Some text
Some text
', 'to' => 'Other text
', 'escape' => false, 'expected' => 'Some textOther text
A comprehensive Library
A comprehensive
A comprehensive Library
A comprehensive
Some textmore stuff
', 'to' => 'Some text
more stuff
', 'escape' => false, 'expected' => 'Sometextmorestuff
morestuff
', ], // Same examples as above, but with escaped HTML [ 'from' => 'Some text
', 'to' => 'Other text
', 'escape' => true, 'expected' => '<p><span>Some text
', 'to' => 'Other text', 'escape' => true, 'expected' => 'Some text
', 'to' => 'Other text
', 'escape' => true, 'expected' => '<p>A comprehensive Library
A comprehensive
Some textmore stuff
', 'to' => 'Some text
more stuff
', 'escape' => true, 'expected' => '<p><span>Some text</span>Row 1 | |
Row 2 | Row 2 |
Row 3 | Row 3 |
Row 1 | |
Row 2 | Row 2 |