3 namespace Drupal\Tests\Component\Diff\Engine;
5 use Drupal\Component\Diff\Engine\DiffEngine;
6 use Drupal\Component\Diff\Engine\DiffOpAdd;
7 use Drupal\Component\Diff\Engine\DiffOpCopy;
8 use Drupal\Component\Diff\Engine\DiffOpChange;
9 use Drupal\Component\Diff\Engine\DiffOpDelete;
10 use PHPUnit\Framework\TestCase;
13 * Test DiffEngine class.
15 * @coversDefaultClass \Drupal\Component\Diff\Engine\DiffEngine
19 class DiffEngineTest extends TestCase {
23 * - Expected output in terms of return class. A list of class names
24 * expected to be returned by DiffEngine::diff().
25 * - An array of strings to change from.
26 * - An array of strings to change to.
28 public function provideTestDiff() {
30 'empty' => [[], [], []],
31 'add' => [[DiffOpAdd::class], [], ['a']],
32 'copy' => [[DiffOpCopy::class], ['a'], ['a']],
33 'change' => [[DiffOpChange::class], ['a'], ['b']],
34 'copy-and-change' => [
42 'copy-change-copy' => [
51 'copy-change-copy-add' => [
73 * Tests whether op classes returned by DiffEngine::diff() match expectations.
76 * @dataProvider provideTestDiff
78 public function testDiff($expected, $from, $to) {
79 $diff_engine = new DiffEngine();
80 $diff = $diff_engine->diff($from, $to);
81 // Make sure we have the same number of results as expected.
82 $this->assertCount(count($expected), $diff);
83 // Make sure the diff objects match our expectations.
84 foreach ($expected as $index => $op_class) {
85 $this->assertEquals($op_class, get_class($diff[$index]));
90 * Tests that two files can be successfully diffed.
94 public function testDiffInfiniteLoop() {
95 $from = explode("\n", file_get_contents(__DIR__ . '/fixtures/file1.txt'));
96 $to = explode("\n", file_get_contents(__DIR__ . '/fixtures/file2.txt'));
97 $diff_engine = new DiffEngine();
98 $diff = $diff_engine->diff($from, $to);
99 $this->assertCount(4, $diff);
100 $this->assertEquals($diff[0], new DiffOpDelete([' - image.style.max_650x650']));
101 $this->assertEquals($diff[1], new DiffOpCopy([' - image.style.max_325x325']));
102 $this->assertEquals($diff[2], new DiffOpAdd([' - image.style.max_650x650', '_core:', ' default_config_hash: 3mjM9p-kQ8syzH7N8T0L9OnCJDSPvHAZoi3q6jcXJKM']));
103 $this->assertEquals($diff[3], new DiffOpCopy(['fallback_image_style: max_325x325', '']));