Version 1
[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
11 /**
12  * Test DiffEngine class.
13  *
14  * @coversDefaultClass \Drupal\Component\Diff\Engine\DiffEngine
15  *
16  * @group Diff
17  */
18 class DiffEngineTest extends \PHPUnit_Framework_TestCase {
19
20   /**
21    * @return array
22    *   - Expected output in terms of return class. A list of class names
23    *     expected to be returned by DiffEngine::diff().
24    *   - An array of strings to change from.
25    *   - An array of strings to change to.
26    */
27   public function provideTestDiff() {
28     return [
29       'empty' => [[], [], []],
30       'add' => [[DiffOpAdd::class], [], ['a']],
31       'copy' => [[DiffOpCopy::class], ['a'], ['a']],
32       'change' => [[DiffOpChange::class], ['a'], ['b']],
33       'copy-and-change' => [
34         [
35           DiffOpCopy::class,
36           DiffOpChange::class,
37         ],
38         ['a', 'b'],
39         ['a', 'c'],
40       ],
41       'copy-change-copy' => [
42         [
43           DiffOpCopy::class,
44           DiffOpChange::class,
45           DiffOpCopy::class,
46         ],
47         ['a', 'b', 'd'],
48         ['a', 'c', 'd'],
49       ],
50       'copy-change-copy-add' => [
51         [
52           DiffOpCopy::class,
53           DiffOpChange::class,
54           DiffOpCopy::class,
55           DiffOpAdd::class,
56         ],
57         ['a', 'b', 'd'],
58         ['a', 'c', 'd', 'e'],
59       ],
60       'copy-delete' => [
61         [
62           DiffOpCopy::class,
63           DiffOpDelete::class,
64         ],
65         ['a', 'b', 'd'],
66         ['a'],
67       ],
68     ];
69   }
70
71   /**
72    * Tests whether op classes returned by DiffEngine::diff() match expectations.
73    *
74    * @covers ::diff
75    * @dataProvider provideTestDiff
76    */
77   public function testDiff($expected, $from, $to) {
78     $diff_engine = new DiffEngine();
79     $diff = $diff_engine->diff($from, $to);
80     // Make sure we have the same number of results as expected.
81     $this->assertCount(count($expected), $diff);
82     // Make sure the diff objects match our expectations.
83     foreach ($expected as $index => $op_class) {
84       $this->assertEquals($op_class, get_class($diff[$index]));
85     }
86   }
87
88 }