69f92daec9c79932f224b993f7e824a957839492
[yaffs-website] / 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 }