View source
<?php
namespace Drupal\Tests\Component\Diff\Engine;
use Drupal\Component\Diff\Engine\DiffEngine;
use Drupal\Component\Diff\Engine\DiffOpAdd;
use Drupal\Component\Diff\Engine\DiffOpCopy;
use Drupal\Component\Diff\Engine\DiffOpChange;
use Drupal\Component\Diff\Engine\DiffOpDelete;
use PHPUnit\Framework\TestCase;
class DiffEngineTest extends TestCase {
public function provideTestDiff() {
return [
'empty' => [
[],
[],
[],
],
'add' => [
[
DiffOpAdd::class,
],
[],
[
'a',
],
],
'copy' => [
[
DiffOpCopy::class,
],
[
'a',
],
[
'a',
],
],
'change' => [
[
DiffOpChange::class,
],
[
'a',
],
[
'b',
],
],
'copy-and-change' => [
[
DiffOpCopy::class,
DiffOpChange::class,
],
[
'a',
'b',
],
[
'a',
'c',
],
],
'copy-change-copy' => [
[
DiffOpCopy::class,
DiffOpChange::class,
DiffOpCopy::class,
],
[
'a',
'b',
'd',
],
[
'a',
'c',
'd',
],
],
'copy-change-copy-add' => [
[
DiffOpCopy::class,
DiffOpChange::class,
DiffOpCopy::class,
DiffOpAdd::class,
],
[
'a',
'b',
'd',
],
[
'a',
'c',
'd',
'e',
],
],
'copy-delete' => [
[
DiffOpCopy::class,
DiffOpDelete::class,
],
[
'a',
'b',
'd',
],
[
'a',
],
],
];
}
public function testDiff($expected, $from, $to) {
$diff_engine = new DiffEngine();
$diff = $diff_engine
->diff($from, $to);
$this
->assertCount(count($expected), $diff);
foreach ($expected as $index => $op_class) {
$this
->assertEquals($op_class, get_class($diff[$index]));
}
}
public function testDiffInfiniteLoop() {
$from = explode("\n", file_get_contents(__DIR__ . '/fixtures/file1.txt'));
$to = explode("\n", file_get_contents(__DIR__ . '/fixtures/file2.txt'));
$diff_engine = new DiffEngine();
$diff = $diff_engine
->diff($from, $to);
$this
->assertCount(4, $diff);
$this
->assertEquals($diff[0], new DiffOpDelete([
' - image.style.max_650x650',
]));
$this
->assertEquals($diff[1], new DiffOpCopy([
' - image.style.max_325x325',
]));
$this
->assertEquals($diff[2], new DiffOpAdd([
' - image.style.max_650x650',
'_core:',
' default_config_hash: 3mjM9p-kQ8syzH7N8T0L9OnCJDSPvHAZoi3q6jcXJKM',
]));
$this
->assertEquals($diff[3], new DiffOpCopy([
'fallback_image_style: max_325x325',
'',
]));
}
}