1714e663be55a4499d5c12f407b8d01c34c61719
[yaffs-website] / web / core / tests / Drupal / Tests / Component / Diff / Engine / DiffEngineTest.php
1 <?php
2
3 namespace Drupal\Tests\Component\Diff\Engine;
4
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;
11
12 /**
13  * Test DiffEngine class.
14  *
15  * @coversDefaultClass \Drupal\Component\Diff\Engine\DiffEngine
16  *
17  * @group Diff
18  */
19 class DiffEngineTest extends TestCase {
20
21   /**
22    * @return array
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.
27    */
28   public function provideTestDiff() {
29     return [
30       'empty' => [[], [], []],
31       'add' => [[DiffOpAdd::class], [], ['a']],
32       'copy' => [[DiffOpCopy::class], ['a'], ['a']],
33       'change' => [[DiffOpChange::class], ['a'], ['b']],
34       'copy-and-change' => [
35         [
36           DiffOpCopy::class,
37           DiffOpChange::class,
38         ],
39         ['a', 'b'],
40         ['a', 'c'],
41       ],
42       'copy-change-copy' => [
43         [
44           DiffOpCopy::class,
45           DiffOpChange::class,
46           DiffOpCopy::class,
47         ],
48         ['a', 'b', 'd'],
49         ['a', 'c', 'd'],
50       ],
51       'copy-change-copy-add' => [
52         [
53           DiffOpCopy::class,
54           DiffOpChange::class,
55           DiffOpCopy::class,
56           DiffOpAdd::class,
57         ],
58         ['a', 'b', 'd'],
59         ['a', 'c', 'd', 'e'],
60       ],
61       'copy-delete' => [
62         [
63           DiffOpCopy::class,
64           DiffOpDelete::class,
65         ],
66         ['a', 'b', 'd'],
67         ['a'],
68       ],
69     ];
70   }
71
72   /**
73    * Tests whether op classes returned by DiffEngine::diff() match expectations.
74    *
75    * @covers ::diff
76    * @dataProvider provideTestDiff
77    */
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]));
86     }
87   }
88
89   /**
90    * Tests that two files can be successfully diffed.
91    *
92    * @covers ::diff
93    */
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', '']));
104   }
105
106 }